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 { NopeBaseModule } from '../lib/module/BaseModule'; import { NopeGenericModule } from '../lib/module/GenericModule'; import { NopeObservable } from '../lib/observables/NopeObservable'; import { INopeModule } from '../lib/types/nopeModule.interface'; import { INopeObservable } from '../lib/types/NopeObservable.interface'; const sleep = promisify(setTimeout) export interface IHelloWorlModule extends INopeModule { testProp: INopeObservable helloWorld(greetingsTo: string): Promise updateTestProp(): Promise } export class HelloWorldModule extends NopeBaseModule implements IHelloWorlModule { public testProp: INopeObservable; /** * Custom Function * * @param {string} greetingsTo * @return {*} * @memberof TestModule */ async helloWorld(greetingsTo: string) { return 'Hello ' + greetingsTo + '! Greetings from ' + this.identifier; } async updateTestProp() { this.testProp.setContent('Internally Updated'); } async init() { this.author = { forename: 'Martin', mail: 'm.karkowski@zema.de', surename: 'karkowski' } this.description = 'Test Hello World Module for Nope 2.0' this.type = 'helloworld'; this.version = { date: new Date('12.10.2020'), version: 1 } await super.init(); const _this = this; this.testProp = new NopeObservable(); this.testProp.subscribe((value,sender) => { console.log(_this.identifier,'got update for "testProp" = ', value, 'from', sender); }); // Register the Function Manually. await this.registerFunction('helloWorld', (...args) => _this.helloWorld(args[0]), { schema: { type: 'function', inputs: [{ name: 'greetingsTo', schema: { type: 'string', description: 'Name who should be greeted.' } }], outputs: { type: 'string', description: 'The Greeting' } } }); await this.registerFunction('updateTestProp', () => _this.updateTestProp(), { schema: { type: 'function', inputs: [], outputs: { type: 'null' } } }); await this.registerProperty('testProp', this.testProp, { mode: ['publish','subscribe'], schema: { type: 'string' }, topic: 'testProp' }) } async dispose(){ console.log('Deleting Module') } } 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 HelloWorldModule(dispather); mod.identifier = identifier; 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') remote_02.deleteInstance(instance_02); await sleep(100); } main().catch(console.error);