From b3fa8c56daa4d57e62b34a01b20f993ae3fc74a2 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Wed, 6 Mar 2024 18:10:41 +0100 Subject: [PATCH 1/6] load refs in sequence, await writing to state --- code/lib/manager-api/src/modules/refs.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/code/lib/manager-api/src/modules/refs.ts b/code/lib/manager-api/src/modules/refs.ts index bc4b94755a53..34e1574b3e2a 100644 --- a/code/lib/manager-api/src/modules/refs.ts +++ b/code/lib/manager-api/src/modules/refs.ts @@ -276,7 +276,7 @@ export const init: ModuleFn = ( return refs; }, - setRef: (id, { storyIndex, setStoriesData, ...rest }, ready = false) => { + setRef: async (id, { storyIndex, setStoriesData, ...rest }, ready = false) => { if (singleStory) { return; } @@ -307,10 +307,10 @@ export const init: ModuleFn = ( 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 }; @@ -320,7 +320,7 @@ export const init: ModuleFn = ( return obj; }, {}); - store.setState({ + await store.setState({ refs: ordered, }); }, @@ -331,9 +331,10 @@ export const init: ModuleFn = ( const initialState: SubState['refs'] = refs; if (runCheck) { - Object.entries(refs).forEach(([id, ref]) => { - api.checkRef({ ...ref!, stories: {} } as API_SetRefData); - }); + Object.entries(refs).reduce(async (accc, [id, ref]) => { + await accc; + await api.checkRef({ ...ref!, stories: {} } as API_SetRefData); + }, Promise.resolve()); } return { From b045da1682eebf623f822e044df1069fd70b4298 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Wed, 6 Mar 2024 19:56:18 +0100 Subject: [PATCH 2/6] rename & fix test --- code/lib/manager-api/src/modules/refs.ts | 4 ++-- code/lib/manager-api/src/tests/refs.test.ts | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/code/lib/manager-api/src/modules/refs.ts b/code/lib/manager-api/src/modules/refs.ts index 34e1574b3e2a..f783f7db6496 100644 --- a/code/lib/manager-api/src/modules/refs.ts +++ b/code/lib/manager-api/src/modules/refs.ts @@ -331,8 +331,8 @@ export const init: ModuleFn = ( const initialState: SubState['refs'] = refs; if (runCheck) { - Object.entries(refs).reduce(async (accc, [id, ref]) => { - await accc; + Object.entries(refs).reduce(async (acc, [id, ref]) => { + await acc; await api.checkRef({ ...ref!, stories: {} } as API_SetRefData); }, Promise.resolve()); } diff --git a/code/lib/manager-api/src/tests/refs.test.ts b/code/lib/manager-api/src/tests/refs.test.ts index 791325c27337..af16eee0bbfa 100644 --- a/code/lib/manager-api/src/tests/refs.test.ts +++ b/code/lib/manager-api/src/tests/refs.test.ts @@ -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 Promise.resolve(); + expect(fetchMock.mock.calls).toMatchInlineSnapshot(` [ [ @@ -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 Promise.resolve(); + expect(fetchMock.mock.calls).toMatchInlineSnapshot(` [ [ From 414193b9365fe7a9f6735e50cc6b7beb8365ef04 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Wed, 6 Mar 2024 21:12:26 +0100 Subject: [PATCH 3/6] improve understandability of the test --- code/lib/manager-api/src/tests/refs.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/lib/manager-api/src/tests/refs.test.ts b/code/lib/manager-api/src/tests/refs.test.ts index af16eee0bbfa..950b5e3e7a63 100644 --- a/code/lib/manager-api/src/tests/refs.test.ts +++ b/code/lib/manager-api/src/tests/refs.test.ts @@ -172,7 +172,7 @@ describe('Refs API', () => { initRefs({ provider, store } as any); // the `runCheck` is async, so we need to wait for it to finish - await Promise.resolve(); + await vi.waitFor(() => fetchMock.mock.calls.length > 0); expect(fetchMock.mock.calls).toMatchInlineSnapshot(` [ @@ -211,7 +211,7 @@ describe('Refs API', () => { initRefs({ provider, store } as any); // the `runCheck` is async, so we need to wait for it to finish - await Promise.resolve(); + await vi.waitFor(() => fetchMock.mock.calls.length > 0); expect(fetchMock.mock.calls).toMatchInlineSnapshot(` [ From 9a1c77cd9ef8273747f99df19057dd3c0a1064be Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Thu, 7 Mar 2024 19:09:39 +0100 Subject: [PATCH 4/6] make `changeRefVersion` async as well, and correct the types --- code/lib/manager-api/src/modules/refs.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/code/lib/manager-api/src/modules/refs.ts b/code/lib/manager-api/src/modules/refs.ts index f783f7db6496..e686f6f1c1b6 100644 --- a/code/lib/manager-api/src/modules/refs.ts +++ b/code/lib/manager-api/src/modules/refs.ts @@ -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; /** * Gets all composed refs. * @returns {API_Refs} - The composed refs object. @@ -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; /** * Changes the state of a composed ref by its ID and previewInitialized flag. * @param {string} id - The ID of the composed ref. @@ -168,12 +168,12 @@ export const init: ModuleFn = ( 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.checkRef(ref); }, changeRefState: (id, previewInitialized) => { const { [id]: ref, ...updated } = api.getRefs(); From 2f5cfe73af54160deaa7c3b2eafd19bd24ade5b5 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Fri, 8 Mar 2024 12:03:36 +0100 Subject: [PATCH 5/6] Update code/lib/manager-api/src/modules/refs.ts Co-authored-by: Jeppe Reinhold --- code/lib/manager-api/src/modules/refs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/lib/manager-api/src/modules/refs.ts b/code/lib/manager-api/src/modules/refs.ts index e686f6f1c1b6..c2404ba7234b 100644 --- a/code/lib/manager-api/src/modules/refs.ts +++ b/code/lib/manager-api/src/modules/refs.ts @@ -172,7 +172,7 @@ export const init: ModuleFn = ( const { versions, title } = api.getRefs()[id]; const ref: API_SetRefData = { id, url, versions, title, index: {}, expanded: true }; - api.setRef(id, { ...ref, type: 'unknown' }, false); + await api.setRef(id, { ...ref, type: 'unknown' }, false); await api.checkRef(ref); }, changeRefState: (id, previewInitialized) => { From 15fa09b070c5d4e71bcd51476d9e3e658f60deb1 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Fri, 8 Mar 2024 13:14:43 +0100 Subject: [PATCH 6/6] make stylistic code change --- code/lib/manager-api/src/modules/refs.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/code/lib/manager-api/src/modules/refs.ts b/code/lib/manager-api/src/modules/refs.ts index c2404ba7234b..4a5fca881f48 100644 --- a/code/lib/manager-api/src/modules/refs.ts +++ b/code/lib/manager-api/src/modules/refs.ts @@ -331,10 +331,12 @@ export const init: ModuleFn = ( const initialState: SubState['refs'] = refs; if (runCheck) { - Object.entries(refs).reduce(async (acc, [id, ref]) => { - await acc; - await api.checkRef({ ...ref!, stories: {} } as API_SetRefData); - }, Promise.resolve()); + new Promise(async (resolve) => { + for (const ref of Object.values(refs)) { + await api.checkRef({ ...ref!, stories: {} } as API_SetRefData); + } + resolve(undefined); + }); } return {