homebridge-kasa-python
Version:
Plugin that uses Python-Kasa API to communicate with Kasa Devices.
44 lines • 1.11 kB
JavaScript
export class TaskQueue {
queue = [];
running = false;
log;
resolveEmptyQueue = null;
constructor(log) {
this.log = log;
}
addTask(task) {
this.queue.push(task);
this.processQueue();
}
async processQueue() {
if (this.running) {
return;
}
this.running = true;
while (this.queue.length > 0) {
const task = this.queue.shift();
if (task) {
try {
await task();
}
catch (error) {
this.log.error('Error processing task:', error);
}
}
}
this.running = false;
if (this.resolveEmptyQueue) {
this.resolveEmptyQueue();
this.resolveEmptyQueue = null;
}
}
async waitForEmptyQueue() {
if (this.queue.length === 0 && !this.running) {
return;
}
return new Promise((resolve) => {
this.resolveEmptyQueue = resolve;
});
}
}
//# sourceMappingURL=taskQueue.js.map