UNPKG

@robotical/appv2-warranty-service-lib

Version:

A tool that gathers data from the Apps and sends it to the Analytics server

48 lines (47 loc) 1.5 kB
export default class TicketQueue { constructor() { this.queue = []; this.isProcessing = false; this.resultsQueue = []; } addTicket(ticket) { this.queue.push(ticket); } getQueue() { return [...this.queue]; } removeTicket(ticket) { this.queue = this.queue.filter((t) => t !== ticket); } async processTicketsQueue(cb) { if (this.isProcessing) return; this.isProcessing = true; try { for (const ticket of this.queue) { try { const resultId = await cb(ticket.email, ticket.serialNumber, ticket.reportTitle, ticket.reportDescription); this.resultsQueue.push({ ticketId: ticket.ticketId, result: resultId }); } catch (error) { console.error(`Error processing ticket: ${error}`); } this.removeTicket(ticket); await new Promise(resolve => setTimeout(resolve, Math.random() * 2000)); } } catch (error) { console.error(`Error processing tickets queue: ${error}`); } finally { this.isProcessing = false; } this.isProcessing = false; } getResultByTicketId(ticketId) { const result = this.resultsQueue.find((r) => r.ticketId === ticketId); if (!result) return -1; return result.result; } }