* Resize BufferLine to `cols` filling excess cells with `fillCellData`. * The underlying array buffer will not change if there is still enough space * to hold the new buffer line data. * Returns a boolean indicating, whether a `cleanupMemory` call would free * excess memory (true after s
(cols: number, fillCellData: ICellData)
| 348 | * excess memory (true after shrinking > CLEANUP_THRESHOLD). |
| 349 | */ |
| 350 | public resize(cols: number, fillCellData: ICellData): boolean { |
| 351 | if (cols === this.length) { |
| 352 | return this._data.length * 4 * CLEANUP_THRESHOLD < this._data.buffer.byteLength; |
| 353 | } |
| 354 | const uint32Cells = cols * CELL_SIZE; |
| 355 | if (cols > this.length) { |
| 356 | if (this._data.buffer.byteLength >= uint32Cells * 4) { |
| 357 | // optimization: avoid alloc and data copy if buffer has enough room |
| 358 | this._data = new Uint32Array(this._data.buffer, 0, uint32Cells); |
| 359 | } else { |
| 360 | // slow path: new alloc and full data copy |
| 361 | const data = new Uint32Array(uint32Cells); |
| 362 | data.set(this._data); |
| 363 | this._data = data; |
| 364 | } |
| 365 | for (let i = this.length; i < cols; ++i) { |
| 366 | this.setCell(i, fillCellData); |
| 367 | } |
| 368 | } else { |
| 369 | // optimization: just shrink the view on existing buffer |
| 370 | this._data = this._data.subarray(0, uint32Cells); |
| 371 | // Remove any cut off combined data |
| 372 | const keys = Object.keys(this._combined); |
| 373 | for (let i = 0; i < keys.length; i++) { |
| 374 | const key = parseInt(keys[i], 10); |
| 375 | if (key >= cols) { |
| 376 | delete this._combined[key]; |
| 377 | } |
| 378 | } |
| 379 | // remove any cut off extended attributes |
| 380 | const extKeys = Object.keys(this._extendedAttrs); |
| 381 | for (let i = 0; i < extKeys.length; i++) { |
| 382 | const key = parseInt(extKeys[i], 10); |
| 383 | if (key >= cols) { |
| 384 | delete this._extendedAttrs[key]; |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | this.length = cols; |
| 389 | return uint32Cells * 4 * CLEANUP_THRESHOLD < this._data.buffer.byteLength; |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Cleanup underlying array buffer. |