@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/mutation.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
MutationOptions,
|
|
3
|
+
MutationStatus,
|
|
4
|
+
MutationMeta,
|
|
5
|
+
RegisteredError,
|
|
6
|
+
} from './types'
|
|
2
7
|
import type { MutationCache } from './mutationCache'
|
|
3
8
|
import type { MutationObserver } from './mutationObserver'
|
|
4
|
-
import type { Logger } from './logger'
|
|
5
|
-
import { defaultLogger } from './logger'
|
|
6
9
|
import { notifyManager } from './notifyManager'
|
|
7
10
|
import { Removable } from './removable'
|
|
8
11
|
import type { Retryer } from './retryer'
|
|
@@ -14,15 +17,13 @@ interface MutationConfig<TData, TError, TVariables, TContext> {
|
|
|
14
17
|
mutationId: number
|
|
15
18
|
mutationCache: MutationCache
|
|
16
19
|
options: MutationOptions<TData, TError, TVariables, TContext>
|
|
17
|
-
logger?: Logger
|
|
18
20
|
defaultOptions?: MutationOptions<TData, TError, TVariables, TContext>
|
|
19
21
|
state?: MutationState<TData, TError, TVariables, TContext>
|
|
20
|
-
meta?: MutationMeta
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
export interface MutationState<
|
|
24
25
|
TData = unknown,
|
|
25
|
-
TError =
|
|
26
|
+
TError = RegisteredError,
|
|
26
27
|
TVariables = void,
|
|
27
28
|
TContext = unknown,
|
|
28
29
|
> {
|
|
@@ -34,6 +35,7 @@ export interface MutationState<
|
|
|
34
35
|
isPaused: boolean
|
|
35
36
|
status: MutationStatus
|
|
36
37
|
variables: TVariables | undefined
|
|
38
|
+
submittedAt: number
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
interface FailedAction<TError> {
|
|
@@ -42,8 +44,8 @@ interface FailedAction<TError> {
|
|
|
42
44
|
error: TError | null
|
|
43
45
|
}
|
|
44
46
|
|
|
45
|
-
interface
|
|
46
|
-
type: '
|
|
47
|
+
interface PendingAction<TVariables, TContext> {
|
|
48
|
+
type: 'pending'
|
|
47
49
|
variables?: TVariables
|
|
48
50
|
context?: TContext
|
|
49
51
|
}
|
|
@@ -66,51 +68,40 @@ interface ContinueAction {
|
|
|
66
68
|
type: 'continue'
|
|
67
69
|
}
|
|
68
70
|
|
|
69
|
-
interface SetStateAction<TData, TError, TVariables, TContext> {
|
|
70
|
-
type: 'setState'
|
|
71
|
-
state: MutationState<TData, TError, TVariables, TContext>
|
|
72
|
-
}
|
|
73
|
-
|
|
74
71
|
export type Action<TData, TError, TVariables, TContext> =
|
|
75
72
|
| ContinueAction
|
|
76
73
|
| ErrorAction<TError>
|
|
77
74
|
| FailedAction<TError>
|
|
78
|
-
|
|
|
75
|
+
| PendingAction<TVariables, TContext>
|
|
79
76
|
| PauseAction
|
|
80
|
-
| SetStateAction<TData, TError, TVariables, TContext>
|
|
81
77
|
| SuccessAction<TData>
|
|
82
78
|
|
|
83
79
|
// CLASS
|
|
84
80
|
|
|
85
81
|
export class Mutation<
|
|
86
82
|
TData = unknown,
|
|
87
|
-
TError =
|
|
83
|
+
TError = RegisteredError,
|
|
88
84
|
TVariables = void,
|
|
89
85
|
TContext = unknown,
|
|
90
86
|
> extends Removable {
|
|
91
87
|
state: MutationState<TData, TError, TVariables, TContext>
|
|
92
|
-
options: MutationOptions<TData, TError, TVariables, TContext>
|
|
93
|
-
mutationId: number
|
|
88
|
+
readonly options: MutationOptions<TData, TError, TVariables, TContext>
|
|
89
|
+
readonly mutationId: number
|
|
94
90
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
private retryer?: Retryer<TData>
|
|
91
|
+
#observers: MutationObserver<TData, TError, TVariables, TContext>[]
|
|
92
|
+
#mutationCache: MutationCache
|
|
93
|
+
#retryer?: Retryer<TData>
|
|
99
94
|
|
|
100
95
|
constructor(config: MutationConfig<TData, TError, TVariables, TContext>) {
|
|
101
96
|
super()
|
|
102
97
|
|
|
103
|
-
this.options =
|
|
104
|
-
...config.defaultOptions,
|
|
105
|
-
...config.options,
|
|
106
|
-
}
|
|
98
|
+
this.options = config.options
|
|
107
99
|
this.mutationId = config.mutationId
|
|
108
|
-
this
|
|
109
|
-
this
|
|
110
|
-
this.observers = []
|
|
100
|
+
this.#mutationCache = config.mutationCache
|
|
101
|
+
this.#observers = []
|
|
111
102
|
this.state = config.state || getDefaultState()
|
|
112
103
|
|
|
113
|
-
this.
|
|
104
|
+
this.updateGcTime(this.options.gcTime)
|
|
114
105
|
this.scheduleGc()
|
|
115
106
|
}
|
|
116
107
|
|
|
@@ -118,18 +109,14 @@ export class Mutation<
|
|
|
118
109
|
return this.options.meta
|
|
119
110
|
}
|
|
120
111
|
|
|
121
|
-
setState(state: MutationState<TData, TError, TVariables, TContext>): void {
|
|
122
|
-
this.dispatch({ type: 'setState', state })
|
|
123
|
-
}
|
|
124
|
-
|
|
125
112
|
addObserver(observer: MutationObserver<any, any, any, any>): void {
|
|
126
|
-
if (this
|
|
127
|
-
this
|
|
113
|
+
if (this.#observers.indexOf(observer) === -1) {
|
|
114
|
+
this.#observers.push(observer)
|
|
128
115
|
|
|
129
116
|
// Stop the mutation from being garbage collected
|
|
130
117
|
this.clearGcTimeout()
|
|
131
118
|
|
|
132
|
-
this
|
|
119
|
+
this.#mutationCache.notify({
|
|
133
120
|
type: 'observerAdded',
|
|
134
121
|
mutation: this,
|
|
135
122
|
observer,
|
|
@@ -138,11 +125,11 @@ export class Mutation<
|
|
|
138
125
|
}
|
|
139
126
|
|
|
140
127
|
removeObserver(observer: MutationObserver<any, any, any, any>): void {
|
|
141
|
-
this
|
|
128
|
+
this.#observers = this.#observers.filter((x) => x !== observer)
|
|
142
129
|
|
|
143
130
|
this.scheduleGc()
|
|
144
131
|
|
|
145
|
-
this
|
|
132
|
+
this.#mutationCache.notify({
|
|
146
133
|
type: 'observerRemoved',
|
|
147
134
|
mutation: this,
|
|
148
135
|
observer,
|
|
@@ -150,122 +137,114 @@ export class Mutation<
|
|
|
150
137
|
}
|
|
151
138
|
|
|
152
139
|
protected optionalRemove() {
|
|
153
|
-
if (!this
|
|
154
|
-
if (this.state.status === '
|
|
140
|
+
if (!this.#observers.length) {
|
|
141
|
+
if (this.state.status === 'pending') {
|
|
155
142
|
this.scheduleGc()
|
|
156
143
|
} else {
|
|
157
|
-
this
|
|
144
|
+
this.#mutationCache.remove(this)
|
|
158
145
|
}
|
|
159
146
|
}
|
|
160
147
|
}
|
|
161
148
|
|
|
162
149
|
continue(): Promise<unknown> {
|
|
163
|
-
return
|
|
150
|
+
return (
|
|
151
|
+
this.#retryer?.continue() ??
|
|
152
|
+
// continuing a mutation assumes that variables are set, mutation must have been dehydrated before
|
|
153
|
+
this.execute(this.state.variables!)
|
|
154
|
+
)
|
|
164
155
|
}
|
|
165
156
|
|
|
166
|
-
async execute(): Promise<TData> {
|
|
157
|
+
async execute(variables: TVariables): Promise<TData> {
|
|
167
158
|
const executeMutation = () => {
|
|
168
|
-
this
|
|
159
|
+
this.#retryer = createRetryer({
|
|
169
160
|
fn: () => {
|
|
170
161
|
if (!this.options.mutationFn) {
|
|
171
|
-
return Promise.reject('No mutationFn found')
|
|
162
|
+
return Promise.reject(new Error('No mutationFn found'))
|
|
172
163
|
}
|
|
173
|
-
return this.options.mutationFn(
|
|
164
|
+
return this.options.mutationFn(variables)
|
|
174
165
|
},
|
|
175
166
|
onFail: (failureCount, error) => {
|
|
176
|
-
this
|
|
167
|
+
this.#dispatch({ type: 'failed', failureCount, error })
|
|
177
168
|
},
|
|
178
169
|
onPause: () => {
|
|
179
|
-
this
|
|
170
|
+
this.#dispatch({ type: 'pause' })
|
|
180
171
|
},
|
|
181
172
|
onContinue: () => {
|
|
182
|
-
this
|
|
173
|
+
this.#dispatch({ type: 'continue' })
|
|
183
174
|
},
|
|
184
175
|
retry: this.options.retry ?? 0,
|
|
185
176
|
retryDelay: this.options.retryDelay,
|
|
186
177
|
networkMode: this.options.networkMode,
|
|
187
178
|
})
|
|
188
179
|
|
|
189
|
-
return this
|
|
180
|
+
return this.#retryer.promise
|
|
190
181
|
}
|
|
191
182
|
|
|
192
|
-
const restored = this.state.status === '
|
|
183
|
+
const restored = this.state.status === 'pending'
|
|
184
|
+
|
|
193
185
|
try {
|
|
194
186
|
if (!restored) {
|
|
195
|
-
this
|
|
187
|
+
this.#dispatch({ type: 'pending', variables })
|
|
196
188
|
// Notify cache callback
|
|
197
|
-
await this
|
|
198
|
-
|
|
189
|
+
await this.#mutationCache.config.onMutate?.(
|
|
190
|
+
variables,
|
|
199
191
|
this as Mutation<unknown, unknown, unknown, unknown>,
|
|
200
192
|
)
|
|
201
|
-
const context = await this.options.onMutate?.(
|
|
193
|
+
const context = await this.options.onMutate?.(variables)
|
|
202
194
|
if (context !== this.state.context) {
|
|
203
|
-
this
|
|
204
|
-
type: '
|
|
195
|
+
this.#dispatch({
|
|
196
|
+
type: 'pending',
|
|
205
197
|
context,
|
|
206
|
-
variables
|
|
198
|
+
variables,
|
|
207
199
|
})
|
|
208
200
|
}
|
|
209
201
|
}
|
|
210
202
|
const data = await executeMutation()
|
|
211
203
|
|
|
212
204
|
// Notify cache callback
|
|
213
|
-
await this
|
|
205
|
+
await this.#mutationCache.config.onSuccess?.(
|
|
214
206
|
data,
|
|
215
|
-
|
|
207
|
+
variables,
|
|
216
208
|
this.state.context,
|
|
217
209
|
this as Mutation<unknown, unknown, unknown, unknown>,
|
|
218
210
|
)
|
|
219
211
|
|
|
220
|
-
await this.options.onSuccess?.(
|
|
221
|
-
data,
|
|
222
|
-
this.state.variables!,
|
|
223
|
-
this.state.context!,
|
|
224
|
-
)
|
|
212
|
+
await this.options.onSuccess?.(data, variables, this.state.context)
|
|
225
213
|
|
|
226
|
-
await this.options.onSettled?.(
|
|
227
|
-
data,
|
|
228
|
-
null,
|
|
229
|
-
this.state.variables!,
|
|
230
|
-
this.state.context,
|
|
231
|
-
)
|
|
214
|
+
await this.options.onSettled?.(data, null, variables, this.state.context)
|
|
232
215
|
|
|
233
|
-
this
|
|
216
|
+
this.#dispatch({ type: 'success', data })
|
|
234
217
|
return data
|
|
235
218
|
} catch (error) {
|
|
236
219
|
try {
|
|
237
220
|
// Notify cache callback
|
|
238
|
-
await this
|
|
221
|
+
await this.#mutationCache.config.onError?.(
|
|
239
222
|
error,
|
|
240
|
-
|
|
223
|
+
variables,
|
|
241
224
|
this.state.context,
|
|
242
225
|
this as Mutation<unknown, unknown, unknown, unknown>,
|
|
243
226
|
)
|
|
244
227
|
|
|
245
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
246
|
-
this.logger.error(error)
|
|
247
|
-
}
|
|
248
|
-
|
|
249
228
|
await this.options.onError?.(
|
|
250
229
|
error as TError,
|
|
251
|
-
|
|
230
|
+
variables,
|
|
252
231
|
this.state.context,
|
|
253
232
|
)
|
|
254
233
|
|
|
255
234
|
await this.options.onSettled?.(
|
|
256
235
|
undefined,
|
|
257
236
|
error as TError,
|
|
258
|
-
|
|
237
|
+
variables,
|
|
259
238
|
this.state.context,
|
|
260
239
|
)
|
|
261
240
|
throw error
|
|
262
241
|
} finally {
|
|
263
|
-
this
|
|
242
|
+
this.#dispatch({ type: 'error', error: error as TError })
|
|
264
243
|
}
|
|
265
244
|
}
|
|
266
245
|
}
|
|
267
246
|
|
|
268
|
-
|
|
247
|
+
#dispatch(action: Action<TData, TError, TVariables, TContext>): void {
|
|
269
248
|
const reducer = (
|
|
270
249
|
state: MutationState<TData, TError, TVariables, TContext>,
|
|
271
250
|
): MutationState<TData, TError, TVariables, TContext> => {
|
|
@@ -286,7 +265,7 @@ export class Mutation<
|
|
|
286
265
|
...state,
|
|
287
266
|
isPaused: false,
|
|
288
267
|
}
|
|
289
|
-
case '
|
|
268
|
+
case 'pending':
|
|
290
269
|
return {
|
|
291
270
|
...state,
|
|
292
271
|
context: action.context,
|
|
@@ -295,8 +274,9 @@ export class Mutation<
|
|
|
295
274
|
failureReason: null,
|
|
296
275
|
error: null,
|
|
297
276
|
isPaused: !canFetch(this.options.networkMode),
|
|
298
|
-
status: '
|
|
277
|
+
status: 'pending',
|
|
299
278
|
variables: action.variables,
|
|
279
|
+
submittedAt: Date.now(),
|
|
300
280
|
}
|
|
301
281
|
case 'success':
|
|
302
282
|
return {
|
|
@@ -318,20 +298,15 @@ export class Mutation<
|
|
|
318
298
|
isPaused: false,
|
|
319
299
|
status: 'error',
|
|
320
300
|
}
|
|
321
|
-
case 'setState':
|
|
322
|
-
return {
|
|
323
|
-
...state,
|
|
324
|
-
...action.state,
|
|
325
|
-
}
|
|
326
301
|
}
|
|
327
302
|
}
|
|
328
303
|
this.state = reducer(this.state)
|
|
329
304
|
|
|
330
305
|
notifyManager.batch(() => {
|
|
331
|
-
this
|
|
306
|
+
this.#observers.forEach((observer) => {
|
|
332
307
|
observer.onMutationUpdate(action)
|
|
333
308
|
})
|
|
334
|
-
this
|
|
309
|
+
this.#mutationCache.notify({
|
|
335
310
|
mutation: this,
|
|
336
311
|
type: 'updated',
|
|
337
312
|
action,
|
|
@@ -355,5 +330,6 @@ export function getDefaultState<
|
|
|
355
330
|
isPaused: false,
|
|
356
331
|
status: 'idle',
|
|
357
332
|
variables: undefined,
|
|
333
|
+
submittedAt: 0,
|
|
358
334
|
}
|
|
359
335
|
}
|
package/src/mutationCache.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { MutationObserver } from './mutationObserver'
|
|
2
|
-
import type { MutationOptions,
|
|
2
|
+
import type { NotifyEvent, MutationOptions, RegisteredError } from './types'
|
|
3
3
|
import type { QueryClient } from './queryClient'
|
|
4
4
|
import { notifyManager } from './notifyManager'
|
|
5
5
|
import type { Action, MutationState } from './mutation'
|
|
@@ -75,17 +75,14 @@ type MutationCacheListener = (event: MutationCacheNotifyEvent) => void
|
|
|
75
75
|
// CLASS
|
|
76
76
|
|
|
77
77
|
export class MutationCache extends Subscribable<MutationCacheListener> {
|
|
78
|
-
|
|
78
|
+
#mutations: Mutation<any, any, any, any>[]
|
|
79
|
+
#mutationId: number
|
|
80
|
+
#resuming: Promise<unknown> | undefined
|
|
79
81
|
|
|
80
|
-
|
|
81
|
-
private mutationId: number
|
|
82
|
-
private resuming: Promise<unknown> | undefined
|
|
83
|
-
|
|
84
|
-
constructor(config?: MutationCacheConfig) {
|
|
82
|
+
constructor(public config: MutationCacheConfig = {}) {
|
|
85
83
|
super()
|
|
86
|
-
this
|
|
87
|
-
this
|
|
88
|
-
this.mutationId = 0
|
|
84
|
+
this.#mutations = []
|
|
85
|
+
this.#mutationId = 0
|
|
89
86
|
}
|
|
90
87
|
|
|
91
88
|
build<TData, TError, TVariables, TContext>(
|
|
@@ -95,13 +92,9 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
|
|
|
95
92
|
): Mutation<TData, TError, TVariables, TContext> {
|
|
96
93
|
const mutation = new Mutation({
|
|
97
94
|
mutationCache: this,
|
|
98
|
-
|
|
99
|
-
mutationId: ++this.mutationId,
|
|
95
|
+
mutationId: ++this.#mutationId,
|
|
100
96
|
options: client.defaultMutationOptions(options),
|
|
101
97
|
state,
|
|
102
|
-
defaultOptions: options.mutationKey
|
|
103
|
-
? client.getMutationDefaults(options.mutationKey)
|
|
104
|
-
: undefined,
|
|
105
98
|
})
|
|
106
99
|
|
|
107
100
|
this.add(mutation)
|
|
@@ -110,39 +103,46 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
|
|
|
110
103
|
}
|
|
111
104
|
|
|
112
105
|
add(mutation: Mutation<any, any, any, any>): void {
|
|
113
|
-
this
|
|
106
|
+
this.#mutations.push(mutation)
|
|
114
107
|
this.notify({ type: 'added', mutation })
|
|
115
108
|
}
|
|
116
109
|
|
|
117
110
|
remove(mutation: Mutation<any, any, any, any>): void {
|
|
118
|
-
this
|
|
111
|
+
this.#mutations = this.#mutations.filter((x) => x !== mutation)
|
|
119
112
|
this.notify({ type: 'removed', mutation })
|
|
120
113
|
}
|
|
121
114
|
|
|
122
115
|
clear(): void {
|
|
123
116
|
notifyManager.batch(() => {
|
|
124
|
-
this
|
|
117
|
+
this.#mutations.forEach((mutation) => {
|
|
125
118
|
this.remove(mutation)
|
|
126
119
|
})
|
|
127
120
|
})
|
|
128
121
|
}
|
|
129
122
|
|
|
130
123
|
getAll(): Mutation[] {
|
|
131
|
-
return this
|
|
124
|
+
return this.#mutations
|
|
132
125
|
}
|
|
133
126
|
|
|
134
|
-
find<
|
|
127
|
+
find<
|
|
128
|
+
TData = unknown,
|
|
129
|
+
TError = RegisteredError,
|
|
130
|
+
TVariables = any,
|
|
131
|
+
TContext = unknown,
|
|
132
|
+
>(
|
|
135
133
|
filters: MutationFilters,
|
|
136
134
|
): Mutation<TData, TError, TVariables, TContext> | undefined {
|
|
137
135
|
if (typeof filters.exact === 'undefined') {
|
|
138
136
|
filters.exact = true
|
|
139
137
|
}
|
|
140
138
|
|
|
141
|
-
return this
|
|
139
|
+
return this.#mutations.find((mutation) => matchMutation(filters, mutation))
|
|
142
140
|
}
|
|
143
141
|
|
|
144
|
-
findAll(filters: MutationFilters): Mutation[] {
|
|
145
|
-
return this
|
|
142
|
+
findAll(filters: MutationFilters = {}): Mutation[] {
|
|
143
|
+
return this.#mutations.filter((mutation) =>
|
|
144
|
+
matchMutation(filters, mutation),
|
|
145
|
+
)
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
notify(event: MutationCacheNotifyEvent) {
|
|
@@ -154,9 +154,9 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
|
|
|
154
154
|
}
|
|
155
155
|
|
|
156
156
|
resumePausedMutations(): Promise<unknown> {
|
|
157
|
-
this
|
|
157
|
+
this.#resuming = (this.#resuming ?? Promise.resolve())
|
|
158
158
|
.then(() => {
|
|
159
|
-
const pausedMutations = this
|
|
159
|
+
const pausedMutations = this.#mutations.filter((x) => x.state.isPaused)
|
|
160
160
|
return notifyManager.batch(() =>
|
|
161
161
|
pausedMutations.reduce(
|
|
162
162
|
(promise, mutation) =>
|
|
@@ -166,9 +166,9 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
|
|
|
166
166
|
)
|
|
167
167
|
})
|
|
168
168
|
.then(() => {
|
|
169
|
-
this
|
|
169
|
+
this.#resuming = undefined
|
|
170
170
|
})
|
|
171
171
|
|
|
172
|
-
return this
|
|
172
|
+
return this.#resuming
|
|
173
173
|
}
|
|
174
174
|
}
|