* Add item to batch queue
(type: string, payload: T)
| 49 | * Add item to batch queue |
| 50 | */ |
| 51 | add(type: string, payload: T): void { |
| 52 | if (this.options.disableBatch) { |
| 53 | // Fire and forget, but catch errors to prevent unhandled promise rejection |
| 54 | this.options.onSingleSend({ type, payload }).catch((error) => { |
| 55 | console.error('Error in onSingleSend:', error); |
| 56 | }); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | this.queue.push({ type, payload }); |
| 61 | |
| 62 | // Send immediately if queue reaches the limit |
| 63 | if (this.queue.length >= (this.options.maxBatchSize || 100)) { |
| 64 | // Fire and forget, but catch errors to prevent unhandled promise rejection |
| 65 | this.flush().catch((error) => { |
| 66 | console.error('Error in auto-flush:', error); |
| 67 | }); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | // Start timer only if not already running (throttling behavior) |
| 72 | if (!this.timer) { |
| 73 | this.timer = setTimeout(() => { |
| 74 | this.sendBatch().catch((error) => { |
| 75 | console.error('Error in scheduled batch send:', error); |
| 76 | }); |
| 77 | }, this.options.batchDelay); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Send batch request |
no test coverage detected