Skip to content

Commit

Permalink
fix: external reject size scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
nmathew98 committed Mar 29, 2024
1 parent 56e90aa commit 2751085
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/shared/src/batcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ export const makeIntervalScheduler = (ms?: number) => {

export const makeSizeScheduler = (minimumSize: number) => {
let externalResolve: null | ((value: any) => void) = null;
let externalReject: null | ((reason: unknown) => void) = null;
let pendingExecution = Promise.resolve();

return (executor: any, pending: any[]) => {
if (pending.length < minimumSize) {
const next: Promise<void> = new Promise(resolve => {
const next: Promise<void> = new Promise((resolve, reject) => {
externalResolve = resolve;
externalReject = reject;
});

pendingExecution = pendingExecution.then(() => next);
Expand All @@ -74,7 +76,11 @@ export const makeSizeScheduler = (minimumSize: number) => {
}

const consumed = pending.splice(0, Infinity);
externalResolve?.(executor(consumed));
try {
externalResolve?.(executor(consumed));
} catch (error: unknown) {
externalReject?.(error);
}

return pendingExecution;
};
Expand Down

0 comments on commit 2751085

Please sign in to comment.