fix(simulator): use subscription pattern for cache hydration#74
Merged
m4dm4rtig4n merged 1 commit intomainfrom Dec 5, 2025
Merged
fix(simulator): use subscription pattern for cache hydration#74m4dm4rtig4n merged 1 commit intomainfrom
m4dm4rtig4n merged 1 commit intomainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a cache hydration race condition in the Simulator page that prevented cached consumption data from displaying on first load. The issue occurred because React Query's IndexedDB persistence hydrates asynchronously, causing direct getQueryData() calls to return undefined even when data exists in the persisted cache.
Key Changes:
- Implemented the hybrid cache pattern (useQuery + useState + subscription) consistent with other pages like Consumption
- Added proper loading state tracking for cache hydration (
isConsumptionCacheLoading) - Updated initialization logic to wait for all data sources including cache hydration before completing
- Modified auto-launch and simulation logic to use the subscription-based
cachedConsumptionDatainstead of direct cache queries
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The previous fix didn't fully address the race condition because queryClient.getQueryData() can return undefined before IndexedDB hydration completes. Now using the same hybrid pattern as other pages: useQuery to trigger cache entry + subscription to react to cache updates including IndexedDB hydration. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
22df424 to
d95f25c
Compare
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 the Simulator page not displaying cached data on first load. The previous fix (#72) didn't fully address the race condition because
queryClient.getQueryData()can returnundefinedbefore IndexedDB hydration completes.Root Cause
React Query's
PersistQueryClientProviderrestores cached data from IndexedDB asynchronously. Direct calls togetQueryData()can returnundefinedeven when data exists in the persisted cache.Solution
Applied the same hybrid pattern used by other pages (e.g.,
useConsumptionData.ts):useQuerywithstaleTime: Infinityto create the cache entry and track loading stateuseState+ cache subscription to react to cache updates, including IndexedDB hydrationisConsumptionCacheLoadingflag to block initialization until cache is readyChanges
useQueryforconsumptionDetailto track hydration loading stateuseEffectwith cache subscription (queryClient.getQueryCache().subscribe())isConsumptionCacheLoadingto befalsecachedConsumptionDatafrom state instead of directgetQueryData()Testing
Users with cached consumption data should now see the simulator automatically populate on first page load without needing to refresh.