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) {
|
2017-08-24 15:51:34 +02:00
|
|
|
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();
|
2017-08-24 15:51:34 +02:00
|
|
|
this.label = label;
|
|
|
|
this.next = lastChainItem;
|
2017-08-09 00:03:57 +02:00
|
|
|
}
|
|
|
|
toString() {
|
2017-08-24 15:51:34 +02:00
|
|
|
return `<${this.label}>`;
|
2017-08-09 00:03:57 +02:00
|
|
|
}
|
|
|
|
registerNext(next) {
|
2017-08-24 15:51:34 +02:00
|
|
|
this.next = next;
|
2017-08-09 00:03:57 +02:00
|
|
|
}
|
|
|
|
send(message) {
|
|
|
|
this.emit('yourturn', message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exports.AChainItem = AChainItem;
|
2017-08-24 15:51:34 +02:00
|
|
|
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() {
|
2017-08-24 15:51:34 +02:00
|
|
|
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);
|
2017-08-24 15:51:34 +02:00
|
|
|
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() {
|
2017-08-24 15:51:34 +02:00
|
|
|
let funcName = (this.chainItemFunc.name === "") ? "lambda" : this.chainItemFunc.name;
|
|
|
|
return `<${funcName}, ${this.label}>`;
|
2017-08-23 15:52:37 +02:00
|
|
|
}
|
|
|
|
registerFunc(func) {
|
2017-08-24 15:51:34 +02:00
|
|
|
this.chainItemFunc = func;
|
2017-08-23 15:52:37 +02:00
|
|
|
}
|
|
|
|
func(message) {
|
2017-08-24 15:51:34 +02:00
|
|
|
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
|