@openhps/core
Version:
Open Hybrid Positioning System - Core component
60 lines • 2.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CallbackNode = void 0;
const Node_1 = require("../Node");
/**
* @category Node
*/
class CallbackNode extends Node_1.Node {
constructor(pushCallback = () => true, pullCallback = () => null, options) {
super(options);
this.pushCallback = pushCallback;
this.pullCallback = pullCallback;
this.on('push', this._onPush.bind(this));
this.on('pull', this._onPull.bind(this));
this.options.autoPush = this.options.autoPush || true;
}
_onPush(frame, options) {
return new Promise((resolve, reject) => {
Promise.resolve(this.pushCallback(frame, options))
.then(() => {
if (this.options.autoPush) {
return Promise.all(this.outlets.map((outlet) => outlet.push(frame, options)));
}
else {
resolve();
}
})
.then(() => {
resolve();
})
.catch(reject);
});
}
_onPull(options) {
return new Promise((resolve, reject) => {
Promise.resolve(this.pullCallback(options))
.then((result) => {
if (result !== undefined && result !== null) {
// Push result
Promise.all(this.outlets.map((outlet) => outlet.push(result, options)))
.then(() => {
resolve();
})
.catch(reject);
}
else {
// Forward pull
Promise.all(this.inlets.map((inlet) => inlet.pull(options)))
.then(() => {
resolve();
})
.catch(reject);
}
})
.catch(reject);
});
}
}
exports.CallbackNode = CallbackNode;
//# sourceMappingURL=CallbackNode.js.map