2024-12-06 04:05:01 +00:00
|
|
|
import util from './util'
|
|
|
|
|
2024-12-12 03:19:11 +00:00
|
|
|
export class Fetcher {
|
2024-08-03 16:52:33 +00:00
|
|
|
private _batch: string[];
|
|
|
|
private _timer: number;
|
|
|
|
private _reqFn: (id: string[])=>void;
|
|
|
|
|
2024-12-06 04:05:01 +00:00
|
|
|
constructor(gateway: string, adapter: string, etype: string) {
|
2024-08-03 16:52:33 +00:00
|
|
|
this._batch = [];
|
|
|
|
this._timer = new Date().getTime();
|
2024-12-06 04:05:01 +00:00
|
|
|
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);
|
|
|
|
};
|
2024-08-03 16:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public queue(id: string, timeout: number){
|
2024-08-23 01:49:17 +00:00
|
|
|
if (!this._batch.includes(id)) {
|
|
|
|
this._timer = new Date().getTime() + timeout;
|
|
|
|
this._batch.push(id);
|
|
|
|
setTimeout(this.checkBatch.bind(this), timeout);
|
|
|
|
}
|
2024-08-03 16:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private checkBatch() {
|
|
|
|
if ((new Date()).getTime() >= this._timer) {
|
|
|
|
this._reqFn(this._batch);
|
|
|
|
this._batch = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|