fix: doSave and newFile read .created off an unresolved Promise#59
Open
adarsh-yadav1 wants to merge 2 commits into
Open
fix: doSave and newFile read .created off an unresolved Promise#59adarsh-yadav1 wants to merge 2 commits into
adarsh-yadav1 wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
While going through issue #38 I traced the bug to both call sites of
_getFile().The method is async but neither
doSave()(Menu.tsx) nornewFile()(NewFile.tsx)awaited it before accessing
.createdon the result. JavaScript doesn't throw whenyou do that — it silently gives you
undefinedon a Promise object, so the wrongtimestamp gets stored with no visible error.
What I changed:
doSave()— made async, awaits_getFile, null-guards withexistingData?.created ?? new Date()for the edge case where the storage entry is missingnewFile()— made async, awaits both_getFileand_saveFilebefore the editor resets, which also fixes the race condition where reset could fire before the write finished_getFile()in LocalStorage.ts — addedtry/catcharound the parse, explicitPromise<File | null>return type, returnsnullon missing or corrupt data instead of throwingAlso added a Vitest unit test covering the three
_getFilecases (missing key, corrupt JSON, valid entry).Tested manually via
ionic serve— verifiedcreatedis preserved across multiple saves on the same file andmodifiedupdates correctly.tsc --noEmitpasses clean.