nope/test/testModule.ts

109 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-10-13 13:18:25 +00:00
import 'reflect-metadata';
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';
export interface IHelloWorlModule extends INopeModule {
helloWorld(greetingsTo: string): Promise<string>
}
export class HelloWorldModule extends NopeBaseModule implements IHelloWorlModule {
/**
* Custom Function
*
* @param {string} greetingsTo
* @return {*}
* @memberof TestModule
*/
async helloWorld(greetingsTo: string) {
return 'Hello ' + greetingsTo + '! Greetings from ' + this.identifier;
}
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;
// 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'
}
}
});
}
}
const communicator = new EventLayer(
'generic',
'generic'
);
const local = new nopeDispatcher({ communicator }, () => new nopeObservable());
const remote = getLinkedDispatcher({ communicator });
const main = async () => {
remote.provideInstanceGeneratorForExternalDispatchers('helloworld', async (dispather, identifier) => {
const mod = new HelloWorldModule(dispather);
mod.identifier = identifier;
console.log('Created HelloWorldModule-Instance with name', identifier);
return mod;
});
local.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 local.generateInstance<IHelloWorlModule>({
identifier: 'instance_01',
params: ['hello', 'world'],
type: 'helloworld'
});
// await instance_01.init();
const greetings = await instance_01.helloWorld('You!');
// Print the Greetings
console.log(greetings);
}
main().catch(console.error);