Skip to content
This repository has been archived by the owner on Sep 1, 2023. It is now read-only.

Fix race condition from early navigation #2

Merged
merged 3 commits into from
Aug 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,23 @@ const fixtures: Fixtures<
if (args) wrapFunctions(args, page, boundCallbacksForMount);

if (typeof storyId !== 'string') return {} as MountResult;
await page.goto('http://localhost:6006/iframe.html?id=' + storyId);
await page.goto('http://localhost:6006/iframe.html');
await page.evaluate(
async ({ storyId, args }) => {
const channel = __STORYBOOK_ADDONS_CHANNEL__;
const waitForStory = () =>
/**
* Perform a function that updates the story store and wait for the story to render
* or error
*/
const waitForStoryRender = async (updateFn: () => void) =>
new Promise((resolve, reject) => {
channel.on('storyRendered', () => resolve(void 0));
channel.on('storyUnchanged', () => resolve(void 0));
channel.on('storyErrored', (error) => reject(error));
channel.on('storyThrewException', (error) => reject(error));
channel.on('playFunctionThrewException', (error) => reject(error));
channel.on('storyMissing', (id) => id === storyId && reject(`Missing story ${id}`));
channel.once('storyRendered', () => resolve(void 0));
channel.once('storyUnchanged', () => resolve(void 0));
channel.once('storyErrored', (error) => reject(error));
channel.once('storyThrewException', (error) => reject(error));
channel.once('playFunctionThrewException', (error) => reject(error));
channel.once('storyMissing', (id) => id === storyId && reject(`Missing story ${id}`));
updateFn();
});

const unwrapFunctions = (object: any) => {
Expand All @@ -93,15 +98,18 @@ const fixtures: Fixtures<
}
};

await waitForStory();
await waitForStoryRender(() =>
channel.emit('setCurrentStory', { storyId, viewMode: 'story' })
);
if (args) {
unwrapFunctions(args);
const updatedArgs = args;
channel.emit('updateStoryArgs', {
storyId,
updatedArgs,
await waitForStoryRender(() => {
channel.emit('updateStoryArgs', {
storyId,
updatedArgs,
});
});
await waitForStory();
}
},
{ storyId, args }
Expand Down
Loading