Skip to content

Conversation

@m4dm4rtig4n
Copy link
Contributor

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 return undefined before IndexedDB hydration completes.

Root Cause

React Query's PersistQueryClientProvider restores cached data from IndexedDB asynchronously. Direct calls to getQueryData() can return undefined even when data exists in the persisted cache.

Solution

Applied the same hybrid pattern used by other pages (e.g., useConsumptionData.ts):

  1. useQuery with staleTime: Infinity to create the cache entry and track loading state
  2. useState + cache subscription to react to cache updates, including IndexedDB hydration
  3. isConsumptionCacheLoading flag to block initialization until cache is ready

Changes

  • Added useQuery for consumptionDetail to track hydration loading state
  • Added useEffect with cache subscription (queryClient.getQueryCache().subscribe())
  • Initialization now waits for isConsumptionCacheLoading to be false
  • Auto-launch uses cachedConsumptionData from state instead of direct getQueryData()

Testing

Users with cached consumption data should now see the simulator automatically populate on first page load without needing to refresh.

Copilot AI review requested due to automatic review settings December 5, 2025 13:37
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 cachedConsumptionData instead 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 <[email protected]>
@m4dm4rtig4n m4dm4rtig4n force-pushed the fix-simulator-cache-display branch from 22df424 to d95f25c Compare December 5, 2025 14:13
@m4dm4rtig4n m4dm4rtig4n merged commit f902c18 into main Dec 5, 2025
5 checks passed
@m4dm4rtig4n m4dm4rtig4n deleted the fix-simulator-cache-display branch December 18, 2025 07:59
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.

2 participants