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: Batch the loading of CSF files for extract() etc #20055

Merged
merged 6 commits into from
Dec 6, 2022
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
26 changes: 26 additions & 0 deletions code/lib/preview-api/src/modules/store/StoryStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ jest.mock('global', () => ({
},
}));

const createGate = (): [Promise<any | undefined>, (_?: any) => void] => {
let openGate = (_?: any) => {};
const gate = new Promise<any | undefined>((resolve) => {
openGate = resolve;
});
return [gate, openGate];
};

const componentOneExports = {
default: { title: 'Component One' },
a: { args: { foo: 'a' } },
Expand Down Expand Up @@ -465,6 +473,24 @@ describe('StoryStore', () => {
'./src/ComponentTwo.stories.js',
]);
});

it('imports in batches', async () => {
const [gate, openGate] = createGate();
const blockedImportFn = jest.fn(async (file) => {
await gate;
return importFn(file);
});
const store = new StoryStore();
store.setProjectAnnotations(projectAnnotations);
store.initialize({ storyIndex, importFn: blockedImportFn, cache: false });

const promise = store.loadAllCSFFiles({ batchSize: 1 });
expect(blockedImportFn).toHaveBeenCalledTimes(1);

openGate();
await promise;
expect(blockedImportFn).toHaveBeenCalledTimes(3);
});
});

describe('extract', () => {
Expand Down
42 changes: 29 additions & 13 deletions code/lib/preview-api/src/modules/store/StoryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ import { ArgsStore } from './ArgsStore';
import { GlobalsStore } from './GlobalsStore';
import { processCSFFile, prepareStory, normalizeProjectAnnotations } from './csf';

// TODO -- what are reasonable values for these?
const CSF_CACHE_SIZE = 1000;
const STORY_CACHE_SIZE = 10000;
const EXTRACT_BATCH_SIZE = 20;

export class StoryStore<TRenderer extends Renderer> {
storyIndex?: StoryIndexStore;
Expand Down Expand Up @@ -143,22 +143,38 @@ export class StoryStore<TRenderer extends Renderer> {
);
}

loadAllCSFFiles(): Promise<StoryStore<TRenderer>['cachedCSFFiles']> {
loadAllCSFFiles({ batchSize = EXTRACT_BATCH_SIZE } = {}): Promise<
StoryStore<TRenderer>['cachedCSFFiles']
> {
if (!this.storyIndex) throw new Error(`loadAllCSFFiles called before initialization`);

const importPaths: Record<Path, StoryId> = {};
Object.entries(this.storyIndex.entries).forEach(([storyId, { importPath }]) => {
importPaths[importPath] = storyId;
});
const importPaths = Object.entries(this.storyIndex.entries).map(([storyId, { importPath }]) => [
importPath,
storyId,
]);

const loadInBatches = (
remainingImportPaths: typeof importPaths
): Promise<{ importPath: Path; csfFile: CSFFile<TRenderer> }[]> => {
if (remainingImportPaths.length === 0) return SynchronousPromise.resolve([]);

const csfFilePromiseList = remainingImportPaths
.slice(0, batchSize)
.map(([importPath, storyId]) =>
this.loadCSFFileByStoryId(storyId).then((csfFile) => ({
importPath,
csfFile,
}))
);

const csfFilePromiseList = Object.entries(importPaths).map(([importPath, storyId]) =>
this.loadCSFFileByStoryId(storyId).then((csfFile) => ({
importPath,
csfFile,
}))
);
return SynchronousPromise.all(csfFilePromiseList).then((firstResults) =>
loadInBatches(remainingImportPaths.slice(batchSize)).then((restResults) =>
firstResults.concat(restResults)
)
);
};
tmeasday marked this conversation as resolved.
Show resolved Hide resolved

return SynchronousPromise.all(csfFilePromiseList).then((list) =>
return loadInBatches(importPaths).then((list) =>
list.reduce((acc, { importPath, csfFile }) => {
acc[importPath] = csfFile;
return acc;
Expand Down