@openhps/core
Version:
Open Hybrid Positioning System - Core component
75 lines • 2.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BufferNode = void 0;
const DataFrame_1 = require("../../data/DataFrame");
const Node_1 = require("../../Node");
/**
* @category Flow shape
*/
class BufferNode extends Node_1.Node {
constructor(options) {
super(options);
this.on('pull', this.onPull.bind(this));
this.on('push', this.onPush.bind(this));
this.on('build', this._initService.bind(this));
}
_initService() {
return new Promise((resolve) => {
if (!this.service) {
this.service = this.model.findDataService(this.options.service || DataFrame_1.DataFrame);
}
resolve();
});
}
onPull(options) {
return new Promise((resolve, reject) => {
this.shift()
.then((frame) => {
if (frame) {
this.outlets.forEach((outlet) => outlet.push(frame, options));
}
resolve();
})
.catch(reject);
});
}
onPush(frame) {
return new Promise((resolve, reject) => {
this.service
.insertFrame(frame)
.then(() => {
resolve();
})
.catch(reject);
});
}
next() {
return new Promise((resolve, reject) => {
this.service
.findOne({}, {
sort: [['createdTimestamp', 1]],
})
.then(resolve)
.catch(reject);
});
}
shift() {
return new Promise((resolve, reject) => {
let result;
this.next()
.then((frame) => {
if (frame) {
result = frame;
return this.service.delete(frame.uid);
}
else {
resolve(undefined);
}
})
.then(() => resolve(result))
.catch(reject);
});
}
}
exports.BufferNode = BufferNode;
//# sourceMappingURL=BufferNode.js.map