Skip to content

fix: doSave and newFile read .created off an unresolved Promise#59

Open
adarsh-yadav1 wants to merge 2 commits into
seetadev:mainfrom
adarsh-yadav1:fix-react-saveflow-async
Open

fix: doSave and newFile read .created off an unresolved Promise#59
adarsh-yadav1 wants to merge 2 commits into
seetadev:mainfrom
adarsh-yadav1:fix-react-saveflow-async

Conversation

@adarsh-yadav1
Copy link
Copy Markdown

While going through issue #38 I traced the bug to both call sites of _getFile().

The method is async but neither doSave() (Menu.tsx) nor newFile() (NewFile.tsx)
awaited it before accessing .created on the result. JavaScript doesn't throw when
you do that — it silently gives you undefined on a Promise object, so the wrong
timestamp gets stored with no visible error.

What I changed:

  • doSave() — made async, awaits _getFile, null-guards with existingData?.created ?? new Date() for the edge case where the storage entry is missing
  • newFile() — made async, awaits both _getFile and _saveFile before the editor resets, which also fixes the race condition where reset could fire before the write finished
  • _getFile() in LocalStorage.ts — added try/catch around the parse, explicit Promise<File | null> return type, returns null on missing or corrupt data instead of throwing

Also added a Vitest unit test covering the three _getFile cases (missing key, corrupt JSON, valid entry).

Tested manually via ionic serve — verified created is preserved across multiple saves on the same file and modified updates correctly. tsc --noEmit passes clean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant