revert to precheck pushWouldTrim

This commit is contained in:
Jörg Breitbart
2018-10-30 17:40:11 +01:00
parent aaffc2b2be
commit fb64c527a5
7 changed files with 25 additions and 46 deletions
-16
View File
@@ -257,20 +257,4 @@ describe('CircularList', () => {
assert.equal(list.get(3), 4);
});
});
describe('trimAndRecycle', function(): void {
it('should return correct element', function(): void {
const list = new CircularList<number[]>(5);
list.push([0]);
list.push([1]);
list.push([2]);
list.push([3]);
list.push([4]);
assert.equal(list.trimAndRecycle()[0], 0);
assert.equal(list.trimAndRecycle()[0], 1);
assert.equal(list.trimAndRecycle()[0], 2);
assert.equal(list.trimAndRecycle()[0], 3);
assert.equal(list.trimAndRecycle()[0], 4);
assert.equal(list.trimAndRecycle()[0], 0);
});
});
});
+5 -15
View File
@@ -90,10 +90,7 @@ export class CircularList<T> extends EventEmitter implements ICircularList<T> {
public push(value: T): void {
this._array[this._getCyclicIndex(this._length)] = value;
if (this._length === this._maxLength) {
this._startIndex++;
if (this._startIndex === this._maxLength) {
this._startIndex = 0;
}
this._startIndex = ++this._startIndex % this._maxLength;
this.emit('trim', 1);
} else {
this._length++;
@@ -101,18 +98,11 @@ export class CircularList<T> extends EventEmitter implements ICircularList<T> {
}
/**
* Recycling trim.
* This is used to recycle buffer lines in Terminal.scroll when
* the list is at maxLength as a push replacement.
* Returns the old line as new one to be recycled.
* Note: There are no bound checks for performance reasons,
* the method is a special optimization for Terminal.scroll,
* do not use it anywhere else.
* Whether a push would trim.
* True when the ringbuffer is full.
*/
public trimAndRecycle(): T | undefined {
this._startIndex = ++this._startIndex % this._maxLength;
this.emit('trim', 1);
return this._array[this._getCyclicIndex(this._length - 1)];
public pushWouldTrim(): boolean {
return this._length === this._maxLength;
}
/**
+1 -1
View File
@@ -28,7 +28,7 @@ export interface ICircularList<T> extends IEventEmitter {
get(index: number): T | undefined;
set(index: number, value: T): void;
push(value: T): void;
trimAndRecycle(): T | undefined;
pushWouldTrim(): boolean;
pop(): T | undefined;
splice(start: number, deleteCount: number, ...items: T[]): void;
trimStart(count: number): void;