Skip to content

Commit

Permalink
chore: more golf
Browse files Browse the repository at this point in the history
  • Loading branch information
maraisr committed Jul 9, 2023
1 parent 16d1d9b commit 02bda3b
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,24 @@ export function load<T, K = string>(
let keys: K[];

if (!batch) {
let tmp;
let i = 0;
batchContainer.set(loadFn, (batch = [[], (keys = []), (tasks = [])]));

// Once we know we have a fresh batch, we schedule this batch to run after
// all currently queued microtasks.
queueMicrotask(function () {
queueMicrotask(function processBatch() {
// As soon as we start processing this batch, we need to delete this
// batch from our container. This is because we want to ensure that
// any new requests for this batch will be added to a new batch.
batchContainer.delete(loadFn);

let tmp, i;
loadFn(keys).then(function (values) {
if (values.length !== tasks.length)
return reject(new Error('loader value length mismatch'));

for (
i = 0;
;
(tmp = values[i++]), i <= values.length;
tmp instanceof Error
? tasks[i - 1].r(tmp)
Expand All @@ -40,7 +41,7 @@ export function load<T, K = string>(
}, reject);

function reject(error: Error) {
for (i = 0; (tmp = tasks[i++]); tmp.r(error));
for (; (tmp = tasks[i++]); tmp.r(error));
}
});
}
Expand Down

0 comments on commit 02bda3b

Please sign in to comment.