@tanstack/query-core 5.0.0-alpha.5 → 5.0.0-alpha.51
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/lib/{focusManager.esm.js → focusManager.cjs} +9 -6
- package/build/lib/{focusManager.mjs.map → focusManager.cjs.map} +1 -1
- package/build/lib/focusManager.d.ts +2 -1
- package/build/lib/focusManager.d.ts.map +1 -0
- package/build/lib/focusManager.js +5 -8
- package/build/lib/focusManager.js.map +1 -1
- package/build/lib/{hydration.mjs → hydration.cjs} +11 -20
- package/build/lib/hydration.cjs.map +1 -0
- package/build/lib/hydration.d.ts +3 -6
- package/build/lib/hydration.d.ts.map +1 -0
- package/build/lib/hydration.js +5 -24
- package/build/lib/hydration.js.map +1 -1
- package/build/lib/index.cjs +40 -0
- package/build/lib/index.cjs.map +1 -0
- package/build/lib/index.d.ts +5 -3
- package/build/lib/index.d.ts.map +1 -0
- package/build/lib/index.js +13 -39
- package/build/lib/index.js.map +1 -1
- package/build/lib/{infiniteQueryBehavior.esm.js → infiniteQueryBehavior.cjs} +9 -4
- package/build/lib/infiniteQueryBehavior.cjs.map +1 -0
- package/build/lib/infiniteQueryBehavior.d.ts +1 -0
- package/build/lib/infiniteQueryBehavior.d.ts.map +1 -0
- package/build/lib/infiniteQueryBehavior.js +4 -7
- package/build/lib/infiniteQueryBehavior.js.map +1 -1
- package/build/lib/{infiniteQueryObserver.esm.js → infiniteQueryObserver.cjs} +11 -9
- package/build/lib/{infiniteQueryObserver.mjs.map → infiniteQueryObserver.cjs.map} +1 -1
- package/build/lib/infiniteQueryObserver.d.ts +2 -1
- package/build/lib/infiniteQueryObserver.d.ts.map +1 -0
- package/build/lib/infiniteQueryObserver.js +8 -10
- package/build/lib/infiniteQueryObserver.js.map +1 -1
- package/build/lib/{mutation.esm.js → mutation.cjs} +12 -9
- package/build/lib/{mutation.mjs.map → mutation.cjs.map} +1 -1
- package/build/lib/mutation.d.ts +2 -1
- package/build/lib/mutation.d.ts.map +1 -0
- package/build/lib/mutation.js +8 -11
- package/build/lib/mutation.js.map +1 -1
- package/build/lib/{mutationCache.esm.js → mutationCache.cjs} +21 -18
- package/build/lib/mutationCache.cjs.map +1 -0
- package/build/lib/mutationCache.d.ts +3 -2
- package/build/lib/mutationCache.d.ts.map +1 -0
- package/build/lib/mutationCache.js +18 -19
- package/build/lib/mutationCache.js.map +1 -1
- package/build/lib/{mutationObserver.esm.js → mutationObserver.cjs} +13 -11
- package/build/lib/mutationObserver.cjs.map +1 -0
- package/build/lib/mutationObserver.d.ts +2 -1
- package/build/lib/mutationObserver.d.ts.map +1 -0
- package/build/lib/mutationObserver.js +10 -12
- package/build/lib/mutationObserver.js.map +1 -1
- package/build/lib/{notifyManager.esm.js → notifyManager.cjs} +8 -5
- package/build/lib/{notifyManager.mjs.map → notifyManager.cjs.map} +1 -1
- package/build/lib/notifyManager.d.ts +4 -3
- package/build/lib/notifyManager.d.ts.map +1 -0
- package/build/lib/notifyManager.js +4 -7
- package/build/lib/notifyManager.js.map +1 -1
- package/build/lib/{onlineManager.esm.js → onlineManager.cjs} +9 -6
- package/build/lib/{onlineManager.mjs.map → onlineManager.cjs.map} +1 -1
- package/build/lib/onlineManager.d.ts +2 -1
- package/build/lib/onlineManager.d.ts.map +1 -0
- package/build/lib/onlineManager.js +5 -8
- package/build/lib/onlineManager.js.map +1 -1
- package/build/lib/{queriesObserver.esm.js → queriesObserver.cjs} +50 -25
- package/build/lib/queriesObserver.cjs.map +1 -0
- package/build/lib/queriesObserver.d.ts +14 -6
- package/build/lib/queriesObserver.d.ts.map +1 -0
- package/build/lib/queriesObserver.js +47 -26
- package/build/lib/queriesObserver.js.map +1 -1
- package/build/lib/{query.mjs → query.cjs} +20 -18
- package/build/lib/query.cjs.map +1 -0
- package/build/lib/query.d.ts +6 -4
- package/build/lib/query.d.ts.map +1 -0
- package/build/lib/query.js +17 -19
- package/build/lib/query.js.map +1 -1
- package/build/lib/{queryCache.mjs → queryCache.cjs} +28 -23
- package/build/lib/queryCache.cjs.map +1 -0
- package/build/lib/queryCache.d.ts +3 -3
- package/build/lib/queryCache.d.ts.map +1 -0
- package/build/lib/queryCache.js +24 -23
- package/build/lib/queryCache.js.map +1 -1
- package/build/lib/{queryClient.esm.js → queryClient.cjs} +50 -42
- package/build/lib/queryClient.cjs.map +1 -0
- package/build/lib/queryClient.d.ts +2 -2
- package/build/lib/queryClient.d.ts.map +1 -0
- package/build/lib/queryClient.js +46 -42
- package/build/lib/queryClient.js.map +1 -1
- package/build/lib/{queryObserver.mjs → queryObserver.cjs} +36 -46
- package/build/lib/queryObserver.cjs.map +1 -0
- package/build/lib/queryObserver.d.ts +5 -7
- package/build/lib/queryObserver.d.ts.map +1 -0
- package/build/lib/queryObserver.js +33 -47
- package/build/lib/queryObserver.js.map +1 -1
- package/build/lib/{removable.esm.js → removable.cjs} +7 -5
- package/build/lib/{removable.mjs.map → removable.cjs.map} +1 -1
- package/build/lib/removable.d.ts +1 -0
- package/build/lib/removable.d.ts.map +1 -0
- package/build/lib/removable.js +4 -6
- package/build/lib/removable.js.map +1 -1
- package/build/lib/{retryer.esm.js → retryer.cjs} +15 -8
- package/build/lib/{retryer.mjs.map → retryer.cjs.map} +1 -1
- package/build/lib/retryer.d.ts +5 -4
- package/build/lib/retryer.d.ts.map +1 -0
- package/build/lib/retryer.js +8 -11
- package/build/lib/retryer.js.map +1 -1
- package/build/lib/{subscribable.esm.js → subscribable.cjs} +8 -6
- package/build/lib/subscribable.cjs.map +1 -0
- package/build/lib/subscribable.d.ts +3 -2
- package/build/lib/subscribable.d.ts.map +1 -0
- package/build/lib/subscribable.js +5 -7
- package/build/lib/subscribable.js.map +1 -1
- package/build/lib/tests/focusManager.test.d.ts +1 -0
- package/build/lib/tests/focusManager.test.d.ts.map +1 -0
- package/build/lib/tests/hydration.test.d.ts +1 -0
- package/build/lib/tests/hydration.test.d.ts.map +1 -0
- package/build/lib/tests/infiniteQueryBehavior.test.d.ts +1 -0
- package/build/lib/tests/infiniteQueryBehavior.test.d.ts.map +1 -0
- package/build/lib/tests/infiniteQueryObserver.test.d.ts +1 -0
- package/build/lib/tests/infiniteQueryObserver.test.d.ts.map +1 -0
- package/build/lib/tests/mutationCache.test.d.ts +1 -0
- package/build/lib/tests/mutationCache.test.d.ts.map +1 -0
- package/build/lib/tests/mutationObserver.test.d.ts +1 -0
- package/build/lib/tests/mutationObserver.test.d.ts.map +1 -0
- package/build/lib/tests/mutations.test.d.ts +1 -0
- package/build/lib/tests/mutations.test.d.ts.map +1 -0
- package/build/lib/tests/notifyManager.test.d.ts +1 -0
- package/build/lib/tests/notifyManager.test.d.ts.map +1 -0
- package/build/lib/tests/onlineManager.test.d.ts +1 -0
- package/build/lib/tests/onlineManager.test.d.ts.map +1 -0
- package/build/lib/tests/queriesObserver.test.d.ts +1 -0
- package/build/lib/tests/queriesObserver.test.d.ts.map +1 -0
- package/build/lib/tests/query.test.d.ts +1 -0
- package/build/lib/tests/query.test.d.ts.map +1 -0
- package/build/lib/tests/queryCache.test.d.ts +1 -0
- package/build/lib/tests/queryCache.test.d.ts.map +1 -0
- package/build/lib/tests/queryClient.test.d.ts +1 -0
- package/build/lib/tests/queryClient.test.d.ts.map +1 -0
- package/build/lib/tests/queryObserver.test.d.ts +1 -0
- package/build/lib/tests/queryObserver.test.d.ts.map +1 -0
- package/build/lib/tests/utils.d.ts +3 -2
- package/build/lib/tests/utils.d.ts.map +1 -0
- package/build/lib/tests/utils.test.d.ts +1 -0
- package/build/lib/tests/utils.test.d.ts.map +1 -0
- package/build/lib/types.d.ts +35 -45
- package/build/lib/types.d.ts.map +1 -0
- package/build/lib/{utils.esm.js → utils.cjs} +23 -2
- package/build/lib/{utils.mjs.map → utils.cjs.map} +1 -1
- package/build/lib/utils.d.ts +4 -3
- package/build/lib/utils.d.ts.map +1 -0
- package/build/lib/utils.js +1 -22
- package/build/lib/utils.js.map +1 -1
- package/package.json +12 -9
- package/src/hydration.ts +18 -37
- package/src/index.ts +3 -4
- package/src/infiniteQueryBehavior.ts +1 -0
- package/src/mutationCache.ts +4 -4
- package/src/mutationObserver.ts +1 -1
- package/src/queriesObserver.ts +74 -24
- package/src/query.ts +7 -5
- package/src/queryCache.ts +5 -8
- package/src/queryClient.ts +20 -20
- package/src/queryObserver.ts +43 -51
- package/src/subscribable.ts +5 -5
- package/src/tests/hydration.test.tsx +7 -3
- package/src/tests/infiniteQueryBehavior.test.tsx +9 -0
- package/src/tests/query.test.tsx +1 -1
- package/src/tests/queryCache.test.tsx +1 -19
- package/src/tests/queryClient.test.tsx +39 -3
- package/src/tests/queryObserver.test.tsx +121 -0
- package/src/types.ts +15 -20
- package/build/lib/focusManager.esm.js.map +0 -1
- package/build/lib/focusManager.mjs +0 -71
- package/build/lib/hydration.esm.js +0 -98
- package/build/lib/hydration.esm.js.map +0 -1
- package/build/lib/hydration.mjs.map +0 -1
- package/build/lib/index.esm.js +0 -14
- package/build/lib/index.esm.js.map +0 -1
- package/build/lib/index.mjs +0 -14
- package/build/lib/index.mjs.map +0 -1
- package/build/lib/infiniteQueryBehavior.esm.js.map +0 -1
- package/build/lib/infiniteQueryBehavior.mjs +0 -125
- package/build/lib/infiniteQueryBehavior.mjs.map +0 -1
- package/build/lib/infiniteQueryObserver.esm.js.map +0 -1
- package/build/lib/infiniteQueryObserver.mjs +0 -75
- package/build/lib/mutation.esm.js.map +0 -1
- package/build/lib/mutation.mjs +0 -234
- package/build/lib/mutationCache.esm.js.map +0 -1
- package/build/lib/mutationCache.mjs +0 -82
- package/build/lib/mutationCache.mjs.map +0 -1
- package/build/lib/mutationObserver.esm.js.map +0 -1
- package/build/lib/mutationObserver.mjs +0 -96
- package/build/lib/mutationObserver.mjs.map +0 -1
- package/build/lib/notifyManager.esm.js.map +0 -1
- package/build/lib/notifyManager.mjs +0 -89
- package/build/lib/onlineManager.esm.js.map +0 -1
- package/build/lib/onlineManager.mjs +0 -73
- package/build/lib/queriesObserver.esm.js.map +0 -1
- package/build/lib/queriesObserver.mjs +0 -136
- package/build/lib/queriesObserver.mjs.map +0 -1
- package/build/lib/query.esm.js +0 -420
- package/build/lib/query.esm.js.map +0 -1
- package/build/lib/query.mjs.map +0 -1
- package/build/lib/queryCache.esm.js +0 -101
- package/build/lib/queryCache.esm.js.map +0 -1
- package/build/lib/queryCache.mjs.map +0 -1
- package/build/lib/queryClient.esm.js.map +0 -1
- package/build/lib/queryClient.mjs +0 -277
- package/build/lib/queryClient.mjs.map +0 -1
- package/build/lib/queryObserver.esm.js +0 -440
- package/build/lib/queryObserver.esm.js.map +0 -1
- package/build/lib/queryObserver.mjs.map +0 -1
- package/build/lib/removable.esm.js.map +0 -1
- package/build/lib/removable.mjs +0 -29
- package/build/lib/retryer.esm.js.map +0 -1
- package/build/lib/retryer.mjs +0 -150
- package/build/lib/subscribable.esm.js.map +0 -1
- package/build/lib/subscribable.mjs +0 -26
- package/build/lib/subscribable.mjs.map +0 -1
- package/build/lib/utils.esm.js.map +0 -1
- package/build/lib/utils.mjs +0 -227
- package/build/umd/index.development.js +0 -2682
- package/build/umd/index.development.js.map +0 -1
- package/build/umd/index.production.js +0 -2
- package/build/umd/index.production.js.map +0 -1
package/src/hydration.ts
CHANGED
|
@@ -11,10 +11,8 @@ import type { Mutation, MutationState } from './mutation'
|
|
|
11
11
|
// TYPES
|
|
12
12
|
|
|
13
13
|
export interface DehydrateOptions {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
shouldDehydrateMutation?: ShouldDehydrateMutationFunction
|
|
17
|
-
shouldDehydrateQuery?: ShouldDehydrateQueryFunction
|
|
14
|
+
shouldDehydrateMutation?: (mutation: Mutation) => boolean
|
|
15
|
+
shouldDehydrateQuery?: (query: Query) => boolean
|
|
18
16
|
}
|
|
19
17
|
|
|
20
18
|
export interface HydrateOptions {
|
|
@@ -40,10 +38,6 @@ export interface DehydratedState {
|
|
|
40
38
|
queries: DehydratedQuery[]
|
|
41
39
|
}
|
|
42
40
|
|
|
43
|
-
export type ShouldDehydrateQueryFunction = (query: Query) => boolean
|
|
44
|
-
|
|
45
|
-
export type ShouldDehydrateMutationFunction = (mutation: Mutation) => boolean
|
|
46
|
-
|
|
47
41
|
// FUNCTIONS
|
|
48
42
|
|
|
49
43
|
function dehydrateMutation(mutation: Mutation): DehydratedMutation {
|
|
@@ -77,36 +71,23 @@ export function dehydrate(
|
|
|
77
71
|
client: QueryClient,
|
|
78
72
|
options: DehydrateOptions = {},
|
|
79
73
|
): DehydratedState {
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
.getAll()
|
|
90
|
-
.forEach((mutation) => {
|
|
91
|
-
if (shouldDehydrateMutation(mutation)) {
|
|
92
|
-
mutations.push(dehydrateMutation(mutation))
|
|
93
|
-
}
|
|
94
|
-
})
|
|
95
|
-
}
|
|
74
|
+
const filterMutation =
|
|
75
|
+
options.shouldDehydrateMutation ?? defaultShouldDehydrateMutation
|
|
76
|
+
|
|
77
|
+
const mutations = client
|
|
78
|
+
.getMutationCache()
|
|
79
|
+
.getAll()
|
|
80
|
+
.flatMap((mutation) =>
|
|
81
|
+
filterMutation(mutation) ? [dehydrateMutation(mutation)] : [],
|
|
82
|
+
)
|
|
96
83
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
.forEach((query) => {
|
|
105
|
-
if (shouldDehydrateQuery(query)) {
|
|
106
|
-
queries.push(dehydrateQuery(query))
|
|
107
|
-
}
|
|
108
|
-
})
|
|
109
|
-
}
|
|
84
|
+
const filterQuery =
|
|
85
|
+
options.shouldDehydrateQuery ?? defaultShouldDehydrateQuery
|
|
86
|
+
|
|
87
|
+
const queries = client
|
|
88
|
+
.getQueryCache()
|
|
89
|
+
.getAll()
|
|
90
|
+
.flatMap((query) => (filterQuery(query) ? [dehydrateQuery(query)] : []))
|
|
110
91
|
|
|
111
92
|
return { mutations, queries }
|
|
112
93
|
}
|
package/src/index.ts
CHANGED
|
@@ -23,18 +23,17 @@ export { isCancelledError } from './retryer'
|
|
|
23
23
|
export {
|
|
24
24
|
dehydrate,
|
|
25
25
|
hydrate,
|
|
26
|
-
defaultShouldDehydrateMutation,
|
|
27
26
|
defaultShouldDehydrateQuery,
|
|
27
|
+
defaultShouldDehydrateMutation,
|
|
28
28
|
} from './hydration'
|
|
29
29
|
|
|
30
30
|
// Types
|
|
31
31
|
export * from './types'
|
|
32
32
|
export type { Query, QueryState } from './query'
|
|
33
|
-
export type { Mutation } from './mutation'
|
|
33
|
+
export type { Mutation, MutationState } from './mutation'
|
|
34
34
|
export type {
|
|
35
35
|
DehydrateOptions,
|
|
36
36
|
DehydratedState,
|
|
37
37
|
HydrateOptions,
|
|
38
|
-
ShouldDehydrateMutationFunction,
|
|
39
|
-
ShouldDehydrateQueryFunction,
|
|
40
38
|
} from './hydration'
|
|
39
|
+
export type { QueriesObserverOptions } from './queriesObserver'
|
package/src/mutationCache.ts
CHANGED
|
@@ -139,11 +139,11 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
|
|
|
139
139
|
>(
|
|
140
140
|
filters: MutationFilters,
|
|
141
141
|
): Mutation<TData, TError, TVariables, TContext> | undefined {
|
|
142
|
-
|
|
143
|
-
filters.exact = true
|
|
144
|
-
}
|
|
142
|
+
const defaultedFilters = { exact: true, ...filters }
|
|
145
143
|
|
|
146
|
-
return this.#mutations.find((mutation) =>
|
|
144
|
+
return this.#mutations.find((mutation) =>
|
|
145
|
+
matchMutation(defaultedFilters, mutation),
|
|
146
|
+
)
|
|
147
147
|
}
|
|
148
148
|
|
|
149
149
|
findAll(filters: MutationFilters = {}): Mutation[] {
|
package/src/mutationObserver.ts
CHANGED
package/src/queriesObserver.ts
CHANGED
|
@@ -8,6 +8,7 @@ import type { QueryClient } from './queryClient'
|
|
|
8
8
|
import type { NotifyOptions } from './queryObserver'
|
|
9
9
|
import { QueryObserver } from './queryObserver'
|
|
10
10
|
import { Subscribable } from './subscribable'
|
|
11
|
+
import { replaceEqualDeep } from './utils'
|
|
11
12
|
|
|
12
13
|
function difference<T>(array1: T[], array2: T[]): T[] {
|
|
13
14
|
return array1.filter((x) => array2.indexOf(x) === -1)
|
|
@@ -21,27 +22,44 @@ function replaceAt<T>(array: T[], index: number, value: T): T[] {
|
|
|
21
22
|
|
|
22
23
|
type QueriesObserverListener = (result: QueryObserverResult[]) => void
|
|
23
24
|
|
|
24
|
-
export
|
|
25
|
+
export interface QueriesObserverOptions<
|
|
26
|
+
TCombinedResult = QueryObserverResult[],
|
|
27
|
+
> {
|
|
28
|
+
combine?: (result: QueryObserverResult[]) => TCombinedResult
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class QueriesObserver<
|
|
32
|
+
TCombinedResult = QueryObserverResult[],
|
|
33
|
+
> extends Subscribable<QueriesObserverListener> {
|
|
25
34
|
#client: QueryClient
|
|
26
|
-
#result
|
|
35
|
+
#result!: QueryObserverResult[]
|
|
27
36
|
#queries: QueryObserverOptions[]
|
|
28
37
|
#observers: QueryObserver[]
|
|
38
|
+
#options?: QueriesObserverOptions<TCombinedResult>
|
|
39
|
+
#combinedResult!: TCombinedResult
|
|
29
40
|
|
|
30
|
-
constructor(
|
|
41
|
+
constructor(
|
|
42
|
+
client: QueryClient,
|
|
43
|
+
queries: QueryObserverOptions[],
|
|
44
|
+
options?: QueriesObserverOptions<TCombinedResult>,
|
|
45
|
+
) {
|
|
31
46
|
super()
|
|
32
47
|
|
|
33
48
|
this.#client = client
|
|
34
49
|
this.#queries = []
|
|
35
|
-
this.#result = []
|
|
36
50
|
this.#observers = []
|
|
37
51
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
52
|
+
this.#setResult([])
|
|
53
|
+
this.setQueries(queries, options)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
#setResult(value: QueryObserverResult[]) {
|
|
57
|
+
this.#result = value
|
|
58
|
+
this.#combinedResult = this.#combineResult(value)
|
|
41
59
|
}
|
|
42
60
|
|
|
43
61
|
protected onSubscribe(): void {
|
|
44
|
-
if (this.listeners.
|
|
62
|
+
if (this.listeners.size === 1) {
|
|
45
63
|
this.#observers.forEach((observer) => {
|
|
46
64
|
observer.subscribe((result) => {
|
|
47
65
|
this.#onUpdate(observer, result)
|
|
@@ -51,13 +69,13 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
|
|
|
51
69
|
}
|
|
52
70
|
|
|
53
71
|
protected onUnsubscribe(): void {
|
|
54
|
-
if (!this.listeners.
|
|
72
|
+
if (!this.listeners.size) {
|
|
55
73
|
this.destroy()
|
|
56
74
|
}
|
|
57
75
|
}
|
|
58
76
|
|
|
59
77
|
destroy(): void {
|
|
60
|
-
this.listeners =
|
|
78
|
+
this.listeners = new Set()
|
|
61
79
|
this.#observers.forEach((observer) => {
|
|
62
80
|
observer.destroy()
|
|
63
81
|
})
|
|
@@ -65,9 +83,11 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
|
|
|
65
83
|
|
|
66
84
|
setQueries(
|
|
67
85
|
queries: QueryObserverOptions[],
|
|
86
|
+
options?: QueriesObserverOptions<TCombinedResult>,
|
|
68
87
|
notifyOptions?: NotifyOptions,
|
|
69
88
|
): void {
|
|
70
89
|
this.#queries = queries
|
|
90
|
+
this.#options = options
|
|
71
91
|
|
|
72
92
|
notifyManager.batch(() => {
|
|
73
93
|
const prevObservers = this.#observers
|
|
@@ -92,7 +112,7 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
|
|
|
92
112
|
}
|
|
93
113
|
|
|
94
114
|
this.#observers = newObservers
|
|
95
|
-
this.#
|
|
115
|
+
this.#setResult(newResult)
|
|
96
116
|
|
|
97
117
|
if (!this.hasListeners()) {
|
|
98
118
|
return
|
|
@@ -112,8 +132,8 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
|
|
|
112
132
|
})
|
|
113
133
|
}
|
|
114
134
|
|
|
115
|
-
getCurrentResult():
|
|
116
|
-
return this.#
|
|
135
|
+
getCurrentResult(): TCombinedResult {
|
|
136
|
+
return this.#combinedResult
|
|
117
137
|
}
|
|
118
138
|
|
|
119
139
|
getQueries() {
|
|
@@ -124,38 +144,68 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
|
|
|
124
144
|
return this.#observers
|
|
125
145
|
}
|
|
126
146
|
|
|
127
|
-
getOptimisticResult(
|
|
128
|
-
|
|
147
|
+
getOptimisticResult(
|
|
148
|
+
queries: QueryObserverOptions[],
|
|
149
|
+
): [
|
|
150
|
+
rawResult: QueryObserverResult[],
|
|
151
|
+
combineResult: (r?: QueryObserverResult[]) => TCombinedResult,
|
|
152
|
+
trackResult: () => QueryObserverResult[],
|
|
153
|
+
] {
|
|
154
|
+
const matches = this.#findMatchingObservers(queries)
|
|
155
|
+
const result = matches.map((match) =>
|
|
129
156
|
match.observer.getOptimisticResult(match.defaultedQueryOptions),
|
|
130
157
|
)
|
|
158
|
+
|
|
159
|
+
return [
|
|
160
|
+
result,
|
|
161
|
+
(r?: QueryObserverResult[]) => {
|
|
162
|
+
return this.#combineResult(r ?? result)
|
|
163
|
+
},
|
|
164
|
+
() => {
|
|
165
|
+
return matches.map((match, index) => {
|
|
166
|
+
const observerResult = result[index]!
|
|
167
|
+
return !match.defaultedQueryOptions.notifyOnChangeProps
|
|
168
|
+
? match.observer.trackResult(observerResult)
|
|
169
|
+
: observerResult
|
|
170
|
+
})
|
|
171
|
+
},
|
|
172
|
+
]
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
#combineResult(input: QueryObserverResult[]): TCombinedResult {
|
|
176
|
+
const combine = this.#options?.combine
|
|
177
|
+
if (combine) {
|
|
178
|
+
return replaceEqualDeep(this.#combinedResult, combine(input))
|
|
179
|
+
}
|
|
180
|
+
return input as any
|
|
131
181
|
}
|
|
132
182
|
|
|
133
183
|
#findMatchingObservers(
|
|
134
184
|
queries: QueryObserverOptions[],
|
|
135
185
|
): QueryObserverMatch[] {
|
|
136
186
|
const prevObservers = this.#observers
|
|
187
|
+
const prevObserversMap = new Map(
|
|
188
|
+
prevObservers.map((observer) => [observer.options.queryHash, observer]),
|
|
189
|
+
)
|
|
190
|
+
|
|
137
191
|
const defaultedQueryOptions = queries.map((options) =>
|
|
138
192
|
this.#client.defaultQueryOptions(options),
|
|
139
193
|
)
|
|
140
194
|
|
|
141
195
|
const matchingObservers: QueryObserverMatch[] =
|
|
142
196
|
defaultedQueryOptions.flatMap((defaultedOptions) => {
|
|
143
|
-
const match =
|
|
144
|
-
(observer) =>
|
|
145
|
-
observer.options.queryHash === defaultedOptions.queryHash,
|
|
146
|
-
)
|
|
197
|
+
const match = prevObserversMap.get(defaultedOptions.queryHash)
|
|
147
198
|
if (match != null) {
|
|
148
199
|
return [{ defaultedQueryOptions: defaultedOptions, observer: match }]
|
|
149
200
|
}
|
|
150
201
|
return []
|
|
151
202
|
})
|
|
152
203
|
|
|
153
|
-
const matchedQueryHashes =
|
|
154
|
-
(match) => match.defaultedQueryOptions.queryHash,
|
|
204
|
+
const matchedQueryHashes = new Set(
|
|
205
|
+
matchingObservers.map((match) => match.defaultedQueryOptions.queryHash),
|
|
155
206
|
)
|
|
156
207
|
const unmatchedQueries = defaultedQueryOptions.filter(
|
|
157
|
-
(defaultedOptions) =>
|
|
158
|
-
!matchedQueryHashes.includes(defaultedOptions.queryHash),
|
|
208
|
+
(defaultedOptions) => !matchedQueryHashes.has(defaultedOptions.queryHash),
|
|
159
209
|
)
|
|
160
210
|
|
|
161
211
|
const getObserver = (options: QueryObserverOptions): QueryObserver => {
|
|
@@ -192,7 +242,7 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
|
|
|
192
242
|
#onUpdate(observer: QueryObserver, result: QueryObserverResult): void {
|
|
193
243
|
const index = this.#observers.indexOf(observer)
|
|
194
244
|
if (index !== -1) {
|
|
195
|
-
this.#
|
|
245
|
+
this.#setResult(replaceAt(this.#result, index, result))
|
|
196
246
|
this.#notify()
|
|
197
247
|
}
|
|
198
248
|
}
|
package/src/query.ts
CHANGED
|
@@ -74,8 +74,10 @@ export interface QueryBehavior<
|
|
|
74
74
|
) => void
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
+
export type FetchDirection = 'forward' | 'backward'
|
|
78
|
+
|
|
77
79
|
export interface FetchMeta {
|
|
78
|
-
fetchMore?: { direction:
|
|
80
|
+
fetchMore?: { direction: FetchDirection }
|
|
79
81
|
}
|
|
80
82
|
|
|
81
83
|
export interface FetchOptions {
|
|
@@ -120,7 +122,7 @@ interface ContinueAction {
|
|
|
120
122
|
|
|
121
123
|
interface SetStateAction<TData, TError> {
|
|
122
124
|
type: 'setState'
|
|
123
|
-
state: QueryState<TData, TError
|
|
125
|
+
state: Partial<QueryState<TData, TError>>
|
|
124
126
|
setStateOptions?: SetStateOptions
|
|
125
127
|
}
|
|
126
128
|
|
|
@@ -211,7 +213,7 @@ export class Query<
|
|
|
211
213
|
}
|
|
212
214
|
|
|
213
215
|
setState(
|
|
214
|
-
state: QueryState<TData, TError
|
|
216
|
+
state: Partial<QueryState<TData, TError>>,
|
|
215
217
|
setStateOptions?: SetStateOptions,
|
|
216
218
|
): void {
|
|
217
219
|
this.#dispatch({ type: 'setState', state, setStateOptions })
|
|
@@ -462,7 +464,7 @@ export class Query<
|
|
|
462
464
|
`Query data cannot be undefined. Please make sure to return a value other than undefined from your query function. Affected query key: ${this.queryHash}`,
|
|
463
465
|
)
|
|
464
466
|
}
|
|
465
|
-
onError(new Error(
|
|
467
|
+
onError(new Error(`${this.queryHash} data is undefined`) as any)
|
|
466
468
|
return
|
|
467
469
|
}
|
|
468
470
|
|
|
@@ -586,7 +588,7 @@ export class Query<
|
|
|
586
588
|
|
|
587
589
|
notifyManager.batch(() => {
|
|
588
590
|
this.#observers.forEach((observer) => {
|
|
589
|
-
observer.onQueryUpdate(
|
|
591
|
+
observer.onQueryUpdate()
|
|
590
592
|
})
|
|
591
593
|
|
|
592
594
|
this.#cache.notify({ query: this, type: 'updated', action })
|
package/src/queryCache.ts
CHANGED
|
@@ -27,7 +27,6 @@ interface QueryCacheConfig {
|
|
|
27
27
|
error: DefaultError | null,
|
|
28
28
|
query: Query<unknown, unknown, unknown>,
|
|
29
29
|
) => void
|
|
30
|
-
createStore?: () => QueryStore
|
|
31
30
|
}
|
|
32
31
|
|
|
33
32
|
interface NotifyEventQueryAdded extends NotifyEvent {
|
|
@@ -95,7 +94,7 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
|
|
|
95
94
|
|
|
96
95
|
constructor(public config: QueryCacheConfig = {}) {
|
|
97
96
|
super()
|
|
98
|
-
this.#queries =
|
|
97
|
+
this.#queries = new Map<string, Query>()
|
|
99
98
|
}
|
|
100
99
|
|
|
101
100
|
build<TQueryFnData, TError, TData, TQueryKey extends QueryKey>(
|
|
@@ -176,13 +175,11 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
|
|
|
176
175
|
find<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData>(
|
|
177
176
|
filters: WithRequired<QueryFilters, 'queryKey'>,
|
|
178
177
|
): Query<TQueryFnData, TError, TData> | undefined {
|
|
179
|
-
|
|
180
|
-
filters.exact = true
|
|
181
|
-
}
|
|
178
|
+
const defaultedFilters = { exact: true, ...filters }
|
|
182
179
|
|
|
183
|
-
return this.getAll().find((query) =>
|
|
184
|
-
|
|
185
|
-
|
|
180
|
+
return this.getAll().find((query) =>
|
|
181
|
+
matchQuery(defaultedFilters, query),
|
|
182
|
+
) as Query<TQueryFnData, TError, TData> | undefined
|
|
186
183
|
}
|
|
187
184
|
|
|
188
185
|
findAll(filters: QueryFilters = {}): Query[] {
|
package/src/queryClient.ts
CHANGED
|
@@ -25,6 +25,8 @@ import type {
|
|
|
25
25
|
ResetOptions,
|
|
26
26
|
SetDataOptions,
|
|
27
27
|
DefaultError,
|
|
28
|
+
CancelOptions,
|
|
29
|
+
DefaultedQueryObserverOptions,
|
|
28
30
|
} from './types'
|
|
29
31
|
import type { QueryState } from './query'
|
|
30
32
|
import { QueryCache } from './queryCache'
|
|
@@ -33,7 +35,6 @@ import { focusManager } from './focusManager'
|
|
|
33
35
|
import { onlineManager } from './onlineManager'
|
|
34
36
|
import { notifyManager } from './notifyManager'
|
|
35
37
|
import { infiniteQueryBehavior } from './infiniteQueryBehavior'
|
|
36
|
-
import type { CancelOptions, DefaultedQueryObserverOptions } from './types'
|
|
37
38
|
|
|
38
39
|
// TYPES
|
|
39
40
|
|
|
@@ -212,14 +213,12 @@ export class QueryClient {
|
|
|
212
213
|
filters: QueryFilters = {},
|
|
213
214
|
cancelOptions: CancelOptions = {},
|
|
214
215
|
): Promise<void> {
|
|
215
|
-
|
|
216
|
-
cancelOptions.revert = true
|
|
217
|
-
}
|
|
216
|
+
const defaultedCancelOptions = { revert: true, ...cancelOptions }
|
|
218
217
|
|
|
219
218
|
const promises = notifyManager.batch(() =>
|
|
220
219
|
this.#queryCache
|
|
221
220
|
.findAll(filters)
|
|
222
|
-
.map((query) => query.cancel(
|
|
221
|
+
.map((query) => query.cancel(defaultedCancelOptions)),
|
|
223
222
|
)
|
|
224
223
|
|
|
225
224
|
return Promise.all(promises).then(noop).catch(noop)
|
|
@@ -249,25 +248,26 @@ export class QueryClient {
|
|
|
249
248
|
filters: RefetchQueryFilters = {},
|
|
250
249
|
options?: RefetchOptions,
|
|
251
250
|
): Promise<void> {
|
|
251
|
+
const fetchOptions = {
|
|
252
|
+
...options,
|
|
253
|
+
cancelRefetch: options?.cancelRefetch ?? true,
|
|
254
|
+
}
|
|
252
255
|
const promises = notifyManager.batch(() =>
|
|
253
256
|
this.#queryCache
|
|
254
257
|
.findAll(filters)
|
|
255
258
|
.filter((query) => !query.isDisabled())
|
|
256
|
-
.map((query) =>
|
|
257
|
-
query.fetch(undefined,
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
}
|
|
261
|
-
|
|
259
|
+
.map((query) => {
|
|
260
|
+
let promise = query.fetch(undefined, fetchOptions)
|
|
261
|
+
if (!fetchOptions.throwOnError) {
|
|
262
|
+
promise = promise.catch(noop)
|
|
263
|
+
}
|
|
264
|
+
return query.state.fetchStatus === 'paused'
|
|
265
|
+
? Promise.resolve()
|
|
266
|
+
: promise
|
|
267
|
+
}),
|
|
262
268
|
)
|
|
263
269
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
if (!options?.throwOnError) {
|
|
267
|
-
promise = promise.catch(noop)
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
return promise
|
|
270
|
+
return Promise.all(promises).then(noop)
|
|
271
271
|
}
|
|
272
272
|
|
|
273
273
|
fetchQuery<
|
|
@@ -480,8 +480,8 @@ export class QueryClient {
|
|
|
480
480
|
defaultedOptions.refetchOnReconnect =
|
|
481
481
|
defaultedOptions.networkMode !== 'always'
|
|
482
482
|
}
|
|
483
|
-
if (typeof defaultedOptions.
|
|
484
|
-
defaultedOptions.
|
|
483
|
+
if (typeof defaultedOptions.throwOnError === 'undefined') {
|
|
484
|
+
defaultedOptions.throwOnError = !!defaultedOptions.suspense
|
|
485
485
|
}
|
|
486
486
|
|
|
487
487
|
return defaultedOptions as DefaultedQueryObserverOptions<
|