251 lines
7.6 KiB
TypeScript
251 lines
7.6 KiB
TypeScript
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 { NopeBaseModule } from '../lib/module/BaseModule';
|
|
import { NopeGenericModule } from '../lib/module/GenericModule';
|
|
import { NopeObservable } from '../lib/observables/NopeObservable';
|
|
import { nopePromise } from '../lib/promise/nopePromise';
|
|
import { IValidPromise } from '../lib/types/nopeDispatcher.interface';
|
|
import { INopeModule } from '../lib/types/nopeModule.interface';
|
|
import { INopeObservable } from '../lib/types/NopeObservable.interface';
|
|
import { INopePromise } from '../lib/types/nopePromise.interface';
|
|
|
|
const sleep = promisify(setTimeout)
|
|
|
|
export interface IHelloWorlModule extends INopeModule {
|
|
testProp: INopeObservable<string>
|
|
helloWorld(greetingsTo: string): IValidPromise<string>
|
|
updateTestProp(): IValidPromise<void>,
|
|
sleep(n: number): IValidPromise<void>
|
|
}
|
|
|
|
export class HelloWorldModule extends NopeBaseModule implements IHelloWorlModule {
|
|
|
|
public testProp: INopeObservable<string>;
|
|
|
|
/**
|
|
* 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');
|
|
}
|
|
|
|
sleep(n: number){
|
|
let timer: any = null;
|
|
return new nopePromise<void>((resolve, reject)=> {
|
|
timer = setTimeout(resolve, n);
|
|
}, (reason) => {
|
|
console.log('Canceling Sleep Function because of:',reason)
|
|
if (timer != null){
|
|
clearTimeout(timer);
|
|
}
|
|
})
|
|
}
|
|
|
|
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.registerFunction('sleep', (amount: number) => _this.sleep(amount), {
|
|
schema: {
|
|
type: 'function',
|
|
inputs: [{
|
|
name: 'amount',
|
|
schema: {
|
|
type: 'number'
|
|
}
|
|
}],
|
|
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<IHelloWorlModule>({
|
|
identifier: 'instance_01',
|
|
params: ['hello', 'world'],
|
|
type: 'helloworld',
|
|
});
|
|
|
|
const copy_of_instance_01 = await remote_01.generateInstance<IHelloWorlModule>({
|
|
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<NopeGenericModule & IHelloWorlModule>({
|
|
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<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);
|
|
await sleep(100);
|
|
}
|
|
|
|
main().catch(console.error);
|
|
|