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 cc60f03
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions code/renderers/react/src/renderToCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ class ErrorBoundary extends ReactComponent<{

const Wrapper = FRAMEWORK_OPTIONS?.strictMode ? StrictMode : Fragment;

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();
};

export async function renderToCanvas(
{
storyContext,
Expand Down Expand Up @@ -81,12 +98,22 @@ export async function renderToCanvas(
unmountElement(canvasElement);
}

await act(async () => {
await renderElement(element, canvasElement, storyContext?.parameters?.react?.rootOptions);
await new Promise<void>((resolve, reject) => {
try {
actQueue.push(async () => {
await act(async () => {
await renderElement(element, canvasElement, storyContext?.parameters?.react?.rootOptions);
resolve();
});
});
processActQueue();
} catch (e) {
reject(e);
}
});

return async () => {
await act(() => {
return () => {
act(() => {
unmountElement(canvasElement);
});
};
Expand Down

0 comments on commit cc60f03

Please sign in to comment.