@any-routing/core
Version:
This library was generated with [Nx](https://nx.dev).
44 lines • 1.54 kB
JavaScript
import { __awaiter } from "tslib";
export class Requester {
constructor() {
this.latestResponseId = 0;
this.lastResponseId = 0;
this.buffer = [];
this.maxBuffer = 4;
this.pending = false;
}
get hasPendingRequests() {
return this.pending;
}
request(url, params) {
return __awaiter(this, void 0, void 0, function* () {
const controller = new AbortController();
const executionId = Date.now();
this.lastResponseId = executionId;
this.pending = true;
if (this.buffer.length > this.maxBuffer) {
this.buffer[0].abort();
this.buffer.splice(0, 1);
}
this.buffer.push(controller);
const response = yield fetch(url, Object.assign(Object.assign({}, params), { signal: controller.signal }));
this.cleanBuffer(controller);
if (response.status !== 200) {
throw response;
}
if (this.latestResponseId > executionId) {
throw new Error('Prev response');
}
this.pending = this.lastResponseId !== executionId;
this.latestResponseId = executionId;
return yield response.json();
});
}
abortAllRequests() {
this.buffer.forEach((buff) => buff.abort());
}
cleanBuffer(controller) {
this.buffer = this.buffer.filter((ctrl) => ctrl !== controller);
}
}
//# sourceMappingURL=requester.js.map