Clean up Explore autosave lifecycle - #1086
Conversation
📝 WalkthroughWalkthroughThe PR replaces module-level game persistence setup with a new ChangesGame Persistence
Estimated code review effort: 2 (Simple) | ~10 minutes Sequence Diagram(s)sequenceDiagram
participant IslandExplorer
participant useGamePersistence
participant GameStore
participant Browser
IslandExplorer->>useGamePersistence: invoke on render
useGamePersistence->>Browser: register beforeunload handler
loop every 5 seconds
useGamePersistence->>GameStore: save state if playing or game-over
end
Browser-->>useGamePersistence: beforeunload event
useGamePersistence->>GameStore: save final state
IslandExplorer->>useGamePersistence: unmount
useGamePersistence->>Browser: clear interval and remove handler
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
tanstack-com | 739fccf | Commit Preview URL Branch Preview URL |
Jul 31 2026, 03:11 PM |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/components/game/hooks/useGameStore.ts`:
- Around line 1015-1018: Update the cleanup around the useGameStore persistence
effect so startOver’s explicit STORAGE_KEY removal is tracked and the unmount
persist() call is skipped after that reset. Clear the tracking state when a new
game starts, preserve normal unmount persistence otherwise, and add a regression
test covering startOver followed by unmount without recreating the save key.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: f7eb677f-8021-4a45-8fa9-97bff3eff9ba
📒 Files selected for processing (2)
src/components/game/IslandExplorer.client.tsxsrc/components/game/hooks/useGameStore.ts
| return () => { | ||
| window.clearInterval(interval) | ||
| window.removeEventListener('beforeunload', persist) | ||
| persist() |
There was a problem hiding this comment.
🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win
Preserve the storage clear during unmount.
startOver removes STORAGE_KEY at Line [887]. When IslandExplorer unmounts, this cleanup calls persist() at Line [1018] and recreates the save key with the reset state. Track the explicit clear and skip the final save until a new game starts. Add a regression test for startOver followed by unmount.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/components/game/hooks/useGameStore.ts` around lines 1015 - 1018, Update
the cleanup around the useGameStore persistence effect so startOver’s explicit
STORAGE_KEY removal is tracked and the unmount persist() call is skipped after
that reset. Clear the tracking state when a new game starts, preserve normal
unmount persistence otherwise, and add a regression test covering startOver
followed by unmount without recreating the save key.
What changed
Move Explore's periodic save timer and
beforeunloadlistener from module scope into theIslandExplorercomponent lifecycle.The lifecycle hook now:
Evidence and impact
Importing
useGameStore.tspreviously started a permanent five-second interval and registered an anonymousbeforeunloadlistener. TanStack Router keeps that loaded module alive after SPA navigation, so an active game continued writing tolocalStorageevery five seconds after leaving/explore; HMR could also register additional timers and listeners.This change ties both resources to the only UI that needs them and prevents background work after leaving Explore.
Validation
pnpm test— TypeScript and type-aware lint clean; 135 tests (134 passed, 1 skipped)git diff --checkRisk
Low. Save cadence and unload behavior are unchanged while Explore is mounted. Cleanup adds one final save before stopping persistence.
Summary by CodeRabbit