| 168 | } |
| 169 | |
| 170 | private flush(): void { |
| 171 | if (this.buffer.size === 0) return; |
| 172 | |
| 173 | const startTime = nowMs(); |
| 174 | const bufferedEvents = Array.from(this.buffer.values()); |
| 175 | const logPayload = getBatchedEventsLogPayload(bufferedEvents); |
| 176 | const { rawEventCount, mergedEventCount } = logPayload; |
| 177 | |
| 178 | const events = bufferedEvents.map(({ key, payload }) => ({ |
| 179 | key, |
| 180 | payload |
| 181 | })); |
| 182 | |
| 183 | log.trace('Flushing batched events', logPayload); |
| 184 | |
| 185 | this.buffer = new Map(); |
| 186 | this.onFlush(events); |
| 187 | |
| 188 | const durationMs = elapsedMs(startTime); |
| 189 | if (durationMs > 10) { |
| 190 | log.warn('Event batch processing took longer than expected', { |
| 191 | rawEventCount, |
| 192 | mergedEventCount, |
| 193 | durationMs, |
| 194 | }); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | flushNow(): void { |
| 199 | if (this.frameId !== null) { |