nope/test/testDispatcher.ts
2020-11-04 17:33:36 +01:00

161 lines
3.7 KiB
TypeScript

import 'reflect-metadata';
import { EventLayer } from "../lib/communication/eventLayer";
import { getLinkedDispatcher } from "../lib/dispatcher/getLinkedDispatcher";
import { nopeDispatcher } from "../lib/dispatcher/nopeDispatcher";
import { exportFunctionToDispatcher } from "../lib/dispatcher/nopeDispatcherDecorators";
import { NopeObservable } from '../lib/observables/NopeObservable';
import { IRemoteInstance } from '../lib/types/remoteInstance.interface';
import { generateBenchmarkFunction } from "../modules/funcs/generateBenchmarkFunction";
let max = 100000;
const communicator = new EventLayer(
'generic',
'generic'
);
const local = new nopeDispatcher({ communicator }, () => new NopeObservable());
const _functionRemote = exportFunctionToDispatcher(async (a: number, b: number, operation: (a: number, b: number) => number) => {
return await operation(a, b);
}, {
uri: 'functionRemote'
});
const _benchmark = exportFunctionToDispatcher(generateBenchmarkFunction(max, ''), {
uri: 'benchmark'
});
const remote = getLinkedDispatcher({ communicator });
const remoteObservable = new NopeObservable<number>()
remote.registerObservable(remoteObservable,{
mode: 'publish',
topic: 'topic'
});
const main = async () => {
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) {
console.log('Successfully thrown Error. Function isnt available.');
console.error(e)
}
let res = await local.performCall<number>('functionRemote', [1, 2, async (a, b) => {
console.log('local callback')
return a + b;
}]);
console.log('1', res)
res = await local.methodInterface.functionRemote<number>(1, 2, async (a, b) => {
console.log('local callback')
return a * b;
});
remote.registerInternalInstanceGenerator('class', async (dispather, identifier) => {
const ret: IRemoteInstance = {
dispose: async () => {
console.log('dispise')
},
description: {
},
init: async (... args) => {
console.log('INIT with', args)
},
type: 'class'
}
console.log('CREATED INSTANCE of type "class" with args',dispather.id, identifier);
return ret
}, {
mode: 'external'
});
local.registerInstanceGenerator('class', async (dispather, identifier) => {
const ret: IRemoteInstance = {
dispose: async () => {
console.log('dispise')
},
identifier: 'null',
init: async (... args) => {
console.log('INIT with', args)
},
type: 'class'
}
console.log('CREATED Wrapper for', identifier);
return ret
}, {
mode: 'internal'
});
const instance_01= await local.generateInstance({
identifier: 'obj1',
params: ['hello','world'],
type: 'class'
});
const instance_02 = await local.generateInstance({
identifier: 'obj1',
params: ['hello','world'],
type: 'class'
});
let i = 0;
const benchmark = generateBenchmarkFunction(max, '');
while (i < max * 10) {
await local.performCall<number>('functionRemote', [1, 2, async (a, b) => {
return 1
}], {
deletableCallbacks: [2],
});
benchmark();
i++;
}
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) {
await benchmark();
i++;
}
}
main().catch(console.error);
setTimeout(console.log, 10000, 'done');