nope/test/modules/HelloWorldModuleWithDecorators.ts
2020-11-05 18:02:01 +01:00

124 lines
3.1 KiB
TypeScript

import { exportMethod, exportProperty } from "../../lib/decorators/moduelDecorators";
import { NopeBaseModule } from "../../lib/module/BaseModule";
import { NopeObservable } from "../../lib/observables/nopeObservable";
import { NopePromise } from "../../lib/promise/nopePromise";
import { IHelloWorlModule } from "./IHelloWorldModule";
export class HelloWorldModuleWithDecorators extends NopeBaseModule implements IHelloWorlModule {
@exportProperty({
mode: ['publish'],
topic: 'string',
schema: {
}
})
public testProp = new NopeObservable<string>();
/**
* Custom Function
*
* @param {string} greetingsTo
* @return {*}
* @memberof TestModule
*/
@exportMethod({
schema: {
type: 'function',
inputs: [{
name: 'greetingsTo',
schema: {
type: 'string',
description: 'Name who should be greeted.'
}
}],
outputs: {
type: 'string',
description: 'The Greeting'
}
}
})
async helloWorld(greetingsTo: string) {
return 'Hello ' + greetingsTo + '! Greetings from ' + this.identifier;
}
/**
* Test Function to Update the Property.
*
* @memberof HelloWorldModuleWithDecorator
*/
@exportMethod({
schema: {
type: 'function',
inputs: [],
outputs: {
type: 'null'
}
},
})
async updateTestProp() {
this.testProp.setContent('Internally Updated');
}
/**
* Function which will delay the Execution.
*
* @param {number} n
* @return {*}
* @memberof HelloWorldModuleWithDecorator
*/
@exportMethod({
schema: {
type: 'function',
inputs: [{
name: 'amount',
schema: {
type: 'number'
}
}],
outputs: {
type: 'null'
}
}
})
public 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);
});
}
async dispose(){
console.log('Deleting Module')
}
}