-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract ConcurrencyLimitedFetch into own file
- Loading branch information
1 parent
57aa1bf
commit e967a4d
Showing
7 changed files
with
67 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Limits the number of concurrent requests to the API. | ||
* | ||
* 1. Prevents flooding the API when full recalculation occurs in a sheet with | ||
* many completion cells (e.g. when opening the workbook). | ||
* 2. Gives the user a chance to cancel mass recalculation (e.g. by undo or | ||
* delete) before all the requests are dispatched. | ||
*/ | ||
export default class ConcurrencyLimitedFetch { | ||
/** | ||
* High enough to be unnoticeable for small scenarios, and to complete large | ||
* scenarios in reasonable time. | ||
* Low enough to avoid incurring excessive costs before the user has a chance | ||
* to cancel, even for large model input/output sizes. | ||
*/ | ||
static _PENDING_LIMIT = 10; | ||
|
||
_queue = []; | ||
_pendingCount = 0; | ||
|
||
fetch(resource, options) { | ||
const promise = new Promise((resolve, reject) => { | ||
const task = { | ||
args: { resource, options }, | ||
resolve, | ||
reject, | ||
}; | ||
this._queue.push(task); | ||
}); | ||
|
||
this._process(); | ||
|
||
return promise; | ||
} | ||
|
||
_process() { | ||
if ( | ||
this._queue.length === 0 || | ||
this._pendingCount > ConcurrencyLimitedFetch._PENDING_LIMIT | ||
) { | ||
return; | ||
} | ||
|
||
const task = this._queue.shift(); | ||
|
||
if (task.args.options.signal.aborted) { | ||
task.reject(task.args.options.signal.reason); | ||
} | ||
|
||
this._pendingCount++; | ||
const promise = fetch(task.args.resource, task.args.options); | ||
|
||
promise.then(task.resolve, task.reject); | ||
promise.finally(() => { | ||
this._pendingCount--; | ||
this._process(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters