homebridge-mqtt-ceiling-fan-remote
Version:
A remote using http commands to control a hunter ceiling fan via a tasmotized sonoff rfbridge for homebridge written in Typescript
43 lines • 1.34 kB
JavaScript
export default class DebounceQueue {
_delay = 300;
_timeout;
_queue;
_queueCallbacks;
constructor() {
this._timeout = null;
this._queue = [];
this._queueCallbacks = [];
}
queueContains(key) {
const queueIndex = this._queue.indexOf(key);
return queueIndex > -1;
}
queue(key, callback) {
const queueIndex = this._queue.indexOf(key);
if (queueIndex >= 0) {
if (queueIndex === 0 && this._timeout) {
clearTimeout(this._timeout);
this._timeout = null;
}
this._queue.splice(queueIndex, 1);
this._queueCallbacks.splice(queueIndex, 1);
}
const timeoutCallback = () => {
this._queue.splice(0, 1);
this._queueCallbacks.splice(0, 1);
callback();
if (this._queueCallbacks.length > 0) {
this._timeout = setTimeout(this._queueCallbacks[0], this._delay);
}
else {
this._timeout = null;
}
};
this._queue.push(key);
this._queueCallbacks.push(timeoutCallback);
if (!this._timeout) {
this._timeout = setTimeout(this._queueCallbacks[0], this._delay);
}
}
}
//# sourceMappingURL=debounceQueue.js.map