nope/test/testDispatcher.ts

102 lines
2.4 KiB
TypeScript
Raw Normal View History

2020-09-17 06:05:45 +00:00
import 'reflect-metadata';
2020-08-21 16:38:21 +00:00
import { EventLayer } from "../lib/communication/eventLayer";
2020-08-25 22:11:26 +00:00
import { getLinkedDispatcher } from "../lib/dispatcher/getLinkedDispatcher";
import { nopeDispatcher } from "../lib/dispatcher/nopeDispatcher";
import { exportFunctionToDispatcher } from "../lib/dispatcher/nopeDispatcherDecorators";
import { generateBenchmarkFunction } from "../modules/funcs/generateBenchmarkFunction";
2020-09-17 06:05:45 +00:00
import { nopeObservable } from '../lib/observables/nopeObservable';
2020-08-21 16:38:21 +00:00
let max = 100000;
const communicator = new EventLayer(
'generic',
'generic'
);
const local = new nopeDispatcher({ communicator });
2020-08-25 22:11:26 +00:00
const _functionRemote = exportFunctionToDispatcher(async (a: number, b: number, operation: (a: number, b: number) => number) => {
2020-08-21 16:38:21 +00:00
return await operation(a, b);
2020-08-25 22:11:26 +00:00
}, {
uri: 'functionRemote'
});
const _benchmark = exportFunctionToDispatcher(generateBenchmarkFunction(max, ''), {
uri: 'benchmark'
2020-08-21 16:38:21 +00:00
});
const remote = getLinkedDispatcher({ communicator });
2020-09-17 06:05:45 +00:00
const remoteObservable = new nopeObservable<number>()
remote.registerObservable(remoteObservable,{
mode: 'publish',
topic: 'topic'
});
2020-08-25 22:11:26 +00:00
2020-08-21 16:38:21 +00:00
const main = async () => {
2020-09-17 06:05:45 +00:00
const localObservable = new nopeObservable<number>();
localObservable.subscribe((data) => {
console.log(data)
});
remoteObservable.setContent(1337);
console.log('No output of the emitter');
local.registerObservable(localObservable,{
mode: 'subscribe',
topic:'topic'
});
remoteObservable.setContent(1338)
try {
await local.performCall('unkown', []);
} catch (e) {
2020-09-17 06:05:45 +00:00
console.log('Successfully thrown Error. Function isnt available.');
console.error(e)
}
let res = await local.performCall<number>('functionRemote', [1, 2, async (a, b) => {
2020-08-21 16:38:21 +00:00
console.log('local callback')
return a + b;
}]);
console.log('1', res)
res = await local.methodInterface.functionRemote<number>(1, 2, async (a, b) => {
2020-08-21 16:38:21 +00:00
console.log('local callback')
return a * b;
2020-09-13 10:35:04 +00:00
});
2020-08-21 16:38:21 +00:00
2020-08-25 22:11:26 +00:00
let i = 0;
const benchmark = generateBenchmarkFunction(max, '');
while (i < max * 10) {
await local.performCall<number>('functionRemote', [1, 2, async (a, b) => {
2020-08-25 22:11:26 +00:00
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++;
}
2020-08-21 16:38:21 +00:00
}
main().catch(console.error);
setTimeout(console.log, 10000, 'done');
2020-08-21 16:38:21 +00:00