mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 19:49:50 +02:00
feat: added queues to fetchBlitz
This commit is contained in:
1 parent
2c45f9759f
commit
fd61dae1c1
14 files changed
+65
-35
No files matched your search
@@ -0,0 +1,53 @@
|
||||
export type BlitzResponse<Data extends object> =
|
||||
| {
|
||||
status: 'error';
|
||||
error: { field: string; message: string; code: 402; value: null };
|
||||
}
|
||||
| {
|
||||
status: 'ok';
|
||||
data: Data;
|
||||
};
|
||||
|
||||
const MAX_CALLS_PER_SECOND = 10;
|
||||
|
||||
const queue: { url: string; resolve: (data: object) => void }[] = [];
|
||||
let inProgress = 0;
|
||||
|
||||
async function manageQueue() {
|
||||
if (queue.length > 0 && inProgress < MAX_CALLS_PER_SECOND) {
|
||||
inProgress++;
|
||||
|
||||
setTimeout(() => {
|
||||
inProgress--;
|
||||
}, 1000);
|
||||
|
||||
const { url, resolve } = queue.shift()!;
|
||||
const data = (await fetch(url)
|
||||
.then((response) => response.json())
|
||||
.catch((error) => ({ status: 'error', error }))) as BlitzResponse<
|
||||
typeof resolve
|
||||
>;
|
||||
|
||||
manageQueue();
|
||||
|
||||
if (data.status === 'ok') {
|
||||
resolve(data.data);
|
||||
} else {
|
||||
throw new Error(`Wargaming response error status:"${data.status}"`, {
|
||||
cause: `Message: "${data.error.message}"\nURL: "${url}"`,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default function fetchBlitz<Data extends object>(url: string) {
|
||||
return new Promise<BlitzResponse<Data>>((resolve) => {
|
||||
queue.push({
|
||||
url,
|
||||
resolve(data) {
|
||||
resolve(data as BlitzResponse<Data>);
|
||||
},
|
||||
});
|
||||
manageQueue();
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user