underbbs/frontend/ts/fetcher.ts

34 lines
No EOL
894 B
TypeScript

import util from './util'
export class Fetcher {
private _batch: string[];
private _timer: number;
private _reqFn: (id: string[])=>void;
constructor(gateway: string, adapter: string, etype: string) {
this._batch = [];
this._timer = new Date().getTime();
this._reqFn = (ids: string[])=>{
let url = `${gateway}/api/adapters/${adapter}/fetch?entity_type=${etype}`;
for (let id of ids) {
url += `&entity_id=${id}`;
}
util.authorizedFetch("GET", url, null);
};
}
public queue(id: string, timeout: number){
if (!this._batch.includes(id)) {
this._timer = new Date().getTime() + timeout;
this._batch.push(id);
setTimeout(this.checkBatch.bind(this), timeout);
}
}
private checkBatch() {
if ((new Date()).getTime() >= this._timer) {
this._reqFn(this._batch);
this._batch = [];
}
}
}