UNPKG

@openhps/core

Version:

Open Hybrid Positioning System - Core component

75 lines 2.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TimeSyncNode = void 0; const service_1 = require("../../service"); const MemoryBufferNode_1 = require("./MemoryBufferNode"); /** * Time synchronization node. * @category Flow shape */ class TimeSyncNode extends MemoryBufferNode_1.MemoryBufferNode { constructor(options) { super(options); this.on('build', this._initTimer.bind(this)); this.on('destroy', this._stopTimer.bind(this)); } _initTimer() { this._timer = setInterval(() => { this.triggerUpdate(); }, this.options.checkInterval || 100); } _stopTimer() { clearInterval(this._timer); } onPush(frame, options) { return new Promise((resolve, reject) => { if (frame.createdTimestamp <= service_1.TimeService.now()) { this.triggerUpdate() .then(() => { this.outlets.forEach((outlet) => outlet.push(frame, options)); resolve(); }) .catch(reject); } else { super .onPush(frame) .then(() => { resolve(); }) .catch(reject); } }); } triggerUpdate() { return new Promise((resolve, reject) => { this.shift() .then((frame) => { if (frame) { this.outlets.forEach((outlet) => outlet.push(frame)); return this.triggerUpdate(); } else { resolve(); } }) .then(resolve) .catch(reject); }); } next() { return new Promise((resolve, reject) => { super .next() .then((frame) => { if (frame && frame.createdTimestamp <= service_1.TimeService.now()) { return resolve(frame); } resolve(undefined); }) .catch(reject); }); } } exports.TimeSyncNode = TimeSyncNode; //# sourceMappingURL=TimeSyncNode.js.map