Fix trimming on a buffer with 0 scrollback

Unfortunately this removed the padding idea from CircularList as it
would only work when the list is not full.
This commit is contained in:
Daniel Imms
2017-08-06 18:56:37 -07:00
parent a57f55dcbf
commit e7ee3ec63c
4 changed files with 24 additions and 56 deletions
+3 -25
View File
@@ -8,15 +8,6 @@
import { EventEmitter } from '../EventEmitter';
import { ICircularList } from '../Interfaces';
/**
* Padding added to the max length of the CircularList, this allows increasing
* the max length of the list without reconstructing the backing array and then
* copying over the values.
*/
const MAX_LENGTH_PADDING = 40;
const MAX_LENGTH_SHRINK_REBUILD_THRESHOLD = MAX_LENGTH_PADDING * 3;
export class CircularList<T> extends EventEmitter implements ICircularList<T> {
protected _array: T[];
private _startIndex: number;
@@ -26,7 +17,7 @@ export class CircularList<T> extends EventEmitter implements ICircularList<T> {
private _maxLength: number
) {
super();
this._array = new Array<T>(this._maxLength + MAX_LENGTH_PADDING);
this._array = new Array<T>(this._maxLength);
this._startIndex = 0;
this._length = 0;
}
@@ -41,22 +32,9 @@ export class CircularList<T> extends EventEmitter implements ICircularList<T> {
return;
}
// Ensure the array has padding to expand into.
if (this._array.length >= newMaxLength) {
// Ensure that the array hasn't shrunk significantly, if it has we
// should rebuild the array with a new max length to reclaim some memory.
if (this._array.length - newMaxLength <= MAX_LENGTH_SHRINK_REBUILD_THRESHOLD) {
// Increase the max length and return.
this._maxLength = newMaxLength;
return;
}
}
// Reconstruct array, starting at index 0. Only transfer values from the
// indexes 0 to length. The new array's size includes some padding that
// allows the array's size to increase a little without rebuilding the
// array.
let newArray = new Array<T>(newMaxLength + MAX_LENGTH_PADDING);
// indexes 0 to length.
let newArray = new Array<T>(newMaxLength);
for (let i = 0; i < Math.min(newMaxLength, this.length); i++) {
newArray[i] = this._array[this._getCyclicIndex(i)];
}