24 lines
No EOL
573 B
TypeScript
24 lines
No EOL
573 B
TypeScript
export class BatchTimer {
|
|
private _batch: string[];
|
|
private _timer: number;
|
|
private _reqFn: (id: string[])=>void;
|
|
|
|
constructor(reqFn: (id: string[])=>void) {
|
|
this._batch = [];
|
|
this._timer = new Date().getTime();
|
|
this._reqFn = reqFn;
|
|
}
|
|
|
|
public queue(id: string, timeout: number){
|
|
this._timer = new Date().getTime() + timeout;
|
|
this._batch.push(id);
|
|
setTimeout(this.checkBatch, timeout);
|
|
}
|
|
|
|
private checkBatch() {
|
|
if ((new Date()).getTime() >= this._timer) {
|
|
this._reqFn(this._batch);
|
|
this._batch = [];
|
|
}
|
|
}
|
|
} |