UNPKG

@openhps/core

Version:

Open Hybrid Positioning System - Core component

43 lines 1.05 kB
import { ProcessingNode } from '../ProcessingNode'; import { TimeUnit } from '../../utils'; /** * @category Flow shape */ export class FrameDebounceNode extends ProcessingNode { constructor(timeout, timeoutUnit) { super(); this._accept = true; this._timeout = timeout; this._timeoutUnit = timeoutUnit; this.once('build', this._start.bind(this)); this.once('destroy', this._stop.bind(this)); } /** * Start the timeout timer * @returns {Promise<void>} Timer promise */ _start() { return new Promise(resolve => { this._timer = setInterval(() => { this._accept = true; }, this._timeoutUnit.convert(this._timeout, TimeUnit.MILLISECOND)); resolve(); this.emit('ready'); }); } _stop() { if (this._timer !== undefined) { clearInterval(this._timer); } } process(frame) { return new Promise(resolve => { if (this._accept) { this._accept = false; resolve(frame); } else { resolve(undefined); } }); } }