UNPKG

@pst-on-npm/homebridge-enocean

Version:
56 lines • 1.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TelegramQueue = void 0; class TelegramQueue { log; queue = []; isSending = false; sendMethod; constructor(sendMethod, log) { this.log = log; this.sendMethod = sendMethod; } enqueue(telegram) { this.queue.push(telegram); if (!this.isSending) { this.startSending(); } } async startSending() { if (this.isSending) { return; } this.isSending = true; while (this.queue.length > 0) { const telegram = this.queue.shift(); if (telegram) { await this.sendWithRetries(telegram, 3); await this.randomSleep(50, 150); } } this.isSending = false; } async sendWithRetries(telegram, maxRetries) { let attempts = 0; let lastError = null; while (attempts < maxRetries) { try { await this.sendMethod(telegram); return; // Success, exit function } catch (error) { attempts++; this.log.debug(`Send attempt ${attempts}/${maxRetries} failed: ${error}. Retrying...`); lastError = error; await this.randomSleep(50, 100); // Delay between retries } } this.log.warn(`Failed to send telegram ${lastError} after ${maxRetries} retries.`); } randomSleep(minMs, maxMs) { const sleepTime = Math.random() * (maxMs - minMs) + minMs; return new Promise(resolve => setTimeout(resolve, sleepTime)); } } exports.TelegramQueue = TelegramQueue; //# sourceMappingURL=TelegramQueue.js.map