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

Composition: Fix refs not loading when there's multiple #26356

Merged
merged 7 commits into from
Mar 8, 2024
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
25 changes: 14 additions & 11 deletions code/lib/manager-api/src/modules/refs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface SubAPI {
* @param {string} id - The ID of the composed ref.
* @param {API_ComposedRefUpdate} ref - The update object for the composed ref.
*/
updateRef: (id: string, ref: API_ComposedRefUpdate) => void;
updateRef: (id: string, ref: API_ComposedRefUpdate) => Promise<void>;
/**
* Gets all composed refs.
* @returns {API_Refs} - The composed refs object.
Expand All @@ -60,7 +60,7 @@ export interface SubAPI {
* @param {string} id - The ID of the composed ref.
* @param {string} url - The new URL for the composed ref.
*/
changeRefVersion: (id: string, url: string) => void;
changeRefVersion: (id: string, url: string) => Promise<void>;
/**
* Changes the state of a composed ref by its ID and previewInitialized flag.
* @param {string} id - The ID of the composed ref.
Expand Down Expand Up @@ -168,12 +168,12 @@ export const init: ModuleFn<SubAPI, SubState> = (

return Object.values(refs).find(({ url }: any) => url.match(source));
},
changeRefVersion: (id, url) => {
changeRefVersion: async (id, url) => {
const { versions, title } = api.getRefs()[id];
const ref: API_SetRefData = { id, url, versions, title, index: {}, expanded: true };

api.setRef(id, { ...ref, type: 'unknown' }, false);
api.checkRef(ref);
await api.setRef(id, { ...ref, type: 'unknown' }, false);
await api.checkRef(ref);
},
changeRefState: (id, previewInitialized) => {
const { [id]: ref, ...updated } = api.getRefs();
Expand Down Expand Up @@ -276,7 +276,7 @@ export const init: ModuleFn<SubAPI, SubState> = (
return refs;
},

setRef: (id, { storyIndex, setStoriesData, ...rest }, ready = false) => {
setRef: async (id, { storyIndex, setStoriesData, ...rest }, ready = false) => {
if (singleStory) {
return;
}
Expand Down Expand Up @@ -307,10 +307,10 @@ export const init: ModuleFn<SubAPI, SubState> = (
index = addRefIds(index, ref);
}

api.updateRef(id, { ...ref, ...rest, index, internal_index });
await api.updateRef(id, { ...ref, ...rest, index, internal_index });
},

updateRef: (id, data) => {
updateRef: async (id, data) => {
const { [id]: ref, ...updated } = api.getRefs();

updated[id] = { ...ref, ...data };
Expand All @@ -320,7 +320,7 @@ export const init: ModuleFn<SubAPI, SubState> = (
return obj;
}, {});

store.setState({
await store.setState({
refs: ordered,
});
},
Expand All @@ -331,8 +331,11 @@ export const init: ModuleFn<SubAPI, SubState> = (
const initialState: SubState['refs'] = refs;

if (runCheck) {
Object.entries(refs).forEach(([id, ref]) => {
api.checkRef({ ...ref!, stories: {} } as API_SetRefData);
new Promise(async (resolve) => {
for (const ref of Object.values(refs)) {
await api.checkRef({ ...ref!, stories: {} } as API_SetRefData);
}
resolve(undefined);
});
}

Expand Down
6 changes: 6 additions & 0 deletions code/lib/manager-api/src/tests/refs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ describe('Refs API', () => {
// given
initRefs({ provider, store } as any);

// the `runCheck` is async, so we need to wait for it to finish
await vi.waitFor(() => fetchMock.mock.calls.length > 0);

expect(fetchMock.mock.calls).toMatchInlineSnapshot(`
[
[
Expand Down Expand Up @@ -207,6 +210,9 @@ describe('Refs API', () => {
};
initRefs({ provider, store } as any);

// the `runCheck` is async, so we need to wait for it to finish
await vi.waitFor(() => fetchMock.mock.calls.length > 0);

expect(fetchMock.mock.calls).toMatchInlineSnapshot(`
[
[
Expand Down
Loading