Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Ensure we have a full story index before caching #16947

Merged
merged 1 commit into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
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
18 changes: 17 additions & 1 deletion lib/store/src/StoryStore.test.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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