mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-21 18:57:03 +02:00
60 lines
1.4 KiB
TypeScript
60 lines
1.4 KiB
TypeScript
import { patientFetch } from '../blitzkrieg/patientFetch';
|
|
|
|
type BlitzResponse<Data extends object> =
|
|
| {
|
|
status: 'error';
|
|
error: { field: string; message: string; code: 402; value: null };
|
|
}
|
|
| {
|
|
status: 'ok';
|
|
data: Data;
|
|
};
|
|
|
|
// 1 less than 10 to ensure not bad things lol
|
|
const MAX_CALLS_PER_SECOND = 9;
|
|
|
|
const queue: { url: string; resolve: (data: object) => void }[] = [];
|
|
let inProgress = 0;
|
|
|
|
async function manageQueue() {
|
|
if (queue.length > 0 && inProgress < MAX_CALLS_PER_SECOND) {
|
|
inProgress++;
|
|
|
|
const timeout = setTimeout(() => {
|
|
inProgress--;
|
|
manageQueue();
|
|
}, 1000);
|
|
|
|
const { url, resolve } = queue.shift()!;
|
|
const data = (await patientFetch(url)
|
|
.then((response) => response.json())
|
|
.catch((error) => ({ status: 'error', error }))) as BlitzResponse<
|
|
typeof resolve
|
|
>;
|
|
|
|
if (data.status === 'ok') {
|
|
resolve(data.data);
|
|
} else {
|
|
clearTimeout(timeout);
|
|
inProgress--;
|
|
manageQueue();
|
|
|
|
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<Data>((resolve) => {
|
|
queue.push({
|
|
url,
|
|
resolve(data) {
|
|
resolve(data as Data);
|
|
},
|
|
});
|
|
manageQueue();
|
|
});
|
|
}
|