nope/test/testModule.ts

130 lines
4.3 KiB
TypeScript
Raw Normal View History

2020-11-04 16:33:36 +00:00
import { assert } from 'console';
2020-10-13 13:18:25 +00:00
import 'reflect-metadata';
2020-10-13 16:22:04 +00:00
import { promisify } from 'util';
2020-10-13 13:18:25 +00:00
import { EventLayer } from "../lib/communication/eventLayer";
import { getLinkedDispatcher } from "../lib/dispatcher/getLinkedDispatcher";
import { nopeDispatcher } from "../lib/dispatcher/nopeDispatcher";
import { NopeGenericModule } from '../lib/module/GenericModule';
2020-10-13 16:22:04 +00:00
import { NopeObservable } from '../lib/observables/NopeObservable';
2020-11-07 00:45:20 +00:00
import { INopePromise } from '../lib/types/nope/nopePromise.interface';
import { HelloWorldModuleWithDecorators } from './modules/HelloWorldModuleWithDecorators';
import { IHelloWorlModule } from './modules/IHelloWorldModule';
2020-10-13 13:18:25 +00:00
2020-10-13 16:22:04 +00:00
const sleep = promisify(setTimeout)
2020-10-13 13:18:25 +00:00
const communicator = new EventLayer(
'generic',
'generic'
);
2020-10-15 09:38:59 +00:00
const remote_01 = new nopeDispatcher({ communicator }, () => new NopeObservable());
const remote_02 = new nopeDispatcher({ communicator }, () => new NopeObservable());
const srv = getLinkedDispatcher({ communicator });
2020-10-13 13:18:25 +00:00
const main = async () => {
2020-10-15 09:38:59 +00:00
srv.provideInstanceGeneratorForExternalDispatchers('helloworld', async (dispather, identifier) => {
2020-11-05 17:02:01 +00:00
const mod = new HelloWorldModuleWithDecorators(dispather);
2020-10-13 13:18:25 +00:00
mod.identifier = identifier;
console.log(HelloWorldModuleWithDecorators.prototype.constructor.name, '==' ,mod.type)
2020-10-13 13:18:25 +00:00
console.log('Created HelloWorldModule-Instance with name', identifier);
return mod;
});
2020-10-15 09:38:59 +00:00
remote_01.registerInternalInstanceGenerator('helloworld', async (dispather, description) => {
2020-10-13 16:22:04 +00:00
const mod = new NopeGenericModule(dispather, () => new NopeObservable());
2020-10-13 13:18:25 +00:00
await mod.fromDescription(description, 'overwrite');
await mod.init();
console.log('Created HelloWorldModule-Wrapper with name', description.identifier);
return mod;
});
2020-10-15 09:38:59 +00:00
remote_02.registerInternalInstanceGenerator('helloworld', async (dispather, description) => {
const mod = new NopeGenericModule(dispather, () => new NopeObservable());
await mod.fromDescription(description, 'overwrite');
await mod.init();
console.log('Created HelloWorldModule-Wrapper with name', description.identifier);
return mod;
});
const instance_01 = await remote_01.generateInstance<IHelloWorlModule>({
identifier: 'instance_01',
params: ['hello', 'world'],
type: 'helloworld',
});
const copy_of_instance_01 = await remote_01.generateInstance<IHelloWorlModule>({
2020-10-13 13:18:25 +00:00
identifier: 'instance_01',
params: ['hello', 'world'],
2020-10-15 09:38:59 +00:00
type: 'helloworld',
2020-10-13 13:18:25 +00:00
});
// await instance_01.init();
2020-10-13 16:22:04 +00:00
console.log('Updating Prop')
instance_01.testProp.setContent('hello prop #1');
instance_01.testProp.setContent('hello prop #2');
await sleep(100);
console.log('Calling helloWorld')
2020-10-13 13:18:25 +00:00
const greetings = await instance_01.helloWorld('You!');
// Print the Greetings
2020-10-13 16:22:04 +00:00
console.log('Result of helloWorld:',greetings);
instance_01.testProp.subscribe((value, sender) => {
console.log('Local update of the property', value)
})
console.log('Got Update by subscribing');
console.log('Updating Variable by updateTestProp Function');
await instance_01.updateTestProp();
2020-10-15 09:38:59 +00:00
console.log('Creating new instance on other remote');
2020-11-04 16:33:36 +00:00
const instance_02 = await remote_02.generateInstance<NopeGenericModule & IHelloWorlModule>({
2020-10-15 09:38:59 +00:00
identifier: 'instance_01',
params: ['hello', 'world'],
type: 'helloworld',
});
console.log('Delte Copies etc');
remote_01.deleteInstance(instance_01);
remote_01.deleteInstance(copy_of_instance_01);
2020-11-04 16:33:36 +00:00
2020-10-15 09:38:59 +00:00
console.log('Delete Instance 2')
2020-11-04 16:33:36 +00:00
// Quick Test of manual Cancelation.
try {
const promise = instance_02.sleep(5000);
if ((promise as INopePromise<void>).cancel){
(promise as INopePromise<void>).cancel('Just for Fun');
}
await promise;
} catch(e){
// The Expected Error should be :'Just for Fun'
assert(e == 'Just for Fun')
}
// Quick Test of manual timeout.
try {
const promise = instance_02.dynamicInstanceMethodsWithOptions.sleep({
timeout: 1000
},5000);
await promise;
} catch(e){
console.log('Expect an Timeout Error',e);
}
remote_02.deleteInstance(instance_02);
2020-10-13 16:22:04 +00:00
await sleep(100);
2020-10-13 13:18:25 +00:00
}
main().catch(console.error);