forked from TanStack/query
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutationCache.ts
More file actions
309 lines (277 loc) 路 9.1 KB
/
Copy pathmutationCache.ts
File metadata and controls
309 lines (277 loc) 路 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import { notifyManager } from './notifyManager'
import { Mutation } from './mutation'
import { hashKey, matchMutation, noop } from './utils'
import { Subscribable } from './subscribable'
import type { MutationObserver } from './mutationObserver'
import type {
DefaultError,
MutationFunctionContext,
MutationOptions,
NotifyEvent,
} from './types'
import type { QueryClient } from './queryClient'
import type { Action, MutationState } from './mutation'
import type { MutationFilters } from './utils'
// TYPES
interface MutationCacheConfig {
onError?: (
error: DefaultError,
variables: unknown,
onMutateResult: unknown,
mutation: Mutation<unknown, unknown, unknown>,
context: MutationFunctionContext,
) => Promise<unknown> | unknown
onSuccess?: (
data: unknown,
variables: unknown,
onMutateResult: unknown,
mutation: Mutation<unknown, unknown, unknown>,
context: MutationFunctionContext,
) => Promise<unknown> | unknown
onMutate?: (
variables: unknown,
mutation: Mutation<unknown, unknown, unknown>,
context: MutationFunctionContext,
) => Promise<unknown> | unknown
onSettled?: (
data: unknown | undefined,
error: DefaultError | null,
variables: unknown,
onMutateResult: unknown,
mutation: Mutation<unknown, unknown, unknown>,
context: MutationFunctionContext,
) => Promise<unknown> | unknown
}
interface NotifyEventMutationAdded extends NotifyEvent {
type: 'added'
mutation: Mutation<any, any, any, any>
}
interface NotifyEventMutationRemoved extends NotifyEvent {
type: 'removed'
mutation: Mutation<any, any, any, any>
}
interface NotifyEventMutationObserverAdded extends NotifyEvent {
type: 'observerAdded'
mutation: Mutation<any, any, any, any>
observer: MutationObserver<any, any, any>
}
interface NotifyEventMutationObserverRemoved extends NotifyEvent {
type: 'observerRemoved'
mutation: Mutation<any, any, any, any>
observer: MutationObserver<any, any, any>
}
interface NotifyEventMutationObserverOptionsUpdated extends NotifyEvent {
type: 'observerOptionsUpdated'
mutation?: Mutation<any, any, any, any>
observer: MutationObserver<any, any, any, any>
}
interface NotifyEventMutationUpdated extends NotifyEvent {
type: 'updated'
mutation: Mutation<any, any, any, any>
action: Action<any, any, any, any>
}
export type MutationCacheNotifyEvent =
| NotifyEventMutationAdded
| NotifyEventMutationRemoved
| NotifyEventMutationObserverAdded
| NotifyEventMutationObserverRemoved
| NotifyEventMutationObserverOptionsUpdated
| NotifyEventMutationUpdated
type MutationCacheListener = (event: MutationCacheNotifyEvent) => void
// CLASS
export class MutationCache extends Subscribable<MutationCacheListener> {
#mutations: Set<Mutation<any, any, any, any>>
#scopes: Map<string, Array<Mutation<any, any, any, any>>>
#byMutationKey: Map<string, Array<Mutation<any, any, any, any>>>
#mutationId: number
constructor(public config: MutationCacheConfig = {}) {
super()
this.#mutations = new Set()
this.#scopes = new Map()
this.#byMutationKey = new Map()
this.#mutationId = 0
}
build<TData, TError, TVariables, TOnMutateResult>(
client: QueryClient,
options: MutationOptions<TData, TError, TVariables, TOnMutateResult>,
state?: MutationState<TData, TError, TVariables, TOnMutateResult>,
): Mutation<TData, TError, TVariables, TOnMutateResult> {
const mutation = new Mutation({
client,
mutationCache: this,
mutationId: ++this.#mutationId,
options: client.defaultMutationOptions(options),
state,
})
this.add(mutation)
return mutation
}
add(mutation: Mutation<any, any, any, any>): void {
this.#mutations.add(mutation)
const scope = scopeFor(mutation)
if (typeof scope === 'string') {
const scopedMutations = this.#scopes.get(scope)
if (scopedMutations) {
scopedMutations.push(mutation)
} else {
this.#scopes.set(scope, [mutation])
}
}
const { mutationKey } = mutation.options
if (mutationKey) {
const hash = hashKey(mutationKey)
const keyed = this.#byMutationKey.get(hash)
if (keyed) keyed.push(mutation)
else this.#byMutationKey.set(hash, [mutation])
}
this.notify({ type: 'added', mutation })
}
remove(mutation: Mutation<any, any, any, any>): void {
if (this.#mutations.delete(mutation)) {
const scope = scopeFor(mutation)
if (typeof scope === 'string') {
const scopedMutations = this.#scopes.get(scope)
if (scopedMutations) {
if (scopedMutations.length > 1) {
const index = scopedMutations.indexOf(mutation)
if (index !== -1) {
scopedMutations.splice(index, 1)
}
} else if (scopedMutations[0] === mutation) {
this.#scopes.delete(scope)
}
}
}
const { mutationKey } = mutation.options
if (mutationKey) {
const hash = hashKey(mutationKey)
const keyed = this.#byMutationKey.get(hash)
if (keyed) {
if (keyed.length > 1) {
const index = keyed.indexOf(mutation)
if (index !== -1) keyed.splice(index, 1)
} else {
this.#byMutationKey.delete(hash)
}
}
}
}
// Currently we notify the removal even if the mutation was already removed.
// Consider making this an error or not notifying of the removal depending on the desired semantics.
this.notify({ type: 'removed', mutation })
}
canRun(mutation: Mutation<any, any, any, any>): boolean {
const scope = scopeFor(mutation)
if (typeof scope === 'string') {
const mutationsWithSameScope = this.#scopes.get(scope)
const firstPendingMutation = mutationsWithSameScope?.find(
(m) => m.state.status === 'pending',
)
// we can run if there is no current pending mutation (start use-case)
// or if WE are the first pending mutation (continue use-case)
return !firstPendingMutation || firstPendingMutation === mutation
} else {
// For unscoped mutations there are never any pending mutations in front of the
// current mutation
return true
}
}
runNext(mutation: Mutation<any, any, any, any>): Promise<unknown> {
const scope = scopeFor(mutation)
if (typeof scope === 'string') {
const foundMutation = this.#scopes
.get(scope)
?.find((m) => m !== mutation && m.state.isPaused)
return foundMutation?.continue() ?? Promise.resolve()
} else {
return Promise.resolve()
}
}
clear(): void {
notifyManager.batch(() => {
this.#mutations.forEach((mutation) => {
this.notify({ type: 'removed', mutation })
})
this.#mutations.clear()
this.#scopes.clear()
this.#byMutationKey.clear()
})
}
getAll(): Array<Mutation> {
return Array.from(this.#mutations)
}
find<
TData = unknown,
TError = DefaultError,
TVariables = any,
TOnMutateResult = unknown,
>(
filters: MutationFilters,
): Mutation<TData, TError, TVariables, TOnMutateResult> | undefined {
const defaultedFilters = { exact: true, ...filters }
if (defaultedFilters.exact && defaultedFilters.mutationKey) {
const candidates = this.#byMutationKey.get(
hashKey(defaultedFilters.mutationKey),
)
if (!candidates) return undefined
const { mutationKey: _m, ...filtersWithoutKey } = defaultedFilters
for (const mutation of candidates) {
if (matchMutation(filtersWithoutKey as MutationFilters, mutation)) {
return mutation as Mutation<
TData,
TError,
TVariables,
TOnMutateResult
>
}
}
return undefined
}
for (const mutation of this.#mutations) {
if (matchMutation(defaultedFilters, mutation)) {
return mutation as Mutation<TData, TError, TVariables, TOnMutateResult>
}
}
return undefined
}
findAll(filters: MutationFilters = {}): Array<Mutation> {
if (filters.exact && filters.mutationKey) {
const candidates = this.#byMutationKey.get(hashKey(filters.mutationKey))
if (!candidates) return []
const { mutationKey: _m, ...filtersWithoutKey } = filters
return candidates.filter((m) =>
matchMutation(filtersWithoutKey as MutationFilters, m),
)
}
const result: Array<Mutation> = []
for (const mutation of this.#mutations) {
if (matchMutation(filters, mutation)) {
result.push(mutation)
}
}
return result
}
notify(event: MutationCacheNotifyEvent) {
notifyManager.batch(() => {
this.listeners.forEach((listener) => {
listener(event)
})
})
}
resumePausedMutations(): Promise<unknown> {
const pausedMutations: Array<Mutation> = []
for (const mutation of this.#mutations) {
if (mutation.state.isPaused) {
pausedMutations.push(mutation)
}
}
return notifyManager.batch(() =>
Promise.all(
pausedMutations.map((mutation) => mutation.continue().catch(noop)),
),
)
}
}
function scopeFor(mutation: Mutation<any, any, any, any>) {
return mutation.options.scope?.id
}