UNPKG

homebridge-tsvesync

Version:

Homebridge plugin for VeSync devices including Levoit air purifiers, humidifiers, and Etekcity smart outlets

54 lines 2.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RetryManager = void 0; class RetryManager { constructor(log, config = { maxRetries: 3 }) { this.log = log; this.config = config; this.retryCount = 0; this.maxRetries = config.maxRetries; } /** * Get the current retry count */ getRetryCount() { return this.retryCount; } /** * Execute an operation with retry logic */ async execute(operation, context) { this.retryCount = 0; while (this.retryCount < this.maxRetries) { try { const result = await operation(); // If result is null and we have retries left, try again if (result === null && this.retryCount < this.maxRetries - 1) { this.retryCount++; const delay = Math.min(2000 * Math.pow(2, this.retryCount - 1), 10000); this.log.debug(`[${context.deviceName}] === RETRY MANAGER: Attempt ${this.retryCount}/${this.maxRetries}, waiting ${delay}ms ===`); await new Promise(resolve => setTimeout(resolve, delay)); continue; } // If this is our last try and result is null, throw error if (result === null && this.retryCount >= this.maxRetries - 1) { throw new Error(`Failed to ${context.operation} - got null result after ${this.maxRetries} attempts`); } return result; } catch (error) { this.retryCount++; if (this.retryCount >= this.maxRetries) { throw error; } // Calculate delay based on attempt number const delay = Math.min(2000 * Math.pow(2, this.retryCount - 1), 10000); this.log.debug(`[${context.deviceName}] === RETRY MANAGER: Attempt ${this.retryCount}/${this.maxRetries}, waiting ${delay}ms ===`); await new Promise(resolve => setTimeout(resolve, delay)); } } throw new Error(`Failed to ${context.operation} after ${this.maxRetries} attempts`); } } exports.RetryManager = RetryManager; //# sourceMappingURL=retry.js.map