dockge/backend/utils/limit-queue.ts
Louis Lam 53b052c1e5
Check TypeScript for backend (#64)
* Check Typescript

* Fix backend typescript issues

* Update
2023-11-18 15:54:43 +08:00

24 lines
532 B
TypeScript

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