@openhps/core
Version:
Open Hybrid Positioning System - Core component
44 lines • 1.4 kB
JavaScript
import { Node } from '../Node';
/**
* @category Node
*/
export class CallbackNode extends 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);
});
}
}