* Send what is queued, one batch now and the rest on the timer. With * `final` the page is going away: the transport is told so, nothing is * rescheduled, and a batch goes out for each page the entries were * recorded on.
(final = false)
| 274 | * recorded on. |
| 275 | */ |
| 276 | async flush(final = false): Promise<void> { |
| 277 | if (this.timer !== undefined) { |
| 278 | this.cancel(this.timer); |
| 279 | this.timer = undefined; |
| 280 | } |
| 281 | if (this.inFlight && !final) { |
| 282 | return; |
| 283 | } |
| 284 | if (final) { |
| 285 | // The page is going away: every count not yet reported goes with it. |
| 286 | for (const repeat of this.repeats.values()) { |
| 287 | if (repeat.count > repeat.reported) { |
| 288 | this.report(repeat); |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | if (this.queue.length === 0) { |
| 293 | return; |
| 294 | } |
| 295 | // One page per post: the batch ends where an entry of another page starts. |
| 296 | const page = this.pageOf(this.queue[0]); |
| 297 | let size = 1; |
| 298 | while (size < Math.min(this.batchMax, this.queue.length) && this.pageOf(this.queue[size]) === page) { |
| 299 | size += 1; |
| 300 | } |
| 301 | const batch = this.queue.splice(0, size); |
| 302 | if (this.droppedSinceFlush > 0) { |
| 303 | const lost = this.droppedSinceFlush; |
| 304 | this.droppedSinceFlush = 0; |
| 305 | const notice: UiLogEntry = { |
| 306 | // Loss can span several projects. Do not attribute the aggregate |
| 307 | // to whichever project happens to be open when delivery resumes. |
| 308 | project: '', |
| 309 | ts: this.now().toISOString(), |
| 310 | seq: ++this.seq, |
| 311 | level: 'warn', |
| 312 | source: 'ui-log', |
| 313 | message: `${lost} entries were dropped before this batch: the page keeps ${this.bufferMax} while the server does not answer`, |
| 314 | }; |
| 315 | this.pages.set(notice, page); |
| 316 | batch.unshift(notice); |
| 317 | } |
| 318 | const payload: UiLogPayload = { |
| 319 | page, |
| 320 | session: this.options.session, |
| 321 | entries: batch, |
| 322 | }; |
| 323 | this.inFlight = true; |
| 324 | let accepted = false; |
| 325 | try { |
| 326 | accepted = await this.options.transport.send(payload, final); |
| 327 | } catch { |
| 328 | accepted = false; |
| 329 | } finally { |
| 330 | this.inFlight = false; |
| 331 | } |
| 332 | if (accepted) { |
| 333 | this.sent += batch.length; |