Merge pull request #16947 from storybookjs/16877-await-init-before-cache

Core: Ensure we have a full story index before caching
This commit is contained in:
Michael Shilman 2021-12-09 09:19:40 +08:00 committed by GitHub
commit addfdbf91b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 5 deletions

View File

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

View File

@ -81,7 +81,7 @@ export class StoryStore<TFramework extends AnyFramework> {
prepareStoryWithCache: typeof prepareStory;
initializationPromise: Promise<void>;
initializationPromise: SynchronousPromise<void>;
resolveInitializationPromise: () => void;
@ -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.