@tanstack/query-core 5.104.1 → 5.104.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/legacy/environmentManager.cjs +3 -1
- package/build/legacy/environmentManager.cjs.map +1 -1
- package/build/legacy/environmentManager.d.cts +6 -1
- package/build/legacy/environmentManager.d.cts.map +1 -1
- package/build/legacy/environmentManager.d.ts +6 -1
- package/build/legacy/environmentManager.d.ts.map +1 -1
- package/build/legacy/environmentManager.js +3 -1
- package/build/legacy/environmentManager.js.map +1 -1
- package/build/legacy/focusManager.cjs +4 -2
- package/build/legacy/focusManager.cjs.map +1 -1
- package/build/legacy/focusManager.d.cts +5 -3
- package/build/legacy/focusManager.d.cts.map +1 -1
- package/build/legacy/focusManager.d.ts +5 -3
- package/build/legacy/focusManager.d.ts.map +1 -1
- package/build/legacy/focusManager.js +4 -2
- package/build/legacy/focusManager.js.map +1 -1
- package/build/legacy/{hydration-B9t5w6NC.d.ts → hydration-Bf0oMcwO.d.ts} +1411 -129
- package/build/legacy/hydration-Bf0oMcwO.d.ts.map +1 -0
- package/build/legacy/{hydration-Br9VIGXa.d.cts → hydration-C6zghADG.d.cts} +1411 -129
- package/build/legacy/hydration-C6zghADG.d.cts.map +1 -0
- package/build/legacy/hydration.cjs +35 -0
- package/build/legacy/hydration.cjs.map +1 -1
- package/build/legacy/hydration.d.cts +1 -1
- package/build/legacy/hydration.d.ts +1 -1
- package/build/legacy/hydration.js +35 -0
- package/build/legacy/hydration.js.map +1 -1
- package/build/legacy/index.d.cts +1 -1
- package/build/legacy/index.d.ts +1 -1
- package/build/legacy/infiniteQueryBehavior.cjs +29 -0
- package/build/legacy/infiniteQueryBehavior.cjs.map +1 -1
- package/build/legacy/infiniteQueryBehavior.d.cts +16 -1
- package/build/legacy/infiniteQueryBehavior.d.cts.map +1 -1
- package/build/legacy/infiniteQueryBehavior.d.ts +16 -1
- package/build/legacy/infiniteQueryBehavior.d.ts.map +1 -1
- package/build/legacy/infiniteQueryBehavior.js +29 -0
- package/build/legacy/infiniteQueryBehavior.js.map +1 -1
- package/build/legacy/infiniteQueryObserver.cjs +13 -7
- package/build/legacy/infiniteQueryObserver.cjs.map +1 -1
- package/build/legacy/infiniteQueryObserver.d.cts +15 -9
- package/build/legacy/infiniteQueryObserver.d.cts.map +1 -1
- package/build/legacy/infiniteQueryObserver.d.ts +15 -9
- package/build/legacy/infiniteQueryObserver.d.ts.map +1 -1
- package/build/legacy/infiniteQueryObserver.js +13 -7
- package/build/legacy/infiniteQueryObserver.js.map +1 -1
- package/build/legacy/mutation.cjs +27 -10
- package/build/legacy/mutation.cjs.map +1 -1
- package/build/legacy/mutation.d.cts +1 -1
- package/build/legacy/mutation.d.ts +1 -1
- package/build/legacy/mutation.js +27 -10
- package/build/legacy/mutation.js.map +1 -1
- package/build/legacy/mutationCache.cjs +53 -12
- package/build/legacy/mutationCache.cjs.map +1 -1
- package/build/legacy/mutationCache.d.cts +1 -1
- package/build/legacy/mutationCache.d.ts +1 -1
- package/build/legacy/mutationCache.js +53 -12
- package/build/legacy/mutationCache.js.map +1 -1
- package/build/legacy/mutationObserver.cjs +27 -16
- package/build/legacy/mutationObserver.cjs.map +1 -1
- package/build/legacy/mutationObserver.d.cts +1 -1
- package/build/legacy/mutationObserver.d.ts +1 -1
- package/build/legacy/mutationObserver.js +27 -16
- package/build/legacy/mutationObserver.js.map +1 -1
- package/build/legacy/notifyManager.cjs +12 -2
- package/build/legacy/notifyManager.cjs.map +1 -1
- package/build/legacy/notifyManager.d.cts +19 -4
- package/build/legacy/notifyManager.d.cts.map +1 -1
- package/build/legacy/notifyManager.d.ts +19 -4
- package/build/legacy/notifyManager.d.ts.map +1 -1
- package/build/legacy/notifyManager.js +12 -2
- package/build/legacy/notifyManager.js.map +1 -1
- package/build/legacy/onlineManager.cjs +4 -2
- package/build/legacy/onlineManager.cjs.map +1 -1
- package/build/legacy/onlineManager.d.cts +5 -3
- package/build/legacy/onlineManager.d.cts.map +1 -1
- package/build/legacy/onlineManager.d.ts +5 -3
- package/build/legacy/onlineManager.d.ts.map +1 -1
- package/build/legacy/onlineManager.js +4 -2
- package/build/legacy/onlineManager.js.map +1 -1
- package/build/legacy/queriesObserver.cjs +15 -3
- package/build/legacy/queriesObserver.cjs.map +1 -1
- package/build/legacy/queriesObserver.d.cts +14 -5
- package/build/legacy/queriesObserver.d.cts.map +1 -1
- package/build/legacy/queriesObserver.d.ts +14 -5
- package/build/legacy/queriesObserver.d.ts.map +1 -1
- package/build/legacy/queriesObserver.js +15 -3
- package/build/legacy/queriesObserver.js.map +1 -1
- package/build/legacy/query.cjs +89 -15
- package/build/legacy/query.cjs.map +1 -1
- package/build/legacy/query.d.cts +1 -1
- package/build/legacy/query.d.ts +1 -1
- package/build/legacy/query.js +89 -15
- package/build/legacy/query.js.map +1 -1
- package/build/legacy/queryCache.cjs +34 -12
- package/build/legacy/queryCache.cjs.map +1 -1
- package/build/legacy/queryCache.d.cts +1 -1
- package/build/legacy/queryCache.d.ts +1 -1
- package/build/legacy/queryCache.js +34 -12
- package/build/legacy/queryCache.js.map +1 -1
- package/build/legacy/queryClient.cjs +104 -32
- package/build/legacy/queryClient.cjs.map +1 -1
- package/build/legacy/queryClient.d.cts +1 -1
- package/build/legacy/queryClient.d.ts +1 -1
- package/build/legacy/queryClient.js +104 -32
- package/build/legacy/queryClient.js.map +1 -1
- package/build/legacy/queryObserver.cjs +78 -6
- package/build/legacy/queryObserver.cjs.map +1 -1
- package/build/legacy/queryObserver.d.cts +1 -1
- package/build/legacy/queryObserver.d.ts +1 -1
- package/build/legacy/queryObserver.js +78 -6
- package/build/legacy/queryObserver.js.map +1 -1
- package/build/{modern/removable-0GzhW1PK.d.cts → legacy/removable-Cw9GNGyK.d.cts} +17 -1
- package/build/legacy/removable-Cw9GNGyK.d.cts.map +1 -0
- package/build/legacy/{removable-0GzhW1PK.d.cts → removable-Cw9GNGyK.d.ts} +17 -1
- package/build/legacy/removable-Cw9GNGyK.d.ts.map +1 -0
- package/build/legacy/removable.cjs +12 -0
- package/build/legacy/removable.cjs.map +1 -1
- package/build/legacy/removable.d.cts +1 -1
- package/build/legacy/removable.d.ts +1 -1
- package/build/legacy/removable.js +12 -0
- package/build/legacy/removable.js.map +1 -1
- package/build/legacy/retryer.cjs +26 -4
- package/build/legacy/retryer.cjs.map +1 -1
- package/build/legacy/retryer.d.cts +1 -1
- package/build/legacy/retryer.d.ts +1 -1
- package/build/legacy/retryer.js +26 -4
- package/build/legacy/retryer.js.map +1 -1
- package/build/legacy/streamedQuery.cjs +3 -1
- package/build/legacy/streamedQuery.cjs.map +1 -1
- package/build/legacy/streamedQuery.d.cts +4 -2
- package/build/legacy/streamedQuery.d.cts.map +1 -1
- package/build/legacy/streamedQuery.d.ts +4 -2
- package/build/legacy/streamedQuery.d.ts.map +1 -1
- package/build/legacy/streamedQuery.js +3 -1
- package/build/legacy/streamedQuery.js.map +1 -1
- package/build/{modern/subscribable-CysQ0tne.d.cts → legacy/subscribable-czlHOxMP.d.cts} +11 -1
- package/build/legacy/subscribable-czlHOxMP.d.cts.map +1 -0
- package/build/legacy/{subscribable-CysQ0tne.d.cts → subscribable-czlHOxMP.d.ts} +11 -1
- package/build/legacy/subscribable-czlHOxMP.d.ts.map +1 -0
- package/build/legacy/subscribable.cjs +10 -0
- package/build/legacy/subscribable.cjs.map +1 -1
- package/build/legacy/subscribable.d.cts +1 -1
- package/build/legacy/subscribable.d.ts +1 -1
- package/build/legacy/subscribable.js +10 -0
- package/build/legacy/subscribable.js.map +1 -1
- package/build/legacy/timeoutManager.cjs +11 -6
- package/build/legacy/timeoutManager.cjs.map +1 -1
- package/build/legacy/timeoutManager.d.cts +23 -6
- package/build/legacy/timeoutManager.d.cts.map +1 -1
- package/build/legacy/timeoutManager.d.ts +23 -6
- package/build/legacy/timeoutManager.d.ts.map +1 -1
- package/build/legacy/timeoutManager.js +11 -6
- package/build/legacy/timeoutManager.js.map +1 -1
- package/build/legacy/types.cjs.map +1 -1
- package/build/legacy/types.d.cts +1 -1
- package/build/legacy/types.d.ts +1 -1
- package/build/legacy/types.js.map +1 -1
- package/build/legacy/utils.cjs +117 -8
- package/build/legacy/utils.cjs.map +1 -1
- package/build/legacy/utils.d.cts +1 -1
- package/build/legacy/utils.d.ts +1 -1
- package/build/legacy/utils.js +117 -8
- package/build/legacy/utils.js.map +1 -1
- package/build/modern/environmentManager.cjs +3 -1
- package/build/modern/environmentManager.cjs.map +1 -1
- package/build/modern/environmentManager.d.cts +6 -1
- package/build/modern/environmentManager.d.cts.map +1 -1
- package/build/modern/environmentManager.d.ts +6 -1
- package/build/modern/environmentManager.d.ts.map +1 -1
- package/build/modern/environmentManager.js +3 -1
- package/build/modern/environmentManager.js.map +1 -1
- package/build/modern/focusManager.cjs +4 -2
- package/build/modern/focusManager.cjs.map +1 -1
- package/build/modern/focusManager.d.cts +5 -3
- package/build/modern/focusManager.d.cts.map +1 -1
- package/build/modern/focusManager.d.ts +5 -3
- package/build/modern/focusManager.d.ts.map +1 -1
- package/build/modern/focusManager.js +4 -2
- package/build/modern/focusManager.js.map +1 -1
- package/build/modern/{hydration-B9t5w6NC.d.ts → hydration-Bf0oMcwO.d.ts} +1411 -129
- package/build/modern/hydration-Bf0oMcwO.d.ts.map +1 -0
- package/build/modern/{hydration-Br9VIGXa.d.cts → hydration-C6zghADG.d.cts} +1411 -129
- package/build/modern/hydration-C6zghADG.d.cts.map +1 -0
- package/build/modern/hydration.cjs +35 -0
- package/build/modern/hydration.cjs.map +1 -1
- package/build/modern/hydration.d.cts +1 -1
- package/build/modern/hydration.d.ts +1 -1
- package/build/modern/hydration.js +35 -0
- package/build/modern/hydration.js.map +1 -1
- package/build/modern/index.d.cts +1 -1
- package/build/modern/index.d.ts +1 -1
- package/build/modern/infiniteQueryBehavior.cjs +29 -0
- package/build/modern/infiniteQueryBehavior.cjs.map +1 -1
- package/build/modern/infiniteQueryBehavior.d.cts +16 -1
- package/build/modern/infiniteQueryBehavior.d.cts.map +1 -1
- package/build/modern/infiniteQueryBehavior.d.ts +16 -1
- package/build/modern/infiniteQueryBehavior.d.ts.map +1 -1
- package/build/modern/infiniteQueryBehavior.js +29 -0
- package/build/modern/infiniteQueryBehavior.js.map +1 -1
- package/build/modern/infiniteQueryObserver.cjs +13 -7
- package/build/modern/infiniteQueryObserver.cjs.map +1 -1
- package/build/modern/infiniteQueryObserver.d.cts +15 -9
- package/build/modern/infiniteQueryObserver.d.cts.map +1 -1
- package/build/modern/infiniteQueryObserver.d.ts +15 -9
- package/build/modern/infiniteQueryObserver.d.ts.map +1 -1
- package/build/modern/infiniteQueryObserver.js +13 -7
- package/build/modern/infiniteQueryObserver.js.map +1 -1
- package/build/modern/mutation.cjs +27 -10
- package/build/modern/mutation.cjs.map +1 -1
- package/build/modern/mutation.d.cts +1 -1
- package/build/modern/mutation.d.ts +1 -1
- package/build/modern/mutation.js +27 -10
- package/build/modern/mutation.js.map +1 -1
- package/build/modern/mutationCache.cjs +53 -12
- package/build/modern/mutationCache.cjs.map +1 -1
- package/build/modern/mutationCache.d.cts +1 -1
- package/build/modern/mutationCache.d.ts +1 -1
- package/build/modern/mutationCache.js +53 -12
- package/build/modern/mutationCache.js.map +1 -1
- package/build/modern/mutationObserver.cjs +23 -12
- package/build/modern/mutationObserver.cjs.map +1 -1
- package/build/modern/mutationObserver.d.cts +1 -1
- package/build/modern/mutationObserver.d.ts +1 -1
- package/build/modern/mutationObserver.js +23 -12
- package/build/modern/mutationObserver.js.map +1 -1
- package/build/modern/notifyManager.cjs +12 -2
- package/build/modern/notifyManager.cjs.map +1 -1
- package/build/modern/notifyManager.d.cts +19 -4
- package/build/modern/notifyManager.d.cts.map +1 -1
- package/build/modern/notifyManager.d.ts +19 -4
- package/build/modern/notifyManager.d.ts.map +1 -1
- package/build/modern/notifyManager.js +12 -2
- package/build/modern/notifyManager.js.map +1 -1
- package/build/modern/onlineManager.cjs +4 -2
- package/build/modern/onlineManager.cjs.map +1 -1
- package/build/modern/onlineManager.d.cts +5 -3
- package/build/modern/onlineManager.d.cts.map +1 -1
- package/build/modern/onlineManager.d.ts +5 -3
- package/build/modern/onlineManager.d.ts.map +1 -1
- package/build/modern/onlineManager.js +4 -2
- package/build/modern/onlineManager.js.map +1 -1
- package/build/modern/queriesObserver.cjs +15 -3
- package/build/modern/queriesObserver.cjs.map +1 -1
- package/build/modern/queriesObserver.d.cts +14 -5
- package/build/modern/queriesObserver.d.cts.map +1 -1
- package/build/modern/queriesObserver.d.ts +14 -5
- package/build/modern/queriesObserver.d.ts.map +1 -1
- package/build/modern/queriesObserver.js +15 -3
- package/build/modern/queriesObserver.js.map +1 -1
- package/build/modern/query.cjs +89 -15
- package/build/modern/query.cjs.map +1 -1
- package/build/modern/query.d.cts +1 -1
- package/build/modern/query.d.ts +1 -1
- package/build/modern/query.js +89 -15
- package/build/modern/query.js.map +1 -1
- package/build/modern/queryCache.cjs +34 -12
- package/build/modern/queryCache.cjs.map +1 -1
- package/build/modern/queryCache.d.cts +1 -1
- package/build/modern/queryCache.d.ts +1 -1
- package/build/modern/queryCache.js +34 -12
- package/build/modern/queryCache.js.map +1 -1
- package/build/modern/queryClient.cjs +104 -32
- package/build/modern/queryClient.cjs.map +1 -1
- package/build/modern/queryClient.d.cts +1 -1
- package/build/modern/queryClient.d.ts +1 -1
- package/build/modern/queryClient.js +104 -32
- package/build/modern/queryClient.js.map +1 -1
- package/build/modern/queryObserver.cjs +78 -6
- package/build/modern/queryObserver.cjs.map +1 -1
- package/build/modern/queryObserver.d.cts +1 -1
- package/build/modern/queryObserver.d.ts +1 -1
- package/build/modern/queryObserver.js +78 -6
- package/build/modern/queryObserver.js.map +1 -1
- package/build/modern/{removable-0GzhW1PK.d.ts → removable-Cw9GNGyK.d.cts} +17 -1
- package/build/modern/removable-Cw9GNGyK.d.cts.map +1 -0
- package/build/{legacy/removable-0GzhW1PK.d.ts → modern/removable-Cw9GNGyK.d.ts} +17 -1
- package/build/modern/removable-Cw9GNGyK.d.ts.map +1 -0
- package/build/modern/removable.cjs +12 -0
- package/build/modern/removable.cjs.map +1 -1
- package/build/modern/removable.d.cts +1 -1
- package/build/modern/removable.d.ts +1 -1
- package/build/modern/removable.js +12 -0
- package/build/modern/removable.js.map +1 -1
- package/build/modern/retryer.cjs +26 -4
- package/build/modern/retryer.cjs.map +1 -1
- package/build/modern/retryer.d.cts +1 -1
- package/build/modern/retryer.d.ts +1 -1
- package/build/modern/retryer.js +26 -4
- package/build/modern/retryer.js.map +1 -1
- package/build/modern/streamedQuery.cjs +3 -1
- package/build/modern/streamedQuery.cjs.map +1 -1
- package/build/modern/streamedQuery.d.cts +4 -2
- package/build/modern/streamedQuery.d.cts.map +1 -1
- package/build/modern/streamedQuery.d.ts +4 -2
- package/build/modern/streamedQuery.d.ts.map +1 -1
- package/build/modern/streamedQuery.js +3 -1
- package/build/modern/streamedQuery.js.map +1 -1
- package/build/modern/{subscribable-CysQ0tne.d.ts → subscribable-czlHOxMP.d.cts} +11 -1
- package/build/modern/subscribable-czlHOxMP.d.cts.map +1 -0
- package/build/{legacy/subscribable-CysQ0tne.d.ts → modern/subscribable-czlHOxMP.d.ts} +11 -1
- package/build/modern/subscribable-czlHOxMP.d.ts.map +1 -0
- package/build/modern/subscribable.cjs +10 -0
- package/build/modern/subscribable.cjs.map +1 -1
- package/build/modern/subscribable.d.cts +1 -1
- package/build/modern/subscribable.d.ts +1 -1
- package/build/modern/subscribable.js +10 -0
- package/build/modern/subscribable.js.map +1 -1
- package/build/modern/timeoutManager.cjs +11 -6
- package/build/modern/timeoutManager.cjs.map +1 -1
- package/build/modern/timeoutManager.d.cts +23 -6
- package/build/modern/timeoutManager.d.cts.map +1 -1
- package/build/modern/timeoutManager.d.ts +23 -6
- package/build/modern/timeoutManager.d.ts.map +1 -1
- package/build/modern/timeoutManager.js +11 -6
- package/build/modern/timeoutManager.js.map +1 -1
- package/build/modern/types.cjs.map +1 -1
- package/build/modern/types.d.cts +1 -1
- package/build/modern/types.d.ts +1 -1
- package/build/modern/types.js.map +1 -1
- package/build/modern/utils.cjs +117 -8
- package/build/modern/utils.cjs.map +1 -1
- package/build/modern/utils.d.cts +1 -1
- package/build/modern/utils.d.ts +1 -1
- package/build/modern/utils.js +117 -8
- package/build/modern/utils.js.map +1 -1
- package/package.json +3 -3
- package/src/environmentManager.ts +6 -1
- package/src/focusManager.ts +8 -6
- package/src/hydration.ts +42 -1
- package/src/infiniteQueryBehavior.ts +29 -0
- package/src/infiniteQueryObserver.ts +20 -14
- package/src/mutation.ts +32 -11
- package/src/mutationCache.ts +53 -12
- package/src/mutationObserver.ts +29 -14
- package/src/notifyManager.ts +12 -2
- package/src/onlineManager.ts +7 -5
- package/src/queriesObserver.ts +20 -5
- package/src/query.ts +153 -20
- package/src/queryCache.ts +54 -13
- package/src/queryClient.ts +114 -42
- package/src/queryObserver.ts +88 -12
- package/src/removable.ts +16 -0
- package/src/retryer.ts +57 -4
- package/src/streamedQuery.ts +4 -3
- package/src/subscribable.ts +10 -0
- package/src/timeoutManager.ts +23 -6
- package/src/types.ts +801 -27
- package/src/utils.ts +130 -10
- package/build/legacy/hydration-B9t5w6NC.d.ts.map +0 -1
- package/build/legacy/hydration-Br9VIGXa.d.cts.map +0 -1
- package/build/legacy/removable-0GzhW1PK.d.cts.map +0 -1
- package/build/legacy/removable-0GzhW1PK.d.ts.map +0 -1
- package/build/legacy/subscribable-CysQ0tne.d.cts.map +0 -1
- package/build/legacy/subscribable-CysQ0tne.d.ts.map +0 -1
- package/build/modern/hydration-B9t5w6NC.d.ts.map +0 -1
- package/build/modern/hydration-Br9VIGXa.d.cts.map +0 -1
- package/build/modern/removable-0GzhW1PK.d.cts.map +0 -1
- package/build/modern/removable-0GzhW1PK.d.ts.map +0 -1
- package/build/modern/subscribable-CysQ0tne.d.cts.map +0 -1
- package/build/modern/subscribable-CysQ0tne.d.ts.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queryCache.cjs","names":["Subscribable","hashQueryKeyByOptions","Query","matchQuery"],"sources":["../../src/queryCache.ts"],"sourcesContent":["import { hashQueryKeyByOptions, matchQuery } from './utils'\nimport { Query } from './query'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport type { QueryFilters } from './utils'\nimport type { Action, QueryState } from './query'\nimport type {\n DefaultError,\n NotifyEvent,\n QueryKey,\n QueryOptions,\n WithRequired,\n} from './types'\nimport type { QueryClient } from './queryClient'\nimport type { QueryObserver } from './queryObserver'\n\n// TYPES\n\n/**\n * Global callbacks that fire for every query handled by a `QueryCache`, regardless of which\n * component or observer triggered it. Unlike `QueryClient`'s `defaultOptions`, which a query can\n * override, these callbacks are always called. Unlike `MutationCacheConfig`'s callbacks, these\n * are fire-and-forget: their return value is not awaited before the query settles.\n */\nexport interface QueryCacheConfig {\n /** Called when any query in the cache encounters an error. */\n onError?: (\n error: DefaultError,\n query: Query<unknown, unknown, unknown>,\n ) => void\n /** Called when any query in the cache is successful. */\n onSuccess?: (data: unknown, query: Query<unknown, unknown, unknown>) => void\n /** Called when any query in the cache is settled, either successfully or with an error. */\n onSettled?: (\n data: unknown | undefined,\n error: DefaultError | null,\n query: Query<unknown, unknown, unknown>,\n ) => void\n}\n\ninterface NotifyEventQueryAdded extends NotifyEvent {\n type: 'added'\n query: Query<any, any, any, any>\n}\n\ninterface NotifyEventQueryRemoved extends NotifyEvent {\n type: 'removed'\n query: Query<any, any, any, any>\n}\n\ninterface NotifyEventQueryUpdated extends NotifyEvent {\n type: 'updated'\n query: Query<any, any, any, any>\n action: Action<any, any>\n}\n\ninterface NotifyEventQueryObserverAdded extends NotifyEvent {\n type: 'observerAdded'\n query: Query<any, any, any, any>\n observer: QueryObserver<any, any, any, any, any>\n}\n\ninterface NotifyEventQueryObserverRemoved extends NotifyEvent {\n type: 'observerRemoved'\n query: Query<any, any, any, any>\n observer: QueryObserver<any, any, any, any, any>\n}\n\ninterface NotifyEventQueryObserverResultsUpdated extends NotifyEvent {\n type: 'observerResultsUpdated'\n query: Query<any, any, any, any>\n}\n\ninterface NotifyEventQueryObserverOptionsUpdated extends NotifyEvent {\n type: 'observerOptionsUpdated'\n query: Query<any, any, any, any>\n observer: QueryObserver<any, any, any, any, any>\n}\n\n/**\n * The event passed to a `QueryCache` subscriber. Fired whenever a query is added or removed from\n * the cache, its state is updated (e.g. via `query.setState` or `queryClient.removeQueries`), or\n * one of its observers is added, removed, or has its results or options updated.\n */\nexport type QueryCacheNotifyEvent =\n | NotifyEventQueryAdded\n | NotifyEventQueryRemoved\n | NotifyEventQueryUpdated\n | NotifyEventQueryObserverAdded\n | NotifyEventQueryObserverRemoved\n | NotifyEventQueryObserverResultsUpdated\n | NotifyEventQueryObserverOptionsUpdated\n\ntype QueryCacheListener = (event: QueryCacheNotifyEvent) => void\n\nexport interface QueryStore {\n has: (queryHash: string) => boolean\n set: (queryHash: string, query: Query) => void\n get: (queryHash: string) => Query | undefined\n delete: (queryHash: string) => void\n values: () => IterableIterator<Query>\n}\n\n// CLASS\n\n/**\n * The `QueryCache` is the storage mechanism for TanStack Query. It stores all the data, meta\n * information, and state of the queries it contains.\n *\n * Normally, you will not interact with the `QueryCache` directly and instead use a `QueryClient`\n * for a specific cache. You can subscribe to it (inherited from `Subscribable`) to be informed of\n * safe/known updates to the cache, such as queries being added, removed, or updated — updates made\n * outside of the cache's own tracked mechanisms (e.g. mutating a query's state object directly) do\n * not notify subscribers.\n *\n * @example\n * ```ts\n * const unsubscribe = queryCache.subscribe((event) => {\n * console.log(event.type, event.query)\n * })\n * ```\n */\nexport class QueryCache extends Subscribable<QueryCacheListener> {\n #queries: QueryStore\n\n constructor(public config: QueryCacheConfig = {}) {\n super()\n this.#queries = new Map<string, Query>()\n }\n\n /**\n * Returns the existing `Query` instance for the given options' `queryKey`/`queryHash`, or\n * builds and adds a new one to the cache if none exists yet. Used by framework adapters and\n * plugins (e.g. broadcast/persistence) that need to get-or-create a `Query` directly, bypassing\n * the reactive `QueryObserver` machinery.\n *\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * const query = queryCache.build(queryClient, {\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * })\n * ```\n */\n build<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n >(\n client: QueryClient,\n options: WithRequired<\n QueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n 'queryKey'\n >,\n state?: QueryState<TData, TError>,\n ): Query<TQueryFnData, TError, TData, TQueryKey> {\n const queryKey = options.queryKey\n const queryHash =\n options.queryHash ?? hashQueryKeyByOptions(queryKey, options)\n let query = this.get<TQueryFnData, TError, TData, TQueryKey>(queryHash)\n\n if (!query) {\n query = new Query({\n client,\n queryKey,\n queryHash,\n options: client.defaultQueryOptions(options),\n state,\n defaultOptions: client.getQueryDefaults(queryKey),\n })\n this.add(query)\n }\n\n return query\n }\n\n /** @internal */\n add(query: Query<any, any, any, any>): void {\n if (!this.#queries.has(query.queryHash)) {\n this.#queries.set(query.queryHash, query)\n\n this.notify({\n type: 'added',\n query,\n })\n }\n }\n\n /**\n * Destroys the given `Query` and removes it from the cache, notifying subscribers with a\n * `'removed'` event. A no-op if the query is no longer the one currently stored under its hash\n * (e.g. it was already replaced). Used by plugins (e.g. the broadcast client) that mirror\n * removals across `QueryCache` instances.\n *\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n * const query = queryCache.find({ queryKey: ['posts'] })\n *\n * if (query) {\n * queryCache.remove(query)\n * }\n * ```\n */\n remove(query: Query<any, any, any, any>): void {\n const queryInMap = this.#queries.get(query.queryHash)\n\n if (queryInMap === query) {\n query.destroy()\n this.#queries.delete(query.queryHash)\n this.notify({ type: 'removed', query })\n }\n }\n\n /**\n * Removes all queries from the cache.\n *\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * queryCache.clear()\n * ```\n */\n clear(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((query) => {\n this.remove(query)\n })\n })\n }\n\n /**\n * Returns the `Query` instance stored under the given `queryHash`, or `undefined` if none\n * exists. Unlike {@link QueryCache#find}, this looks up by the already-computed hash rather\n * than by `QueryFilters`. Used by plugins (e.g. broadcast/hydration) that already have a hash\n * to look up directly.\n *\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n * const queryHash = hashKey(['posts'])\n *\n * const query = queryCache.get(queryHash)\n * ```\n */\n get<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n >(\n queryHash: string,\n ): Query<TQueryFnData, TError, TData, TQueryKey> | undefined {\n return this.#queries.get(queryHash) as\n Query<TQueryFnData, TError, TData, TQueryKey> | undefined\n }\n\n /**\n * Returns all queries within the cache.\n *\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * const queries = queryCache.getAll()\n * ```\n */\n getAll(): Array<Query> {\n return [...this.#queries.values()]\n }\n\n /**\n * A slightly more advanced method that can be used to get an existing query instance from the\n * cache. This instance not only contains all the state for the query, but all of the instances,\n * and underlying guts of the query as well. If the query does not exist, `undefined` is\n * returned.\n *\n * This is not typically needed for most applications, but can come in handy when needing more\n * information about a query in rare scenarios (e.g. looking at `query.state.dataUpdatedAt` to\n * decide whether a query is fresh enough to be used as an initial value).\n *\n * @see {@link QueryCache#findAll}\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * const query = queryCache.find({ queryKey: ['posts'] })\n * ```\n */\n find<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData>(\n filters: WithRequired<QueryFilters, 'queryKey'>,\n ): Query<TQueryFnData, TError, TData> | undefined {\n const defaultedFilters = { exact: true, ...filters }\n\n return this.getAll().find((query) =>\n matchQuery(defaultedFilters, query),\n ) as Query<TQueryFnData, TError, TData> | undefined\n }\n\n /**\n * An even more advanced method that can be used to get existing query instances from the cache\n * that partially match a query key. If no queries match, an empty array is returned.\n *\n * This is not typically needed for most applications, but can come in handy when needing more\n * information about queries in rare scenarios.\n *\n * @see {@link QueryCache#find}\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * const queries = queryCache.findAll({ queryKey: ['posts'] })\n * ```\n */\n findAll(filters: QueryFilters<any> = {}): Array<Query> {\n const queries = this.getAll()\n return Object.keys(filters).length > 0\n ? queries.filter((query) => matchQuery(filters, query))\n : queries\n }\n\n /** @internal */\n notify(event: QueryCacheNotifyEvent): void {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(event)\n })\n })\n }\n\n /** @internal */\n onFocus(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((query) => {\n query.onFocus()\n })\n })\n }\n\n /** @internal */\n onOnline(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((query) => {\n query.onOnline()\n })\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA0HA,IAAa,aAAb,cAAgCA,qBAAAA,aAAiC;CAG/D,YAAY,SAAkC,CAAC,GAAG;EAChD,MAAM;EADW,KAAA,SAAA;;EAEjB,8BAAA,uBAAA,UAAA,sBAAgB,IAAI,IAAmB,CAAA;CACzC;;;;;;;;;;;;;;;;;CAkBA,MAME,QACA,SAIA,OAC+C;EAC/C,MAAM,WAAW,QAAQ;EACzB,MAAM,YACJ,QAAQ,aAAaC,cAAAA,sBAAsB,UAAU,OAAO;EAC9D,IAAI,QAAQ,KAAK,IAA4C,SAAS;EAEtE,IAAI,CAAC,OAAO;GACV,QAAQ,IAAIC,cAAAA,MAAM;IAChB;IACA;IACA;IACA,SAAS,OAAO,oBAAoB,OAAO;IAC3C;IACA,gBAAgB,OAAO,iBAAiB,QAAQ;GAClD,CAAC;GACD,KAAK,IAAI,KAAK;EAChB;EAEA,OAAO;CACT;;CAGA,IAAI,OAAwC;EAC1C,IAAI,CAAA,8BAAA,uBAAA,UAAC,IAAA,CAAA,CAAc,IAAI,MAAM,SAAS,GAAG;GACvC,8BAAA,uBAAA,UAAA,IAAA,CAAA,CAAc,IAAI,MAAM,WAAW,KAAK;GAExC,KAAK,OAAO;IACV,MAAM;IACN;GACF,CAAC;EACH;CACF;;;;;;;;;;;;;;;;;CAkBA,OAAO,OAAwC;EAG7C,IAAA,8BAAA,uBAAA,UAFmB,IAAA,CAAA,CAAc,IAAI,MAAM,SAE9B,MAAM,OAAO;GACxB,MAAM,QAAQ;GACd,8BAAA,uBAAA,UAAA,IAAA,CAAA,CAAc,OAAO,MAAM,SAAS;GACpC,KAAK,OAAO;IAAE,MAAM;IAAW;GAAM,CAAC;EACxC;CACF;;;;;;;;;;;CAYA,QAAc;EACZ,sBAAA,cAAc,YAAY;GACxB,KAAK,OAAO,CAAC,CAAC,SAAS,UAAU;IAC/B,KAAK,OAAO,KAAK;GACnB,CAAC;EACH,CAAC;CACH;;;;;;;;;;;;;;;CAgBA,IAME,WAC2D;EAC3D,OAAA,8BAAA,uBAAA,UAAO,IAAA,CAAA,CAAc,IAAI,SAAS;CAEpC;;;;;;;;;;;CAYA,SAAuB;EACrB,OAAO,CAAC,GAAA,8BAAA,uBAAA,UAAG,IAAA,CAAA,CAAc,OAAO,CAAC;CACnC;;;;;;;;;;;;;;;;;;;CAoBA,KACE,SACgD;EAChD,MAAM,mBAAmB;GAAE,OAAO;GAAM,GAAG;EAAQ;EAEnD,OAAO,KAAK,OAAO,CAAC,CAAC,MAAM,UACzBC,cAAAA,WAAW,kBAAkB,KAAK,CACpC;CACF;;;;;;;;;;;;;;;;CAiBA,QAAQ,UAA6B,CAAC,GAAiB;EACrD,MAAM,UAAU,KAAK,OAAO;EAC5B,OAAO,OAAO,KAAK,OAAO,CAAC,CAAC,SAAS,IACjC,QAAQ,QAAQ,UAAUA,cAAAA,WAAW,SAAS,KAAK,CAAC,IACpD;CACN;;CAGA,OAAO,OAAoC;EACzC,sBAAA,cAAc,YAAY;GACxB,KAAK,UAAU,SAAS,aAAa;IACnC,SAAS,KAAK;GAChB,CAAC;EACH,CAAC;CACH;;CAGA,UAAgB;EACd,sBAAA,cAAc,YAAY;GACxB,KAAK,OAAO,CAAC,CAAC,SAAS,UAAU;IAC/B,MAAM,QAAQ;GAChB,CAAC;EACH,CAAC;CACH;;CAGA,WAAiB;EACf,sBAAA,cAAc,YAAY;GACxB,KAAK,OAAO,CAAC,CAAC,SAAS,UAAU;IAC/B,MAAM,SAAS;GACjB,CAAC;EACH,CAAC;CACH;AACF"}
|
|
1
|
+
{"version":3,"file":"queryCache.cjs","names":["Subscribable","hashQueryKeyByOptions","Query","matchQuery"],"sources":["../../src/queryCache.ts"],"sourcesContent":["import { hashQueryKeyByOptions, matchQuery } from './utils'\nimport { Query } from './query'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport type { QueryFilters } from './utils'\nimport type { Action, QueryState } from './query'\nimport type {\n DefaultError,\n NotifyEvent,\n QueryKey,\n QueryOptions,\n WithRequired,\n} from './types'\nimport type { QueryClient } from './queryClient'\nimport type { QueryObserver } from './queryObserver'\n\n// TYPES\n\n/**\n * Global callbacks that fire for every query handled by a `QueryCache`, regardless of which\n * component or observer triggered it. Unlike `QueryClient`'s `defaultOptions`, which a query can\n * override, these callbacks are always called. Unlike `MutationCacheConfig`'s callbacks, these\n * are fire-and-forget: their return value is not awaited before the query settles.\n */\nexport interface QueryCacheConfig {\n /** Called when any query in the cache encounters an error. */\n onError?: (\n error: DefaultError,\n query: Query<unknown, unknown, unknown>,\n ) => void\n /** Called when any query in the cache is successful. */\n onSuccess?: (data: unknown, query: Query<unknown, unknown, unknown>) => void\n /** Called when any query in the cache is settled, either successfully or with an error. */\n onSettled?: (\n data: unknown | undefined,\n error: DefaultError | null,\n query: Query<unknown, unknown, unknown>,\n ) => void\n}\n\ninterface NotifyEventQueryAdded extends NotifyEvent {\n type: 'added'\n query: Query<any, any, any, any>\n}\n\ninterface NotifyEventQueryRemoved extends NotifyEvent {\n type: 'removed'\n query: Query<any, any, any, any>\n}\n\ninterface NotifyEventQueryUpdated extends NotifyEvent {\n type: 'updated'\n query: Query<any, any, any, any>\n action: Action<any, any>\n}\n\ninterface NotifyEventQueryObserverAdded extends NotifyEvent {\n type: 'observerAdded'\n query: Query<any, any, any, any>\n observer: QueryObserver<any, any, any, any, any>\n}\n\ninterface NotifyEventQueryObserverRemoved extends NotifyEvent {\n type: 'observerRemoved'\n query: Query<any, any, any, any>\n observer: QueryObserver<any, any, any, any, any>\n}\n\ninterface NotifyEventQueryObserverResultsUpdated extends NotifyEvent {\n type: 'observerResultsUpdated'\n query: Query<any, any, any, any>\n}\n\ninterface NotifyEventQueryObserverOptionsUpdated extends NotifyEvent {\n type: 'observerOptionsUpdated'\n query: Query<any, any, any, any>\n observer: QueryObserver<any, any, any, any, any>\n}\n\n/**\n * The event passed to a `QueryCache` subscriber. Fired whenever a query is added or removed from\n * the cache, its state is updated (e.g. via `query.setState` or `queryClient.removeQueries`), or\n * one of its observers is added, removed, or has its results or options updated.\n */\nexport type QueryCacheNotifyEvent =\n | NotifyEventQueryAdded\n | NotifyEventQueryRemoved\n | NotifyEventQueryUpdated\n | NotifyEventQueryObserverAdded\n | NotifyEventQueryObserverRemoved\n | NotifyEventQueryObserverResultsUpdated\n | NotifyEventQueryObserverOptionsUpdated\n\ntype QueryCacheListener = (event: QueryCacheNotifyEvent) => void\n\n/**\n * The store a `QueryCache` keeps its queries in, keyed by query hash.\n */\nexport interface QueryStore {\n /**\n * Returns whether a query with the given hash is stored.\n */\n has: (queryHash: string) => boolean\n /**\n * Stores a query under its hash.\n */\n set: (queryHash: string, query: Query) => void\n /**\n * Returns the query with the given hash, if any.\n */\n get: (queryHash: string) => Query | undefined\n /**\n * Removes the query with the given hash.\n */\n delete: (queryHash: string) => void\n /**\n * Returns an iterator over all stored queries.\n */\n values: () => IterableIterator<Query>\n}\n\n// CLASS\n\n/**\n * The `QueryCache` is the storage mechanism for TanStack Query. It stores all the data, meta\n * information, and state of the queries it contains.\n *\n * Normally, you will not interact with the `QueryCache` directly and instead use a `QueryClient`\n * for a specific cache. You can subscribe to it (inherited from `Subscribable`) to be informed of\n * safe/known updates to the cache, such as queries being added, removed, or updated — updates made\n * outside of the cache's own tracked mechanisms (e.g. mutating a query's state object directly) do\n * not notify subscribers.\n * @example\n * ```ts\n * const unsubscribe = queryCache.subscribe((event) => {\n * console.log(event.type, event.query)\n * })\n * ```\n */\nexport class QueryCache extends Subscribable<QueryCacheListener> {\n #queries: QueryStore\n\n constructor(public config: QueryCacheConfig = {}) {\n super()\n this.#queries = new Map<string, Query>()\n }\n\n /**\n * Returns the existing `Query` instance for the given options' `queryKey`/`queryHash`, or\n * builds and adds a new one to the cache if none exists yet. Used by framework adapters and\n * plugins (e.g. broadcast/persistence) that need to get-or-create a `Query` directly, bypassing\n * the reactive `QueryObserver` machinery.\n * @param client - The client the query belongs to, used to default its options.\n * @param options - The query options, including the `queryKey`. A new query is created with the\n * options defaulted by {@link QueryClient#defaultQueryOptions}.\n * @param state - The initial state of a newly created query, e.g. when hydrating. Ignored if the\n * query already exists.\n * @returns The existing or newly created query.\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * const query = queryCache.build(queryClient, {\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * })\n * ```\n */\n build<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n >(\n client: QueryClient,\n options: WithRequired<\n QueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n 'queryKey'\n >,\n state?: QueryState<TData, TError>,\n ): Query<TQueryFnData, TError, TData, TQueryKey> {\n const queryKey = options.queryKey\n const queryHash =\n options.queryHash ?? hashQueryKeyByOptions(queryKey, options)\n let query = this.get<TQueryFnData, TError, TData, TQueryKey>(queryHash)\n\n if (!query) {\n query = new Query({\n client,\n queryKey,\n queryHash,\n options: client.defaultQueryOptions(options),\n state,\n defaultOptions: client.getQueryDefaults(queryKey),\n })\n this.add(query)\n }\n\n return query\n }\n\n /**\n * Adds a query to the cache.\n * @internal\n * @param query - The query to add. If a query with the same hash is already cached, nothing\n * happens.\n */\n add(query: Query<any, any, any, any>): void {\n if (!this.#queries.has(query.queryHash)) {\n this.#queries.set(query.queryHash, query)\n\n this.notify({\n type: 'added',\n query,\n })\n }\n }\n\n /**\n * Destroys the given `Query` and removes it from the cache, notifying subscribers with a\n * `'removed'` event. A no-op if the query is no longer the one currently stored under its hash\n * (e.g. it was already replaced). Used by plugins (e.g. the broadcast client) that mirror\n * removals across `QueryCache` instances.\n * @param query - The query to remove.\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n * const query = queryCache.find({ queryKey: ['posts'] })\n *\n * if (query) {\n * queryCache.remove(query)\n * }\n * ```\n */\n remove(query: Query<any, any, any, any>): void {\n const queryInMap = this.#queries.get(query.queryHash)\n\n if (queryInMap === query) {\n query.destroy()\n this.#queries.delete(query.queryHash)\n this.notify({ type: 'removed', query })\n }\n }\n\n /**\n * Removes all queries from the cache.\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * queryCache.clear()\n * ```\n */\n clear(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((query) => {\n this.remove(query)\n })\n })\n }\n\n /**\n * Returns the `Query` instance stored under the given `queryHash`, or `undefined` if none\n * exists. Unlike {@link QueryCache#find}, this looks up by the already-computed hash rather\n * than by `QueryFilters`. Used by plugins (e.g. broadcast/hydration) that already have a hash\n * to look up directly.\n * @param queryHash - The hash of the query to look up.\n * @returns The query stored under the hash, or `undefined`.\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n * const queryHash = hashKey(['posts'])\n *\n * const query = queryCache.get(queryHash)\n * ```\n */\n get<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n >(\n queryHash: string,\n ): Query<TQueryFnData, TError, TData, TQueryKey> | undefined {\n return this.#queries.get(queryHash) as\n | Query<TQueryFnData, TError, TData, TQueryKey>\n | undefined\n }\n\n /**\n * Returns all queries within the cache.\n * @returns Every query in the cache.\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * const queries = queryCache.getAll()\n * ```\n */\n getAll(): Array<Query> {\n return [...this.#queries.values()]\n }\n\n /**\n * A slightly more advanced method that can be used to get an existing query instance from the\n * cache. This instance not only contains all the state for the query, but all of the instances,\n * and underlying guts of the query as well. If the query does not exist, `undefined` is\n * returned.\n *\n * This is not typically needed for most applications, but can come in handy when needing more\n * information about a query in rare scenarios (e.g. looking at `query.state.dataUpdatedAt` to\n * decide whether a query is fresh enough to be used as an initial value).\n * @param filters - The filters to match, including the required `queryKey`. `exact` defaults to\n * `true`.\n * @returns The first matching query, or `undefined`.\n * @see {@link QueryCache#findAll}\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * const query = queryCache.find({ queryKey: ['posts'] })\n * ```\n */\n find<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData>(\n filters: WithRequired<QueryFilters, 'queryKey'>,\n ): Query<TQueryFnData, TError, TData> | undefined {\n const defaultedFilters = { exact: true, ...filters }\n\n return this.getAll().find((query) =>\n matchQuery(defaultedFilters, query),\n ) as Query<TQueryFnData, TError, TData> | undefined\n }\n\n /**\n * An even more advanced method that can be used to get existing query instances from the cache\n * that partially match a query key. If no queries match, an empty array is returned.\n *\n * This is not typically needed for most applications, but can come in handy when needing more\n * information about queries in rare scenarios.\n * @param filters - The filters to match. Without filters, every query is returned.\n * @returns The matching queries.\n * @see {@link QueryCache#find}\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * const queries = queryCache.findAll({ queryKey: ['posts'] })\n * ```\n */\n findAll(filters: QueryFilters<any> = {}): Array<Query> {\n const queries = this.getAll()\n return Object.keys(filters).length > 0\n ? queries.filter((query) => matchQuery(filters, query))\n : queries\n }\n\n /**\n * Notifies every cache listener of an event, in one batch.\n * @internal\n * @param event - The event passed to every listener.\n */\n notify(event: QueryCacheNotifyEvent): void {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(event)\n })\n })\n }\n\n /**\n * Calls {@link Query#onFocus} on every cached query, in one batch.\n * @internal\n */\n onFocus(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((query) => {\n query.onFocus()\n })\n })\n }\n\n /**\n * Calls {@link Query#onOnline} on every cached query, in one batch.\n * @internal\n */\n onOnline(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((query) => {\n query.onOnline()\n })\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA2IA,IAAa,aAAb,cAAgCA,qBAAAA,aAAiC;CAG/D,YAAY,SAAkC,CAAC,GAAG;EAChD,MAAM;EADW,KAAA,SAAA;;EAEjB,8BAAA,uBAAA,UAAA,sBAAgB,IAAI,IAAmB,CAAA;CACzC;;;;;;;;;;;;;;;;;;;;;;CAuBA,MAME,QACA,SAIA,OAC+C;EAC/C,MAAM,WAAW,QAAQ;EACzB,MAAM,YACJ,QAAQ,aAAaC,cAAAA,sBAAsB,UAAU,OAAO;EAC9D,IAAI,QAAQ,KAAK,IAA4C,SAAS;EAEtE,IAAI,CAAC,OAAO;GACV,QAAQ,IAAIC,cAAAA,MAAM;IAChB;IACA;IACA;IACA,SAAS,OAAO,oBAAoB,OAAO;IAC3C;IACA,gBAAgB,OAAO,iBAAiB,QAAQ;GAClD,CAAC;GACD,KAAK,IAAI,KAAK;EAChB;EAEA,OAAO;CACT;;;;;;;CAQA,IAAI,OAAwC;EAC1C,IAAI,CAAA,8BAAA,uBAAA,UAAC,IAAA,CAAA,CAAc,IAAI,MAAM,SAAS,GAAG;GACvC,8BAAA,uBAAA,UAAA,IAAA,CAAA,CAAc,IAAI,MAAM,WAAW,KAAK;GAExC,KAAK,OAAO;IACV,MAAM;IACN;GACF,CAAC;EACH;CACF;;;;;;;;;;;;;;;;;CAkBA,OAAO,OAAwC;EAG7C,IAAA,8BAAA,uBAAA,UAFmB,IAAA,CAAA,CAAc,IAAI,MAAM,SAE9B,MAAM,OAAO;GACxB,MAAM,QAAQ;GACd,8BAAA,uBAAA,UAAA,IAAA,CAAA,CAAc,OAAO,MAAM,SAAS;GACpC,KAAK,OAAO;IAAE,MAAM;IAAW;GAAM,CAAC;EACxC;CACF;;;;;;;;;;CAWA,QAAc;EACZ,sBAAA,cAAc,YAAY;GACxB,KAAK,OAAO,CAAC,CAAC,SAAS,UAAU;IAC/B,KAAK,OAAO,KAAK;GACnB,CAAC;EACH,CAAC;CACH;;;;;;;;;;;;;;;;CAiBA,IAME,WAC2D;EAC3D,OAAA,8BAAA,uBAAA,UAAO,IAAA,CAAA,CAAc,IAAI,SAAS;CAGpC;;;;;;;;;;;CAYA,SAAuB;EACrB,OAAO,CAAC,GAAA,8BAAA,uBAAA,UAAG,IAAA,CAAA,CAAc,OAAO,CAAC;CACnC;;;;;;;;;;;;;;;;;;;;;CAsBA,KACE,SACgD;EAChD,MAAM,mBAAmB;GAAE,OAAO;GAAM,GAAG;EAAQ;EAEnD,OAAO,KAAK,OAAO,CAAC,CAAC,MAAM,UACzBC,cAAAA,WAAW,kBAAkB,KAAK,CACpC;CACF;;;;;;;;;;;;;;;;;CAkBA,QAAQ,UAA6B,CAAC,GAAiB;EACrD,MAAM,UAAU,KAAK,OAAO;EAC5B,OAAO,OAAO,KAAK,OAAO,CAAC,CAAC,SAAS,IACjC,QAAQ,QAAQ,UAAUA,cAAAA,WAAW,SAAS,KAAK,CAAC,IACpD;CACN;;;;;;CAOA,OAAO,OAAoC;EACzC,sBAAA,cAAc,YAAY;GACxB,KAAK,UAAU,SAAS,aAAa;IACnC,SAAS,KAAK;GAChB,CAAC;EACH,CAAC;CACH;;;;;CAMA,UAAgB;EACd,sBAAA,cAAc,YAAY;GACxB,KAAK,OAAO,CAAC,CAAC,SAAS,UAAU;IAC/B,MAAM,QAAQ;GAChB,CAAC;EACH,CAAC;CACH;;;;;CAMA,WAAiB;EACf,sBAAA,cAAc,YAAY;GACxB,KAAK,OAAO,CAAC,CAAC,SAAS,UAAU;IAC/B,MAAM,SAAS;GACjB,CAAC;EACH,CAAC;CACH;AACF"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { cn as QueryCacheConfig, ln as QueryCacheNotifyEvent, sn as QueryCache, un as QueryStore } from "./hydration-
|
|
1
|
+
import { cn as QueryCacheConfig, ln as QueryCacheNotifyEvent, sn as QueryCache, un as QueryStore } from "./hydration-C6zghADG.cjs";
|
|
2
2
|
export { QueryCache, QueryCacheConfig, QueryCacheNotifyEvent, QueryStore };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { cn as QueryCacheConfig, ln as QueryCacheNotifyEvent, sn as QueryCache, un as QueryStore } from "./hydration-
|
|
1
|
+
import { cn as QueryCacheConfig, ln as QueryCacheNotifyEvent, sn as QueryCache, un as QueryStore } from "./hydration-Bf0oMcwO.js";
|
|
2
2
|
export { QueryCache, QueryCacheConfig, QueryCacheNotifyEvent, QueryStore };
|
|
@@ -14,7 +14,6 @@ var _queries = /* @__PURE__ */ new WeakMap();
|
|
|
14
14
|
* safe/known updates to the cache, such as queries being added, removed, or updated — updates made
|
|
15
15
|
* outside of the cache's own tracked mechanisms (e.g. mutating a query's state object directly) do
|
|
16
16
|
* not notify subscribers.
|
|
17
|
-
*
|
|
18
17
|
* @example
|
|
19
18
|
* ```ts
|
|
20
19
|
* const unsubscribe = queryCache.subscribe((event) => {
|
|
@@ -34,7 +33,12 @@ var QueryCache = class extends Subscribable {
|
|
|
34
33
|
* builds and adds a new one to the cache if none exists yet. Used by framework adapters and
|
|
35
34
|
* plugins (e.g. broadcast/persistence) that need to get-or-create a `Query` directly, bypassing
|
|
36
35
|
* the reactive `QueryObserver` machinery.
|
|
37
|
-
*
|
|
36
|
+
* @param client - The client the query belongs to, used to default its options.
|
|
37
|
+
* @param options - The query options, including the `queryKey`. A new query is created with the
|
|
38
|
+
* options defaulted by {@link QueryClient#defaultQueryOptions}.
|
|
39
|
+
* @param state - The initial state of a newly created query, e.g. when hydrating. Ignored if the
|
|
40
|
+
* query already exists.
|
|
41
|
+
* @returns The existing or newly created query.
|
|
38
42
|
* @example
|
|
39
43
|
* ```ts
|
|
40
44
|
* const queryCache = queryClient.getQueryCache()
|
|
@@ -62,7 +66,12 @@ var QueryCache = class extends Subscribable {
|
|
|
62
66
|
}
|
|
63
67
|
return query;
|
|
64
68
|
}
|
|
65
|
-
/**
|
|
69
|
+
/**
|
|
70
|
+
* Adds a query to the cache.
|
|
71
|
+
* @internal
|
|
72
|
+
* @param query - The query to add. If a query with the same hash is already cached, nothing
|
|
73
|
+
* happens.
|
|
74
|
+
*/
|
|
66
75
|
add(query) {
|
|
67
76
|
if (!_classPrivateFieldGet2(_queries, this).has(query.queryHash)) {
|
|
68
77
|
_classPrivateFieldGet2(_queries, this).set(query.queryHash, query);
|
|
@@ -77,7 +86,7 @@ var QueryCache = class extends Subscribable {
|
|
|
77
86
|
* `'removed'` event. A no-op if the query is no longer the one currently stored under its hash
|
|
78
87
|
* (e.g. it was already replaced). Used by plugins (e.g. the broadcast client) that mirror
|
|
79
88
|
* removals across `QueryCache` instances.
|
|
80
|
-
*
|
|
89
|
+
* @param query - The query to remove.
|
|
81
90
|
* @example
|
|
82
91
|
* ```ts
|
|
83
92
|
* const queryCache = queryClient.getQueryCache()
|
|
@@ -100,7 +109,6 @@ var QueryCache = class extends Subscribable {
|
|
|
100
109
|
}
|
|
101
110
|
/**
|
|
102
111
|
* Removes all queries from the cache.
|
|
103
|
-
*
|
|
104
112
|
* @example
|
|
105
113
|
* ```ts
|
|
106
114
|
* const queryCache = queryClient.getQueryCache()
|
|
@@ -120,7 +128,8 @@ var QueryCache = class extends Subscribable {
|
|
|
120
128
|
* exists. Unlike {@link QueryCache#find}, this looks up by the already-computed hash rather
|
|
121
129
|
* than by `QueryFilters`. Used by plugins (e.g. broadcast/hydration) that already have a hash
|
|
122
130
|
* to look up directly.
|
|
123
|
-
*
|
|
131
|
+
* @param queryHash - The hash of the query to look up.
|
|
132
|
+
* @returns The query stored under the hash, or `undefined`.
|
|
124
133
|
* @example
|
|
125
134
|
* ```ts
|
|
126
135
|
* const queryCache = queryClient.getQueryCache()
|
|
@@ -134,7 +143,7 @@ var QueryCache = class extends Subscribable {
|
|
|
134
143
|
}
|
|
135
144
|
/**
|
|
136
145
|
* Returns all queries within the cache.
|
|
137
|
-
*
|
|
146
|
+
* @returns Every query in the cache.
|
|
138
147
|
* @example
|
|
139
148
|
* ```ts
|
|
140
149
|
* const queryCache = queryClient.getQueryCache()
|
|
@@ -154,7 +163,9 @@ var QueryCache = class extends Subscribable {
|
|
|
154
163
|
* This is not typically needed for most applications, but can come in handy when needing more
|
|
155
164
|
* information about a query in rare scenarios (e.g. looking at `query.state.dataUpdatedAt` to
|
|
156
165
|
* decide whether a query is fresh enough to be used as an initial value).
|
|
157
|
-
*
|
|
166
|
+
* @param filters - The filters to match, including the required `queryKey`. `exact` defaults to
|
|
167
|
+
* `true`.
|
|
168
|
+
* @returns The first matching query, or `undefined`.
|
|
158
169
|
* @see {@link QueryCache#findAll}
|
|
159
170
|
* @example
|
|
160
171
|
* ```ts
|
|
@@ -176,7 +187,8 @@ var QueryCache = class extends Subscribable {
|
|
|
176
187
|
*
|
|
177
188
|
* This is not typically needed for most applications, but can come in handy when needing more
|
|
178
189
|
* information about queries in rare scenarios.
|
|
179
|
-
*
|
|
190
|
+
* @param filters - The filters to match. Without filters, every query is returned.
|
|
191
|
+
* @returns The matching queries.
|
|
180
192
|
* @see {@link QueryCache#find}
|
|
181
193
|
* @example
|
|
182
194
|
* ```ts
|
|
@@ -189,7 +201,11 @@ var QueryCache = class extends Subscribable {
|
|
|
189
201
|
const queries = this.getAll();
|
|
190
202
|
return Object.keys(filters).length > 0 ? queries.filter((query) => matchQuery(filters, query)) : queries;
|
|
191
203
|
}
|
|
192
|
-
/**
|
|
204
|
+
/**
|
|
205
|
+
* Notifies every cache listener of an event, in one batch.
|
|
206
|
+
* @internal
|
|
207
|
+
* @param event - The event passed to every listener.
|
|
208
|
+
*/
|
|
193
209
|
notify(event) {
|
|
194
210
|
notifyManager.batch(() => {
|
|
195
211
|
this.listeners.forEach((listener) => {
|
|
@@ -197,7 +213,10 @@ var QueryCache = class extends Subscribable {
|
|
|
197
213
|
});
|
|
198
214
|
});
|
|
199
215
|
}
|
|
200
|
-
/**
|
|
216
|
+
/**
|
|
217
|
+
* Calls {@link Query#onFocus} on every cached query, in one batch.
|
|
218
|
+
* @internal
|
|
219
|
+
*/
|
|
201
220
|
onFocus() {
|
|
202
221
|
notifyManager.batch(() => {
|
|
203
222
|
this.getAll().forEach((query) => {
|
|
@@ -205,7 +224,10 @@ var QueryCache = class extends Subscribable {
|
|
|
205
224
|
});
|
|
206
225
|
});
|
|
207
226
|
}
|
|
208
|
-
/**
|
|
227
|
+
/**
|
|
228
|
+
* Calls {@link Query#onOnline} on every cached query, in one batch.
|
|
229
|
+
* @internal
|
|
230
|
+
*/
|
|
209
231
|
onOnline() {
|
|
210
232
|
notifyManager.batch(() => {
|
|
211
233
|
this.getAll().forEach((query) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queryCache.js","names":[],"sources":["../../src/queryCache.ts"],"sourcesContent":["import { hashQueryKeyByOptions, matchQuery } from './utils'\nimport { Query } from './query'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport type { QueryFilters } from './utils'\nimport type { Action, QueryState } from './query'\nimport type {\n DefaultError,\n NotifyEvent,\n QueryKey,\n QueryOptions,\n WithRequired,\n} from './types'\nimport type { QueryClient } from './queryClient'\nimport type { QueryObserver } from './queryObserver'\n\n// TYPES\n\n/**\n * Global callbacks that fire for every query handled by a `QueryCache`, regardless of which\n * component or observer triggered it. Unlike `QueryClient`'s `defaultOptions`, which a query can\n * override, these callbacks are always called. Unlike `MutationCacheConfig`'s callbacks, these\n * are fire-and-forget: their return value is not awaited before the query settles.\n */\nexport interface QueryCacheConfig {\n /** Called when any query in the cache encounters an error. */\n onError?: (\n error: DefaultError,\n query: Query<unknown, unknown, unknown>,\n ) => void\n /** Called when any query in the cache is successful. */\n onSuccess?: (data: unknown, query: Query<unknown, unknown, unknown>) => void\n /** Called when any query in the cache is settled, either successfully or with an error. */\n onSettled?: (\n data: unknown | undefined,\n error: DefaultError | null,\n query: Query<unknown, unknown, unknown>,\n ) => void\n}\n\ninterface NotifyEventQueryAdded extends NotifyEvent {\n type: 'added'\n query: Query<any, any, any, any>\n}\n\ninterface NotifyEventQueryRemoved extends NotifyEvent {\n type: 'removed'\n query: Query<any, any, any, any>\n}\n\ninterface NotifyEventQueryUpdated extends NotifyEvent {\n type: 'updated'\n query: Query<any, any, any, any>\n action: Action<any, any>\n}\n\ninterface NotifyEventQueryObserverAdded extends NotifyEvent {\n type: 'observerAdded'\n query: Query<any, any, any, any>\n observer: QueryObserver<any, any, any, any, any>\n}\n\ninterface NotifyEventQueryObserverRemoved extends NotifyEvent {\n type: 'observerRemoved'\n query: Query<any, any, any, any>\n observer: QueryObserver<any, any, any, any, any>\n}\n\ninterface NotifyEventQueryObserverResultsUpdated extends NotifyEvent {\n type: 'observerResultsUpdated'\n query: Query<any, any, any, any>\n}\n\ninterface NotifyEventQueryObserverOptionsUpdated extends NotifyEvent {\n type: 'observerOptionsUpdated'\n query: Query<any, any, any, any>\n observer: QueryObserver<any, any, any, any, any>\n}\n\n/**\n * The event passed to a `QueryCache` subscriber. Fired whenever a query is added or removed from\n * the cache, its state is updated (e.g. via `query.setState` or `queryClient.removeQueries`), or\n * one of its observers is added, removed, or has its results or options updated.\n */\nexport type QueryCacheNotifyEvent =\n | NotifyEventQueryAdded\n | NotifyEventQueryRemoved\n | NotifyEventQueryUpdated\n | NotifyEventQueryObserverAdded\n | NotifyEventQueryObserverRemoved\n | NotifyEventQueryObserverResultsUpdated\n | NotifyEventQueryObserverOptionsUpdated\n\ntype QueryCacheListener = (event: QueryCacheNotifyEvent) => void\n\nexport interface QueryStore {\n has: (queryHash: string) => boolean\n set: (queryHash: string, query: Query) => void\n get: (queryHash: string) => Query | undefined\n delete: (queryHash: string) => void\n values: () => IterableIterator<Query>\n}\n\n// CLASS\n\n/**\n * The `QueryCache` is the storage mechanism for TanStack Query. It stores all the data, meta\n * information, and state of the queries it contains.\n *\n * Normally, you will not interact with the `QueryCache` directly and instead use a `QueryClient`\n * for a specific cache. You can subscribe to it (inherited from `Subscribable`) to be informed of\n * safe/known updates to the cache, such as queries being added, removed, or updated — updates made\n * outside of the cache's own tracked mechanisms (e.g. mutating a query's state object directly) do\n * not notify subscribers.\n *\n * @example\n * ```ts\n * const unsubscribe = queryCache.subscribe((event) => {\n * console.log(event.type, event.query)\n * })\n * ```\n */\nexport class QueryCache extends Subscribable<QueryCacheListener> {\n #queries: QueryStore\n\n constructor(public config: QueryCacheConfig = {}) {\n super()\n this.#queries = new Map<string, Query>()\n }\n\n /**\n * Returns the existing `Query` instance for the given options' `queryKey`/`queryHash`, or\n * builds and adds a new one to the cache if none exists yet. Used by framework adapters and\n * plugins (e.g. broadcast/persistence) that need to get-or-create a `Query` directly, bypassing\n * the reactive `QueryObserver` machinery.\n *\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * const query = queryCache.build(queryClient, {\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * })\n * ```\n */\n build<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n >(\n client: QueryClient,\n options: WithRequired<\n QueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n 'queryKey'\n >,\n state?: QueryState<TData, TError>,\n ): Query<TQueryFnData, TError, TData, TQueryKey> {\n const queryKey = options.queryKey\n const queryHash =\n options.queryHash ?? hashQueryKeyByOptions(queryKey, options)\n let query = this.get<TQueryFnData, TError, TData, TQueryKey>(queryHash)\n\n if (!query) {\n query = new Query({\n client,\n queryKey,\n queryHash,\n options: client.defaultQueryOptions(options),\n state,\n defaultOptions: client.getQueryDefaults(queryKey),\n })\n this.add(query)\n }\n\n return query\n }\n\n /** @internal */\n add(query: Query<any, any, any, any>): void {\n if (!this.#queries.has(query.queryHash)) {\n this.#queries.set(query.queryHash, query)\n\n this.notify({\n type: 'added',\n query,\n })\n }\n }\n\n /**\n * Destroys the given `Query` and removes it from the cache, notifying subscribers with a\n * `'removed'` event. A no-op if the query is no longer the one currently stored under its hash\n * (e.g. it was already replaced). Used by plugins (e.g. the broadcast client) that mirror\n * removals across `QueryCache` instances.\n *\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n * const query = queryCache.find({ queryKey: ['posts'] })\n *\n * if (query) {\n * queryCache.remove(query)\n * }\n * ```\n */\n remove(query: Query<any, any, any, any>): void {\n const queryInMap = this.#queries.get(query.queryHash)\n\n if (queryInMap === query) {\n query.destroy()\n this.#queries.delete(query.queryHash)\n this.notify({ type: 'removed', query })\n }\n }\n\n /**\n * Removes all queries from the cache.\n *\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * queryCache.clear()\n * ```\n */\n clear(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((query) => {\n this.remove(query)\n })\n })\n }\n\n /**\n * Returns the `Query` instance stored under the given `queryHash`, or `undefined` if none\n * exists. Unlike {@link QueryCache#find}, this looks up by the already-computed hash rather\n * than by `QueryFilters`. Used by plugins (e.g. broadcast/hydration) that already have a hash\n * to look up directly.\n *\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n * const queryHash = hashKey(['posts'])\n *\n * const query = queryCache.get(queryHash)\n * ```\n */\n get<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n >(\n queryHash: string,\n ): Query<TQueryFnData, TError, TData, TQueryKey> | undefined {\n return this.#queries.get(queryHash) as\n Query<TQueryFnData, TError, TData, TQueryKey> | undefined\n }\n\n /**\n * Returns all queries within the cache.\n *\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * const queries = queryCache.getAll()\n * ```\n */\n getAll(): Array<Query> {\n return [...this.#queries.values()]\n }\n\n /**\n * A slightly more advanced method that can be used to get an existing query instance from the\n * cache. This instance not only contains all the state for the query, but all of the instances,\n * and underlying guts of the query as well. If the query does not exist, `undefined` is\n * returned.\n *\n * This is not typically needed for most applications, but can come in handy when needing more\n * information about a query in rare scenarios (e.g. looking at `query.state.dataUpdatedAt` to\n * decide whether a query is fresh enough to be used as an initial value).\n *\n * @see {@link QueryCache#findAll}\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * const query = queryCache.find({ queryKey: ['posts'] })\n * ```\n */\n find<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData>(\n filters: WithRequired<QueryFilters, 'queryKey'>,\n ): Query<TQueryFnData, TError, TData> | undefined {\n const defaultedFilters = { exact: true, ...filters }\n\n return this.getAll().find((query) =>\n matchQuery(defaultedFilters, query),\n ) as Query<TQueryFnData, TError, TData> | undefined\n }\n\n /**\n * An even more advanced method that can be used to get existing query instances from the cache\n * that partially match a query key. If no queries match, an empty array is returned.\n *\n * This is not typically needed for most applications, but can come in handy when needing more\n * information about queries in rare scenarios.\n *\n * @see {@link QueryCache#find}\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * const queries = queryCache.findAll({ queryKey: ['posts'] })\n * ```\n */\n findAll(filters: QueryFilters<any> = {}): Array<Query> {\n const queries = this.getAll()\n return Object.keys(filters).length > 0\n ? queries.filter((query) => matchQuery(filters, query))\n : queries\n }\n\n /** @internal */\n notify(event: QueryCacheNotifyEvent): void {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(event)\n })\n })\n }\n\n /** @internal */\n onFocus(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((query) => {\n query.onFocus()\n })\n })\n }\n\n /** @internal */\n onOnline(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((query) => {\n query.onOnline()\n })\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA0HA,IAAa,aAAb,cAAgC,aAAiC;CAG/D,YAAY,SAAkC,CAAC,GAAG;EAChD,MAAM;EADW,KAAA,SAAA;;EAEjB,uBAAA,UAAA,sBAAgB,IAAI,IAAmB,CAAA;CACzC;;;;;;;;;;;;;;;;;CAkBA,MAME,QACA,SAIA,OAC+C;EAC/C,MAAM,WAAW,QAAQ;EACzB,MAAM,YACJ,QAAQ,aAAa,sBAAsB,UAAU,OAAO;EAC9D,IAAI,QAAQ,KAAK,IAA4C,SAAS;EAEtE,IAAI,CAAC,OAAO;GACV,QAAQ,IAAI,MAAM;IAChB;IACA;IACA;IACA,SAAS,OAAO,oBAAoB,OAAO;IAC3C;IACA,gBAAgB,OAAO,iBAAiB,QAAQ;GAClD,CAAC;GACD,KAAK,IAAI,KAAK;EAChB;EAEA,OAAO;CACT;;CAGA,IAAI,OAAwC;EAC1C,IAAI,CAAA,uBAAA,UAAC,IAAA,CAAA,CAAc,IAAI,MAAM,SAAS,GAAG;GACvC,uBAAA,UAAA,IAAA,CAAA,CAAc,IAAI,MAAM,WAAW,KAAK;GAExC,KAAK,OAAO;IACV,MAAM;IACN;GACF,CAAC;EACH;CACF;;;;;;;;;;;;;;;;;CAkBA,OAAO,OAAwC;EAG7C,IAAA,uBAAA,UAFmB,IAAA,CAAA,CAAc,IAAI,MAAM,SAE9B,MAAM,OAAO;GACxB,MAAM,QAAQ;GACd,uBAAA,UAAA,IAAA,CAAA,CAAc,OAAO,MAAM,SAAS;GACpC,KAAK,OAAO;IAAE,MAAM;IAAW;GAAM,CAAC;EACxC;CACF;;;;;;;;;;;CAYA,QAAc;EACZ,cAAc,YAAY;GACxB,KAAK,OAAO,CAAC,CAAC,SAAS,UAAU;IAC/B,KAAK,OAAO,KAAK;GACnB,CAAC;EACH,CAAC;CACH;;;;;;;;;;;;;;;CAgBA,IAME,WAC2D;EAC3D,OAAA,uBAAA,UAAO,IAAA,CAAA,CAAc,IAAI,SAAS;CAEpC;;;;;;;;;;;CAYA,SAAuB;EACrB,OAAO,CAAC,GAAA,uBAAA,UAAG,IAAA,CAAA,CAAc,OAAO,CAAC;CACnC;;;;;;;;;;;;;;;;;;;CAoBA,KACE,SACgD;EAChD,MAAM,mBAAmB;GAAE,OAAO;GAAM,GAAG;EAAQ;EAEnD,OAAO,KAAK,OAAO,CAAC,CAAC,MAAM,UACzB,WAAW,kBAAkB,KAAK,CACpC;CACF;;;;;;;;;;;;;;;;CAiBA,QAAQ,UAA6B,CAAC,GAAiB;EACrD,MAAM,UAAU,KAAK,OAAO;EAC5B,OAAO,OAAO,KAAK,OAAO,CAAC,CAAC,SAAS,IACjC,QAAQ,QAAQ,UAAU,WAAW,SAAS,KAAK,CAAC,IACpD;CACN;;CAGA,OAAO,OAAoC;EACzC,cAAc,YAAY;GACxB,KAAK,UAAU,SAAS,aAAa;IACnC,SAAS,KAAK;GAChB,CAAC;EACH,CAAC;CACH;;CAGA,UAAgB;EACd,cAAc,YAAY;GACxB,KAAK,OAAO,CAAC,CAAC,SAAS,UAAU;IAC/B,MAAM,QAAQ;GAChB,CAAC;EACH,CAAC;CACH;;CAGA,WAAiB;EACf,cAAc,YAAY;GACxB,KAAK,OAAO,CAAC,CAAC,SAAS,UAAU;IAC/B,MAAM,SAAS;GACjB,CAAC;EACH,CAAC;CACH;AACF"}
|
|
1
|
+
{"version":3,"file":"queryCache.js","names":[],"sources":["../../src/queryCache.ts"],"sourcesContent":["import { hashQueryKeyByOptions, matchQuery } from './utils'\nimport { Query } from './query'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport type { QueryFilters } from './utils'\nimport type { Action, QueryState } from './query'\nimport type {\n DefaultError,\n NotifyEvent,\n QueryKey,\n QueryOptions,\n WithRequired,\n} from './types'\nimport type { QueryClient } from './queryClient'\nimport type { QueryObserver } from './queryObserver'\n\n// TYPES\n\n/**\n * Global callbacks that fire for every query handled by a `QueryCache`, regardless of which\n * component or observer triggered it. Unlike `QueryClient`'s `defaultOptions`, which a query can\n * override, these callbacks are always called. Unlike `MutationCacheConfig`'s callbacks, these\n * are fire-and-forget: their return value is not awaited before the query settles.\n */\nexport interface QueryCacheConfig {\n /** Called when any query in the cache encounters an error. */\n onError?: (\n error: DefaultError,\n query: Query<unknown, unknown, unknown>,\n ) => void\n /** Called when any query in the cache is successful. */\n onSuccess?: (data: unknown, query: Query<unknown, unknown, unknown>) => void\n /** Called when any query in the cache is settled, either successfully or with an error. */\n onSettled?: (\n data: unknown | undefined,\n error: DefaultError | null,\n query: Query<unknown, unknown, unknown>,\n ) => void\n}\n\ninterface NotifyEventQueryAdded extends NotifyEvent {\n type: 'added'\n query: Query<any, any, any, any>\n}\n\ninterface NotifyEventQueryRemoved extends NotifyEvent {\n type: 'removed'\n query: Query<any, any, any, any>\n}\n\ninterface NotifyEventQueryUpdated extends NotifyEvent {\n type: 'updated'\n query: Query<any, any, any, any>\n action: Action<any, any>\n}\n\ninterface NotifyEventQueryObserverAdded extends NotifyEvent {\n type: 'observerAdded'\n query: Query<any, any, any, any>\n observer: QueryObserver<any, any, any, any, any>\n}\n\ninterface NotifyEventQueryObserverRemoved extends NotifyEvent {\n type: 'observerRemoved'\n query: Query<any, any, any, any>\n observer: QueryObserver<any, any, any, any, any>\n}\n\ninterface NotifyEventQueryObserverResultsUpdated extends NotifyEvent {\n type: 'observerResultsUpdated'\n query: Query<any, any, any, any>\n}\n\ninterface NotifyEventQueryObserverOptionsUpdated extends NotifyEvent {\n type: 'observerOptionsUpdated'\n query: Query<any, any, any, any>\n observer: QueryObserver<any, any, any, any, any>\n}\n\n/**\n * The event passed to a `QueryCache` subscriber. Fired whenever a query is added or removed from\n * the cache, its state is updated (e.g. via `query.setState` or `queryClient.removeQueries`), or\n * one of its observers is added, removed, or has its results or options updated.\n */\nexport type QueryCacheNotifyEvent =\n | NotifyEventQueryAdded\n | NotifyEventQueryRemoved\n | NotifyEventQueryUpdated\n | NotifyEventQueryObserverAdded\n | NotifyEventQueryObserverRemoved\n | NotifyEventQueryObserverResultsUpdated\n | NotifyEventQueryObserverOptionsUpdated\n\ntype QueryCacheListener = (event: QueryCacheNotifyEvent) => void\n\n/**\n * The store a `QueryCache` keeps its queries in, keyed by query hash.\n */\nexport interface QueryStore {\n /**\n * Returns whether a query with the given hash is stored.\n */\n has: (queryHash: string) => boolean\n /**\n * Stores a query under its hash.\n */\n set: (queryHash: string, query: Query) => void\n /**\n * Returns the query with the given hash, if any.\n */\n get: (queryHash: string) => Query | undefined\n /**\n * Removes the query with the given hash.\n */\n delete: (queryHash: string) => void\n /**\n * Returns an iterator over all stored queries.\n */\n values: () => IterableIterator<Query>\n}\n\n// CLASS\n\n/**\n * The `QueryCache` is the storage mechanism for TanStack Query. It stores all the data, meta\n * information, and state of the queries it contains.\n *\n * Normally, you will not interact with the `QueryCache` directly and instead use a `QueryClient`\n * for a specific cache. You can subscribe to it (inherited from `Subscribable`) to be informed of\n * safe/known updates to the cache, such as queries being added, removed, or updated — updates made\n * outside of the cache's own tracked mechanisms (e.g. mutating a query's state object directly) do\n * not notify subscribers.\n * @example\n * ```ts\n * const unsubscribe = queryCache.subscribe((event) => {\n * console.log(event.type, event.query)\n * })\n * ```\n */\nexport class QueryCache extends Subscribable<QueryCacheListener> {\n #queries: QueryStore\n\n constructor(public config: QueryCacheConfig = {}) {\n super()\n this.#queries = new Map<string, Query>()\n }\n\n /**\n * Returns the existing `Query` instance for the given options' `queryKey`/`queryHash`, or\n * builds and adds a new one to the cache if none exists yet. Used by framework adapters and\n * plugins (e.g. broadcast/persistence) that need to get-or-create a `Query` directly, bypassing\n * the reactive `QueryObserver` machinery.\n * @param client - The client the query belongs to, used to default its options.\n * @param options - The query options, including the `queryKey`. A new query is created with the\n * options defaulted by {@link QueryClient#defaultQueryOptions}.\n * @param state - The initial state of a newly created query, e.g. when hydrating. Ignored if the\n * query already exists.\n * @returns The existing or newly created query.\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * const query = queryCache.build(queryClient, {\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * })\n * ```\n */\n build<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n >(\n client: QueryClient,\n options: WithRequired<\n QueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n 'queryKey'\n >,\n state?: QueryState<TData, TError>,\n ): Query<TQueryFnData, TError, TData, TQueryKey> {\n const queryKey = options.queryKey\n const queryHash =\n options.queryHash ?? hashQueryKeyByOptions(queryKey, options)\n let query = this.get<TQueryFnData, TError, TData, TQueryKey>(queryHash)\n\n if (!query) {\n query = new Query({\n client,\n queryKey,\n queryHash,\n options: client.defaultQueryOptions(options),\n state,\n defaultOptions: client.getQueryDefaults(queryKey),\n })\n this.add(query)\n }\n\n return query\n }\n\n /**\n * Adds a query to the cache.\n * @internal\n * @param query - The query to add. If a query with the same hash is already cached, nothing\n * happens.\n */\n add(query: Query<any, any, any, any>): void {\n if (!this.#queries.has(query.queryHash)) {\n this.#queries.set(query.queryHash, query)\n\n this.notify({\n type: 'added',\n query,\n })\n }\n }\n\n /**\n * Destroys the given `Query` and removes it from the cache, notifying subscribers with a\n * `'removed'` event. A no-op if the query is no longer the one currently stored under its hash\n * (e.g. it was already replaced). Used by plugins (e.g. the broadcast client) that mirror\n * removals across `QueryCache` instances.\n * @param query - The query to remove.\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n * const query = queryCache.find({ queryKey: ['posts'] })\n *\n * if (query) {\n * queryCache.remove(query)\n * }\n * ```\n */\n remove(query: Query<any, any, any, any>): void {\n const queryInMap = this.#queries.get(query.queryHash)\n\n if (queryInMap === query) {\n query.destroy()\n this.#queries.delete(query.queryHash)\n this.notify({ type: 'removed', query })\n }\n }\n\n /**\n * Removes all queries from the cache.\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * queryCache.clear()\n * ```\n */\n clear(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((query) => {\n this.remove(query)\n })\n })\n }\n\n /**\n * Returns the `Query` instance stored under the given `queryHash`, or `undefined` if none\n * exists. Unlike {@link QueryCache#find}, this looks up by the already-computed hash rather\n * than by `QueryFilters`. Used by plugins (e.g. broadcast/hydration) that already have a hash\n * to look up directly.\n * @param queryHash - The hash of the query to look up.\n * @returns The query stored under the hash, or `undefined`.\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n * const queryHash = hashKey(['posts'])\n *\n * const query = queryCache.get(queryHash)\n * ```\n */\n get<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n >(\n queryHash: string,\n ): Query<TQueryFnData, TError, TData, TQueryKey> | undefined {\n return this.#queries.get(queryHash) as\n | Query<TQueryFnData, TError, TData, TQueryKey>\n | undefined\n }\n\n /**\n * Returns all queries within the cache.\n * @returns Every query in the cache.\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * const queries = queryCache.getAll()\n * ```\n */\n getAll(): Array<Query> {\n return [...this.#queries.values()]\n }\n\n /**\n * A slightly more advanced method that can be used to get an existing query instance from the\n * cache. This instance not only contains all the state for the query, but all of the instances,\n * and underlying guts of the query as well. If the query does not exist, `undefined` is\n * returned.\n *\n * This is not typically needed for most applications, but can come in handy when needing more\n * information about a query in rare scenarios (e.g. looking at `query.state.dataUpdatedAt` to\n * decide whether a query is fresh enough to be used as an initial value).\n * @param filters - The filters to match, including the required `queryKey`. `exact` defaults to\n * `true`.\n * @returns The first matching query, or `undefined`.\n * @see {@link QueryCache#findAll}\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * const query = queryCache.find({ queryKey: ['posts'] })\n * ```\n */\n find<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData>(\n filters: WithRequired<QueryFilters, 'queryKey'>,\n ): Query<TQueryFnData, TError, TData> | undefined {\n const defaultedFilters = { exact: true, ...filters }\n\n return this.getAll().find((query) =>\n matchQuery(defaultedFilters, query),\n ) as Query<TQueryFnData, TError, TData> | undefined\n }\n\n /**\n * An even more advanced method that can be used to get existing query instances from the cache\n * that partially match a query key. If no queries match, an empty array is returned.\n *\n * This is not typically needed for most applications, but can come in handy when needing more\n * information about queries in rare scenarios.\n * @param filters - The filters to match. Without filters, every query is returned.\n * @returns The matching queries.\n * @see {@link QueryCache#find}\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * const queries = queryCache.findAll({ queryKey: ['posts'] })\n * ```\n */\n findAll(filters: QueryFilters<any> = {}): Array<Query> {\n const queries = this.getAll()\n return Object.keys(filters).length > 0\n ? queries.filter((query) => matchQuery(filters, query))\n : queries\n }\n\n /**\n * Notifies every cache listener of an event, in one batch.\n * @internal\n * @param event - The event passed to every listener.\n */\n notify(event: QueryCacheNotifyEvent): void {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(event)\n })\n })\n }\n\n /**\n * Calls {@link Query#onFocus} on every cached query, in one batch.\n * @internal\n */\n onFocus(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((query) => {\n query.onFocus()\n })\n })\n }\n\n /**\n * Calls {@link Query#onOnline} on every cached query, in one batch.\n * @internal\n */\n onOnline(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((query) => {\n query.onOnline()\n })\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA2IA,IAAa,aAAb,cAAgC,aAAiC;CAG/D,YAAY,SAAkC,CAAC,GAAG;EAChD,MAAM;EADW,KAAA,SAAA;;EAEjB,uBAAA,UAAA,sBAAgB,IAAI,IAAmB,CAAA;CACzC;;;;;;;;;;;;;;;;;;;;;;CAuBA,MAME,QACA,SAIA,OAC+C;EAC/C,MAAM,WAAW,QAAQ;EACzB,MAAM,YACJ,QAAQ,aAAa,sBAAsB,UAAU,OAAO;EAC9D,IAAI,QAAQ,KAAK,IAA4C,SAAS;EAEtE,IAAI,CAAC,OAAO;GACV,QAAQ,IAAI,MAAM;IAChB;IACA;IACA;IACA,SAAS,OAAO,oBAAoB,OAAO;IAC3C;IACA,gBAAgB,OAAO,iBAAiB,QAAQ;GAClD,CAAC;GACD,KAAK,IAAI,KAAK;EAChB;EAEA,OAAO;CACT;;;;;;;CAQA,IAAI,OAAwC;EAC1C,IAAI,CAAA,uBAAA,UAAC,IAAA,CAAA,CAAc,IAAI,MAAM,SAAS,GAAG;GACvC,uBAAA,UAAA,IAAA,CAAA,CAAc,IAAI,MAAM,WAAW,KAAK;GAExC,KAAK,OAAO;IACV,MAAM;IACN;GACF,CAAC;EACH;CACF;;;;;;;;;;;;;;;;;CAkBA,OAAO,OAAwC;EAG7C,IAAA,uBAAA,UAFmB,IAAA,CAAA,CAAc,IAAI,MAAM,SAE9B,MAAM,OAAO;GACxB,MAAM,QAAQ;GACd,uBAAA,UAAA,IAAA,CAAA,CAAc,OAAO,MAAM,SAAS;GACpC,KAAK,OAAO;IAAE,MAAM;IAAW;GAAM,CAAC;EACxC;CACF;;;;;;;;;;CAWA,QAAc;EACZ,cAAc,YAAY;GACxB,KAAK,OAAO,CAAC,CAAC,SAAS,UAAU;IAC/B,KAAK,OAAO,KAAK;GACnB,CAAC;EACH,CAAC;CACH;;;;;;;;;;;;;;;;CAiBA,IAME,WAC2D;EAC3D,OAAA,uBAAA,UAAO,IAAA,CAAA,CAAc,IAAI,SAAS;CAGpC;;;;;;;;;;;CAYA,SAAuB;EACrB,OAAO,CAAC,GAAA,uBAAA,UAAG,IAAA,CAAA,CAAc,OAAO,CAAC;CACnC;;;;;;;;;;;;;;;;;;;;;CAsBA,KACE,SACgD;EAChD,MAAM,mBAAmB;GAAE,OAAO;GAAM,GAAG;EAAQ;EAEnD,OAAO,KAAK,OAAO,CAAC,CAAC,MAAM,UACzB,WAAW,kBAAkB,KAAK,CACpC;CACF;;;;;;;;;;;;;;;;;CAkBA,QAAQ,UAA6B,CAAC,GAAiB;EACrD,MAAM,UAAU,KAAK,OAAO;EAC5B,OAAO,OAAO,KAAK,OAAO,CAAC,CAAC,SAAS,IACjC,QAAQ,QAAQ,UAAU,WAAW,SAAS,KAAK,CAAC,IACpD;CACN;;;;;;CAOA,OAAO,OAAoC;EACzC,cAAc,YAAY;GACxB,KAAK,UAAU,SAAS,aAAa;IACnC,SAAS,KAAK;GAChB,CAAC;EACH,CAAC;CACH;;;;;CAMA,UAAgB;EACd,cAAc,YAAY;GACxB,KAAK,OAAO,CAAC,CAAC,SAAS,UAAU;IAC/B,MAAM,QAAQ;GAChB,CAAC;EACH,CAAC;CACH;;;;;CAMA,WAAiB;EACf,cAAc,YAAY;GACxB,KAAK,OAAO,CAAC,CAAC,SAAS,UAAU;IAC/B,MAAM,SAAS;GACjB,CAAC;EACH,CAAC;CACH;AACF"}
|
|
@@ -19,7 +19,6 @@ var _unsubscribeOnline = /* @__PURE__ */ new WeakMap();
|
|
|
19
19
|
* `QueryClient` is used to interact with a cache of queries and mutations. It owns a
|
|
20
20
|
* `QueryCache` and a `MutationCache` (creating default ones if none are passed in) and holds
|
|
21
21
|
* the default options that are applied to queries and mutations created through it.
|
|
22
|
-
*
|
|
23
22
|
* @example
|
|
24
23
|
* ```ts
|
|
25
24
|
* const queryClient = new QueryClient({
|
|
@@ -91,7 +90,9 @@ var QueryClient = class {
|
|
|
91
90
|
* Returns the number of queries in the cache that are currently fetching, optionally
|
|
92
91
|
* matching a set of filters. This includes background-fetching, loading new pages, and
|
|
93
92
|
* loading more infinite query results.
|
|
94
|
-
*
|
|
93
|
+
* @param filters - Narrows down which fetching queries are counted. Without filters, every
|
|
94
|
+
* fetching query is counted.
|
|
95
|
+
* @returns The number of matching queries whose `fetchStatus` is `'fetching'`.
|
|
95
96
|
* @example
|
|
96
97
|
* ```ts
|
|
97
98
|
* if (queryClient.isFetching()) {
|
|
@@ -108,7 +109,9 @@ var QueryClient = class {
|
|
|
108
109
|
/**
|
|
109
110
|
* Returns the number of mutations in the cache that are currently pending, optionally
|
|
110
111
|
* matching a set of filters.
|
|
111
|
-
*
|
|
112
|
+
* @param filters - Narrows down which pending mutations are counted. Without filters, every
|
|
113
|
+
* pending mutation is counted.
|
|
114
|
+
* @returns The number of matching mutations whose `status` is `'pending'`.
|
|
112
115
|
* @example
|
|
113
116
|
* ```ts
|
|
114
117
|
* if (queryClient.isMutating()) {
|
|
@@ -128,7 +131,7 @@ var QueryClient = class {
|
|
|
128
131
|
*
|
|
129
132
|
* Hint: Do not use this function inside a component, because it won't receive updates.
|
|
130
133
|
* Use `useQuery` to create a `QueryObserver` that subscribes to changes.
|
|
131
|
-
*
|
|
134
|
+
* @param queryKey - The query key of the query to read.
|
|
132
135
|
* @returns The cached data for the query, or `undefined` if no query with this key has been observed yet.
|
|
133
136
|
* @see {@link QueryClient#getQueriesData}
|
|
134
137
|
*/
|
|
@@ -138,6 +141,12 @@ var QueryClient = class {
|
|
|
138
141
|
return (_classPrivateFieldGet4 = require_classPrivateFieldSet2._classPrivateFieldGet2(_queryCache, this).get(options.queryHash)) === null || _classPrivateFieldGet4 === void 0 ? void 0 : _classPrivateFieldGet4.state.data;
|
|
139
142
|
}
|
|
140
143
|
/**
|
|
144
|
+
* Returns the cached data of a query, or fetches it if there is none. With `revalidateIfStale`,
|
|
145
|
+
* stale cached data is also refetched in the background.
|
|
146
|
+
* @param options - The query options. If the query has no cached data yet, it is fetched with
|
|
147
|
+
* these options.
|
|
148
|
+
* @returns A promise that resolves to the cached data, or to the fetched data if nothing was
|
|
149
|
+
* cached yet.
|
|
141
150
|
* @deprecated Use queryClient.query({ ...options, staleTime: 'static' }) instead. This method will be removed in the next major version.
|
|
142
151
|
*/
|
|
143
152
|
ensureQueryData(options) {
|
|
@@ -158,7 +167,7 @@ var QueryClient = class {
|
|
|
158
167
|
* than being inferred. Passing a more specific type is a convenience for call sites that know
|
|
159
168
|
* every matched query holds the same shape — it is not checked against the actual cache
|
|
160
169
|
* contents.
|
|
161
|
-
*
|
|
170
|
+
* @param filters - The filters that select which queries to read.
|
|
162
171
|
* @returns An array of query key and data pairs. The data is `undefined` for a query with no cached data.
|
|
163
172
|
* @see {@link QueryClient#getQueryData}
|
|
164
173
|
* @example
|
|
@@ -179,14 +188,12 @@ var QueryClient = class {
|
|
|
179
188
|
*
|
|
180
189
|
* Updates must be performed immutably: do not mutate `oldData`, or data previously retrieved
|
|
181
190
|
* via {@link QueryClient#getQueryData}, in place.
|
|
182
|
-
*
|
|
183
191
|
* @param queryKey - The query key to set data for.
|
|
184
192
|
* @param updater - Either the new data, or a function that receives the current data (which
|
|
185
193
|
* may be `undefined`) and returns the new data.
|
|
186
194
|
* @param options - Set `updatedAt` to override the timestamp the written data is recorded with.
|
|
187
195
|
* @returns The data that was written, or `undefined` if the updater returned `undefined` — in that case
|
|
188
196
|
* the write is skipped and the cache is left unchanged.
|
|
189
|
-
*
|
|
190
197
|
* @example
|
|
191
198
|
* ```ts
|
|
192
199
|
* queryClient.setQueryData(['posts'], newPosts)
|
|
@@ -211,7 +218,10 @@ var QueryClient = class {
|
|
|
211
218
|
* filters or partial query key matching. Only queries that already exist and match the given
|
|
212
219
|
* filters are updated; no new cache entries are created. Internally this calls
|
|
213
220
|
* {@link QueryClient#setQueryData} for each matching query.
|
|
214
|
-
*
|
|
221
|
+
* @param filters - The filters that select which existing queries to update.
|
|
222
|
+
* @param updater - Either the new data, or a function that receives each matched query's current
|
|
223
|
+
* data (which may be `undefined`) and returns the new data.
|
|
224
|
+
* @param options - Set `updatedAt` to override the timestamp the written data is recorded with.
|
|
215
225
|
* @returns One `[queryKey, data]` tuple per matched query, in the same shape and with the same
|
|
216
226
|
* `undefined` case as {@link QueryClient#setQueryData}.
|
|
217
227
|
* @example
|
|
@@ -227,7 +237,8 @@ var QueryClient = class {
|
|
|
227
237
|
/**
|
|
228
238
|
* Imperative (non-reactive) way to retrieve an existing query's state. If the query does not
|
|
229
239
|
* exist, `undefined` is returned.
|
|
230
|
-
*
|
|
240
|
+
* @param queryKey - The query key of the query to read.
|
|
241
|
+
* @returns The query's state, or `undefined` if no query with this key exists.
|
|
231
242
|
* @example
|
|
232
243
|
* ```ts
|
|
233
244
|
* const state = queryClient.getQueryState(['posts'])
|
|
@@ -244,7 +255,8 @@ var QueryClient = class {
|
|
|
244
255
|
* {@link QueryClient#invalidateQueries} or {@link QueryClient#refetchQueries}, this removes
|
|
245
256
|
* matching queries from the cache instead of refetching them. Without filters, every query in
|
|
246
257
|
* the cache is removed.
|
|
247
|
-
*
|
|
258
|
+
* @param filters - The filters that select which queries to remove. Without filters, every query
|
|
259
|
+
* is removed.
|
|
248
260
|
* @example
|
|
249
261
|
* ```ts
|
|
250
262
|
* queryClient.removeQueries({ queryKey: ['posts'], exact: true })
|
|
@@ -262,7 +274,11 @@ var QueryClient = class {
|
|
|
262
274
|
* Resets queries matching the given filters back to their initial state (e.g. any
|
|
263
275
|
* `initialData`), notifying subscribers rather than removing them. Active queries among the
|
|
264
276
|
* matched set are then refetched, and the returned promise resolves once that refetch settles.
|
|
265
|
-
*
|
|
277
|
+
* @param filters - The filters that select which queries to reset. Without filters, every query
|
|
278
|
+
* is reset.
|
|
279
|
+
* @param options - Passed to the refetch of the active matched queries, e.g. `cancelRefetch` and
|
|
280
|
+
* `throwOnError`.
|
|
281
|
+
* @returns A promise that resolves once the refetch of the active matched queries settles.
|
|
266
282
|
* @example
|
|
267
283
|
* ```ts
|
|
268
284
|
* await queryClient.resetQueries({ queryKey: ['posts'], exact: true })
|
|
@@ -289,7 +305,11 @@ var QueryClient = class {
|
|
|
289
305
|
* reverted to its state before the outgoing fetch started.
|
|
290
306
|
*
|
|
291
307
|
* The returned promise never rejects, even if individual cancellations fail.
|
|
292
|
-
*
|
|
308
|
+
* @param filters - The filters that select which queries to cancel. Without filters, every query
|
|
309
|
+
* is cancelled.
|
|
310
|
+
* @param cancelOptions - Passed to each matched query's cancellation. `revert` defaults to
|
|
311
|
+
* `true`.
|
|
312
|
+
* @returns A promise that resolves once every cancellation has settled.
|
|
293
313
|
* @example
|
|
294
314
|
* ```ts
|
|
295
315
|
* await queryClient.cancelQueries({ queryKey: ['posts'], exact: true })
|
|
@@ -310,7 +330,12 @@ var QueryClient = class {
|
|
|
310
330
|
* Unless `filters.refetchType` is `'none'`, matching queries are then refetched via
|
|
311
331
|
* {@link QueryClient#refetchQueries}, using `filters.refetchType` if set, otherwise
|
|
312
332
|
* `filters.type`, otherwise `'active'`.
|
|
313
|
-
*
|
|
333
|
+
* @param filters - The filters that select which queries to invalidate, plus `refetchType` to
|
|
334
|
+
* control which of them to refetch afterwards. Without filters, every query is invalidated.
|
|
335
|
+
* @param options - Passed to {@link QueryClient#refetchQueries}, e.g. `cancelRefetch` and
|
|
336
|
+
* `throwOnError`.
|
|
337
|
+
* @returns A promise that resolves once the refetch settles, or immediately if `refetchType` is
|
|
338
|
+
* `'none'`.
|
|
314
339
|
* @example
|
|
315
340
|
* ```ts
|
|
316
341
|
* await queryClient.invalidateQueries({ queryKey: ['posts'], refetchType: 'active' })
|
|
@@ -336,7 +361,11 @@ var QueryClient = class {
|
|
|
336
361
|
* By default (`cancelRefetch: true`), a currently running fetch is cancelled before the new
|
|
337
362
|
* one starts. The returned promise resolves once all matching queries have settled; it does
|
|
338
363
|
* not reject on individual query failures unless `throwOnError` is set.
|
|
339
|
-
*
|
|
364
|
+
* @param filters - The filters that select which queries to refetch. Without filters, every query
|
|
365
|
+
* in the cache is included.
|
|
366
|
+
* @param options - Set `cancelRefetch` to `false` to keep a running fetch instead of cancelling
|
|
367
|
+
* it, and `throwOnError` to `true` to reject when a refetch fails.
|
|
368
|
+
* @returns A promise that resolves once every matched query has settled.
|
|
340
369
|
* @example
|
|
341
370
|
* ```ts
|
|
342
371
|
* // refetch all active queries partially matching a query key:
|
|
@@ -376,7 +405,10 @@ var QueryClient = class {
|
|
|
376
405
|
*
|
|
377
406
|
* This method replaces the deprecated `fetchQuery`, and — combined with
|
|
378
407
|
* `{ staleTime: 'static' }` — the deprecated `ensureQueryData`.
|
|
379
|
-
*
|
|
408
|
+
* @param options - The query options, including the `queryKey` and the `queryFn` used if the
|
|
409
|
+
* query needs to fetch.
|
|
410
|
+
* @returns A promise that resolves to the data, or to the result of `select` if provided. It
|
|
411
|
+
* rejects with the error from the fetch or from `select`.
|
|
380
412
|
* @example
|
|
381
413
|
* ```ts
|
|
382
414
|
* try {
|
|
@@ -396,6 +428,12 @@ var QueryClient = class {
|
|
|
396
428
|
return queryData;
|
|
397
429
|
}
|
|
398
430
|
/**
|
|
431
|
+
* Fetches and caches a query, or returns its cached data if it isn't stale. Unlike in observers,
|
|
432
|
+
* the fetch isn't retried unless `retry` is set.
|
|
433
|
+
* @param options - The query options, including the `queryKey` and the `queryFn` used if the
|
|
434
|
+
* query needs to fetch.
|
|
435
|
+
* @returns A promise that resolves to the cached or fetched data, or rejects with the fetch
|
|
436
|
+
* error.
|
|
399
437
|
* @deprecated Use queryClient.query(options) instead. This method will be removed in the next major version.
|
|
400
438
|
*/
|
|
401
439
|
fetchQuery(options) {
|
|
@@ -405,6 +443,11 @@ var QueryClient = class {
|
|
|
405
443
|
return query.isStaleByTime(require_utils.resolveQueryValue(defaultedOptions.staleTime, query)) ? query.fetch(defaultedOptions) : Promise.resolve(query.state.data);
|
|
406
444
|
}
|
|
407
445
|
/**
|
|
446
|
+
* Fetches and caches a query like {@link QueryClient#fetchQuery}, but ignores the result and any
|
|
447
|
+
* error.
|
|
448
|
+
* @param options - The query options, including the `queryKey` and the `queryFn` used if the
|
|
449
|
+
* query needs to fetch.
|
|
450
|
+
* @returns A promise that resolves once the fetch settles. It never rejects.
|
|
408
451
|
* @deprecated Use queryClient.query(options) instead. You can swallow errors with `.catch(noop)`. This method will be removed in the next major version.
|
|
409
452
|
*/
|
|
410
453
|
prefetchQuery(options) {
|
|
@@ -420,7 +463,10 @@ var QueryClient = class {
|
|
|
420
463
|
*
|
|
421
464
|
* This method replaces the deprecated `fetchInfiniteQuery`, and — combined with
|
|
422
465
|
* `{ staleTime: 'static' }` — the deprecated `ensureInfiniteQueryData`.
|
|
423
|
-
*
|
|
466
|
+
* @param options - The infinite query options, including the `queryKey`, the `queryFn`, and the
|
|
467
|
+
* `initialPageParam`.
|
|
468
|
+
* @returns A promise that resolves to the {@link InfiniteData}, or to the result of `select` if
|
|
469
|
+
* provided. It rejects with the error from the fetch or from `select`.
|
|
424
470
|
* @example
|
|
425
471
|
* ```ts
|
|
426
472
|
* try {
|
|
@@ -436,6 +482,11 @@ var QueryClient = class {
|
|
|
436
482
|
return this.query(options);
|
|
437
483
|
}
|
|
438
484
|
/**
|
|
485
|
+
* Fetches and caches an infinite query, or returns its cached data if it isn't stale.
|
|
486
|
+
* @param options - The infinite query options, including the `queryKey`, the `queryFn`, and the
|
|
487
|
+
* `initialPageParam`.
|
|
488
|
+
* @returns A promise that resolves to the cached or fetched {@link InfiniteData}, or rejects with
|
|
489
|
+
* the fetch error.
|
|
439
490
|
* @deprecated Use queryClient.infiniteQuery(options) instead. This method will be removed in the next major version.
|
|
440
491
|
*/
|
|
441
492
|
fetchInfiniteQuery(options) {
|
|
@@ -443,12 +494,23 @@ var QueryClient = class {
|
|
|
443
494
|
return this.fetchQuery(options);
|
|
444
495
|
}
|
|
445
496
|
/**
|
|
497
|
+
* Fetches and caches an infinite query like {@link QueryClient#fetchInfiniteQuery}, but ignores
|
|
498
|
+
* the result and any error.
|
|
499
|
+
* @param options - The infinite query options, including the `queryKey`, the `queryFn`, and the
|
|
500
|
+
* `initialPageParam`.
|
|
501
|
+
* @returns A promise that resolves once the fetch settles. It never rejects.
|
|
446
502
|
* @deprecated Use queryClient.infiniteQuery(options) instead. You can swallow errors with `.catch(noop)`. This method will be removed in the next major version.
|
|
447
503
|
*/
|
|
448
504
|
prefetchInfiniteQuery(options) {
|
|
449
505
|
return this.fetchInfiniteQuery(options).then(require_utils.noop).catch(require_utils.noop);
|
|
450
506
|
}
|
|
451
507
|
/**
|
|
508
|
+
* Returns the cached data of an infinite query, or fetches it if there is none. With
|
|
509
|
+
* `revalidateIfStale`, stale cached data is also refetched in the background.
|
|
510
|
+
* @param options - The infinite query options. If the query has no cached data yet, it is fetched
|
|
511
|
+
* with these options.
|
|
512
|
+
* @returns A promise that resolves to the cached {@link InfiniteData}, or to the fetched data if
|
|
513
|
+
* nothing was cached yet.
|
|
452
514
|
* @deprecated Use queryClient.infiniteQuery({ ...options, staleTime: 'static' }) instead. This method will be removed in the next major version.
|
|
453
515
|
*/
|
|
454
516
|
ensureInfiniteQueryData(options) {
|
|
@@ -458,7 +520,7 @@ var QueryClient = class {
|
|
|
458
520
|
/**
|
|
459
521
|
* Resumes mutations that were paused because there was no network connection. Does nothing
|
|
460
522
|
* (resolving immediately) if the client is currently offline.
|
|
461
|
-
*
|
|
523
|
+
* @returns A promise that resolves once the resumed mutations have settled.
|
|
462
524
|
* @example
|
|
463
525
|
* ```ts
|
|
464
526
|
* import { QueryClient } from '@tanstack/query-core'
|
|
@@ -473,7 +535,7 @@ var QueryClient = class {
|
|
|
473
535
|
}
|
|
474
536
|
/**
|
|
475
537
|
* Returns the query cache this client is connected to.
|
|
476
|
-
*
|
|
538
|
+
* @returns The {@link QueryCache} instance.
|
|
477
539
|
* @example
|
|
478
540
|
* ```ts
|
|
479
541
|
* import { QueryClient } from '@tanstack/query-core'
|
|
@@ -488,7 +550,7 @@ var QueryClient = class {
|
|
|
488
550
|
}
|
|
489
551
|
/**
|
|
490
552
|
* Returns the mutation cache this client is connected to.
|
|
491
|
-
*
|
|
553
|
+
* @returns The {@link MutationCache} instance.
|
|
492
554
|
* @example
|
|
493
555
|
* ```ts
|
|
494
556
|
* import { QueryClient } from '@tanstack/query-core'
|
|
@@ -504,7 +566,7 @@ var QueryClient = class {
|
|
|
504
566
|
/**
|
|
505
567
|
* Returns the default options that were set when creating the client, or via
|
|
506
568
|
* {@link QueryClient#setDefaultOptions}.
|
|
507
|
-
*
|
|
569
|
+
* @returns The client's current default options.
|
|
508
570
|
* @example
|
|
509
571
|
* ```ts
|
|
510
572
|
* import { QueryClient } from '@tanstack/query-core'
|
|
@@ -519,7 +581,7 @@ var QueryClient = class {
|
|
|
519
581
|
/**
|
|
520
582
|
* Dynamically sets the default options for this client, overwriting any previously defined
|
|
521
583
|
* default options.
|
|
522
|
-
*
|
|
584
|
+
* @param options - The new default options for queries and mutations.
|
|
523
585
|
* @see {@link QueryClient#getDefaultOptions}
|
|
524
586
|
* @example
|
|
525
587
|
* ```ts
|
|
@@ -543,7 +605,8 @@ var QueryClient = class {
|
|
|
543
605
|
* registration order by {@link QueryClient#getQueryDefaults}, so register defaults from the
|
|
544
606
|
* most generic key to the least generic one — more specific defaults should be registered
|
|
545
607
|
* after more generic ones so they take precedence.
|
|
546
|
-
*
|
|
608
|
+
* @param queryKey - The query key that query keys are partially matched against.
|
|
609
|
+
* @param options - The default options applied to matching queries.
|
|
547
610
|
* @example
|
|
548
611
|
* ```ts
|
|
549
612
|
* queryClient.setQueryDefaults(['posts'], { queryFn: fetchPosts })
|
|
@@ -561,7 +624,9 @@ var QueryClient = class {
|
|
|
561
624
|
* Returns the default options registered for queries whose query key partially matches the
|
|
562
625
|
* given `queryKey`, via {@link QueryClient#setQueryDefaults}. If multiple registered defaults
|
|
563
626
|
* match, they are merged together in registration order.
|
|
564
|
-
*
|
|
627
|
+
* @param queryKey - The query key to look up registered defaults for.
|
|
628
|
+
* @returns The merged default options of every registration that matches, or an empty object if
|
|
629
|
+
* none match.
|
|
565
630
|
* @example
|
|
566
631
|
* ```ts
|
|
567
632
|
* const defaultOptions = queryClient.getQueryDefaults(['posts'])
|
|
@@ -579,7 +644,8 @@ var QueryClient = class {
|
|
|
579
644
|
* Sets default options for mutations whose mutation key partially matches the given
|
|
580
645
|
* `mutationKey`. As with {@link QueryClient#setQueryDefaults}, the order of registration
|
|
581
646
|
* matters when several registered defaults match the same mutation key.
|
|
582
|
-
*
|
|
647
|
+
* @param mutationKey - The mutation key that mutation keys are partially matched against.
|
|
648
|
+
* @param options - The default options applied to matching mutations.
|
|
583
649
|
* @see {@link QueryClient#getMutationDefaults}
|
|
584
650
|
* @example
|
|
585
651
|
* ```ts
|
|
@@ -596,7 +662,9 @@ var QueryClient = class {
|
|
|
596
662
|
* Returns the default options registered for mutations whose mutation key partially matches
|
|
597
663
|
* the given `mutationKey`, via {@link QueryClient#setMutationDefaults}. If multiple registered
|
|
598
664
|
* defaults match, they are merged together in registration order.
|
|
599
|
-
*
|
|
665
|
+
* @param mutationKey - The mutation key to look up registered defaults for.
|
|
666
|
+
* @returns The merged default options of every registration that matches, or an empty object if
|
|
667
|
+
* none match.
|
|
600
668
|
* @example
|
|
601
669
|
* ```ts
|
|
602
670
|
* const defaultOptions = queryClient.getMutationDefaults(['addPost'])
|
|
@@ -612,9 +680,12 @@ var QueryClient = class {
|
|
|
612
680
|
}
|
|
613
681
|
/**
|
|
614
682
|
* Called by framework adapters (e.g. inside `useQuery`) to resolve the options passed by the
|
|
615
|
-
* caller into their final, defaulted form: merging `
|
|
616
|
-
*
|
|
617
|
-
*
|
|
683
|
+
* caller into their final, defaulted form: merging the client's own `defaultOptions.queries`,
|
|
684
|
+
* then `queryClient.setQueryDefaults` for the given `queryKey`, then the caller's options on
|
|
685
|
+
* top. A no-op if the options are already defaulted (`_defaulted: true`).
|
|
686
|
+
* @param options - The query options passed by the caller.
|
|
687
|
+
* @returns The defaulted options, with `queryHash` and dependent defaults (e.g.
|
|
688
|
+
* `refetchOnReconnect`) filled in.
|
|
618
689
|
*/
|
|
619
690
|
defaultQueryOptions(options) {
|
|
620
691
|
if (options._defaulted) return options;
|
|
@@ -633,9 +704,11 @@ var QueryClient = class {
|
|
|
633
704
|
}
|
|
634
705
|
/**
|
|
635
706
|
* The mutation counterpart of {@link QueryClient#defaultQueryOptions}. Called by framework
|
|
636
|
-
* adapters (e.g. inside `useMutation`) to merge `
|
|
637
|
-
*
|
|
638
|
-
*
|
|
707
|
+
* adapters (e.g. inside `useMutation`) to merge the client's `defaultOptions.mutations`, then
|
|
708
|
+
* `queryClient.setMutationDefaults` for the given `mutationKey`, then the caller's options on
|
|
709
|
+
* top. A no-op if the options are already defaulted (`_defaulted: true`).
|
|
710
|
+
* @param options - The mutation options passed by the caller.
|
|
711
|
+
* @returns The defaulted options.
|
|
639
712
|
*/
|
|
640
713
|
defaultMutationOptions(options) {
|
|
641
714
|
if (options === null || options === void 0 ? void 0 : options._defaulted) return options;
|
|
@@ -648,7 +721,6 @@ var QueryClient = class {
|
|
|
648
721
|
}
|
|
649
722
|
/**
|
|
650
723
|
* Clears both the query cache and the mutation cache this client is connected to.
|
|
651
|
-
*
|
|
652
724
|
* @example
|
|
653
725
|
* ```ts
|
|
654
726
|
* import { QueryClient } from '@tanstack/query-core'
|