homebridge-smartsystem
Version:
SmartServer (Proxy Websockets to TCP sockets, Smappee MQTT, Duotecno IP Nodes, Homekit interface)
52 lines • 1.72 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Q = void 0;
const logger_1 = require("./logger");
class Q {
constructor() {
this.timer = null;
this.queue = [];
logger_1.logSettings["Q"] = logger_1.LogLevel.log;
}
exec(fn) {
const len = this.queue.length;
this.queue.push(fn);
// start timer to execute this functon if nobody else calls "do"
// this.logger("exec, we've put stuff in the queue, start " + (len ? "long timer" : "short timer"));
this.startWaiter(len ? 500 : 0);
}
endWaiter() {
if (this.timer) {
(0, logger_1.debug)("Q", "Waiter: clearing timer");
clearTimeout(this.timer);
}
}
startWaiter(mSecs = 1000) {
this.endWaiter();
(0, logger_1.debug)("Q", "Waiter: starting timer for " + mSecs + " mSec");
this.timer = setTimeout(() => {
(0, logger_1.debug)("Q", "Waiter: timer finished, calling 'do' because nobody else did");
this.do();
}, mSecs);
}
do() {
(0, logger_1.debug)("Q", "Do, qlen=" + this.queue.length);
// Get the oldest function and execute
if (this.queue.length > 0) {
const fn = this.queue[0];
this.queue.splice(0, 1);
(0, logger_1.debug)("Q", "Do: calling function");
fn();
}
// Still stuff to execute
if (this.queue.length > 0) {
(0, logger_1.debug)("Q", "Do, still stuff in the queue, start timer");
this.startWaiter();
}
else {
this.endWaiter();
}
}
}
exports.Q = Q;
//# sourceMappingURL=Q.js.map