MqttDispatcher/dist/callchain.js

74 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-08-09 00:03:57 +02:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const log = require("./log");
const events = require("events");
2017-08-23 15:52:37 +02:00
class LastChainItem {
begin() {
}
send(message) {
log.info(`Last chain item, final result ${JSON.stringify(message)}`);
2017-08-23 15:52:37 +02:00
}
}
let lastChainItem = new LastChainItem();
2017-08-09 00:03:57 +02:00
class AChainItem extends events.EventEmitter {
constructor(label) {
super();
this.label = label;
this.next = lastChainItem;
2017-08-09 00:03:57 +02:00
}
toString() {
return `<${this.label}>`;
2017-08-09 00:03:57 +02:00
}
registerNext(next) {
this.next = next;
2017-08-09 00:03:57 +02:00
}
send(message) {
this.emit('yourturn', message);
}
}
exports.AChainItem = AChainItem;
class AAsyncBaseChainItem extends AChainItem {
constructor(label) {
super(label);
}
begin() {
if (this.next != null) {
this.next.begin();
}
this.addListener('yourturn', (message) => {
log.info(`Calling ${this.toString()}`);
this.func(message, this.next.send);
});
}
}
exports.AAsyncBaseChainItem = AAsyncBaseChainItem;
2017-08-23 15:52:37 +02:00
class ABaseChainItem extends AChainItem {
2017-08-09 00:03:57 +02:00
constructor(label) {
super(label);
}
begin() {
if (this.next != null) {
this.next.begin();
2017-08-09 00:03:57 +02:00
}
this.addListener('yourturn', (message) => {
log.info(`Calling ${this.toString()}`);
2017-08-23 15:52:37 +02:00
let result = this.func(message);
this.next.send(result);
2017-08-09 00:03:57 +02:00
});
}
}
2017-08-23 15:52:37 +02:00
exports.ABaseChainItem = ABaseChainItem;
class ChainItem extends ABaseChainItem {
toString() {
let funcName = (this.chainItemFunc.name === "") ? "lambda" : this.chainItemFunc.name;
return `<${funcName}, ${this.label}>`;
2017-08-23 15:52:37 +02:00
}
registerFunc(func) {
this.chainItemFunc = func;
2017-08-23 15:52:37 +02:00
}
func(message) {
return this.chainItemFunc(message);
2017-08-23 15:52:37 +02:00
}
}
2017-08-09 00:03:57 +02:00
exports.ChainItem = ChainItem;
//# sourceMappingURL=callchain.js.map