Keep this in a separate method so that GC doesn't keep it alive for any longer than it should.
()
| 187 | |
| 188 | /** Keep this in a separate method so that GC doesn't keep it alive for any longer than it should. */ |
| 189 | #queueToBuffers(): Buffer { |
| 190 | // Use .splice to in-place mutate the array, informing the GC it can free it. |
| 191 | const queue = this.#queue.splice(0, this.#queue.length) |
| 192 | |
| 193 | let totalLength = 0 |
| 194 | for (const str of queue) { |
| 195 | totalLength += Buffer.byteLength(str, 'utf8') |
| 196 | } |
| 197 | |
| 198 | const buffer = Buffer.allocUnsafe(totalLength) |
| 199 | let offset = 0 |
| 200 | for (const str of queue) { |
| 201 | offset += buffer.write(str, offset, 'utf8') |
| 202 | } |
| 203 | |
| 204 | return buffer |
| 205 | } |
| 206 | |
| 207 | async #drain(): Promise<void> { |
| 208 | try { |