Skip to content

Commit

Permalink
Merge pull request #16947 from storybookjs/16877-await-init-before-cache
Browse files Browse the repository at this point in the history
Core: Ensure we have a full story index before caching
  • Loading branch information
shilman committed Dec 9, 2021
1 parent 3013a32 commit fc9853a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
18 changes: 17 additions & 1 deletion lib/store/src/StoryStore.test.ts
Expand Up @@ -281,7 +281,7 @@ describe('StoryStore', () => {
const store = new StoryStore();
store.setProjectAnnotations(projectAnnotations);
store.initialize({ storyIndex, importFn, cache: false });
await store.cacheAllCSFFiles(false);
await store.cacheAllCSFFiles();

await store.loadStory({ storyId: 'component-one--a' });
expect(importFn).toHaveBeenCalledWith(storyIndex.stories['component-one--a'].importPath);
Expand Down Expand Up @@ -943,4 +943,20 @@ describe('StoryStore', () => {
});
});
});

describe('cacheAllCsfFiles', () => {
describe('if the store is not yet initialized', () => {
it('waits for initialization', async () => {
const store = new StoryStore();

importFn.mockClear();
const cachePromise = store.cacheAllCSFFiles();

store.setProjectAnnotations(projectAnnotations);
store.initialize({ storyIndex, importFn, cache: false });

await expect(cachePromise).resolves.toEqual(undefined);
});
});
});
});
10 changes: 6 additions & 4 deletions lib/store/src/StoryStore.ts
Expand Up @@ -81,7 +81,7 @@ export class StoryStore<TFramework extends AnyFramework> {

prepareStoryWithCache: typeof prepareStory;

initializationPromise: Promise<void>;
initializationPromise: SynchronousPromise<void>;

resolveInitializationPromise: () => void;

Expand Down Expand Up @@ -175,9 +175,11 @@ export class StoryStore<TFramework extends AnyFramework> {
}

cacheAllCSFFiles(): PromiseLike<void> {
return this.loadAllCSFFiles().then((csfFiles) => {
this.cachedCSFFiles = csfFiles;
});
return this.initializationPromise.then(() =>
this.loadAllCSFFiles().then((csfFiles) => {
this.cachedCSFFiles = csfFiles;
})
);
}

// Load the CSF file for a story and prepare the story from it and the project annotations.
Expand Down

0 comments on commit fc9853a

Please sign in to comment.