callchains implemented
This commit is contained in:
50
dist/callchain.js
vendored
Normal file
50
dist/callchain.js
vendored
Normal file
@ -0,0 +1,50 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const log = require("./log");
|
||||
const events = require("events");
|
||||
class AChainItem extends events.EventEmitter {
|
||||
constructor(label) {
|
||||
super();
|
||||
this._label = label;
|
||||
this._next = null;
|
||||
}
|
||||
toString() {
|
||||
return `<${this._label}`;
|
||||
}
|
||||
registerNext(next) {
|
||||
this._next = next;
|
||||
}
|
||||
send(message) {
|
||||
this.emit('yourturn', message);
|
||||
}
|
||||
}
|
||||
exports.AChainItem = AChainItem;
|
||||
class ChainItem extends AChainItem {
|
||||
constructor(label) {
|
||||
super(label);
|
||||
}
|
||||
toString() {
|
||||
let funcName = (this._chainItemFunc.name === "") ? "lambda" : this._chainItemFunc.name;
|
||||
return `<${funcName}, ${this._label}>`;
|
||||
}
|
||||
registerFunc(func) {
|
||||
this._chainItemFunc = func;
|
||||
}
|
||||
begin() {
|
||||
if (this._next != null) {
|
||||
this._next.begin();
|
||||
}
|
||||
this.addListener('yourturn', (message) => {
|
||||
log.info(`Calling ${this.toString()}`);
|
||||
let result = this._chainItemFunc(message);
|
||||
if (this._next == null) {
|
||||
log.info(`Last chain item, final result ${result}`);
|
||||
}
|
||||
else {
|
||||
this._next.send(result);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
exports.ChainItem = ChainItem;
|
||||
//# sourceMappingURL=callchain.js.map
|
Reference in New Issue
Block a user