nope/test/modules/HelloWorldModuleWithDecorators.ts

140 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-11-07 00:45:20 +00:00
import { injectable } from "inversify";
import {
exportMethod,
exportProperty
} from "../../lib/decorators/moduleDecorators";
import { InjectableNopeBaseModule } from "../../lib/module/BaseModule.injectable";
2020-11-05 17:02:01 +00:00
import { NopeObservable } from "../../lib/observables/nopeObservable";
import { NopePromise } from "../../lib/promise/nopePromise";
import { IHelloWorlModule } from "./IHelloWorldModule";
2020-11-07 00:45:20 +00:00
@injectable()
export class HelloWorldModuleWithDecorators
extends InjectableNopeBaseModule
implements IHelloWorlModule {
@exportProperty({
mode: ["publish"],
topic: "string",
schema: {}
})
public testProp = new NopeObservable<string>();
2020-11-05 17:02:01 +00:00
/**
* Custom Function
*
* @param {string} greetingsTo
* @return {*}
* @memberof TestModule
*/
@exportMethod({
schema: {
type: "function",
inputs: [
{
name: "greetingsTo",
schema: {
type: "string",
description: "Name who should be greeted."
}
2020-11-05 17:02:01 +00:00
}
],
outputs: {
type: "string",
description: "The Greeting"
}
2020-11-05 17:02:01 +00:00
}
})
async helloWorld(greetingsTo: string) {
return "Hello " + greetingsTo + "! Greetings from " + this.identifier;
}
2020-11-05 17:02:01 +00:00
/**
* Test Function to Update the Property.
*
* @memberof HelloWorldModuleWithDecorator
*/
@exportMethod({
schema: {
type: "function",
inputs: [],
outputs: {
type: "null"
}
2020-11-05 17:02:01 +00:00
}
})
async updateTestProp() {
this.testProp.setContent("Internally Updated");
}
2020-11-05 17:02:01 +00:00
/**
* Function which will delay the Execution.
*
* @param {number} n
* @return {*}
* @memberof HelloWorldModuleWithDecorator
*/
@exportMethod({
schema: {
type: "function",
inputs: [
{
name: "amount",
schema: {
type: "number"
}
2020-11-05 17:02:01 +00:00
}
],
outputs: {
type: "null"
}
2020-11-05 17:02:01 +00:00
}
})
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);
2020-11-05 17:02:01 +00:00
}
}
);
}
2020-11-05 17:02:01 +00:00
async init() {
this.author = {
forename: "Martin",
mail: "m.karkowski@zema.de",
surename: "karkowski"
};
this.description = "Test Hello World Module for Nope 2.0";
this.version = {
date: new Date("12.10.2020"),
version: 1
};
2020-11-05 17:02:01 +00:00
await super.init();
2020-11-05 17:02:01 +00:00
const _this = this;
2020-11-05 17:02:01 +00:00
this.testProp = new NopeObservable();
2020-11-05 17:02:01 +00:00
this.testProp.subscribe((value, sender) => {
console.log(
_this.identifier,
"got update for \"testProp\" = ",
value,
"from",
sender
);
});
}
async dispose() {
console.log("Deleting Module");
}
}