From 23508d92fe4bc5fbda0306e165f92b59e77ce67f Mon Sep 17 00:00:00 2001 From: thenick775 Date: Mon, 18 Nov 2024 19:24:59 -0800 Subject: [PATCH] fix: cheats and saves not persisting properly automatically - fixes issues with bulk upload of cheats/saves not persisting properly - due to the fact that we weren't using the callback on these helpers, as they don't return a promise (yet) - probably a cleaner way to express this, will do in the future after some core updates to these helper funcs --- .../src/components/modals/upload-cheats.spec.tsx | 15 ++++++++++++--- gbajs3/src/components/modals/upload-cheats.tsx | 12 ++++++++++-- .../src/components/modals/upload-saves.spec.tsx | 15 ++++++++++++--- gbajs3/src/components/modals/upload-saves.tsx | 12 ++++++++++-- 4 files changed, 44 insertions(+), 10 deletions(-) diff --git a/gbajs3/src/components/modals/upload-cheats.spec.tsx b/gbajs3/src/components/modals/upload-cheats.spec.tsx index 3656d8c2..4019ac9f 100644 --- a/gbajs3/src/components/modals/upload-cheats.spec.tsx +++ b/gbajs3/src/components/modals/upload-cheats.spec.tsx @@ -52,7 +52,10 @@ describe('', () => { await userEvent.click(screen.getByRole('button', { name: 'Upload' })); expect(uploadCheatsSpy).toHaveBeenCalledOnce(); - expect(uploadCheatsSpy).toHaveBeenCalledWith(testCheatFile); + expect(uploadCheatsSpy).toHaveBeenCalledWith( + testCheatFile, + expect.anything() + ); expect(syncActionIfEnabledSpy).toHaveBeenCalledOnce(); expect(screen.getByText('Upload complete!')).toBeVisible(); @@ -105,8 +108,14 @@ describe('', () => { await userEvent.click(screen.getByRole('button', { name: 'Upload' })); expect(uploadCheatsSpy).toHaveBeenCalledTimes(2); - expect(uploadCheatsSpy).toHaveBeenCalledWith(testCheatFiles[0]); - expect(uploadCheatsSpy).toHaveBeenCalledWith(testCheatFiles[1]); + expect(uploadCheatsSpy).toHaveBeenCalledWith( + testCheatFiles[0], + expect.anything() + ); + expect(uploadCheatsSpy).toHaveBeenCalledWith( + testCheatFiles[1], + expect.anything() + ); expect(syncActionIfEnabledSpy).toHaveBeenCalledOnce(); expect(screen.getByText('Upload complete!')).toBeVisible(); diff --git a/gbajs3/src/components/modals/upload-cheats.tsx b/gbajs3/src/components/modals/upload-cheats.tsx index 8c61980b..8cd9ff07 100644 --- a/gbajs3/src/components/modals/upload-cheats.tsx +++ b/gbajs3/src/components/modals/upload-cheats.tsx @@ -42,8 +42,16 @@ export const UploadCheatsModal = () => { [reset, setValue] ); - const onSubmit: SubmitHandler = ({ cheatFiles }) => { - cheatFiles.forEach((cheatFiles) => emulator?.uploadCheats(cheatFiles)); + const onSubmit: SubmitHandler = async ({ cheatFiles }) => { + await Promise.all( + cheatFiles.map( + (cheatFile) => + new Promise((resolve) => { + emulator?.uploadCheats(cheatFile, resolve); + }) + ) + ); + reset(); syncActionIfEnabled(); }; diff --git a/gbajs3/src/components/modals/upload-saves.spec.tsx b/gbajs3/src/components/modals/upload-saves.spec.tsx index c2888ba7..d90d6414 100644 --- a/gbajs3/src/components/modals/upload-saves.spec.tsx +++ b/gbajs3/src/components/modals/upload-saves.spec.tsx @@ -51,7 +51,10 @@ describe('', () => { await userEvent.click(screen.getByRole('button', { name: 'Upload' })); expect(uploadSaveOrSaveStateSpy).toHaveBeenCalledOnce(); - expect(uploadSaveOrSaveStateSpy).toHaveBeenCalledWith(testSaveFile); + expect(uploadSaveOrSaveStateSpy).toHaveBeenCalledWith( + testSaveFile, + expect.anything() + ); expect(syncActionIfEnabledSpy).toHaveBeenCalledOnce(); expect(screen.getByText('Upload complete!')).toBeVisible(); @@ -104,8 +107,14 @@ describe('', () => { await userEvent.click(screen.getByRole('button', { name: 'Upload' })); expect(uploadSaveSpy).toHaveBeenCalledTimes(2); - expect(uploadSaveSpy).toHaveBeenCalledWith(testSaveFiles[0]); - expect(uploadSaveSpy).toHaveBeenCalledWith(testSaveFiles[1]); + expect(uploadSaveSpy).toHaveBeenCalledWith( + testSaveFiles[0], + expect.anything() + ); + expect(uploadSaveSpy).toHaveBeenCalledWith( + testSaveFiles[1], + expect.anything() + ); expect(syncActionIfEnabledSpy).toHaveBeenCalledOnce(); expect(screen.getByText('Upload complete!')).toBeVisible(); diff --git a/gbajs3/src/components/modals/upload-saves.tsx b/gbajs3/src/components/modals/upload-saves.tsx index 33bff3e2..a0ca300e 100644 --- a/gbajs3/src/components/modals/upload-saves.tsx +++ b/gbajs3/src/components/modals/upload-saves.tsx @@ -45,8 +45,16 @@ export const UploadSavesModal = () => { [reset, setValue] ); - const onSubmit: SubmitHandler = ({ saveFiles }) => { - saveFiles.forEach((saveFile) => emulator?.uploadSaveOrSaveState(saveFile)); + const onSubmit: SubmitHandler = async ({ saveFiles }) => { + await Promise.all( + saveFiles.map( + (saveFile) => + new Promise((resolve) => { + emulator?.uploadSaveOrSaveState(saveFile, resolve); + }) + ) + ); + reset(); syncActionIfEnabled(); };