nope/test/testDispatcher.ts

126 lines
2.7 KiB
TypeScript
Raw Normal View History

import "reflect-metadata";
2020-08-21 16:38:21 +00:00
import { EventLayer } from "../lib/communication/eventLayer";
2021-01-15 15:33:52 +00:00
import { exportFunctionToDispatcher } from "../lib/decorators/dispatcherDecorators";
2020-08-25 22:11:26 +00:00
import { getLinkedDispatcher } from "../lib/dispatcher/getLinkedDispatcher";
import { nopeDispatcher } from "../lib/dispatcher/nopeDispatcher";
import { NopeObservable } from "../lib/observables/NopeObservable";
2020-10-13 16:22:04 +00:00
import { generateBenchmarkFunction } from "../modules/funcs/generateBenchmarkFunction";
const max = 100000;
2021-01-15 15:33:52 +00:00
const communicator = new EventLayer();
2020-10-13 16:22:04 +00:00
const local = new nopeDispatcher({ communicator }, () => new NopeObservable());
2021-01-15 15:33:52 +00:00
const _functionRemote = exportFunctionToDispatcher(
async (a: number, b: number, operation: (a: number, b: number) => number) => {
return await operation(a, b);
},
{
id: "functionRemote"
}
);
const _benchmark = exportFunctionToDispatcher(
generateBenchmarkFunction(max, "remote-dispatcher"),
{
id: "benchmark"
}
);
const remote = getLinkedDispatcher({ communicator });
const remoteObservable = new NopeObservable<number>();
2021-01-15 15:33:52 +00:00
remote.registerObservable(remoteObservable, {
mode: "publish",
2021-01-15 15:33:52 +00:00
topic: "topic",
schema: {}
2020-09-17 06:05:45 +00:00
});
2020-08-25 22:11:26 +00:00
2020-08-21 16:38:21 +00:00
const main = async () => {
2020-10-13 16:22:04 +00:00
const localObservable = new NopeObservable<number>();
2020-09-17 06:05:45 +00:00
localObservable.subscribe((data) => {
console.log(data);
2020-09-17 06:05:45 +00:00
});
remoteObservable.setContent(1337);
console.log("No output of the emitter");
2020-09-17 06:05:45 +00:00
2021-01-15 15:33:52 +00:00
local.registerObservable(localObservable, {
mode: "subscribe",
2021-01-15 15:33:52 +00:00
topic: "topic",
schema: {}
2020-09-17 06:05:45 +00:00
});
remoteObservable.setContent(1338);
2020-09-17 06:05:45 +00:00
try {
await local.performCall("unkown", []);
} catch (e) {
console.log("Successfully thrown Error. Function isnt available.");
console.error(e);
}
2021-01-15 15:33:52 +00:00
let res = await local.performCall<number>("functionRemote", [
1,
2,
async (a, b) => {
console.log("local callback");
return a + b;
}
]);
2020-08-21 16:38:21 +00:00
console.log("1", res);
2020-08-21 16:38:21 +00:00
2021-01-15 15:33:52 +00:00
res = await local.methodInterface.functionRemote<number>(
1,
2,
async (a, b) => {
console.log("local callback");
return a * b;
}
);
2020-08-25 22:11:26 +00:00
let i = 0;
const benchmark = generateBenchmarkFunction(max, "");
2020-08-25 22:11:26 +00:00
while (i < max * 10) {
2021-01-15 15:33:52 +00:00
// await local.performCall<number>(
// "functionRemote",
// [
// 1,
// 2,
// async (a, b) => {
// return 1;
// }
// ],
// {
// deletableCallbacks: [2]
// }
// );
2020-08-25 22:11:26 +00:00
benchmark();
i++;
}
2020-08-21 16:38:21 +00:00
const opts = {
deletableCallbacks: []
};
const args = [];
i = 0;
while (i < max * 10) {
await local.performCall<void>("benchmark", args, opts);
i++;
}
i = 0;
while (i < max * 10) {
2021-01-15 15:33:52 +00:00
await _benchmark();
i++;
}
2021-01-15 15:33:52 +00:00
await local.dispose();
await remote.dispose();
};
2020-08-21 16:38:21 +00:00
main().catch(console.error);