import { assert } from 'console'; import 'reflect-metadata'; import { promisify } from 'util'; import { EventLayer } from "../lib/communication/eventLayer"; import { getLinkedDispatcher } from "../lib/dispatcher/getLinkedDispatcher"; import { nopeDispatcher } from "../lib/dispatcher/nopeDispatcher"; import { NopeGenericModule } from '../lib/module/GenericModule'; import { NopeObservable } from '../lib/observables/NopeObservable'; import { INopePromise } from '../lib/types/nope/nopePromise.interface'; import { HelloWorldModuleWithDecorators } from './modules/HelloWorldModuleWithDecorators'; import { IHelloWorlModule } from './modules/IHelloWorldModule'; const sleep = promisify(setTimeout) const communicator = new EventLayer( 'generic', 'generic' ); const remote_01 = new nopeDispatcher({ communicator }, () => new NopeObservable()); const remote_02 = new nopeDispatcher({ communicator }, () => new NopeObservable()); const srv = getLinkedDispatcher({ communicator }); const main = async () => { srv.provideInstanceGeneratorForExternalDispatchers('helloworld', async (dispather, identifier) => { const mod = new HelloWorldModuleWithDecorators(dispather); mod.identifier = identifier; console.log(HelloWorldModuleWithDecorators.prototype.constructor.name, '==' ,mod.type) console.log('Created HelloWorldModule-Instance with name', identifier); return mod; }); remote_01.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; }); 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({ identifier: 'instance_01', params: ['hello', 'world'], type: 'helloworld', }); const copy_of_instance_01 = await remote_01.generateInstance({ identifier: 'instance_01', params: ['hello', 'world'], type: 'helloworld', }); // await instance_01.init(); 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') const greetings = await instance_01.helloWorld('You!'); // Print the Greetings 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(); console.log('Creating new instance on other remote'); const instance_02 = await remote_02.generateInstance({ 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); console.log('Delete Instance 2') // Quick Test of manual Cancelation. try { const promise = instance_02.sleep(5000); if ((promise as INopePromise).cancel){ (promise as INopePromise).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); await sleep(100); } main().catch(console.error);