fix(useChallengeGame): replace stale hasWon closure with wonNow in handleStreamComplete - #6
Open
Adityakk9031 wants to merge 1 commit into
Open
fix(useChallengeGame): replace stale hasWon closure with wonNow in handleStreamComplete#6Adityakk9031 wants to merge 1 commit into
Adityakk9031 wants to merge 1 commit into
Conversation
…ndleStreamComplete The hasWon flag was read from a stale useCallback closure inside handleStreamComplete. On the first winning turn, setHasWon(true) is queued but React has not re-rendered yet when storage.saveSession runs, so the closure still holds hasWon=false. This caused the session to be persisted as unsolved on the winning turn, breaking win state restore after a page reload. Fix: use wonNow (derived directly from result.success in the same synchronous tick) instead of the stale hasWon from the closure. Also remove hasWon from the useCallback dependency array since it is no longer referenced inside the callback, preventing unnecessary re-creations. Adds src/hooks/__tests__/useChallengeGame.haswon.test.ts with 12 tests covering old vs new behaviour, the race condition scenario, and the dep array change (all 12 pass). Fixes fabraix#5
Author
|
@zachdotai have a look |
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.
Summary
Fixes a stale closure bug where
handleStreamCompletepersistedhasWon: falseto
sessionStorageon the winning turn, causing the win state to be lost aftera page reload.
Closes #5
Root Cause
handleStreamCompleteis auseCallbackthat capturedhasWon(a React statevalue) in its closure. On the first winning turn,
setHasWon(true)is called,but React has not re-rendered yet when
storage.saveSessionexecutes in thesame synchronous tick. The closure still holds the pre-win value
hasWon = false:On a subsequent non-winning message (after a reload restores
hasWon = falsefrom storage), the expressionfalse || falsewrites
hasWon: falseagain, permanently burying the win.Fix
Replace the stale
hasWonread withwonNow, which is derived directly fromresult.successin the same synchronous tick and is always accurate. RemovehasWonfrom the dependency array since it is no longer referenced.Files Changed
src/hooks/useChallengeGame.tssrc/hooks/__tests__/useChallengeGame.haswon.test.tsImpact Before This Fix
sessionStoragewas written withhasWon: false.setHasWon()andsaveSession()executed in the same React batch before any re-render.Tests
Checklist
useChallengeGame.ts)master)