Skip to content

Commit

Permalink
React: Force act running always in sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed Jan 6, 2025
1 parent de65bec commit 0f5d551
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion code/renderers/react/src/act-compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,41 @@ function withGlobalActEnvironment(actImplementation: (callback: () => void) => P
};
}

const actQueue: (() => Promise<void>)[] = [];
let isActing = false;

const processActQueue = async () => {
if (isActing || actQueue.length === 0) {
return;
}

isActing = true;
const actTask = actQueue.shift();
if (actTask) {
await actTask();
}
isActing = false;
processActQueue();
};

function withQueuedActEnvironment(actImplementation: (callback: () => void) => Promise<any>) {
const wrappedAct = withGlobalActEnvironment(actImplementation);
return (callback: () => any) => {
return new Promise<void>((resolve, reject) => {
actQueue.push(async () => {
try {
await wrappedAct(callback);
resolve();
} catch (error) {
reject(error);
}
});
processActQueue();
});
};
}

export const act =
process.env.NODE_ENV === 'production'
? (cb: (...args: any[]) => any) => cb()
: withGlobalActEnvironment(reactAct);
: withQueuedActEnvironment(reactAct);

0 comments on commit 0f5d551

Please sign in to comment.