@tanstack/query-core 4.24.10 → 5.0.0-alpha.1
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.d.ts +1 -3
- package/build/lib/focusManager.esm.js +19 -36
- package/build/lib/focusManager.esm.js.map +1 -1
- package/build/lib/focusManager.js +19 -38
- package/build/lib/focusManager.js.map +1 -1
- package/build/lib/focusManager.mjs +19 -36
- package/build/lib/focusManager.mjs.map +1 -1
- package/build/lib/hydration.esm.js +21 -23
- package/build/lib/hydration.esm.js.map +1 -1
- package/build/lib/hydration.js +21 -25
- package/build/lib/hydration.js.map +1 -1
- package/build/lib/hydration.mjs +21 -23
- package/build/lib/hydration.mjs.map +1 -1
- package/build/lib/index.d.ts +1 -2
- package/build/lib/index.esm.js +1 -1
- package/build/lib/index.js +2 -8
- package/build/lib/index.js.map +1 -1
- package/build/lib/index.mjs +1 -1
- package/build/lib/infiniteQueryBehavior.d.ts +3 -7
- package/build/lib/infiniteQueryBehavior.esm.js +52 -75
- package/build/lib/infiniteQueryBehavior.esm.js.map +1 -1
- package/build/lib/infiniteQueryBehavior.js +50 -77
- package/build/lib/infiniteQueryBehavior.js.map +1 -1
- package/build/lib/infiniteQueryBehavior.mjs +52 -75
- package/build/lib/infiniteQueryBehavior.mjs.map +1 -1
- package/build/lib/infiniteQueryObserver.d.ts +4 -4
- package/build/lib/infiniteQueryObserver.esm.js +18 -26
- package/build/lib/infiniteQueryObserver.esm.js.map +1 -1
- package/build/lib/infiniteQueryObserver.js +18 -28
- package/build/lib/infiniteQueryObserver.js.map +1 -1
- package/build/lib/infiniteQueryObserver.mjs +18 -26
- package/build/lib/infiniteQueryObserver.mjs.map +1 -1
- package/build/lib/mutation.d.ts +11 -22
- package/build/lib/mutation.esm.js +73 -105
- package/build/lib/mutation.esm.js.map +1 -1
- package/build/lib/mutation.js +73 -107
- package/build/lib/mutation.js.map +1 -1
- package/build/lib/mutation.mjs +73 -105
- package/build/lib/mutation.mjs.map +1 -1
- package/build/lib/mutationCache.d.ts +4 -6
- package/build/lib/mutationCache.esm.js +23 -32
- package/build/lib/mutationCache.esm.js.map +1 -1
- package/build/lib/mutationCache.js +23 -34
- package/build/lib/mutationCache.js.map +1 -1
- package/build/lib/mutationCache.mjs +23 -32
- package/build/lib/mutationCache.mjs.map +1 -1
- package/build/lib/mutationObserver.d.ts +4 -9
- package/build/lib/mutationObserver.esm.js +42 -73
- package/build/lib/mutationObserver.esm.js.map +1 -1
- package/build/lib/mutationObserver.js +42 -75
- package/build/lib/mutationObserver.js.map +1 -1
- package/build/lib/mutationObserver.mjs +42 -73
- package/build/lib/mutationObserver.mjs.map +1 -1
- package/build/lib/notifyManager.esm.js +7 -17
- package/build/lib/notifyManager.esm.js.map +1 -1
- package/build/lib/notifyManager.js +7 -19
- package/build/lib/notifyManager.js.map +1 -1
- package/build/lib/notifyManager.mjs +7 -17
- package/build/lib/notifyManager.mjs.map +1 -1
- package/build/lib/onlineManager.d.ts +1 -3
- package/build/lib/onlineManager.esm.js +16 -30
- package/build/lib/onlineManager.esm.js.map +1 -1
- package/build/lib/onlineManager.js +16 -32
- package/build/lib/onlineManager.js.map +1 -1
- package/build/lib/onlineManager.mjs +16 -30
- package/build/lib/onlineManager.mjs.map +1 -1
- package/build/lib/queriesObserver.d.ts +3 -10
- package/build/lib/queriesObserver.esm.js +47 -71
- package/build/lib/queriesObserver.esm.js.map +1 -1
- package/build/lib/queriesObserver.js +49 -75
- package/build/lib/queriesObserver.js.map +1 -1
- package/build/lib/queriesObserver.mjs +47 -71
- package/build/lib/queriesObserver.mjs.map +1 -1
- package/build/lib/query.d.ts +14 -21
- package/build/lib/query.esm.js +140 -194
- package/build/lib/query.esm.js.map +1 -1
- package/build/lib/query.js +139 -195
- package/build/lib/query.js.map +1 -1
- package/build/lib/query.mjs +140 -194
- package/build/lib/query.mjs.map +1 -1
- package/build/lib/queryCache.d.ts +12 -7
- package/build/lib/queryCache.esm.js +21 -45
- package/build/lib/queryCache.esm.js.map +1 -1
- package/build/lib/queryCache.js +20 -46
- package/build/lib/queryCache.js.map +1 -1
- package/build/lib/queryCache.mjs +21 -45
- package/build/lib/queryCache.mjs.map +1 -1
- package/build/lib/queryClient.d.ts +18 -46
- package/build/lib/queryClient.esm.js +137 -216
- package/build/lib/queryClient.esm.js.map +1 -1
- package/build/lib/queryClient.js +136 -217
- package/build/lib/queryClient.js.map +1 -1
- package/build/lib/queryClient.mjs +137 -216
- package/build/lib/queryClient.mjs.map +1 -1
- package/build/lib/queryObserver.d.ts +4 -29
- package/build/lib/queryObserver.esm.js +176 -258
- package/build/lib/queryObserver.esm.js.map +1 -1
- package/build/lib/queryObserver.js +176 -260
- package/build/lib/queryObserver.js.map +1 -1
- package/build/lib/queryObserver.mjs +176 -258
- package/build/lib/queryObserver.mjs.map +1 -1
- package/build/lib/removable.d.ts +3 -3
- package/build/lib/removable.esm.js +10 -14
- package/build/lib/removable.esm.js.map +1 -1
- package/build/lib/removable.js +10 -16
- package/build/lib/removable.js.map +1 -1
- package/build/lib/removable.mjs +10 -14
- package/build/lib/removable.mjs.map +1 -1
- package/build/lib/retryer.d.ts +5 -5
- package/build/lib/retryer.esm.js +27 -44
- package/build/lib/retryer.esm.js.map +1 -1
- package/build/lib/retryer.js +27 -46
- package/build/lib/retryer.js.map +1 -1
- package/build/lib/retryer.mjs +27 -44
- package/build/lib/retryer.mjs.map +1 -1
- package/build/lib/subscribable.esm.js +4 -7
- package/build/lib/subscribable.esm.js.map +1 -1
- package/build/lib/subscribable.js +4 -9
- package/build/lib/subscribable.js.map +1 -1
- package/build/lib/subscribable.mjs +4 -7
- package/build/lib/subscribable.mjs.map +1 -1
- package/build/lib/tests/utils.d.ts +3 -12
- package/build/lib/types.d.ts +111 -99
- package/build/lib/utils.d.ts +8 -18
- package/build/lib/utils.esm.js +39 -132
- package/build/lib/utils.esm.js.map +1 -1
- package/build/lib/utils.js +42 -144
- package/build/lib/utils.js.map +1 -1
- package/build/lib/utils.mjs +39 -132
- package/build/lib/utils.mjs.map +1 -1
- package/build/umd/index.development.js +867 -1399
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +1 -1
- package/src/focusManager.ts +17 -24
- package/src/index.ts +1 -11
- package/src/infiniteQueryBehavior.ts +54 -94
- package/src/infiniteQueryObserver.ts +10 -12
- package/src/mutation.ts +68 -92
- package/src/mutationCache.ts +27 -27
- package/src/mutationObserver.ts +58 -97
- package/src/onlineManager.ts +14 -14
- package/src/queriesObserver.ts +50 -54
- package/src/query.ts +107 -111
- package/src/queryCache.ts +42 -41
- package/src/queryClient.ts +155 -434
- package/src/queryObserver.ts +155 -192
- package/src/removable.ts +13 -13
- package/src/retryer.ts +5 -5
- package/src/subscribable.ts +1 -1
- package/src/tests/focusManager.test.tsx +25 -25
- package/src/tests/hydration.test.tsx +167 -81
- package/src/tests/infiniteQueryBehavior.test.tsx +209 -17
- package/src/tests/infiniteQueryObserver.test.tsx +6 -2
- package/src/tests/mutationCache.test.tsx +127 -127
- package/src/tests/mutationObserver.test.tsx +1 -31
- package/src/tests/mutations.test.tsx +62 -43
- package/src/tests/onlineManager.test.tsx +12 -4
- package/src/tests/queriesObserver.test.tsx +41 -77
- package/src/tests/query.test.tsx +175 -243
- package/src/tests/queryCache.test.tsx +170 -93
- package/src/tests/queryClient.test.tsx +229 -378
- package/src/tests/queryObserver.test.tsx +23 -147
- package/src/tests/utils.test.tsx +84 -29
- package/src/tests/utils.ts +9 -18
- package/src/types.ts +187 -140
- package/src/utils.ts +31 -124
- package/build/lib/logger.d.ts +0 -8
- package/build/lib/logger.esm.js +0 -4
- package/build/lib/logger.esm.js.map +0 -1
- package/build/lib/logger.js +0 -8
- package/build/lib/logger.js.map +0 -1
- package/build/lib/logger.mjs +0 -4
- package/build/lib/logger.mjs.map +0 -1
- package/build/lib/logger.native.d.ts +0 -6
- package/build/lib/logger.native.esm.js +0 -12
- package/build/lib/logger.native.esm.js.map +0 -1
- package/build/lib/logger.native.js +0 -16
- package/build/lib/logger.native.js.map +0 -1
- package/build/lib/logger.native.mjs +0 -12
- package/build/lib/logger.native.mjs.map +0 -1
- package/src/logger.native.ts +0 -11
- package/src/logger.ts +0 -9
package/src/mutationObserver.ts
CHANGED
|
@@ -5,9 +5,9 @@ import type { QueryClient } from './queryClient'
|
|
|
5
5
|
import { Subscribable } from './subscribable'
|
|
6
6
|
import type {
|
|
7
7
|
MutateOptions,
|
|
8
|
-
MutationObserverBaseResult,
|
|
9
8
|
MutationObserverResult,
|
|
10
9
|
MutationObserverOptions,
|
|
10
|
+
RegisteredError,
|
|
11
11
|
} from './types'
|
|
12
12
|
import { shallowEqualObjects } from './utils'
|
|
13
13
|
|
|
@@ -17,17 +17,11 @@ type MutationObserverListener<TData, TError, TVariables, TContext> = (
|
|
|
17
17
|
result: MutationObserverResult<TData, TError, TVariables, TContext>,
|
|
18
18
|
) => void
|
|
19
19
|
|
|
20
|
-
interface NotifyOptions {
|
|
21
|
-
listeners?: boolean
|
|
22
|
-
onError?: boolean
|
|
23
|
-
onSuccess?: boolean
|
|
24
|
-
}
|
|
25
|
-
|
|
26
20
|
// CLASS
|
|
27
21
|
|
|
28
22
|
export class MutationObserver<
|
|
29
23
|
TData = unknown,
|
|
30
|
-
TError =
|
|
24
|
+
TError = RegisteredError,
|
|
31
25
|
TVariables = void,
|
|
32
26
|
TContext = unknown,
|
|
33
27
|
> extends Subscribable<
|
|
@@ -35,15 +29,11 @@ export class MutationObserver<
|
|
|
35
29
|
> {
|
|
36
30
|
options!: MutationObserverOptions<TData, TError, TVariables, TContext>
|
|
37
31
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
TContext
|
|
44
|
-
>
|
|
45
|
-
private currentMutation?: Mutation<TData, TError, TVariables, TContext>
|
|
46
|
-
private mutateOptions?: MutateOptions<TData, TError, TVariables, TContext>
|
|
32
|
+
#client: QueryClient
|
|
33
|
+
#currentResult: MutationObserverResult<TData, TError, TVariables, TContext> =
|
|
34
|
+
undefined!
|
|
35
|
+
#currentMutation?: Mutation<TData, TError, TVariables, TContext>
|
|
36
|
+
#mutateOptions?: MutateOptions<TData, TError, TVariables, TContext>
|
|
47
37
|
|
|
48
38
|
constructor(
|
|
49
39
|
client: QueryClient,
|
|
@@ -51,10 +41,10 @@ export class MutationObserver<
|
|
|
51
41
|
) {
|
|
52
42
|
super()
|
|
53
43
|
|
|
54
|
-
this
|
|
44
|
+
this.#client = client
|
|
55
45
|
this.setOptions(options)
|
|
56
46
|
this.bindMethods()
|
|
57
|
-
this
|
|
47
|
+
this.#updateResult()
|
|
58
48
|
}
|
|
59
49
|
|
|
60
50
|
protected bindMethods(): void {
|
|
@@ -66,11 +56,11 @@ export class MutationObserver<
|
|
|
66
56
|
options?: MutationObserverOptions<TData, TError, TVariables, TContext>,
|
|
67
57
|
) {
|
|
68
58
|
const prevOptions = this.options
|
|
69
|
-
this.options = this
|
|
59
|
+
this.options = this.#client.defaultMutationOptions(options)
|
|
70
60
|
if (!shallowEqualObjects(prevOptions, this.options)) {
|
|
71
|
-
this
|
|
61
|
+
this.#client.getMutationCache().notify({
|
|
72
62
|
type: 'observerOptionsUpdated',
|
|
73
|
-
mutation: this
|
|
63
|
+
mutation: this.#currentMutation,
|
|
74
64
|
observer: this,
|
|
75
65
|
})
|
|
76
66
|
}
|
|
@@ -78,25 +68,14 @@ export class MutationObserver<
|
|
|
78
68
|
|
|
79
69
|
protected onUnsubscribe(): void {
|
|
80
70
|
if (!this.listeners.length) {
|
|
81
|
-
this
|
|
71
|
+
this.#currentMutation?.removeObserver(this)
|
|
82
72
|
}
|
|
83
73
|
}
|
|
84
74
|
|
|
85
75
|
onMutationUpdate(action: Action<TData, TError, TVariables, TContext>): void {
|
|
86
|
-
this
|
|
87
|
-
|
|
88
|
-
// Determine which callbacks to trigger
|
|
89
|
-
const notifyOptions: NotifyOptions = {
|
|
90
|
-
listeners: true,
|
|
91
|
-
}
|
|
76
|
+
this.#updateResult()
|
|
92
77
|
|
|
93
|
-
|
|
94
|
-
notifyOptions.onSuccess = true
|
|
95
|
-
} else if (action.type === 'error') {
|
|
96
|
-
notifyOptions.onError = true
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
this.notify(notifyOptions)
|
|
78
|
+
this.#notify(action)
|
|
100
79
|
}
|
|
101
80
|
|
|
102
81
|
getCurrentResult(): MutationObserverResult<
|
|
@@ -105,101 +84,83 @@ export class MutationObserver<
|
|
|
105
84
|
TVariables,
|
|
106
85
|
TContext
|
|
107
86
|
> {
|
|
108
|
-
return this
|
|
87
|
+
return this.#currentResult
|
|
109
88
|
}
|
|
110
89
|
|
|
111
90
|
reset(): void {
|
|
112
|
-
this
|
|
113
|
-
this
|
|
114
|
-
this
|
|
91
|
+
this.#currentMutation = undefined
|
|
92
|
+
this.#updateResult()
|
|
93
|
+
this.#notify()
|
|
115
94
|
}
|
|
116
95
|
|
|
117
96
|
mutate(
|
|
118
|
-
variables
|
|
97
|
+
variables: TVariables,
|
|
119
98
|
options?: MutateOptions<TData, TError, TVariables, TContext>,
|
|
120
99
|
): Promise<TData> {
|
|
121
|
-
this
|
|
100
|
+
this.#mutateOptions = options
|
|
122
101
|
|
|
123
|
-
|
|
124
|
-
this.currentMutation.removeObserver(this)
|
|
125
|
-
}
|
|
102
|
+
this.#currentMutation?.removeObserver(this)
|
|
126
103
|
|
|
127
|
-
this
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
typeof variables !== 'undefined' ? variables : this.options.variables,
|
|
131
|
-
})
|
|
104
|
+
this.#currentMutation = this.#client
|
|
105
|
+
.getMutationCache()
|
|
106
|
+
.build(this.#client, this.options)
|
|
132
107
|
|
|
133
|
-
this
|
|
108
|
+
this.#currentMutation.addObserver(this)
|
|
134
109
|
|
|
135
|
-
return this
|
|
110
|
+
return this.#currentMutation.execute(variables)
|
|
136
111
|
}
|
|
137
112
|
|
|
138
|
-
|
|
139
|
-
const state =
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
TData,
|
|
145
|
-
TError,
|
|
146
|
-
TVariables,
|
|
147
|
-
TContext
|
|
148
|
-
> = {
|
|
113
|
+
#updateResult(): void {
|
|
114
|
+
const state =
|
|
115
|
+
this.#currentMutation?.state ??
|
|
116
|
+
getDefaultState<TData, TError, TVariables, TContext>()
|
|
117
|
+
|
|
118
|
+
this.#currentResult = {
|
|
149
119
|
...state,
|
|
150
|
-
|
|
120
|
+
isPending: state.status === 'pending',
|
|
151
121
|
isSuccess: state.status === 'success',
|
|
152
122
|
isError: state.status === 'error',
|
|
153
123
|
isIdle: state.status === 'idle',
|
|
154
124
|
mutate: this.mutate,
|
|
155
125
|
reset: this.reset,
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
this.currentResult = result as MutationObserverResult<
|
|
159
|
-
TData,
|
|
160
|
-
TError,
|
|
161
|
-
TVariables,
|
|
162
|
-
TContext
|
|
163
|
-
>
|
|
126
|
+
} as MutationObserverResult<TData, TError, TVariables, TContext>
|
|
164
127
|
}
|
|
165
128
|
|
|
166
|
-
|
|
129
|
+
#notify(action?: Action<TData, TError, TVariables, TContext>): void {
|
|
167
130
|
notifyManager.batch(() => {
|
|
168
131
|
// First trigger the mutate callbacks
|
|
169
|
-
if (this
|
|
170
|
-
if (
|
|
171
|
-
this
|
|
172
|
-
|
|
173
|
-
this
|
|
174
|
-
this
|
|
132
|
+
if (this.#mutateOptions && this.hasListeners()) {
|
|
133
|
+
if (action?.type === 'success') {
|
|
134
|
+
this.#mutateOptions.onSuccess?.(
|
|
135
|
+
action.data,
|
|
136
|
+
this.#currentResult.variables!,
|
|
137
|
+
this.#currentResult.context!,
|
|
175
138
|
)
|
|
176
|
-
this
|
|
177
|
-
|
|
139
|
+
this.#mutateOptions.onSettled?.(
|
|
140
|
+
action.data,
|
|
178
141
|
null,
|
|
179
|
-
this
|
|
180
|
-
this
|
|
142
|
+
this.#currentResult.variables!,
|
|
143
|
+
this.#currentResult.context,
|
|
181
144
|
)
|
|
182
|
-
} else if (
|
|
183
|
-
this
|
|
184
|
-
|
|
185
|
-
this
|
|
186
|
-
this
|
|
145
|
+
} else if (action?.type === 'error') {
|
|
146
|
+
this.#mutateOptions.onError?.(
|
|
147
|
+
action.error,
|
|
148
|
+
this.#currentResult.variables!,
|
|
149
|
+
this.#currentResult.context,
|
|
187
150
|
)
|
|
188
|
-
this
|
|
151
|
+
this.#mutateOptions.onSettled?.(
|
|
189
152
|
undefined,
|
|
190
|
-
|
|
191
|
-
this
|
|
192
|
-
this
|
|
153
|
+
action.error,
|
|
154
|
+
this.#currentResult.variables!,
|
|
155
|
+
this.#currentResult.context,
|
|
193
156
|
)
|
|
194
157
|
}
|
|
195
158
|
}
|
|
196
159
|
|
|
197
160
|
// Then trigger the listeners
|
|
198
|
-
|
|
199
|
-
this
|
|
200
|
-
|
|
201
|
-
})
|
|
202
|
-
}
|
|
161
|
+
this.listeners.forEach((listener) => {
|
|
162
|
+
listener(this.#currentResult)
|
|
163
|
+
})
|
|
203
164
|
})
|
|
204
165
|
}
|
|
205
166
|
}
|
package/src/onlineManager.ts
CHANGED
|
@@ -6,14 +6,14 @@ type SetupFn = (
|
|
|
6
6
|
) => (() => void) | undefined
|
|
7
7
|
|
|
8
8
|
export class OnlineManager extends Subscribable {
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
#online?: boolean
|
|
10
|
+
#cleanup?: () => void
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
#setup: SetupFn
|
|
13
13
|
|
|
14
14
|
constructor() {
|
|
15
15
|
super()
|
|
16
|
-
this
|
|
16
|
+
this.#setup = (onOnline) => {
|
|
17
17
|
// addEventListener does not exist in React Native, but window does
|
|
18
18
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
19
19
|
if (!isServer && window.addEventListener) {
|
|
@@ -34,22 +34,22 @@ export class OnlineManager extends Subscribable {
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
protected onSubscribe(): void {
|
|
37
|
-
if (!this
|
|
38
|
-
this.setEventListener(this
|
|
37
|
+
if (!this.#cleanup) {
|
|
38
|
+
this.setEventListener(this.#setup)
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
protected onUnsubscribe() {
|
|
43
43
|
if (!this.hasListeners()) {
|
|
44
|
-
this
|
|
45
|
-
this
|
|
44
|
+
this.#cleanup?.()
|
|
45
|
+
this.#cleanup = undefined
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
setEventListener(setup: SetupFn): void {
|
|
50
|
-
this
|
|
51
|
-
this
|
|
52
|
-
this
|
|
50
|
+
this.#setup = setup
|
|
51
|
+
this.#cleanup?.()
|
|
52
|
+
this.#cleanup = setup((online?: boolean) => {
|
|
53
53
|
if (typeof online === 'boolean') {
|
|
54
54
|
this.setOnline(online)
|
|
55
55
|
} else {
|
|
@@ -59,7 +59,7 @@ export class OnlineManager extends Subscribable {
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
setOnline(online?: boolean): void {
|
|
62
|
-
this
|
|
62
|
+
this.#online = online
|
|
63
63
|
|
|
64
64
|
if (online) {
|
|
65
65
|
this.onOnline()
|
|
@@ -73,8 +73,8 @@ export class OnlineManager extends Subscribable {
|
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
isOnline(): boolean {
|
|
76
|
-
if (typeof this
|
|
77
|
-
return this
|
|
76
|
+
if (typeof this.#online === 'boolean') {
|
|
77
|
+
return this.#online
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
if (
|
package/src/queriesObserver.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { difference, replaceAt } from './utils'
|
|
2
1
|
import { notifyManager } from './notifyManager'
|
|
3
2
|
import type {
|
|
4
3
|
QueryObserverOptions,
|
|
@@ -10,23 +9,33 @@ import type { NotifyOptions } from './queryObserver'
|
|
|
10
9
|
import { QueryObserver } from './queryObserver'
|
|
11
10
|
import { Subscribable } from './subscribable'
|
|
12
11
|
|
|
12
|
+
function difference<T>(array1: T[], array2: T[]): T[] {
|
|
13
|
+
return array1.filter((x) => array2.indexOf(x) === -1)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function replaceAt<T>(array: T[], index: number, value: T): T[] {
|
|
17
|
+
const copy = array.slice(0)
|
|
18
|
+
copy[index] = value
|
|
19
|
+
return copy
|
|
20
|
+
}
|
|
21
|
+
|
|
13
22
|
type QueriesObserverListener = (result: QueryObserverResult[]) => void
|
|
14
23
|
|
|
15
24
|
export class QueriesObserver extends Subscribable<QueriesObserverListener> {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
25
|
+
#client: QueryClient
|
|
26
|
+
#result: QueryObserverResult[]
|
|
27
|
+
#queries: QueryObserverOptions[]
|
|
28
|
+
#observers: QueryObserver[]
|
|
29
|
+
#observersMap: Record<string, QueryObserver>
|
|
21
30
|
|
|
22
31
|
constructor(client: QueryClient, queries?: QueryObserverOptions[]) {
|
|
23
32
|
super()
|
|
24
33
|
|
|
25
|
-
this
|
|
26
|
-
this
|
|
27
|
-
this
|
|
28
|
-
this
|
|
29
|
-
this
|
|
34
|
+
this.#client = client
|
|
35
|
+
this.#queries = []
|
|
36
|
+
this.#result = []
|
|
37
|
+
this.#observers = []
|
|
38
|
+
this.#observersMap = {}
|
|
30
39
|
|
|
31
40
|
if (queries) {
|
|
32
41
|
this.setQueries(queries)
|
|
@@ -35,9 +44,9 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
|
|
|
35
44
|
|
|
36
45
|
protected onSubscribe(): void {
|
|
37
46
|
if (this.listeners.length === 1) {
|
|
38
|
-
this
|
|
47
|
+
this.#observers.forEach((observer) => {
|
|
39
48
|
observer.subscribe((result) => {
|
|
40
|
-
this
|
|
49
|
+
this.#onUpdate(observer, result)
|
|
41
50
|
})
|
|
42
51
|
})
|
|
43
52
|
}
|
|
@@ -51,7 +60,7 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
|
|
|
51
60
|
|
|
52
61
|
destroy(): void {
|
|
53
62
|
this.listeners = []
|
|
54
|
-
this
|
|
63
|
+
this.#observers.forEach((observer) => {
|
|
55
64
|
observer.destroy()
|
|
56
65
|
})
|
|
57
66
|
}
|
|
@@ -60,12 +69,12 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
|
|
|
60
69
|
queries: QueryObserverOptions[],
|
|
61
70
|
notifyOptions?: NotifyOptions,
|
|
62
71
|
): void {
|
|
63
|
-
this
|
|
72
|
+
this.#queries = queries
|
|
64
73
|
|
|
65
74
|
notifyManager.batch(() => {
|
|
66
|
-
const prevObservers = this
|
|
75
|
+
const prevObservers = this.#observers
|
|
67
76
|
|
|
68
|
-
const newObserverMatches = this
|
|
77
|
+
const newObserverMatches = this.#findMatchingObservers(this.#queries)
|
|
69
78
|
|
|
70
79
|
// set options for the new observers to notify of changes
|
|
71
80
|
newObserverMatches.forEach((match) =>
|
|
@@ -87,9 +96,9 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
|
|
|
87
96
|
return
|
|
88
97
|
}
|
|
89
98
|
|
|
90
|
-
this
|
|
91
|
-
this
|
|
92
|
-
this
|
|
99
|
+
this.#observers = newObservers
|
|
100
|
+
this.#observersMap = newObserversMap
|
|
101
|
+
this.#result = newResult
|
|
93
102
|
|
|
94
103
|
if (!this.hasListeners()) {
|
|
95
104
|
return
|
|
@@ -101,38 +110,38 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
|
|
|
101
110
|
|
|
102
111
|
difference(newObservers, prevObservers).forEach((observer) => {
|
|
103
112
|
observer.subscribe((result) => {
|
|
104
|
-
this
|
|
113
|
+
this.#onUpdate(observer, result)
|
|
105
114
|
})
|
|
106
115
|
})
|
|
107
116
|
|
|
108
|
-
this
|
|
117
|
+
this.#notify()
|
|
109
118
|
})
|
|
110
119
|
}
|
|
111
120
|
|
|
112
121
|
getCurrentResult(): QueryObserverResult[] {
|
|
113
|
-
return this
|
|
122
|
+
return this.#result
|
|
114
123
|
}
|
|
115
124
|
|
|
116
125
|
getQueries() {
|
|
117
|
-
return this
|
|
126
|
+
return this.#observers.map((observer) => observer.getCurrentQuery())
|
|
118
127
|
}
|
|
119
128
|
|
|
120
129
|
getObservers() {
|
|
121
|
-
return this
|
|
130
|
+
return this.#observers
|
|
122
131
|
}
|
|
123
132
|
|
|
124
133
|
getOptimisticResult(queries: QueryObserverOptions[]): QueryObserverResult[] {
|
|
125
|
-
return this
|
|
134
|
+
return this.#findMatchingObservers(queries).map((match) =>
|
|
126
135
|
match.observer.getOptimisticResult(match.defaultedQueryOptions),
|
|
127
136
|
)
|
|
128
137
|
}
|
|
129
138
|
|
|
130
|
-
|
|
139
|
+
#findMatchingObservers(
|
|
131
140
|
queries: QueryObserverOptions[],
|
|
132
141
|
): QueryObserverMatch[] {
|
|
133
|
-
const prevObservers = this
|
|
142
|
+
const prevObservers = this.#observers
|
|
134
143
|
const defaultedQueryOptions = queries.map((options) =>
|
|
135
|
-
this
|
|
144
|
+
this.#client.defaultQueryOptions(options),
|
|
136
145
|
)
|
|
137
146
|
|
|
138
147
|
const matchingObservers: QueryObserverMatch[] =
|
|
@@ -155,29 +164,16 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
|
|
|
155
164
|
!matchedQueryHashes.includes(defaultedOptions.queryHash),
|
|
156
165
|
)
|
|
157
166
|
|
|
158
|
-
const unmatchedObservers = prevObservers.filter(
|
|
159
|
-
(prevObserver) =>
|
|
160
|
-
!matchingObservers.some((match) => match.observer === prevObserver),
|
|
161
|
-
)
|
|
162
|
-
|
|
163
167
|
const getObserver = (options: QueryObserverOptions): QueryObserver => {
|
|
164
|
-
const defaultedOptions = this
|
|
165
|
-
const currentObserver = this
|
|
166
|
-
return
|
|
168
|
+
const defaultedOptions = this.#client.defaultQueryOptions(options)
|
|
169
|
+
const currentObserver = this.#observersMap[defaultedOptions.queryHash!]
|
|
170
|
+
return (
|
|
171
|
+
currentObserver ?? new QueryObserver(this.#client, defaultedOptions)
|
|
172
|
+
)
|
|
167
173
|
}
|
|
168
174
|
|
|
169
175
|
const newOrReusedObservers: QueryObserverMatch[] = unmatchedQueries.map(
|
|
170
|
-
(options
|
|
171
|
-
if (options.keepPreviousData) {
|
|
172
|
-
// return previous data from one of the observers that no longer match
|
|
173
|
-
const previouslyUsedObserver = unmatchedObservers[index]
|
|
174
|
-
if (previouslyUsedObserver !== undefined) {
|
|
175
|
-
return {
|
|
176
|
-
defaultedQueryOptions: options,
|
|
177
|
-
observer: previouslyUsedObserver,
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
}
|
|
176
|
+
(options) => {
|
|
181
177
|
return {
|
|
182
178
|
defaultedQueryOptions: options,
|
|
183
179
|
observer: getObserver(options),
|
|
@@ -197,18 +193,18 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
|
|
|
197
193
|
.sort(sortMatchesByOrderOfQueries)
|
|
198
194
|
}
|
|
199
195
|
|
|
200
|
-
|
|
201
|
-
const index = this
|
|
196
|
+
#onUpdate(observer: QueryObserver, result: QueryObserverResult): void {
|
|
197
|
+
const index = this.#observers.indexOf(observer)
|
|
202
198
|
if (index !== -1) {
|
|
203
|
-
this
|
|
204
|
-
this
|
|
199
|
+
this.#result = replaceAt(this.#result, index, result)
|
|
200
|
+
this.#notify()
|
|
205
201
|
}
|
|
206
202
|
}
|
|
207
203
|
|
|
208
|
-
|
|
204
|
+
#notify(): void {
|
|
209
205
|
notifyManager.batch(() => {
|
|
210
206
|
this.listeners.forEach((listener) => {
|
|
211
|
-
listener(this
|
|
207
|
+
listener(this.#result)
|
|
212
208
|
})
|
|
213
209
|
})
|
|
214
210
|
}
|