Limit number of simultaneous requests to the API

This commit is contained in:
D. Berge
2025-08-01 17:11:34 +02:00
parent ad013ea642
commit af7485370c
3 changed files with 8 additions and 2 deletions

View File

@@ -1,3 +1,4 @@
const ConcurrencyLimiter = require('@dougal/concurrency');
/** Make an API request
*
@@ -18,6 +19,9 @@
* to `true`).
*/
async function api ({state, getters, commit, dispatch}, [resource, init = {}, cb, opts = {}]) {
const limiter = api.limiter || (api.limiter = new ConcurrencyLimiter(state.maxConcurrent));
try {
commit("queueRequest");
if (init && init.hasOwnProperty("body")) {
@@ -63,7 +67,7 @@ async function api ({state, getters, commit, dispatch}, [resource, init = {}, cb
}
if (!res) {
res = await fetch(url, init);
res = await limiter.enqueue(async () => await fetch(url, init));
}
if (cache && !isCached) {

View File

@@ -1,6 +1,7 @@
const state = () => ({
apiUrl: "/api",
requestsCount: 0
requestsCount: 0,
maxConcurrent: 15
});
export default state;