2023-11-11 15:18:37 +01:00
|
|
|
/**
|
|
|
|
* Limit Queue
|
|
|
|
* The first element will be removed when the length exceeds the limit
|
|
|
|
*/
|
|
|
|
export class LimitQueue<T> extends Array<T> {
|
|
|
|
__limit;
|
2023-11-18 08:54:43 +01:00
|
|
|
__onExceed? : (item : T | undefined) => void;
|
2023-11-11 15:18:37 +01:00
|
|
|
|
|
|
|
constructor(limit: number) {
|
|
|
|
super();
|
|
|
|
this.__limit = limit;
|
|
|
|
}
|
|
|
|
|
2023-11-18 08:54:43 +01:00
|
|
|
pushItem(value : T) {
|
2023-11-11 15:18:37 +01:00
|
|
|
super.push(value);
|
|
|
|
if (this.length > this.__limit) {
|
|
|
|
const item = this.shift();
|
|
|
|
if (this.__onExceed) {
|
|
|
|
this.__onExceed(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|