nope-js-browser
Version:
NoPE Runtime for the Browser. For nodejs please use nope-js-node
157 lines (156 loc) • 5.22 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { injectable } from "inversify";
import { InjectableNopeBaseModule } from "../../module";
import { NopeObservable } from "../../observables";
import { NopePromise } from "../../promise";
import { nopeMethod, nopeProperty } from "../../decorators";
import { getNopeLogger } from "../../logger/index.browser";
let HelloWorldModuleWithDecorators = class HelloWorldModuleWithDecorators extends InjectableNopeBaseModule {
constructor() {
super(...arguments);
// @ts-ignore
this.testProp = new NopeObservable();
// @ts-ignore
this.currentTime = new NopeObservable();
}
/**
* Custom Function
*
* @param {string} greetingsTo
* @return {*}
* @memberof TestModule
*/
async helloWorld(greetingsTo) {
return "Hello " + greetingsTo + "! Greetings from " + this.identifier;
}
/**
* Test Function to Update the Property.
*
* @memberof HelloWorldModuleWithDecorator
*/
async updateTestProp() {
this.testProp.setContent("Internally Updated using updateTestProp()");
}
/**
* Function which will delay the Execution.
*
* @param {number} n
* @return {*}
* @memberof HelloWorldModuleWithDecorator
*/
sleep(n) {
let timer = null;
const _this = this;
return new NopePromise((resolve, reject) => {
timer = setTimeout(resolve, n);
}, (reason) => {
_this._logger.info("Canceling Sleep Function because of:", reason);
if (timer != null) {
clearTimeout(timer);
}
});
}
async init() {
this._logger = getNopeLogger("HelloWorldModule");
this._logger.info("Created by dispatcher:", this._core.id);
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,
};
await super.init();
// Every 1000 ms publish an update of the current time.
this._interval = setInterval(() => {
_this.currentTime.setContent(new Date().toISOString());
}, 1000);
const _this = this;
this.testProp.setContent("INITAL_VALUE");
this.testProp.subscribe((value, sender) => {
_this._logger.info(_this.identifier, 'got update for "testProp" = ', value, "from", sender);
});
}
async dispose() {
clearInterval(this._interval);
this._logger.info("Deleting Module");
await super.dispose();
}
};
__decorate([
nopeProperty({
mode: ["publish"],
topic: "testProp",
schema: {},
})
], HelloWorldModuleWithDecorators.prototype, "testProp", void 0);
__decorate([
nopeProperty({
mode: ["publish"],
topic: "currentTime",
schema: {
type: "string",
},
})
], HelloWorldModuleWithDecorators.prototype, "currentTime", void 0);
__decorate([
nopeMethod({
schema: {
type: "function",
inputs: [
{
name: "greetingsTo",
schema: {
type: "string",
description: "Name who should be greeted.",
},
},
],
outputs: {
type: "string",
description: "The Greeting",
},
},
})
], HelloWorldModuleWithDecorators.prototype, "helloWorld", null);
__decorate([
nopeMethod({
schema: {
type: "function",
inputs: [],
outputs: {
type: "null",
},
},
})
], HelloWorldModuleWithDecorators.prototype, "updateTestProp", null);
__decorate([
nopeMethod({
schema: {
type: "function",
inputs: [
{
name: "amount",
schema: {
type: "number",
},
},
],
outputs: {
type: "null",
},
},
})
], HelloWorldModuleWithDecorators.prototype, "sleep", null);
HelloWorldModuleWithDecorators = __decorate([
injectable()
], HelloWorldModuleWithDecorators);
export { HelloWorldModuleWithDecorators };