@openhps/core
Version:
Open Hybrid Positioning System - Core component
57 lines • 1.55 kB
JavaScript
import { TimeService } from '../../service';
import { MemoryBufferNode } from './MemoryBufferNode';
/**
* Time synchronization node.
* @category Flow shape
*/
export class TimeSyncNode extends 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 <= 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 <= TimeService.now()) {
return resolve(frame);
}
resolve(undefined);
}).catch(reject);
});
}
}