new approach

This commit is contained in:
Jörg Breitbart
2018-10-09 23:48:58 +02:00
parent febc3ba2b8
commit 33a358033c
3 changed files with 35 additions and 11 deletions
+18
View File
@@ -100,6 +100,24 @@ export class CircularList<T> extends EventEmitter implements ICircularList<T> {
}
}
/**
* Recycling push variant with a callback.
* The callback gets the value at the current position to be overwritten.
* Return the new value from the callback.
*/
public pushRecycling(callback: (el: T | undefined) => T): void {
this._array[this._getCyclicIndex(this._length)] = callback(this._array[this._getCyclicIndex(this._length)]);
if (this._length === this._maxLength) {
this._startIndex++;
if (this._startIndex === this._maxLength) {
this._startIndex = 0;
}
this.emit('trim', 1);
} else {
this._length++;
}
}
/**
* Removes and returns the last value on the list.
* @return The popped value.