initialData gives the guarantee that data cannot be undefined, but this falls short when you create a Query via prefetching, and then mount the observer that has initialData set.
That's because initialData is only doing something in the constructor of the Query, so if the Observer isn't the one who creates the Query, it does nothing
This fix makes sure that when new options are applied on a Query (which e.g. happens when an observer mounts), initialData is taken into account when the Query doesn't have any data yet
* add timeoutManager class
* add additional types & export functions
* convert all setTimeout/setInterval to managed versions
* tweaks
* add claude-generated tests
* tests
* revert changes in query-async-storage-persister: no path to import query-core
* re-export more types
* console.warn -> non-production console.error
* query-async-storage-persister: use query-core managedSetTimeout
* pdate pnpm-lock for new dependency edge
* sleep: always managedSetTimeout
* remove managed* functions, call method directly
* remove runtime coercion and accept unsafe any within TimeoutManager class
* cleanup; fix test after changes
* name is __TEST_ONLY__
* notifyManager: default scheduler === systemSetTimeoutZero
* Improve TimeoutCallback comment since ai was confused
* remove unnecessary timeoutManager-related exports
* prettier-ify index.ts (seems my editor messed with it already this pr?)
* continue to export defaultTimeoutProvider for tests
* oops missing import
* fix: export systemSetTimeoutZero from core
* ref: use notifyManager.schedule in createPersister
because it needs to work with whatever scheduleFn we have set there
* ref: move provider check behind env check
* docs
* doc tweaks
* doc tweaks
* docs: reference timeoutManager in discussion of 24 day setTimout limit
* Apply suggestion from @TkDodo
* Apply suggestion from @TkDodo
* chore: fix broken links
* docs: syntax fix
---------
Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
* feat(query-core): add custom reducer support to streamedQuery
Replace maxChunks parameter with flexible reducer function that delegates
data aggregation to consumer code. This provides full control over how
streamed chunks are combined into the final data structure.
Add support for custom placeholderData that works seamlessly with the
reducer function, allowing initialization of complex data types beyond
simple arrays.
https://github.com/TanStack/query/discussions/9065
BREAKING CHANGE: The maxChunks parameter has been removed from streamedQuery.
Use a custom reducer function to control data aggregation behavior instead.
* ci: apply automated fixes
* feat(query-core): require initialValue when using custom reducer in streamedQuery
Add type safety by making initialValue mandatory when providing a custom
reducer function. This prevents runtime errors and ensures proper data
initialization for custom data structures beyond simple arrays.
Use conditional types to enforce the relationship between reducer and
initialValue parameters, maintaining backward compatibility for simple
array-based streaming while requiring explicit initialization for
custom reducers.
BREAKING CHANGE: When using a custom reducer function with streamedQuery,
the initialValue parameter is now required and must be provided.
* feat(query-core): require initialValue when using custom reducer in streamedQuery
Add type safety by making initialValue mandatory when providing a custom
reducer function. This prevents runtime errors and ensures proper data
initialization for custom data structures beyond simple arrays.
Use conditional types to enforce the relationship between reducer and
initialValue parameters, maintaining backward compatibility for simple
array-based streaming while requiring explicit initialization for
custom reducers.
BREAKING CHANGE: When using a custom reducer function with streamedQuery,
the initialValue parameter is now required and must be provided.
* feat(query-core): require initialValue when using custom reducer in streamedQuery
Add type safety by making initialValue mandatory when providing a custom
reducer function. This prevents runtime errors and ensures proper data
initialization for custom data structures beyond simple arrays.
Use conditional types to enforce the relationship between reducer and
initialValue parameters, maintaining backward compatibility for simple
array-based streaming while requiring explicit initialization for
custom reducers.
BREAKING CHANGE: When using a custom reducer function with streamedQuery,
the initialValue parameter is now required and must be provided.
* removed personal vscode workspace file
* updated documentation
* fix(docs): clarify reducer function description in streamedQuery documentation
* ci: apply automated fixes
* fix(tests): Code Review :: update streamedQuery tests to use correct initialValue type
---------
Co-authored-by: gobbimar <marco.gobbi@adidas.com>
Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
* fix(query-core): ensure combine re-executes after cache restoration with memoized combine
* fix(query-core): add error field to result change detection
* test(query-core): add test for QueriesObserver early return notification
* test(query-core): improve test for QueriesObserver early return notification
* refactor(query-core): use shallowEqualObjects for result comparison
* refactor(query-core): improve setQueries flow and fix persist/restore issue with memoized combine
* refactor(query-core): improve setQueries flow and remove log
* test(react-query): update useQueries expectations for metadata change detection
* test(react-query): fix test comments
* test(react-query): add test for stable combine optimization on unrelated re-renders
---------
Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
* refactor(query-core): improve replaceEqualDeep performance
* try object.prototype that broke eslint in router
* no object flag
* improved version w/ more code
* ci: apply automated fixes
* less code, but still 2x faster
---------
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
* docs(queryClient): add example for refetchQueries with stale: true and type: active
* docs(react-native): modify useRefreshOnFocus example
* docs(react-native): add queryKey in useRefreshOnFocus example
* Apply suggestion from @TkDodo
* ci: apply automated fixes
---------
Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
the switch from the callback approach to async/await with catch was necessary to be able to transform errors into revert-state data for imperative query calls; however, this was a change in behavior because catch in async/await doesn't happen immediately - it runs in the next microtask. That opened up an opportunity for a slight race condition if we re-start a fetch in-between. And who does that? Of course, React Strict Mode.
This PR moves the actual state reverting back to a callback (so it happens synchronously), while still keeping the try/catch refactoring merely to transform the promise and the usual error handling
* fix(core): never revert to undefined data
* fix: throw CancelledError for imperative fetches, but keep query reverted to idle state
* chore: improve test
* fix: we don't want errors that get reverted internally to throw on imperative methods
to achieve that, we let the retryer's `onError` "transform" the result back to potentially TData, and if so, we'll resolve the promise to that instead; that means calls to `fetchQuery` will be "successful" when a cancellation happens in the meantime, but they will then resolve to the reverted data; also, if the initial fetch is cancelled, we would still throw, as there is no data to revert to.
* refactor: async/await
* minimal test adjustments
* test: fix it
* fix: revertState is now a local variable
* fix: make sure "silent" reverts do not reject ongoing promises
up until now, silent reverts have only stopped the query from going into error state, but the exposed promise was still rejected
now, the promise won't reject because a silent revert indicates another refetch happening, so we'd want to get that promise's result instead
this also means "silent" is an internal thing that shouldn't be used by consumers, because it can lead to never resolving promises instead
* chore: bring back isCancelledError and deprecate it
* test: revert changes
* test: minimal adjustment to timings