| 50 | async finish(): Promise<void> { await this.flush(true); } |
| 51 | |
| 52 | private async flush(final: boolean): Promise<void> { |
| 53 | if (this.flushing) { this.dirty = true; return; } // serialize: one in-flight network op at a time |
| 54 | this.flushing = true; |
| 55 | try { |
| 56 | this.dirty = false; |
| 57 | const tail = this.buf.slice(this.offset); |
| 58 | if (tail === '' && !this.cur) return; // nothing to show yet |
| 59 | const pieces = splitForStream(tail, this.io.maxLen); |
| 60 | for (let i = 0; i < pieces.length; i++) { |
| 61 | const piece = pieces[i]!; |
| 62 | const last = i === pieces.length - 1; |
| 63 | const body = piece.display || (last ? '…' : ''); |
| 64 | if (!this.cur) { this.cur = await this.io.send(body); this.curText = body; } |
| 65 | else if (this.curText !== body) { await this.io.edit(this.cur, body); this.curText = body; } |
| 66 | if (!last) { this.offset += piece.consumed; this.cur = null; this.curText = ''; } // finalize & move on |
| 67 | } |
| 68 | this.lastFlushAt = this.io.now(); |
| 69 | } finally { |
| 70 | this.flushing = false; |
| 71 | } |
| 72 | if (this.dirty && final) await this.flush(true); // re-entrant push happened mid-flight |
| 73 | } |
| 74 | } |