@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":"queriesObserver.cjs","names":["Subscribable","shallowEqualObjects","replaceEqualDeep","QueryObserver"],"sources":["../../src/queriesObserver.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport { QueryObserver } from './queryObserver'\nimport { Subscribable } from './subscribable'\nimport { replaceEqualDeep, shallowEqualObjects } from './utils'\nimport type {\n DefaultedQueryObserverOptions,\n QueryObserverOptions,\n QueryObserverResult,\n} from './types'\nimport type { QueryClient } from './queryClient'\n\nfunction difference<T>(array1: Array<T>, array2: Array<T>): Array<T> {\n const excludeSet = new Set(array2)\n return array1.filter((x) => !excludeSet.has(x))\n}\n\ntype QueriesObserverListener = (result: Array<QueryObserverResult>) => void\n\ntype CombineFn<TCombinedResult> = (\n result: Array<QueryObserverResult>,\n) => TCombinedResult\n\nexport interface QueriesObserverOptions<\n TCombinedResult = Array<QueryObserverResult>,\n> {\n /**\n * A function that combines the array of `QueryObserverResult`s (one per\n * observed query) into a single value. The combined value is memoized and\n * only recomputed when one of the underlying results, the query hashes, or\n * the `combine` function itself changes.\n *\n * Defaults to returning the array of `QueryObserverResult`s unchanged.\n */\n combine?: CombineFn<TCombinedResult>\n}\n\n/**\n * A `QueriesObserver` watches an array of queries at once, exposing them as\n * a single array of `QueryObserverResult`s (or, when a `combine` option is\n * given, as a combined value derived from that array). It manages one\n * internal `QueryObserver` per query, and is the primitive that framework\n * adapters (e.g. `useQueries`) build their hooks on top of.\n *\n * @example\n * ```ts\n * const observer = new QueriesObserver(queryClient, [\n * { queryKey: ['post', 1], queryFn: fetchPost },\n * { queryKey: ['post', 2], queryFn: fetchPost },\n * ])\n *\n * const unsubscribe = observer.subscribe((result) => {\n * console.log(result)\n * })\n * ```\n */\nexport class QueriesObserver<\n TCombinedResult = Array<QueryObserverResult>,\n> extends Subscribable<QueriesObserverListener> {\n #client: QueryClient\n #result!: Array<QueryObserverResult>\n #queries: Array<QueryObserverOptions>\n #options?: QueriesObserverOptions<TCombinedResult>\n #observers: Array<QueryObserver>\n #combinedResult!: TCombinedResult\n #lastCombine?: CombineFn<TCombinedResult>\n #lastResult?: Array<QueryObserverResult>\n #lastQueryHashes?: Array<string>\n #observerMatches: Array<QueryObserverMatch> = []\n\n constructor(\n client: QueryClient,\n queries: Array<QueryObserverOptions<any, any, any, any, any>>,\n options?: QueriesObserverOptions<TCombinedResult>,\n ) {\n super()\n\n this.#client = client\n this.#options = options\n this.#queries = []\n this.#observers = []\n this.#result = []\n\n this.setQueries(queries)\n }\n\n protected onSubscribe(): void {\n if (this.listeners.size === 1) {\n this.#observers.forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n }\n }\n\n protected onUnsubscribe(): void {\n if (!this.listeners.size) {\n this.destroy()\n }\n }\n\n /**\n * Stops observing all queries: clears all listeners and destroys every\n * underlying `QueryObserver` this observer manages.\n */\n destroy(): void {\n this.listeners = new Set()\n this.#observers.forEach((observer) => {\n observer.destroy()\n })\n }\n\n /**\n * Replaces the set of queries being observed. Existing `QueryObserver`s\n * are reused for queries that match an already-observed query hash;\n * observers for queries that are no longer present are destroyed, and new\n * observers are created and subscribed to for newly added queries.\n *\n * @example\n * ```ts\n * observer.setQueries([\n * { queryKey: ['post', 1], queryFn: fetchPost },\n * { queryKey: ['post', 3], queryFn: fetchPost },\n * ])\n * ```\n */\n setQueries(\n queries: Array<QueryObserverOptions>,\n options?: QueriesObserverOptions<TCombinedResult>,\n ): void {\n this.#queries = queries\n this.#options = options\n\n if (process.env.NODE_ENV !== 'production') {\n const queryHashes = queries.map(\n (query) => this.#client.defaultQueryOptions(query).queryHash,\n )\n if (new Set(queryHashes).size !== queryHashes.length) {\n console.warn(\n '[QueriesObserver]: Duplicate Queries found. This might result in unexpected behavior.',\n )\n }\n }\n\n notifyManager.batch(() => {\n const prevObservers = this.#observers\n\n const newObserverMatches = this.#findMatchingObservers(this.#queries)\n\n // set options for the new observers to notify of changes\n newObserverMatches.forEach((match) =>\n match.observer.setOptions(match.defaultedQueryOptions),\n )\n\n const newObservers = newObserverMatches.map((match) => match.observer)\n const newResult = newObservers.map((observer) =>\n observer.getCurrentResult(),\n )\n\n const hasLengthChange = prevObservers.length !== newObservers.length\n const hasIndexChange = newObservers.some(\n (observer, index) => observer !== prevObservers[index],\n )\n const hasStructuralChange = hasLengthChange || hasIndexChange\n\n const hasResultChange = hasStructuralChange\n ? true\n : newResult.some((result, index) => {\n const prev = this.#result[index]\n return !prev || !shallowEqualObjects(result, prev)\n })\n\n if (!hasStructuralChange && !hasResultChange) return\n\n if (hasStructuralChange) {\n this.#observerMatches = newObserverMatches\n this.#observers = newObservers\n }\n\n this.#result = newResult\n\n if (!this.hasListeners()) return\n\n if (hasStructuralChange) {\n difference(prevObservers, newObservers).forEach((observer) => {\n observer.destroy()\n })\n difference(newObservers, prevObservers).forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n }\n\n this.#notify()\n })\n }\n\n /**\n * Returns the most recently computed array of `QueryObserverResult`s, one\n * per observed query, in the same order as the queries passed to the\n * constructor or `setQueries`.\n *\n * @example\n * ```ts\n * const results = observer.getCurrentResult()\n * const data = results.map((result) => result.data)\n * ```\n */\n getCurrentResult(): Array<QueryObserverResult> {\n return this.#result\n }\n\n /**\n * Returns the underlying `Query` instances currently being observed, in\n * the same order as the queries passed to the constructor or `setQueries`.\n */\n getQueries() {\n return this.#observers.map((observer) => observer.getCurrentQuery())\n }\n\n /**\n * Returns the underlying `QueryObserver` instances this observer manages,\n * in the same order as the queries passed to the constructor or\n * `setQueries`.\n */\n getObservers() {\n return this.#observers\n }\n\n /**\n * The `QueriesObserver` counterpart of {@link QueryObserver#getOptimisticResult} — computes\n * the result for the given (already-defaulted) queries right now, synchronously. Called by\n * framework adapters (e.g. `useQueries`) ahead of subscribing, returning a tuple of the raw\n * per-query results, a function to compute the combined result from them, and a function to\n * wrap the results for property-access tracking.\n */\n getOptimisticResult(\n queries: Array<QueryObserverOptions>,\n combine: CombineFn<TCombinedResult> | undefined,\n ): [\n rawResult: Array<QueryObserverResult>,\n combineResult: (r?: Array<QueryObserverResult>) => TCombinedResult,\n trackResult: () => Array<QueryObserverResult>,\n ] {\n const matches = this.#findMatchingObservers(queries)\n const result = matches.map((match) =>\n match.observer.getOptimisticResult(match.defaultedQueryOptions),\n )\n const queryHashes = matches.map(\n (match) => match.defaultedQueryOptions.queryHash,\n )\n\n return [\n result,\n (r?: Array<QueryObserverResult>) => {\n return this.#combineResult(r ?? result, combine, queryHashes)\n },\n () => {\n return this.#trackResult(result, matches)\n },\n ]\n }\n\n #trackResult(\n result: Array<QueryObserverResult>,\n matches: Array<QueryObserverMatch>,\n ) {\n const trackedProps = new Set<keyof QueryObserverResult>()\n\n return matches.map((match, index) => {\n const observerResult = result[index]!\n return !match.defaultedQueryOptions.notifyOnChangeProps\n ? match.observer.trackResult(observerResult, (accessedProp) => {\n // track property on all observers to ensure proper (synchronized) tracking (#7000)\n if (!trackedProps.has(accessedProp)) {\n trackedProps.add(accessedProp)\n matches.forEach((m) => {\n m.observer.trackProp(accessedProp)\n })\n }\n })\n : observerResult\n })\n }\n\n #combineResult(\n input: Array<QueryObserverResult>,\n combine: CombineFn<TCombinedResult> | undefined,\n queryHashes?: Array<string>,\n ): TCombinedResult {\n if (combine) {\n const lastHashes = this.#lastQueryHashes\n const queryHashesChanged =\n queryHashes !== undefined &&\n lastHashes !== undefined &&\n (lastHashes.length !== queryHashes.length ||\n queryHashes.some((hash, i) => hash !== lastHashes[i]))\n\n if (\n this.#result !== this.#lastResult ||\n queryHashesChanged ||\n combine !== this.#lastCombine\n ) {\n this.#lastCombine = combine\n this.#lastResult = this.#result\n\n if (queryHashes !== undefined) {\n this.#lastQueryHashes = queryHashes\n }\n this.#combinedResult = replaceEqualDeep(\n this.#combinedResult,\n combine(input),\n )\n }\n\n return this.#combinedResult\n }\n return input as any\n }\n\n #shouldSkipCombine(): boolean {\n return (\n !this.#options?.combine ||\n this.#observers.some((observer, index) => {\n return (\n observer.options.suspense && this.#result[index]?.data === undefined\n )\n })\n )\n }\n\n #findMatchingObservers(\n queries: Array<QueryObserverOptions>,\n ): Array<QueryObserverMatch> {\n const prevObserversMap = new Map<string, Array<QueryObserver>>()\n\n this.#observers.forEach((observer) => {\n const key = observer.options.queryHash\n if (!key) return\n\n const previousObservers = prevObserversMap.get(key)\n\n if (previousObservers) {\n previousObservers.push(observer)\n } else {\n prevObserversMap.set(key, [observer])\n }\n })\n\n const observers: Array<QueryObserverMatch> = []\n\n queries.forEach((options) => {\n const defaultedOptions = this.#client.defaultQueryOptions(options)\n const match = prevObserversMap.get(defaultedOptions.queryHash)?.shift()\n const observer =\n match ?? new QueryObserver(this.#client, defaultedOptions)\n\n observers.push({\n defaultedQueryOptions: defaultedOptions,\n observer,\n })\n })\n\n return observers\n }\n\n #onUpdate(observer: QueryObserver, result: QueryObserverResult): void {\n const index = this.#observers.indexOf(observer)\n if (index !== -1) {\n this.#result = this.#result.slice()\n this.#result[index] = result\n this.#notify()\n }\n }\n\n #notify(): void {\n if (this.hasListeners()) {\n const shouldSkipCombine = this.#shouldSkipCombine()\n const previousResult = this.#combinedResult\n const newResult = shouldSkipCombine\n ? previousResult\n : this.#combineResult(\n this.#trackResult(this.#result, this.#observerMatches),\n this.#options?.combine,\n )\n\n if (shouldSkipCombine || previousResult !== newResult) {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(this.#result)\n })\n })\n }\n }\n }\n}\n\ntype QueryObserverMatch = {\n defaultedQueryOptions: DefaultedQueryObserverOptions\n observer: QueryObserver\n}\n"],"mappings":";;;;;;AAWA,SAAS,WAAc,QAAkB,QAA4B;CACnE,MAAM,aAAa,IAAI,IAAI,MAAM;CACjC,OAAO,OAAO,QAAQ,MAAM,CAAC,WAAW,IAAI,CAAC,CAAC;AAChD;;;;;;;;;;;;;;;;;;;;AAyCA,IAAa,kBAAb,cAEUA,qBAAAA,aAAsC;CAC9C;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA,mBAA8C,CAAC;CAE/C,YACE,QACA,SACA,SACA;EACA,MAAM;EAEN,KAAK,UAAU;E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,IAAIC,sBAAAA,cAAc,KAAK,SAAS,gBAAgB;GAE3D,UAAU,KAAK;IACb,uBAAuB;IACvB;GACF,CAAC;EACH,CAAC;EAED,OAAO;CACT;CAEA,UAAU,UAAyB,QAAmC;EACpE,MAAM,QAAQ,KAAK,WAAW,QAAQ,QAAQ;EAC9C,IAAI,UAAU,IAAI;GAChB,KAAK,UAAU,KAAK,QAAQ,MAAM;GAClC,KAAK,QAAQ,SAAS;GACtB,KAAK,QAAQ;EACf;CACF;CAEA,UAAgB;EACd,IAAI,KAAK,aAAa,GAAG;GACvB,MAAM,oBAAoB,KAAK,mBAAmB;GAClD,MAAM,iBAAiB,KAAK;GAC5B,MAAM,YAAY,oBACd,iBACA,KAAK,eACH,KAAK,aAAa,KAAK,SAAS,KAAK,gBAAgB,GACrD,KAAK,UAAU,OACjB;GAEJ,IAAI,qBAAqB,mBAAmB,WAC1C,sBAAA,cAAc,YAAY;IACxB,KAAK,UAAU,SAAS,aAAa;KACnC,SAAS,KAAK,OAAO;IACvB,CAAC;GACH,CAAC;EAEL;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"queriesObserver.cjs","names":["Subscribable","shallowEqualObjects","replaceEqualDeep","QueryObserver"],"sources":["../../src/queriesObserver.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport { QueryObserver } from './queryObserver'\nimport { Subscribable } from './subscribable'\nimport { replaceEqualDeep, shallowEqualObjects } from './utils'\nimport type {\n DefaultedQueryObserverOptions,\n QueryObserverOptions,\n QueryObserverResult,\n} from './types'\nimport type { QueryClient } from './queryClient'\n\n/**\n * Returns the items of `array1` that are not in `array2`.\n * @param array1 - The items to filter.\n * @param array2 - The items to exclude.\n * @returns A new array with the items of `array1` that are not in `array2`.\n */\nfunction difference<T>(array1: Array<T>, array2: Array<T>): Array<T> {\n const excludeSet = new Set(array2)\n return array1.filter((x) => !excludeSet.has(x))\n}\n\ntype QueriesObserverListener = (result: Array<QueryObserverResult>) => void\n\ntype CombineFn<TCombinedResult> = (\n result: Array<QueryObserverResult>,\n) => TCombinedResult\n\n/**\n * Options for a `QueriesObserver` that apply to all of its queries at once.\n */\nexport interface QueriesObserverOptions<\n TCombinedResult = Array<QueryObserverResult>,\n> {\n /**\n * A function that combines the array of `QueryObserverResult`s (one per\n * observed query) into a single value. The combined value is memoized and\n * only recomputed when one of the underlying results, the query hashes, or\n * the `combine` function itself changes.\n *\n * Defaults to returning the array of `QueryObserverResult`s unchanged.\n */\n combine?: CombineFn<TCombinedResult>\n}\n\n/**\n * A `QueriesObserver` watches an array of queries at once, exposing them as\n * a single array of `QueryObserverResult`s (or, when a `combine` option is\n * given, as a combined value derived from that array). It manages one\n * internal `QueryObserver` per query, and is the primitive that framework\n * adapters (e.g. `useQueries`) build their hooks on top of.\n * @example\n * ```ts\n * const observer = new QueriesObserver(queryClient, [\n * { queryKey: ['post', 1], queryFn: fetchPost },\n * { queryKey: ['post', 2], queryFn: fetchPost },\n * ])\n *\n * const unsubscribe = observer.subscribe((result) => {\n * console.log(result)\n * })\n * ```\n */\nexport class QueriesObserver<\n TCombinedResult = Array<QueryObserverResult>,\n> extends Subscribable<QueriesObserverListener> {\n #client: QueryClient\n #result!: Array<QueryObserverResult>\n #queries: Array<QueryObserverOptions>\n #options?: QueriesObserverOptions<TCombinedResult>\n #observers: Array<QueryObserver>\n #combinedResult!: TCombinedResult\n #lastCombine?: CombineFn<TCombinedResult>\n #lastResult?: Array<QueryObserverResult>\n #lastQueryHashes?: Array<string>\n #observerMatches: Array<QueryObserverMatch> = []\n\n constructor(\n client: QueryClient,\n queries: Array<QueryObserverOptions<any, any, any, any, any>>,\n options?: QueriesObserverOptions<TCombinedResult>,\n ) {\n super()\n\n this.#client = client\n this.#options = options\n this.#queries = []\n this.#observers = []\n this.#result = []\n\n this.setQueries(queries)\n }\n\n protected override onSubscribe(): void {\n if (this.listeners.size === 1) {\n this.#observers.forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n }\n }\n\n protected override onUnsubscribe(): void {\n if (!this.listeners.size) {\n this.destroy()\n }\n }\n\n /**\n * Stops observing all queries: clears all listeners and destroys every\n * underlying `QueryObserver` this observer manages.\n */\n destroy(): void {\n this.listeners = new Set()\n this.#observers.forEach((observer) => {\n observer.destroy()\n })\n }\n\n /**\n * Replaces the set of queries being observed. Existing `QueryObserver`s\n * are reused for queries that match an already-observed query hash;\n * observers for queries that are no longer present are destroyed, and new\n * observers are created and subscribed to for newly added queries.\n * @param queries - The options of the queries to observe.\n * @param options - Replaces the observer's options, e.g. its `combine` function.\n * @example\n * ```ts\n * observer.setQueries([\n * { queryKey: ['post', 1], queryFn: fetchPost },\n * { queryKey: ['post', 3], queryFn: fetchPost },\n * ])\n * ```\n */\n setQueries(\n queries: Array<QueryObserverOptions>,\n options?: QueriesObserverOptions<TCombinedResult>,\n ): void {\n this.#queries = queries\n this.#options = options\n\n if (process.env.NODE_ENV !== 'production') {\n const queryHashes = queries.map(\n (query) => this.#client.defaultQueryOptions(query).queryHash,\n )\n if (new Set(queryHashes).size !== queryHashes.length) {\n console.warn(\n '[QueriesObserver]: Duplicate Queries found. This might result in unexpected behavior.',\n )\n }\n }\n\n notifyManager.batch(() => {\n const prevObservers = this.#observers\n\n const newObserverMatches = this.#findMatchingObservers(this.#queries)\n\n // set options for the new observers to notify of changes\n newObserverMatches.forEach((match) =>\n match.observer.setOptions(match.defaultedQueryOptions),\n )\n\n const newObservers = newObserverMatches.map((match) => match.observer)\n const newResult = newObservers.map((observer) =>\n observer.getCurrentResult(),\n )\n\n const hasLengthChange = prevObservers.length !== newObservers.length\n const hasIndexChange = newObservers.some(\n (observer, index) => observer !== prevObservers[index],\n )\n const hasStructuralChange = hasLengthChange || hasIndexChange\n\n const hasResultChange = hasStructuralChange\n ? true\n : newResult.some((result, index) => {\n const prev = this.#result[index]\n return !prev || !shallowEqualObjects(result, prev)\n })\n\n if (!hasStructuralChange && !hasResultChange) return\n\n if (hasStructuralChange) {\n this.#observerMatches = newObserverMatches\n this.#observers = newObservers\n }\n\n this.#result = newResult\n\n if (!this.hasListeners()) return\n\n if (hasStructuralChange) {\n difference(prevObservers, newObservers).forEach((observer) => {\n observer.destroy()\n })\n difference(newObservers, prevObservers).forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n }\n\n this.#notify()\n })\n }\n\n /**\n * Returns the most recently computed array of `QueryObserverResult`s, one\n * per observed query, in the same order as the queries passed to the\n * constructor or `setQueries`.\n * @returns The current results.\n * @example\n * ```ts\n * const results = observer.getCurrentResult()\n * const data = results.map((result) => result.data)\n * ```\n */\n getCurrentResult(): Array<QueryObserverResult> {\n return this.#result\n }\n\n /**\n * Returns the underlying `Query` instances currently being observed, in\n * the same order as the queries passed to the constructor or `setQueries`.\n * @returns The observed queries.\n */\n getQueries() {\n return this.#observers.map((observer) => observer.getCurrentQuery())\n }\n\n /**\n * Returns the underlying `QueryObserver` instances this observer manages,\n * in the same order as the queries passed to the constructor or\n * `setQueries`.\n * @returns The managed query observers.\n */\n getObservers() {\n return this.#observers\n }\n\n /**\n * The `QueriesObserver` counterpart of {@link QueryObserver#getOptimisticResult} — computes\n * the result for the given (already-defaulted) queries right now, synchronously. Called by\n * framework adapters (e.g. `useQueries`) ahead of subscribing, returning a tuple of the raw\n * per-query results, a function to compute the combined result from them, and a function to\n * wrap the results for property-access tracking.\n * @param queries - The defaulted options of the queries to compute the result for.\n * @param combine - The `combine` function used by the returned `combineResult`, if any.\n * @returns A tuple of the per-query results, a function that computes the combined result, and a\n * function that returns the results wrapped for property-access tracking.\n */\n getOptimisticResult(\n queries: Array<QueryObserverOptions>,\n combine: CombineFn<TCombinedResult> | undefined,\n ): [\n rawResult: Array<QueryObserverResult>,\n combineResult: (r?: Array<QueryObserverResult>) => TCombinedResult,\n trackResult: () => Array<QueryObserverResult>,\n ] {\n const matches = this.#findMatchingObservers(queries)\n const result = matches.map((match) =>\n match.observer.getOptimisticResult(match.defaultedQueryOptions),\n )\n const queryHashes = matches.map(\n (match) => match.defaultedQueryOptions.queryHash,\n )\n\n return [\n result,\n (r?: Array<QueryObserverResult>) => {\n return this.#combineResult(r ?? result, combine, queryHashes)\n },\n () => {\n return this.#trackResult(result, matches)\n },\n ]\n }\n\n #trackResult(\n result: Array<QueryObserverResult>,\n matches: Array<QueryObserverMatch>,\n ) {\n const trackedProps = new Set<keyof QueryObserverResult>()\n\n return matches.map((match, index) => {\n const observerResult = result[index]!\n return !match.defaultedQueryOptions.notifyOnChangeProps\n ? match.observer.trackResult(observerResult, (accessedProp) => {\n // track property on all observers to ensure proper (synchronized) tracking (#7000)\n if (!trackedProps.has(accessedProp)) {\n trackedProps.add(accessedProp)\n matches.forEach((m) => {\n m.observer.trackProp(accessedProp)\n })\n }\n })\n : observerResult\n })\n }\n\n #combineResult(\n input: Array<QueryObserverResult>,\n combine: CombineFn<TCombinedResult> | undefined,\n queryHashes?: Array<string>,\n ): TCombinedResult {\n if (combine) {\n const lastHashes = this.#lastQueryHashes\n const queryHashesChanged =\n queryHashes !== undefined &&\n lastHashes !== undefined &&\n (lastHashes.length !== queryHashes.length ||\n queryHashes.some((hash, i) => hash !== lastHashes[i]))\n\n if (\n this.#result !== this.#lastResult ||\n queryHashesChanged ||\n combine !== this.#lastCombine\n ) {\n this.#lastCombine = combine\n this.#lastResult = this.#result\n\n if (queryHashes !== undefined) {\n this.#lastQueryHashes = queryHashes\n }\n this.#combinedResult = replaceEqualDeep(\n this.#combinedResult,\n combine(input),\n )\n }\n\n return this.#combinedResult\n }\n return input as any\n }\n\n #shouldSkipCombine(): boolean {\n return (\n !this.#options?.combine ||\n this.#observers.some((observer, index) => {\n return (\n observer.options.suspense && this.#result[index]?.data === undefined\n )\n })\n )\n }\n\n #findMatchingObservers(\n queries: Array<QueryObserverOptions>,\n ): Array<QueryObserverMatch> {\n const prevObserversMap = new Map<string, Array<QueryObserver>>()\n\n this.#observers.forEach((observer) => {\n const key = observer.options.queryHash\n if (!key) return\n\n const previousObservers = prevObserversMap.get(key)\n\n if (previousObservers) {\n previousObservers.push(observer)\n } else {\n prevObserversMap.set(key, [observer])\n }\n })\n\n const observers: Array<QueryObserverMatch> = []\n\n queries.forEach((options) => {\n const defaultedOptions = this.#client.defaultQueryOptions(options)\n const match = prevObserversMap.get(defaultedOptions.queryHash)?.shift()\n const observer =\n match ?? new QueryObserver(this.#client, defaultedOptions)\n\n observers.push({\n defaultedQueryOptions: defaultedOptions,\n observer,\n })\n })\n\n return observers\n }\n\n #onUpdate(observer: QueryObserver, result: QueryObserverResult): void {\n const index = this.#observers.indexOf(observer)\n if (index !== -1) {\n this.#result = this.#result.slice()\n this.#result[index] = result\n this.#notify()\n }\n }\n\n #notify(): void {\n if (this.hasListeners()) {\n const shouldSkipCombine = this.#shouldSkipCombine()\n const previousResult = this.#combinedResult\n const newResult = shouldSkipCombine\n ? previousResult\n : this.#combineResult(\n this.#trackResult(this.#result, this.#observerMatches),\n this.#options?.combine,\n )\n\n if (shouldSkipCombine || previousResult !== newResult) {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(this.#result)\n })\n })\n }\n }\n }\n}\n\ntype QueryObserverMatch = {\n defaultedQueryOptions: DefaultedQueryObserverOptions\n observer: QueryObserver\n}\n"],"mappings":";;;;;;;;;;;;AAiBA,SAAS,WAAc,QAAkB,QAA4B;CACnE,MAAM,aAAa,IAAI,IAAI,MAAM;CACjC,OAAO,OAAO,QAAQ,MAAM,CAAC,WAAW,IAAI,CAAC,CAAC;AAChD;;;;;;;;;;;;;;;;;;;AA2CA,IAAa,kBAAb,cAEUA,qBAAAA,aAAsC;CAC9C;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA,mBAA8C,CAAC;CAE/C,YACE,QACA,SACA,SACA;EACA,MAAM;EAEN,KAAK,UAAU;EACf,KAAK,WAAW;EAChB,KAAK,WAAW,CAAC;EACjB,KAAK,aAAa,CAAC;EACnB,KAAK,UAAU,CAAC;EAEhB,KAAK,WAAW,OAAO;CACzB;CAEA,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;CAEA,UAAU,UAAyB,QAAmC;EACpE,MAAM,QAAQ,KAAK,WAAW,QAAQ,QAAQ;EAC9C,IAAI,UAAU,IAAI;GAChB,KAAK,UAAU,KAAK,QAAQ,MAAM;GAClC,KAAK,QAAQ,SAAS;GACtB,KAAK,QAAQ;EACf;CACF;CAEA,UAAgB;EACd,IAAI,KAAK,aAAa,GAAG;GACvB,MAAM,oBAAoB,KAAK,mBAAmB;GAClD,MAAM,iBAAiB,KAAK;GAC5B,MAAM,YAAY,oBACd,iBACA,KAAK,eACH,KAAK,aAAa,KAAK,SAAS,KAAK,gBAAgB,GACrD,KAAK,UAAU,OACjB;GAEJ,IAAI,qBAAqB,mBAAmB,WAC1C,sBAAA,cAAc,YAAY;IACxB,KAAK,UAAU,SAAS,aAAa;KACnC,SAAS,KAAK,OAAO;IACvB,CAAC;GACH,CAAC;EAEL;CACF;AACF"}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import { t as Subscribable } from "./subscribable-
|
|
2
|
-
import { $n as Query, At as QueryObserverOptions, Pt as QueryObserverResult, on as QueryClient, rr as QueryObserver } from "./hydration-
|
|
1
|
+
import { t as Subscribable } from "./subscribable-czlHOxMP.cjs";
|
|
2
|
+
import { $n as Query, At as QueryObserverOptions, Pt as QueryObserverResult, on as QueryClient, rr as QueryObserver } from "./hydration-C6zghADG.cjs";
|
|
3
3
|
//#region src/queriesObserver.d.ts
|
|
4
4
|
type QueriesObserverListener = (result: Array<QueryObserverResult>) => void;
|
|
5
5
|
type CombineFn<TCombinedResult> = (result: Array<QueryObserverResult>) => TCombinedResult;
|
|
6
|
+
/**
|
|
7
|
+
* Options for a `QueriesObserver` that apply to all of its queries at once.
|
|
8
|
+
*/
|
|
6
9
|
export interface QueriesObserverOptions<TCombinedResult = Array<QueryObserverResult>> {
|
|
7
10
|
/**
|
|
8
11
|
* A function that combines the array of `QueryObserverResult`s (one per
|
|
@@ -20,7 +23,6 @@ export interface QueriesObserverOptions<TCombinedResult = Array<QueryObserverRes
|
|
|
20
23
|
* given, as a combined value derived from that array). It manages one
|
|
21
24
|
* internal `QueryObserver` per query, and is the primitive that framework
|
|
22
25
|
* adapters (e.g. `useQueries`) build their hooks on top of.
|
|
23
|
-
*
|
|
24
26
|
* @example
|
|
25
27
|
* ```ts
|
|
26
28
|
* const observer = new QueriesObserver(queryClient, [
|
|
@@ -48,7 +50,8 @@ export declare class QueriesObserver<TCombinedResult = Array<QueryObserverResult
|
|
|
48
50
|
* are reused for queries that match an already-observed query hash;
|
|
49
51
|
* observers for queries that are no longer present are destroyed, and new
|
|
50
52
|
* observers are created and subscribed to for newly added queries.
|
|
51
|
-
*
|
|
53
|
+
* @param queries - The options of the queries to observe.
|
|
54
|
+
* @param options - Replaces the observer's options, e.g. its `combine` function.
|
|
52
55
|
* @example
|
|
53
56
|
* ```ts
|
|
54
57
|
* observer.setQueries([
|
|
@@ -62,7 +65,7 @@ export declare class QueriesObserver<TCombinedResult = Array<QueryObserverResult
|
|
|
62
65
|
* Returns the most recently computed array of `QueryObserverResult`s, one
|
|
63
66
|
* per observed query, in the same order as the queries passed to the
|
|
64
67
|
* constructor or `setQueries`.
|
|
65
|
-
*
|
|
68
|
+
* @returns The current results.
|
|
66
69
|
* @example
|
|
67
70
|
* ```ts
|
|
68
71
|
* const results = observer.getCurrentResult()
|
|
@@ -73,12 +76,14 @@ export declare class QueriesObserver<TCombinedResult = Array<QueryObserverResult
|
|
|
73
76
|
/**
|
|
74
77
|
* Returns the underlying `Query` instances currently being observed, in
|
|
75
78
|
* the same order as the queries passed to the constructor or `setQueries`.
|
|
79
|
+
* @returns The observed queries.
|
|
76
80
|
*/
|
|
77
81
|
getQueries(): Query<unknown, Error, unknown, readonly unknown[]>[];
|
|
78
82
|
/**
|
|
79
83
|
* Returns the underlying `QueryObserver` instances this observer manages,
|
|
80
84
|
* in the same order as the queries passed to the constructor or
|
|
81
85
|
* `setQueries`.
|
|
86
|
+
* @returns The managed query observers.
|
|
82
87
|
*/
|
|
83
88
|
getObservers(): QueryObserver<unknown, Error, unknown, unknown, readonly unknown[]>[];
|
|
84
89
|
/**
|
|
@@ -87,6 +92,10 @@ export declare class QueriesObserver<TCombinedResult = Array<QueryObserverResult
|
|
|
87
92
|
* framework adapters (e.g. `useQueries`) ahead of subscribing, returning a tuple of the raw
|
|
88
93
|
* per-query results, a function to compute the combined result from them, and a function to
|
|
89
94
|
* wrap the results for property-access tracking.
|
|
95
|
+
* @param queries - The defaulted options of the queries to compute the result for.
|
|
96
|
+
* @param combine - The `combine` function used by the returned `combineResult`, if any.
|
|
97
|
+
* @returns A tuple of the per-query results, a function that computes the combined result, and a
|
|
98
|
+
* function that returns the results wrapped for property-access tracking.
|
|
90
99
|
*/
|
|
91
100
|
getOptimisticResult(queries: Array<QueryObserverOptions>, combine: CombineFn<TCombinedResult> | undefined): [rawResult: Array<QueryObserverResult>, combineResult: (r?: Array<QueryObserverResult>) => TCombinedResult, trackResult: () => Array<QueryObserverResult>];
|
|
92
101
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queriesObserver.d.cts","names":["_$__query0.Query"],"sources":["../../src/queriesObserver.ts"],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"queriesObserver.d.cts","names":["_$__query0.Query"],"sources":["../../src/queriesObserver.ts"],"mappings":";;;KAsBK,2BAA2B,QAAQ,MAAM;KAEzC,UAAU,oBACb,QAAQ,MAAM,yBACX;;;;iBAKY,uBACf,kBAAkB,MAAM;;;;;;;;;EAUxB,UAAU,UAAU;;;;;;;;;;;;;;;;;;;;qBAqBT,gBACX,kBAAkB,MAAM,8BAChB,aAAa;;EAanB,YAAA,QAAQ,aACR,SAAS,MAAM,gDACf,UAAU,uBAAuB;YAahB;YAUA;;;;;EAUnB;;;;;;;;;;;;;;;;EAsBA,WACE,SAAS,MAAM,uBACf,UAAU,uBAAuB;;;;;;;;;;;;EAiFnC,oBAAoB,MAAM;;;;;;EAS1B,cAAUA,eAAA;;;;;;;EAUV,gBAAY,uBAAA;;;;;;;;;;;;EAeZ,oBACE,SAAS,MAAM,uBACf,SAAS,UAAU,gCAEnB,WAAW,MAAM,sBACjB,gBAAgB,IAAI,MAAM,yBAAyB,iBACnD,mBAAmB,MAAM"}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import { t as Subscribable } from "./subscribable-
|
|
2
|
-
import { $n as Query, At as QueryObserverOptions, Pt as QueryObserverResult, on as QueryClient, rr as QueryObserver } from "./hydration-
|
|
1
|
+
import { t as Subscribable } from "./subscribable-czlHOxMP.js";
|
|
2
|
+
import { $n as Query, At as QueryObserverOptions, Pt as QueryObserverResult, on as QueryClient, rr as QueryObserver } from "./hydration-Bf0oMcwO.js";
|
|
3
3
|
//#region src/queriesObserver.d.ts
|
|
4
4
|
type QueriesObserverListener = (result: Array<QueryObserverResult>) => void;
|
|
5
5
|
type CombineFn<TCombinedResult> = (result: Array<QueryObserverResult>) => TCombinedResult;
|
|
6
|
+
/**
|
|
7
|
+
* Options for a `QueriesObserver` that apply to all of its queries at once.
|
|
8
|
+
*/
|
|
6
9
|
export interface QueriesObserverOptions<TCombinedResult = Array<QueryObserverResult>> {
|
|
7
10
|
/**
|
|
8
11
|
* A function that combines the array of `QueryObserverResult`s (one per
|
|
@@ -20,7 +23,6 @@ export interface QueriesObserverOptions<TCombinedResult = Array<QueryObserverRes
|
|
|
20
23
|
* given, as a combined value derived from that array). It manages one
|
|
21
24
|
* internal `QueryObserver` per query, and is the primitive that framework
|
|
22
25
|
* adapters (e.g. `useQueries`) build their hooks on top of.
|
|
23
|
-
*
|
|
24
26
|
* @example
|
|
25
27
|
* ```ts
|
|
26
28
|
* const observer = new QueriesObserver(queryClient, [
|
|
@@ -48,7 +50,8 @@ export declare class QueriesObserver<TCombinedResult = Array<QueryObserverResult
|
|
|
48
50
|
* are reused for queries that match an already-observed query hash;
|
|
49
51
|
* observers for queries that are no longer present are destroyed, and new
|
|
50
52
|
* observers are created and subscribed to for newly added queries.
|
|
51
|
-
*
|
|
53
|
+
* @param queries - The options of the queries to observe.
|
|
54
|
+
* @param options - Replaces the observer's options, e.g. its `combine` function.
|
|
52
55
|
* @example
|
|
53
56
|
* ```ts
|
|
54
57
|
* observer.setQueries([
|
|
@@ -62,7 +65,7 @@ export declare class QueriesObserver<TCombinedResult = Array<QueryObserverResult
|
|
|
62
65
|
* Returns the most recently computed array of `QueryObserverResult`s, one
|
|
63
66
|
* per observed query, in the same order as the queries passed to the
|
|
64
67
|
* constructor or `setQueries`.
|
|
65
|
-
*
|
|
68
|
+
* @returns The current results.
|
|
66
69
|
* @example
|
|
67
70
|
* ```ts
|
|
68
71
|
* const results = observer.getCurrentResult()
|
|
@@ -73,12 +76,14 @@ export declare class QueriesObserver<TCombinedResult = Array<QueryObserverResult
|
|
|
73
76
|
/**
|
|
74
77
|
* Returns the underlying `Query` instances currently being observed, in
|
|
75
78
|
* the same order as the queries passed to the constructor or `setQueries`.
|
|
79
|
+
* @returns The observed queries.
|
|
76
80
|
*/
|
|
77
81
|
getQueries(): Query<unknown, Error, unknown, readonly unknown[]>[];
|
|
78
82
|
/**
|
|
79
83
|
* Returns the underlying `QueryObserver` instances this observer manages,
|
|
80
84
|
* in the same order as the queries passed to the constructor or
|
|
81
85
|
* `setQueries`.
|
|
86
|
+
* @returns The managed query observers.
|
|
82
87
|
*/
|
|
83
88
|
getObservers(): QueryObserver<unknown, Error, unknown, unknown, readonly unknown[]>[];
|
|
84
89
|
/**
|
|
@@ -87,6 +92,10 @@ export declare class QueriesObserver<TCombinedResult = Array<QueryObserverResult
|
|
|
87
92
|
* framework adapters (e.g. `useQueries`) ahead of subscribing, returning a tuple of the raw
|
|
88
93
|
* per-query results, a function to compute the combined result from them, and a function to
|
|
89
94
|
* wrap the results for property-access tracking.
|
|
95
|
+
* @param queries - The defaulted options of the queries to compute the result for.
|
|
96
|
+
* @param combine - The `combine` function used by the returned `combineResult`, if any.
|
|
97
|
+
* @returns A tuple of the per-query results, a function that computes the combined result, and a
|
|
98
|
+
* function that returns the results wrapped for property-access tracking.
|
|
90
99
|
*/
|
|
91
100
|
getOptimisticResult(queries: Array<QueryObserverOptions>, combine: CombineFn<TCombinedResult> | undefined): [rawResult: Array<QueryObserverResult>, combineResult: (r?: Array<QueryObserverResult>) => TCombinedResult, trackResult: () => Array<QueryObserverResult>];
|
|
92
101
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queriesObserver.d.ts","names":["_$__query0.Query"],"sources":["../../src/queriesObserver.ts"],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"queriesObserver.d.ts","names":["_$__query0.Query"],"sources":["../../src/queriesObserver.ts"],"mappings":";;;KAsBK,2BAA2B,QAAQ,MAAM;KAEzC,UAAU,oBACb,QAAQ,MAAM,yBACX;;;;iBAKY,uBACf,kBAAkB,MAAM;;;;;;;;;EAUxB,UAAU,UAAU;;;;;;;;;;;;;;;;;;;;qBAqBT,gBACX,kBAAkB,MAAM,8BAChB,aAAa;;EAanB,YAAA,QAAQ,aACR,SAAS,MAAM,gDACf,UAAU,uBAAuB;YAahB;YAUA;;;;;EAUnB;;;;;;;;;;;;;;;;EAsBA,WACE,SAAS,MAAM,uBACf,UAAU,uBAAuB;;;;;;;;;;;;EAiFnC,oBAAoB,MAAM;;;;;;EAS1B,cAAUA,eAAA;;;;;;;EAUV,gBAAY,uBAAA;;;;;;;;;;;;EAeZ,oBACE,SAAS,MAAM,uBACf,SAAS,UAAU,gCAEnB,WAAW,MAAM,sBACjB,gBAAgB,IAAI,MAAM,yBAAyB,iBACnD,mBAAmB,MAAM"}
|
|
@@ -3,6 +3,12 @@ import { Subscribable } from "./subscribable.js";
|
|
|
3
3
|
import { notifyManager } from "./notifyManager.js";
|
|
4
4
|
import { QueryObserver } from "./queryObserver.js";
|
|
5
5
|
//#region src/queriesObserver.ts
|
|
6
|
+
/**
|
|
7
|
+
* Returns the items of `array1` that are not in `array2`.
|
|
8
|
+
* @param array1 - The items to filter.
|
|
9
|
+
* @param array2 - The items to exclude.
|
|
10
|
+
* @returns A new array with the items of `array1` that are not in `array2`.
|
|
11
|
+
*/
|
|
6
12
|
function difference(array1, array2) {
|
|
7
13
|
const excludeSet = new Set(array2);
|
|
8
14
|
return array1.filter((x) => !excludeSet.has(x));
|
|
@@ -13,7 +19,6 @@ function difference(array1, array2) {
|
|
|
13
19
|
* given, as a combined value derived from that array). It manages one
|
|
14
20
|
* internal `QueryObserver` per query, and is the primitive that framework
|
|
15
21
|
* adapters (e.g. `useQueries`) build their hooks on top of.
|
|
16
|
-
*
|
|
17
22
|
* @example
|
|
18
23
|
* ```ts
|
|
19
24
|
* const observer = new QueriesObserver(queryClient, [
|
|
@@ -71,7 +76,8 @@ var QueriesObserver = class extends Subscribable {
|
|
|
71
76
|
* are reused for queries that match an already-observed query hash;
|
|
72
77
|
* observers for queries that are no longer present are destroyed, and new
|
|
73
78
|
* observers are created and subscribed to for newly added queries.
|
|
74
|
-
*
|
|
79
|
+
* @param queries - The options of the queries to observe.
|
|
80
|
+
* @param options - Replaces the observer's options, e.g. its `combine` function.
|
|
75
81
|
* @example
|
|
76
82
|
* ```ts
|
|
77
83
|
* observer.setQueries([
|
|
@@ -124,7 +130,7 @@ var QueriesObserver = class extends Subscribable {
|
|
|
124
130
|
* Returns the most recently computed array of `QueryObserverResult`s, one
|
|
125
131
|
* per observed query, in the same order as the queries passed to the
|
|
126
132
|
* constructor or `setQueries`.
|
|
127
|
-
*
|
|
133
|
+
* @returns The current results.
|
|
128
134
|
* @example
|
|
129
135
|
* ```ts
|
|
130
136
|
* const results = observer.getCurrentResult()
|
|
@@ -137,6 +143,7 @@ var QueriesObserver = class extends Subscribable {
|
|
|
137
143
|
/**
|
|
138
144
|
* Returns the underlying `Query` instances currently being observed, in
|
|
139
145
|
* the same order as the queries passed to the constructor or `setQueries`.
|
|
146
|
+
* @returns The observed queries.
|
|
140
147
|
*/
|
|
141
148
|
getQueries() {
|
|
142
149
|
return this.#observers.map((observer) => observer.getCurrentQuery());
|
|
@@ -145,6 +152,7 @@ var QueriesObserver = class extends Subscribable {
|
|
|
145
152
|
* Returns the underlying `QueryObserver` instances this observer manages,
|
|
146
153
|
* in the same order as the queries passed to the constructor or
|
|
147
154
|
* `setQueries`.
|
|
155
|
+
* @returns The managed query observers.
|
|
148
156
|
*/
|
|
149
157
|
getObservers() {
|
|
150
158
|
return this.#observers;
|
|
@@ -155,6 +163,10 @@ var QueriesObserver = class extends Subscribable {
|
|
|
155
163
|
* framework adapters (e.g. `useQueries`) ahead of subscribing, returning a tuple of the raw
|
|
156
164
|
* per-query results, a function to compute the combined result from them, and a function to
|
|
157
165
|
* wrap the results for property-access tracking.
|
|
166
|
+
* @param queries - The defaulted options of the queries to compute the result for.
|
|
167
|
+
* @param combine - The `combine` function used by the returned `combineResult`, if any.
|
|
168
|
+
* @returns A tuple of the per-query results, a function that computes the combined result, and a
|
|
169
|
+
* function that returns the results wrapped for property-access tracking.
|
|
158
170
|
*/
|
|
159
171
|
getOptimisticResult(queries, combine) {
|
|
160
172
|
const matches = this.#findMatchingObservers(queries);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queriesObserver.js","names":[],"sources":["../../src/queriesObserver.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport { QueryObserver } from './queryObserver'\nimport { Subscribable } from './subscribable'\nimport { replaceEqualDeep, shallowEqualObjects } from './utils'\nimport type {\n DefaultedQueryObserverOptions,\n QueryObserverOptions,\n QueryObserverResult,\n} from './types'\nimport type { QueryClient } from './queryClient'\n\nfunction difference<T>(array1: Array<T>, array2: Array<T>): Array<T> {\n const excludeSet = new Set(array2)\n return array1.filter((x) => !excludeSet.has(x))\n}\n\ntype QueriesObserverListener = (result: Array<QueryObserverResult>) => void\n\ntype CombineFn<TCombinedResult> = (\n result: Array<QueryObserverResult>,\n) => TCombinedResult\n\nexport interface QueriesObserverOptions<\n TCombinedResult = Array<QueryObserverResult>,\n> {\n /**\n * A function that combines the array of `QueryObserverResult`s (one per\n * observed query) into a single value. The combined value is memoized and\n * only recomputed when one of the underlying results, the query hashes, or\n * the `combine` function itself changes.\n *\n * Defaults to returning the array of `QueryObserverResult`s unchanged.\n */\n combine?: CombineFn<TCombinedResult>\n}\n\n/**\n * A `QueriesObserver` watches an array of queries at once, exposing them as\n * a single array of `QueryObserverResult`s (or, when a `combine` option is\n * given, as a combined value derived from that array). It manages one\n * internal `QueryObserver` per query, and is the primitive that framework\n * adapters (e.g. `useQueries`) build their hooks on top of.\n *\n * @example\n * ```ts\n * const observer = new QueriesObserver(queryClient, [\n * { queryKey: ['post', 1], queryFn: fetchPost },\n * { queryKey: ['post', 2], queryFn: fetchPost },\n * ])\n *\n * const unsubscribe = observer.subscribe((result) => {\n * console.log(result)\n * })\n * ```\n */\nexport class QueriesObserver<\n TCombinedResult = Array<QueryObserverResult>,\n> extends Subscribable<QueriesObserverListener> {\n #client: QueryClient\n #result!: Array<QueryObserverResult>\n #queries: Array<QueryObserverOptions>\n #options?: QueriesObserverOptions<TCombinedResult>\n #observers: Array<QueryObserver>\n #combinedResult!: TCombinedResult\n #lastCombine?: CombineFn<TCombinedResult>\n #lastResult?: Array<QueryObserverResult>\n #lastQueryHashes?: Array<string>\n #observerMatches: Array<QueryObserverMatch> = []\n\n constructor(\n client: QueryClient,\n queries: Array<QueryObserverOptions<any, any, any, any, any>>,\n options?: QueriesObserverOptions<TCombinedResult>,\n ) {\n super()\n\n this.#client = client\n this.#options = options\n this.#queries = []\n this.#observers = []\n this.#result = []\n\n this.setQueries(queries)\n }\n\n protected onSubscribe(): void {\n if (this.listeners.size === 1) {\n this.#observers.forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n }\n }\n\n protected onUnsubscribe(): void {\n if (!this.listeners.size) {\n this.destroy()\n }\n }\n\n /**\n * Stops observing all queries: clears all listeners and destroys every\n * underlying `QueryObserver` this observer manages.\n */\n destroy(): void {\n this.listeners = new Set()\n this.#observers.forEach((observer) => {\n observer.destroy()\n })\n }\n\n /**\n * Replaces the set of queries being observed. Existing `QueryObserver`s\n * are reused for queries that match an already-observed query hash;\n * observers for queries that are no longer present are destroyed, and new\n * observers are created and subscribed to for newly added queries.\n *\n * @example\n * ```ts\n * observer.setQueries([\n * { queryKey: ['post', 1], queryFn: fetchPost },\n * { queryKey: ['post', 3], queryFn: fetchPost },\n * ])\n * ```\n */\n setQueries(\n queries: Array<QueryObserverOptions>,\n options?: QueriesObserverOptions<TCombinedResult>,\n ): void {\n this.#queries = queries\n this.#options = options\n\n if (process.env.NODE_ENV !== 'production') {\n const queryHashes = queries.map(\n (query) => this.#client.defaultQueryOptions(query).queryHash,\n )\n if (new Set(queryHashes).size !== queryHashes.length) {\n console.warn(\n '[QueriesObserver]: Duplicate Queries found. This might result in unexpected behavior.',\n )\n }\n }\n\n notifyManager.batch(() => {\n const prevObservers = this.#observers\n\n const newObserverMatches = this.#findMatchingObservers(this.#queries)\n\n // set options for the new observers to notify of changes\n newObserverMatches.forEach((match) =>\n match.observer.setOptions(match.defaultedQueryOptions),\n )\n\n const newObservers = newObserverMatches.map((match) => match.observer)\n const newResult = newObservers.map((observer) =>\n observer.getCurrentResult(),\n )\n\n const hasLengthChange = prevObservers.length !== newObservers.length\n const hasIndexChange = newObservers.some(\n (observer, index) => observer !== prevObservers[index],\n )\n const hasStructuralChange = hasLengthChange || hasIndexChange\n\n const hasResultChange = hasStructuralChange\n ? true\n : newResult.some((result, index) => {\n const prev = this.#result[index]\n return !prev || !shallowEqualObjects(result, prev)\n })\n\n if (!hasStructuralChange && !hasResultChange) return\n\n if (hasStructuralChange) {\n this.#observerMatches = newObserverMatches\n this.#observers = newObservers\n }\n\n this.#result = newResult\n\n if (!this.hasListeners()) return\n\n if (hasStructuralChange) {\n difference(prevObservers, newObservers).forEach((observer) => {\n observer.destroy()\n })\n difference(newObservers, prevObservers).forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n }\n\n this.#notify()\n })\n }\n\n /**\n * Returns the most recently computed array of `QueryObserverResult`s, one\n * per observed query, in the same order as the queries passed to the\n * constructor or `setQueries`.\n *\n * @example\n * ```ts\n * const results = observer.getCurrentResult()\n * const data = results.map((result) => result.data)\n * ```\n */\n getCurrentResult(): Array<QueryObserverResult> {\n return this.#result\n }\n\n /**\n * Returns the underlying `Query` instances currently being observed, in\n * the same order as the queries passed to the constructor or `setQueries`.\n */\n getQueries() {\n return this.#observers.map((observer) => observer.getCurrentQuery())\n }\n\n /**\n * Returns the underlying `QueryObserver` instances this observer manages,\n * in the same order as the queries passed to the constructor or\n * `setQueries`.\n */\n getObservers() {\n return this.#observers\n }\n\n /**\n * The `QueriesObserver` counterpart of {@link QueryObserver#getOptimisticResult} — computes\n * the result for the given (already-defaulted) queries right now, synchronously. Called by\n * framework adapters (e.g. `useQueries`) ahead of subscribing, returning a tuple of the raw\n * per-query results, a function to compute the combined result from them, and a function to\n * wrap the results for property-access tracking.\n */\n getOptimisticResult(\n queries: Array<QueryObserverOptions>,\n combine: CombineFn<TCombinedResult> | undefined,\n ): [\n rawResult: Array<QueryObserverResult>,\n combineResult: (r?: Array<QueryObserverResult>) => TCombinedResult,\n trackResult: () => Array<QueryObserverResult>,\n ] {\n const matches = this.#findMatchingObservers(queries)\n const result = matches.map((match) =>\n match.observer.getOptimisticResult(match.defaultedQueryOptions),\n )\n const queryHashes = matches.map(\n (match) => match.defaultedQueryOptions.queryHash,\n )\n\n return [\n result,\n (r?: Array<QueryObserverResult>) => {\n return this.#combineResult(r ?? result, combine, queryHashes)\n },\n () => {\n return this.#trackResult(result, matches)\n },\n ]\n }\n\n #trackResult(\n result: Array<QueryObserverResult>,\n matches: Array<QueryObserverMatch>,\n ) {\n const trackedProps = new Set<keyof QueryObserverResult>()\n\n return matches.map((match, index) => {\n const observerResult = result[index]!\n return !match.defaultedQueryOptions.notifyOnChangeProps\n ? match.observer.trackResult(observerResult, (accessedProp) => {\n // track property on all observers to ensure proper (synchronized) tracking (#7000)\n if (!trackedProps.has(accessedProp)) {\n trackedProps.add(accessedProp)\n matches.forEach((m) => {\n m.observer.trackProp(accessedProp)\n })\n }\n })\n : observerResult\n })\n }\n\n #combineResult(\n input: Array<QueryObserverResult>,\n combine: CombineFn<TCombinedResult> | undefined,\n queryHashes?: Array<string>,\n ): TCombinedResult {\n if (combine) {\n const lastHashes = this.#lastQueryHashes\n const queryHashesChanged =\n queryHashes !== undefined &&\n lastHashes !== undefined &&\n (lastHashes.length !== queryHashes.length ||\n queryHashes.some((hash, i) => hash !== lastHashes[i]))\n\n if (\n this.#result !== this.#lastResult ||\n queryHashesChanged ||\n combine !== this.#lastCombine\n ) {\n this.#lastCombine = combine\n this.#lastResult = this.#result\n\n if (queryHashes !== undefined) {\n this.#lastQueryHashes = queryHashes\n }\n this.#combinedResult = replaceEqualDeep(\n this.#combinedResult,\n combine(input),\n )\n }\n\n return this.#combinedResult\n }\n return input as any\n }\n\n #shouldSkipCombine(): boolean {\n return (\n !this.#options?.combine ||\n this.#observers.some((observer, index) => {\n return (\n observer.options.suspense && this.#result[index]?.data === undefined\n )\n })\n )\n }\n\n #findMatchingObservers(\n queries: Array<QueryObserverOptions>,\n ): Array<QueryObserverMatch> {\n const prevObserversMap = new Map<string, Array<QueryObserver>>()\n\n this.#observers.forEach((observer) => {\n const key = observer.options.queryHash\n if (!key) return\n\n const previousObservers = prevObserversMap.get(key)\n\n if (previousObservers) {\n previousObservers.push(observer)\n } else {\n prevObserversMap.set(key, [observer])\n }\n })\n\n const observers: Array<QueryObserverMatch> = []\n\n queries.forEach((options) => {\n const defaultedOptions = this.#client.defaultQueryOptions(options)\n const match = prevObserversMap.get(defaultedOptions.queryHash)?.shift()\n const observer =\n match ?? new QueryObserver(this.#client, defaultedOptions)\n\n observers.push({\n defaultedQueryOptions: defaultedOptions,\n observer,\n })\n })\n\n return observers\n }\n\n #onUpdate(observer: QueryObserver, result: QueryObserverResult): void {\n const index = this.#observers.indexOf(observer)\n if (index !== -1) {\n this.#result = this.#result.slice()\n this.#result[index] = result\n this.#notify()\n }\n }\n\n #notify(): void {\n if (this.hasListeners()) {\n const shouldSkipCombine = this.#shouldSkipCombine()\n const previousResult = this.#combinedResult\n const newResult = shouldSkipCombine\n ? previousResult\n : this.#combineResult(\n this.#trackResult(this.#result, this.#observerMatches),\n this.#options?.combine,\n )\n\n if (shouldSkipCombine || previousResult !== newResult) {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(this.#result)\n })\n })\n }\n }\n }\n}\n\ntype QueryObserverMatch = {\n defaultedQueryOptions: DefaultedQueryObserverOptions\n observer: QueryObserver\n}\n"],"mappings":";;;;;AAWA,SAAS,WAAc,QAAkB,QAA4B;CACnE,MAAM,aAAa,IAAI,IAAI,MAAM;CACjC,OAAO,OAAO,QAAQ,MAAM,CAAC,WAAW,IAAI,CAAC,CAAC;AAChD;;;;;;;;;;;;;;;;;;;;AAyCA,IAAa,kBAAb,cAEU,aAAsC;CAC9C;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA,mBAA8C,CAAC;CAE/C,YACE,QACA,SACA,SACA;EACA,MAAM;EAEN,KAAK,UAAU;EACf,KAAK,WAAW;EAChB,KAAK,WAAW,CAAC;EACjB,KAAK,aAAa,CAAC;EACnB,KAAK,UAAU,CAAC;EAEhB,KAAK,WAAW,OAAO;CACzB;CAEA,cAA8B;EAC5B,IAAI,KAAK,UAAU,SAAS,GAC1B,KAAK,WAAW,SAAS,aAAa;GACpC,SAAS,WAAW,WAAW;IAC7B,KAAK,UAAU,UAAU,MAAM;GACjC,CAAC;EACH,CAAC;CAEL;CAEA,gBAAgC;EAC9B,IAAI,CAAC,KAAK,UAAU,MAClB,KAAK,QAAQ;CAEjB;;;;;CAMA,UAAgB;EACd,KAAK,4BAAY,IAAI,IAAI;EACzB,KAAK,WAAW,SAAS,aAAa;GACpC,SAAS,QAAQ;EACnB,CAAC;CACH;;;;;;;;;;;;;;;CAgBA,WACE,SACA,SACM;EACN,KAAK,WAAW;EAChB,KAAK,WAAW;EAEh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,UAAU,SAAS,aAAa;KACnC,SAAS,KAAK,OAAO;IACvB,CAAC;GACH,CAAC;EAEL;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"queriesObserver.js","names":[],"sources":["../../src/queriesObserver.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport { QueryObserver } from './queryObserver'\nimport { Subscribable } from './subscribable'\nimport { replaceEqualDeep, shallowEqualObjects } from './utils'\nimport type {\n DefaultedQueryObserverOptions,\n QueryObserverOptions,\n QueryObserverResult,\n} from './types'\nimport type { QueryClient } from './queryClient'\n\n/**\n * Returns the items of `array1` that are not in `array2`.\n * @param array1 - The items to filter.\n * @param array2 - The items to exclude.\n * @returns A new array with the items of `array1` that are not in `array2`.\n */\nfunction difference<T>(array1: Array<T>, array2: Array<T>): Array<T> {\n const excludeSet = new Set(array2)\n return array1.filter((x) => !excludeSet.has(x))\n}\n\ntype QueriesObserverListener = (result: Array<QueryObserverResult>) => void\n\ntype CombineFn<TCombinedResult> = (\n result: Array<QueryObserverResult>,\n) => TCombinedResult\n\n/**\n * Options for a `QueriesObserver` that apply to all of its queries at once.\n */\nexport interface QueriesObserverOptions<\n TCombinedResult = Array<QueryObserverResult>,\n> {\n /**\n * A function that combines the array of `QueryObserverResult`s (one per\n * observed query) into a single value. The combined value is memoized and\n * only recomputed when one of the underlying results, the query hashes, or\n * the `combine` function itself changes.\n *\n * Defaults to returning the array of `QueryObserverResult`s unchanged.\n */\n combine?: CombineFn<TCombinedResult>\n}\n\n/**\n * A `QueriesObserver` watches an array of queries at once, exposing them as\n * a single array of `QueryObserverResult`s (or, when a `combine` option is\n * given, as a combined value derived from that array). It manages one\n * internal `QueryObserver` per query, and is the primitive that framework\n * adapters (e.g. `useQueries`) build their hooks on top of.\n * @example\n * ```ts\n * const observer = new QueriesObserver(queryClient, [\n * { queryKey: ['post', 1], queryFn: fetchPost },\n * { queryKey: ['post', 2], queryFn: fetchPost },\n * ])\n *\n * const unsubscribe = observer.subscribe((result) => {\n * console.log(result)\n * })\n * ```\n */\nexport class QueriesObserver<\n TCombinedResult = Array<QueryObserverResult>,\n> extends Subscribable<QueriesObserverListener> {\n #client: QueryClient\n #result!: Array<QueryObserverResult>\n #queries: Array<QueryObserverOptions>\n #options?: QueriesObserverOptions<TCombinedResult>\n #observers: Array<QueryObserver>\n #combinedResult!: TCombinedResult\n #lastCombine?: CombineFn<TCombinedResult>\n #lastResult?: Array<QueryObserverResult>\n #lastQueryHashes?: Array<string>\n #observerMatches: Array<QueryObserverMatch> = []\n\n constructor(\n client: QueryClient,\n queries: Array<QueryObserverOptions<any, any, any, any, any>>,\n options?: QueriesObserverOptions<TCombinedResult>,\n ) {\n super()\n\n this.#client = client\n this.#options = options\n this.#queries = []\n this.#observers = []\n this.#result = []\n\n this.setQueries(queries)\n }\n\n protected override onSubscribe(): void {\n if (this.listeners.size === 1) {\n this.#observers.forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n }\n }\n\n protected override onUnsubscribe(): void {\n if (!this.listeners.size) {\n this.destroy()\n }\n }\n\n /**\n * Stops observing all queries: clears all listeners and destroys every\n * underlying `QueryObserver` this observer manages.\n */\n destroy(): void {\n this.listeners = new Set()\n this.#observers.forEach((observer) => {\n observer.destroy()\n })\n }\n\n /**\n * Replaces the set of queries being observed. Existing `QueryObserver`s\n * are reused for queries that match an already-observed query hash;\n * observers for queries that are no longer present are destroyed, and new\n * observers are created and subscribed to for newly added queries.\n * @param queries - The options of the queries to observe.\n * @param options - Replaces the observer's options, e.g. its `combine` function.\n * @example\n * ```ts\n * observer.setQueries([\n * { queryKey: ['post', 1], queryFn: fetchPost },\n * { queryKey: ['post', 3], queryFn: fetchPost },\n * ])\n * ```\n */\n setQueries(\n queries: Array<QueryObserverOptions>,\n options?: QueriesObserverOptions<TCombinedResult>,\n ): void {\n this.#queries = queries\n this.#options = options\n\n if (process.env.NODE_ENV !== 'production') {\n const queryHashes = queries.map(\n (query) => this.#client.defaultQueryOptions(query).queryHash,\n )\n if (new Set(queryHashes).size !== queryHashes.length) {\n console.warn(\n '[QueriesObserver]: Duplicate Queries found. This might result in unexpected behavior.',\n )\n }\n }\n\n notifyManager.batch(() => {\n const prevObservers = this.#observers\n\n const newObserverMatches = this.#findMatchingObservers(this.#queries)\n\n // set options for the new observers to notify of changes\n newObserverMatches.forEach((match) =>\n match.observer.setOptions(match.defaultedQueryOptions),\n )\n\n const newObservers = newObserverMatches.map((match) => match.observer)\n const newResult = newObservers.map((observer) =>\n observer.getCurrentResult(),\n )\n\n const hasLengthChange = prevObservers.length !== newObservers.length\n const hasIndexChange = newObservers.some(\n (observer, index) => observer !== prevObservers[index],\n )\n const hasStructuralChange = hasLengthChange || hasIndexChange\n\n const hasResultChange = hasStructuralChange\n ? true\n : newResult.some((result, index) => {\n const prev = this.#result[index]\n return !prev || !shallowEqualObjects(result, prev)\n })\n\n if (!hasStructuralChange && !hasResultChange) return\n\n if (hasStructuralChange) {\n this.#observerMatches = newObserverMatches\n this.#observers = newObservers\n }\n\n this.#result = newResult\n\n if (!this.hasListeners()) return\n\n if (hasStructuralChange) {\n difference(prevObservers, newObservers).forEach((observer) => {\n observer.destroy()\n })\n difference(newObservers, prevObservers).forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n }\n\n this.#notify()\n })\n }\n\n /**\n * Returns the most recently computed array of `QueryObserverResult`s, one\n * per observed query, in the same order as the queries passed to the\n * constructor or `setQueries`.\n * @returns The current results.\n * @example\n * ```ts\n * const results = observer.getCurrentResult()\n * const data = results.map((result) => result.data)\n * ```\n */\n getCurrentResult(): Array<QueryObserverResult> {\n return this.#result\n }\n\n /**\n * Returns the underlying `Query` instances currently being observed, in\n * the same order as the queries passed to the constructor or `setQueries`.\n * @returns The observed queries.\n */\n getQueries() {\n return this.#observers.map((observer) => observer.getCurrentQuery())\n }\n\n /**\n * Returns the underlying `QueryObserver` instances this observer manages,\n * in the same order as the queries passed to the constructor or\n * `setQueries`.\n * @returns The managed query observers.\n */\n getObservers() {\n return this.#observers\n }\n\n /**\n * The `QueriesObserver` counterpart of {@link QueryObserver#getOptimisticResult} — computes\n * the result for the given (already-defaulted) queries right now, synchronously. Called by\n * framework adapters (e.g. `useQueries`) ahead of subscribing, returning a tuple of the raw\n * per-query results, a function to compute the combined result from them, and a function to\n * wrap the results for property-access tracking.\n * @param queries - The defaulted options of the queries to compute the result for.\n * @param combine - The `combine` function used by the returned `combineResult`, if any.\n * @returns A tuple of the per-query results, a function that computes the combined result, and a\n * function that returns the results wrapped for property-access tracking.\n */\n getOptimisticResult(\n queries: Array<QueryObserverOptions>,\n combine: CombineFn<TCombinedResult> | undefined,\n ): [\n rawResult: Array<QueryObserverResult>,\n combineResult: (r?: Array<QueryObserverResult>) => TCombinedResult,\n trackResult: () => Array<QueryObserverResult>,\n ] {\n const matches = this.#findMatchingObservers(queries)\n const result = matches.map((match) =>\n match.observer.getOptimisticResult(match.defaultedQueryOptions),\n )\n const queryHashes = matches.map(\n (match) => match.defaultedQueryOptions.queryHash,\n )\n\n return [\n result,\n (r?: Array<QueryObserverResult>) => {\n return this.#combineResult(r ?? result, combine, queryHashes)\n },\n () => {\n return this.#trackResult(result, matches)\n },\n ]\n }\n\n #trackResult(\n result: Array<QueryObserverResult>,\n matches: Array<QueryObserverMatch>,\n ) {\n const trackedProps = new Set<keyof QueryObserverResult>()\n\n return matches.map((match, index) => {\n const observerResult = result[index]!\n return !match.defaultedQueryOptions.notifyOnChangeProps\n ? match.observer.trackResult(observerResult, (accessedProp) => {\n // track property on all observers to ensure proper (synchronized) tracking (#7000)\n if (!trackedProps.has(accessedProp)) {\n trackedProps.add(accessedProp)\n matches.forEach((m) => {\n m.observer.trackProp(accessedProp)\n })\n }\n })\n : observerResult\n })\n }\n\n #combineResult(\n input: Array<QueryObserverResult>,\n combine: CombineFn<TCombinedResult> | undefined,\n queryHashes?: Array<string>,\n ): TCombinedResult {\n if (combine) {\n const lastHashes = this.#lastQueryHashes\n const queryHashesChanged =\n queryHashes !== undefined &&\n lastHashes !== undefined &&\n (lastHashes.length !== queryHashes.length ||\n queryHashes.some((hash, i) => hash !== lastHashes[i]))\n\n if (\n this.#result !== this.#lastResult ||\n queryHashesChanged ||\n combine !== this.#lastCombine\n ) {\n this.#lastCombine = combine\n this.#lastResult = this.#result\n\n if (queryHashes !== undefined) {\n this.#lastQueryHashes = queryHashes\n }\n this.#combinedResult = replaceEqualDeep(\n this.#combinedResult,\n combine(input),\n )\n }\n\n return this.#combinedResult\n }\n return input as any\n }\n\n #shouldSkipCombine(): boolean {\n return (\n !this.#options?.combine ||\n this.#observers.some((observer, index) => {\n return (\n observer.options.suspense && this.#result[index]?.data === undefined\n )\n })\n )\n }\n\n #findMatchingObservers(\n queries: Array<QueryObserverOptions>,\n ): Array<QueryObserverMatch> {\n const prevObserversMap = new Map<string, Array<QueryObserver>>()\n\n this.#observers.forEach((observer) => {\n const key = observer.options.queryHash\n if (!key) return\n\n const previousObservers = prevObserversMap.get(key)\n\n if (previousObservers) {\n previousObservers.push(observer)\n } else {\n prevObserversMap.set(key, [observer])\n }\n })\n\n const observers: Array<QueryObserverMatch> = []\n\n queries.forEach((options) => {\n const defaultedOptions = this.#client.defaultQueryOptions(options)\n const match = prevObserversMap.get(defaultedOptions.queryHash)?.shift()\n const observer =\n match ?? new QueryObserver(this.#client, defaultedOptions)\n\n observers.push({\n defaultedQueryOptions: defaultedOptions,\n observer,\n })\n })\n\n return observers\n }\n\n #onUpdate(observer: QueryObserver, result: QueryObserverResult): void {\n const index = this.#observers.indexOf(observer)\n if (index !== -1) {\n this.#result = this.#result.slice()\n this.#result[index] = result\n this.#notify()\n }\n }\n\n #notify(): void {\n if (this.hasListeners()) {\n const shouldSkipCombine = this.#shouldSkipCombine()\n const previousResult = this.#combinedResult\n const newResult = shouldSkipCombine\n ? previousResult\n : this.#combineResult(\n this.#trackResult(this.#result, this.#observerMatches),\n this.#options?.combine,\n )\n\n if (shouldSkipCombine || previousResult !== newResult) {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(this.#result)\n })\n })\n }\n }\n }\n}\n\ntype QueryObserverMatch = {\n defaultedQueryOptions: DefaultedQueryObserverOptions\n observer: QueryObserver\n}\n"],"mappings":";;;;;;;;;;;AAiBA,SAAS,WAAc,QAAkB,QAA4B;CACnE,MAAM,aAAa,IAAI,IAAI,MAAM;CACjC,OAAO,OAAO,QAAQ,MAAM,CAAC,WAAW,IAAI,CAAC,CAAC;AAChD;;;;;;;;;;;;;;;;;;;AA2CA,IAAa,kBAAb,cAEU,aAAsC;CAC9C;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA,mBAA8C,CAAC;CAE/C,YACE,QACA,SACA,SACA;EACA,MAAM;EAEN,KAAK,UAAU;EACf,KAAK,WAAW;EAChB,KAAK,WAAW,CAAC;EACjB,KAAK,aAAa,CAAC;EACnB,KAAK,UAAU,CAAC;EAEhB,KAAK,WAAW,OAAO;CACzB;CAEA,cAAuC;EACrC,IAAI,KAAK,UAAU,SAAS,GAC1B,KAAK,WAAW,SAAS,aAAa;GACpC,SAAS,WAAW,WAAW;IAC7B,KAAK,UAAU,UAAU,MAAM;GACjC,CAAC;EACH,CAAC;CAEL;CAEA,gBAAyC;EACvC,IAAI,CAAC,KAAK,UAAU,MAClB,KAAK,QAAQ;CAEjB;;;;;CAMA,UAAgB;EACd,KAAK,4BAAY,IAAI,IAAI;EACzB,KAAK,WAAW,SAAS,aAAa;GACpC,SAAS,QAAQ;EACnB,CAAC;CACH;;;;;;;;;;;;;;;;CAiBA,WACE,SACA,SACM;EACN,KAAK,WAAW;EAChB,KAAK,WAAW;EAEhB,IAAI,QAAQ,IAAI,aAAa,cAAc;GACzC,MAAM,cAAc,QAAQ,KACzB,UAAU,KAAK,QAAQ,oBAAoB,KAAK,CAAC,CAAC,SACrD;GACA,IAAI,IAAI,IAAI,WAAW,CAAC,CAAC,SAAS,YAAY,QAC5C,QAAQ,KACN,uFACF;EAEJ;EAEA,cAAc,YAAY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;CAEA,uBACE,SAC2B;EAC3B,MAAM,mCAAmB,IAAI,IAAkC;EAE/D,KAAK,WAAW,SAAS,aAAa;GACpC,MAAM,MAAM,SAAS,QAAQ;GAC7B,IAAI,CAAC,KAAK;GAEV,MAAM,oBAAoB,iBAAiB,IAAI,GAAG;GAElD,IAAI,mBACF,kBAAkB,KAAK,QAAQ;QAE/B,iBAAiB,IAAI,KAAK,CAAC,QAAQ,CAAC;EAExC,CAAC;EAED,MAAM,YAAuC,CAAC;EAE9C,QAAQ,SAAS,YAAY;GAC3B,MAAM,mBAAmB,KAAK,QAAQ,oBAAoB,OAAO;GAEjE,MAAM,WADQ,iBAAiB,IAAI,iBAAiB,SAAS,CAAC,EAAE,MAAM,KAE3D,IAAI,cAAc,KAAK,SAAS,gBAAgB;GAE3D,UAAU,KAAK;IACb,uBAAuB;IACvB;GACF,CAAC;EACH,CAAC;EAED,OAAO;CACT;CAEA,UAAU,UAAyB,QAAmC;EACpE,MAAM,QAAQ,KAAK,WAAW,QAAQ,QAAQ;EAC9C,IAAI,UAAU,IAAI;GAChB,KAAK,UAAU,KAAK,QAAQ,MAAM;GAClC,KAAK,QAAQ,SAAS;GACtB,KAAK,QAAQ;EACf;CACF;CAEA,UAAgB;EACd,IAAI,KAAK,aAAa,GAAG;GACvB,MAAM,oBAAoB,KAAK,mBAAmB;GAClD,MAAM,iBAAiB,KAAK;GAC5B,MAAM,YAAY,oBACd,iBACA,KAAK,eACH,KAAK,aAAa,KAAK,SAAS,KAAK,gBAAgB,GACrD,KAAK,UAAU,OACjB;GAEJ,IAAI,qBAAqB,mBAAmB,WAC1C,cAAc,YAAY;IACxB,KAAK,UAAU,SAAS,aAAa;KACnC,SAAS,KAAK,OAAO;IACvB,CAAC;GACH,CAAC;EAEL;CACF;AACF"}
|
package/build/modern/query.cjs
CHANGED
|
@@ -13,7 +13,6 @@ const require_infiniteQueryBehavior = require("./infiniteQueryBehavior.cjs");
|
|
|
13
13
|
* code typically interacts with queries indirectly through `QueryClient` or
|
|
14
14
|
* a framework hook like `useQuery`. Direct access to a `Query` instance is
|
|
15
15
|
* possible via `queryCache.find()`/`findAll()` for inspecting cache state.
|
|
16
|
-
*
|
|
17
16
|
* @example
|
|
18
17
|
* ```ts
|
|
19
18
|
* const queryCache = queryClient.getQueryCache()
|
|
@@ -49,22 +48,33 @@ var Query = class extends require_removable.Removable {
|
|
|
49
48
|
}
|
|
50
49
|
/**
|
|
51
50
|
* The `meta` object passed in the query's options, if any.
|
|
51
|
+
* @returns The query's `meta`, or `undefined` if none was set.
|
|
52
52
|
*/
|
|
53
53
|
get meta() {
|
|
54
54
|
return this.options.meta;
|
|
55
55
|
}
|
|
56
|
-
/**
|
|
56
|
+
/**
|
|
57
|
+
* `'infinite'` for an infinite query, otherwise `undefined`.
|
|
58
|
+
* @internal
|
|
59
|
+
* @returns The type of the query, set from the `_type` option (e.g. `'infinite'`).
|
|
60
|
+
*/
|
|
57
61
|
get queryType() {
|
|
58
62
|
return this.#queryType;
|
|
59
63
|
}
|
|
60
64
|
/**
|
|
61
65
|
* The promise for the currently in-flight fetch, if the query is fetching.
|
|
62
66
|
* `undefined` when the query is not fetching.
|
|
67
|
+
* @returns The promise of the in-flight fetch, or `undefined`.
|
|
63
68
|
*/
|
|
64
69
|
get promise() {
|
|
65
70
|
return this.#retryer?.promise;
|
|
66
71
|
}
|
|
67
|
-
/**
|
|
72
|
+
/**
|
|
73
|
+
* Replaces the query's options and applies their `gcTime`. If the query has no data yet, it gets
|
|
74
|
+
* the `initialData` from the new options, if any.
|
|
75
|
+
* @internal
|
|
76
|
+
* @param options - The new query options, merged on top of the query's default options.
|
|
77
|
+
*/
|
|
68
78
|
setOptions(options) {
|
|
69
79
|
this.options = {
|
|
70
80
|
...this.#defaultOptions,
|
|
@@ -83,7 +93,14 @@ var Query = class extends require_removable.Removable {
|
|
|
83
93
|
optionalRemove() {
|
|
84
94
|
if (!this.observers.length && this.state.fetchStatus === "idle") this.#cache.remove(this);
|
|
85
95
|
}
|
|
86
|
-
/**
|
|
96
|
+
/**
|
|
97
|
+
* Writes new data to the query and marks it as successfully fetched.
|
|
98
|
+
* @internal
|
|
99
|
+
* @param newData - The data to write. Structural sharing with the current data is applied.
|
|
100
|
+
* @param options - Set `updatedAt` to override the timestamp, and `manual` to mark the write as
|
|
101
|
+
* manual (e.g. from `queryClient.setQueryData`).
|
|
102
|
+
* @returns The data that was written.
|
|
103
|
+
*/
|
|
87
104
|
setData(newData, options) {
|
|
88
105
|
const data = require_utils.replaceData(this.state.data, newData, this.options);
|
|
89
106
|
this.#dispatch({
|
|
@@ -98,6 +115,7 @@ var Query = class extends require_removable.Removable {
|
|
|
98
115
|
* Merges the given partial state directly into this query's state, notifying observers. Used
|
|
99
116
|
* by persistence and broadcast plugins to restore a state snapshot, and by devtools to let a
|
|
100
117
|
* user manually trigger a loading/error state or edit the cached data.
|
|
118
|
+
* @param state - The partial state to merge into the query's state.
|
|
101
119
|
*/
|
|
102
120
|
setState(state) {
|
|
103
121
|
this.#dispatch({
|
|
@@ -109,7 +127,9 @@ var Query = class extends require_removable.Removable {
|
|
|
109
127
|
* Cancels the query's currently in-flight fetch, if any.
|
|
110
128
|
* - Returns a promise that resolves once the cancellation has settled.
|
|
111
129
|
* - If no fetch is in progress, resolves immediately.
|
|
112
|
-
*
|
|
130
|
+
* @param options - Set `revert` to restore the state from before the fetch started, and `silent`
|
|
131
|
+
* to suppress the cancellation error when a new fetch replaces the cancelled one.
|
|
132
|
+
* @returns A promise that resolves once the cancellation has settled.
|
|
113
133
|
* @example
|
|
114
134
|
* ```ts
|
|
115
135
|
* await query.cancel()
|
|
@@ -124,14 +144,17 @@ var Query = class extends require_removable.Removable {
|
|
|
124
144
|
* Clears the query's garbage collection timeout and silently cancels any
|
|
125
145
|
* in-flight fetch. Called by `QueryCache` when the query is removed from
|
|
126
146
|
* the cache.
|
|
127
|
-
*
|
|
128
147
|
* @see {@link Query#cancel}
|
|
129
148
|
*/
|
|
130
149
|
destroy() {
|
|
131
150
|
super.destroy();
|
|
132
151
|
this.cancel({ silent: true });
|
|
133
152
|
}
|
|
134
|
-
/**
|
|
153
|
+
/**
|
|
154
|
+
* The state the query was created with.
|
|
155
|
+
* @internal
|
|
156
|
+
* @returns The state the query had when it was first created, used by {@link Query#reset}.
|
|
157
|
+
*/
|
|
135
158
|
get resetState() {
|
|
136
159
|
return this.#initialState;
|
|
137
160
|
}
|
|
@@ -147,6 +170,7 @@ var Query = class extends require_removable.Removable {
|
|
|
147
170
|
/**
|
|
148
171
|
* Returns `true` if the query has at least one observer for which `enabled`
|
|
149
172
|
* does not resolve to `false`.
|
|
173
|
+
* @returns `true` if the query has an enabled observer.
|
|
150
174
|
*/
|
|
151
175
|
isActive() {
|
|
152
176
|
return this.observers.some((observer) => require_utils.resolveQueryValue(observer.options.enabled, this) !== false);
|
|
@@ -158,6 +182,7 @@ var Query = class extends require_removable.Removable {
|
|
|
158
182
|
* (see `isActive`).
|
|
159
183
|
* - If the query has no observers, it is disabled when its `queryFn` is
|
|
160
184
|
* `skipToken` or it has never been fetched.
|
|
185
|
+
* @returns `true` if the query is disabled.
|
|
161
186
|
*/
|
|
162
187
|
isDisabled() {
|
|
163
188
|
if (this.getObserversCount() > 0) return !this.isActive();
|
|
@@ -166,6 +191,7 @@ var Query = class extends require_removable.Removable {
|
|
|
166
191
|
/**
|
|
167
192
|
* Returns `true` if the query has been fetched, i.e. it has resolved with
|
|
168
193
|
* either data or an error at least once.
|
|
194
|
+
* @returns `true` if the query has been fetched.
|
|
169
195
|
*/
|
|
170
196
|
isFetched() {
|
|
171
197
|
return this.state.dataUpdateCount + this.state.errorUpdateCount > 0;
|
|
@@ -173,6 +199,7 @@ var Query = class extends require_removable.Removable {
|
|
|
173
199
|
/**
|
|
174
200
|
* Returns `true` if the query has at least one observer configured with
|
|
175
201
|
* `staleTime: 'static'`, meaning it is treated as never stale.
|
|
202
|
+
* @returns `true` if the query is static.
|
|
176
203
|
*/
|
|
177
204
|
isStatic() {
|
|
178
205
|
if (this.getObserversCount() > 0) return this.observers.some((observer) => require_utils.resolveQueryValue(observer.options.staleTime, this) === "static");
|
|
@@ -185,7 +212,7 @@ var Query = class extends require_removable.Removable {
|
|
|
185
212
|
* `staleTime` and `enabled` state).
|
|
186
213
|
* - If the query has no observers, it is considered stale when it has no
|
|
187
214
|
* data or has been invalidated.
|
|
188
|
-
*
|
|
215
|
+
* @returns `true` if the query is stale.
|
|
189
216
|
* @see {@link Query#isStaleByTime}
|
|
190
217
|
* @example
|
|
191
218
|
* ```ts
|
|
@@ -205,7 +232,9 @@ var Query = class extends require_removable.Removable {
|
|
|
205
232
|
* - `staleTime: 'static'` is never stale.
|
|
206
233
|
* - An invalidated query is always stale.
|
|
207
234
|
* - Otherwise, staleness is based on elapsed time since `dataUpdatedAt`.
|
|
208
|
-
*
|
|
235
|
+
* @param staleTime - The time, in milliseconds, after which data is considered stale, or
|
|
236
|
+
* `'static'` to never treat existing data as stale. A query without data is stale either way.
|
|
237
|
+
* @returns `true` if the query's data is stale.
|
|
209
238
|
* @see {@link Query#isStale}
|
|
210
239
|
* @example
|
|
211
240
|
* ```ts
|
|
@@ -218,17 +247,30 @@ var Query = class extends require_removable.Removable {
|
|
|
218
247
|
if (this.state.isInvalidated) return true;
|
|
219
248
|
return !require_utils.timeUntilStale(this.state.dataUpdatedAt, staleTime);
|
|
220
249
|
}
|
|
221
|
-
/**
|
|
250
|
+
/**
|
|
251
|
+
* Refetches the query when the window regains focus, if an observer wants that, and continues a
|
|
252
|
+
* paused fetch.
|
|
253
|
+
* @internal
|
|
254
|
+
*/
|
|
222
255
|
onFocus() {
|
|
223
256
|
this.observers.find((x) => x.shouldFetchOnWindowFocus())?.refetch({ cancelRefetch: false });
|
|
224
257
|
this.#retryer?.continue();
|
|
225
258
|
}
|
|
226
|
-
/**
|
|
259
|
+
/**
|
|
260
|
+
* Refetches the query when the app reconnects, if an observer wants that, and continues a paused
|
|
261
|
+
* fetch.
|
|
262
|
+
* @internal
|
|
263
|
+
*/
|
|
227
264
|
onOnline() {
|
|
228
265
|
this.observers.find((x) => x.shouldFetchOnReconnect())?.refetch({ cancelRefetch: false });
|
|
229
266
|
this.#retryer?.continue();
|
|
230
267
|
}
|
|
231
|
-
/**
|
|
268
|
+
/**
|
|
269
|
+
* Subscribes an observer to the query and stops its garbage collection.
|
|
270
|
+
* @internal
|
|
271
|
+
* @param observer - The observer to add. Adding an observer that is already subscribed does
|
|
272
|
+
* nothing.
|
|
273
|
+
*/
|
|
232
274
|
addObserver(observer) {
|
|
233
275
|
if (!this.observers.includes(observer)) {
|
|
234
276
|
this.observers.push(observer);
|
|
@@ -240,7 +282,13 @@ var Query = class extends require_removable.Removable {
|
|
|
240
282
|
});
|
|
241
283
|
}
|
|
242
284
|
}
|
|
243
|
-
/**
|
|
285
|
+
/**
|
|
286
|
+
* Unsubscribes an observer from the query.
|
|
287
|
+
* @internal
|
|
288
|
+
* @param observer - The observer to remove. When the last observer is removed, the in-flight
|
|
289
|
+
* fetch is cancelled if its abort signal was consumed (otherwise only its retries are stopped),
|
|
290
|
+
* and garbage collection is scheduled.
|
|
291
|
+
*/
|
|
244
292
|
removeObserver(observer) {
|
|
245
293
|
const index = this.observers.indexOf(observer);
|
|
246
294
|
if (index !== -1) {
|
|
@@ -261,7 +309,7 @@ var Query = class extends require_removable.Removable {
|
|
|
261
309
|
}
|
|
262
310
|
/**
|
|
263
311
|
* Returns the number of observers currently subscribed to this query.
|
|
264
|
-
*
|
|
312
|
+
* @returns The number of observers.
|
|
265
313
|
* @example
|
|
266
314
|
* ```ts
|
|
267
315
|
* if (query.getObserversCount() === 0) {
|
|
@@ -276,7 +324,6 @@ var Query = class extends require_removable.Removable {
|
|
|
276
324
|
* Marks the query as invalidated, unless it is already invalidated. This
|
|
277
325
|
* updates `state.isInvalidated` and notifies observers, but does not by
|
|
278
326
|
* itself trigger a refetch.
|
|
279
|
-
*
|
|
280
327
|
* @example
|
|
281
328
|
* ```ts
|
|
282
329
|
* query.invalidate()
|
|
@@ -294,6 +341,13 @@ var Query = class extends require_removable.Removable {
|
|
|
294
341
|
* cancelled first.
|
|
295
342
|
* - If `options` is passed, it replaces the query's current options
|
|
296
343
|
* before fetching.
|
|
344
|
+
* @param options - Query options that replace the query's current options before fetching. They
|
|
345
|
+
* are not applied when an in-flight fetch is reused.
|
|
346
|
+
* @param fetchOptions - Set `cancelRefetch` to cancel an in-flight fetch first (only if the query
|
|
347
|
+
* already has data), and `meta` to pass extra information to the query's behavior.
|
|
348
|
+
* @returns A promise that resolves with the fetched data, or rejects with the fetch error. If the
|
|
349
|
+
* fetch is cancelled with `revert` while the query has data, it resolves with the restored data
|
|
350
|
+
* instead.
|
|
297
351
|
*/
|
|
298
352
|
async fetch(options, fetchOptions) {
|
|
299
353
|
if (this.state.fetchStatus !== "idle" && this.#retryer?.status() !== "rejected") {
|
|
@@ -484,6 +538,14 @@ var Query = class extends require_removable.Removable {
|
|
|
484
538
|
});
|
|
485
539
|
}
|
|
486
540
|
};
|
|
541
|
+
/**
|
|
542
|
+
* Returns the state changes applied when a query starts fetching.
|
|
543
|
+
* @param data - The query's current data. If it is `undefined`, the query is also reset to
|
|
544
|
+
* `'pending'` with no error.
|
|
545
|
+
* @param options - The query options, whose `networkMode` decides whether the query is `'fetching'`
|
|
546
|
+
* or `'paused'`.
|
|
547
|
+
* @returns The partial query state for a starting fetch.
|
|
548
|
+
*/
|
|
487
549
|
function fetchState(data, options) {
|
|
488
550
|
return {
|
|
489
551
|
fetchFailureCount: 0,
|
|
@@ -495,6 +557,12 @@ function fetchState(data, options) {
|
|
|
495
557
|
}
|
|
496
558
|
};
|
|
497
559
|
}
|
|
560
|
+
/**
|
|
561
|
+
* Returns the state changes applied when a query receives data.
|
|
562
|
+
* @param data - The new data.
|
|
563
|
+
* @param dataUpdatedAt - The timestamp the data is recorded with. Defaults to the current time.
|
|
564
|
+
* @returns The partial query state for a successful update.
|
|
565
|
+
*/
|
|
498
566
|
function successState(data, dataUpdatedAt) {
|
|
499
567
|
return {
|
|
500
568
|
data,
|
|
@@ -504,6 +572,12 @@ function successState(data, dataUpdatedAt) {
|
|
|
504
572
|
status: "success"
|
|
505
573
|
};
|
|
506
574
|
}
|
|
575
|
+
/**
|
|
576
|
+
* Returns the initial state of a query, based on its `initialData` and `initialDataUpdatedAt`
|
|
577
|
+
* options.
|
|
578
|
+
* @param options - The query options.
|
|
579
|
+
* @returns The initial query state: `'success'` if `initialData` is set, otherwise `'pending'`.
|
|
580
|
+
*/
|
|
507
581
|
function getDefaultState(options) {
|
|
508
582
|
const data = typeof options.initialData === "function" ? options.initialData() : options.initialData;
|
|
509
583
|
const hasData = data !== void 0;
|