mirror of https://github.com/vector-im/riot-web
Merge remote-tracking branch 'origin/develop' into develop
commit
023f96e817
|
@ -48,25 +48,24 @@ function checkBacklog() {
|
||||||
|
|
||||||
// Limit the maximum number of ongoing promises returned by fn to LIMIT and
|
// Limit the maximum number of ongoing promises returned by fn to LIMIT and
|
||||||
// use a FIFO queue to handle the backlog.
|
// use a FIFO queue to handle the backlog.
|
||||||
function limitConcurrency(fn) {
|
async function limitConcurrency(fn) {
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const item = () => {
|
|
||||||
ongoingRequestCount++;
|
|
||||||
resolve();
|
|
||||||
};
|
|
||||||
if (ongoingRequestCount >= LIMIT) {
|
if (ongoingRequestCount >= LIMIT) {
|
||||||
// Enqueue this request for later execution
|
// Enqueue this request for later execution
|
||||||
backlogQueue.push(item);
|
await new Promise((resolve, reject) => {
|
||||||
} else {
|
backlogQueue.push(resolve);
|
||||||
item();
|
});
|
||||||
}
|
}
|
||||||
})
|
|
||||||
.then(fn)
|
ongoingRequestCount++;
|
||||||
.then((result) => {
|
try {
|
||||||
|
return await fn();
|
||||||
|
} catch (err) {
|
||||||
|
// We explicitly do not handle the error here, but let it propogate.
|
||||||
|
throw err;
|
||||||
|
} finally {
|
||||||
ongoingRequestCount--;
|
ongoingRequestCount--;
|
||||||
checkBacklog();
|
checkBacklog();
|
||||||
return result;
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue