CodeVetter · Repo Unpack · Behavior traces
TanStack/query
The investigation followed startup, primary fetch, optimistic mutation, and persistent restoration through concrete source files. These are source execution traces rather than observed runtime recordings. Each trace retains important conditional branches, including stale-cache reuse, restoration gating, cancellation, and scope queues. The offline example supplies an application-level path connecting these library mechanisms.
- Offline example startup calls worker.start, resolves #root, and renders App; App supplies its configured client and localStorage persister to PersistQueryClientProvider.
- Provider lifecycle runs QueryClientProvider's effect → client.mount → focusManager/onlineManager subscriptions; positive events await resumePausedMutations before notifying the query cache, and the last unmount removes subscriptions.
- React query flow runs useQuery → useBaseQuery → useQueryClient/defaultQueryOptions → new QueryObserver → getOptimisticResult → useSyncExternalStore subscription; restoring or subscribed:false prevents that subscription.
- The first observer subscriber runs addObserver → shouldFetchOnMount → executeFetch → Query.fetch → retryer.start → queryFn; success calls setData → dispatch → observer.onQueryUpdate → updateResult and framework notification.
- Imperative fetch runs queryClient.query → defaultQueryOptions → QueryCache.build → isStaleByTime; stale entries fetch and fresh entries reuse state.data, with retries disabled unless explicitly configured and select applied afterward.
- Comment submission runs Detail.submitForm → updateMovie.mutate → MutationObserver.mutate → Mutation.execute; useMovie.onMutate cancels detail fetches, snapshots old data, and writes optimistic data, with rollback onError and invalidation onSettled.
- Mutation completion awaits global onSuccess → local onSuccess → global onSettled → local onSettled before success dispatch; restored pending mutations skip onMutate, and finally releases the retryer and advances the scope queue.
- Persistent startup runs provider restore → persister.restoreClient → timestamp/maxAge/buster checks → hydrate → awaited onSuccess → isRestoring:false → cache subscriptions; later added/removed/updated events run save → dehydrate → persistClient.