@tanstack/query-core 4.24.10 → 5.0.0-alpha.0

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.
Files changed (183) hide show
  1. package/build/lib/focusManager.d.ts +1 -3
  2. package/build/lib/focusManager.esm.js +19 -36
  3. package/build/lib/focusManager.esm.js.map +1 -1
  4. package/build/lib/focusManager.js +19 -38
  5. package/build/lib/focusManager.js.map +1 -1
  6. package/build/lib/focusManager.mjs +19 -36
  7. package/build/lib/focusManager.mjs.map +1 -1
  8. package/build/lib/hydration.esm.js +21 -23
  9. package/build/lib/hydration.esm.js.map +1 -1
  10. package/build/lib/hydration.js +21 -25
  11. package/build/lib/hydration.js.map +1 -1
  12. package/build/lib/hydration.mjs +21 -23
  13. package/build/lib/hydration.mjs.map +1 -1
  14. package/build/lib/index.d.ts +1 -2
  15. package/build/lib/index.esm.js +1 -1
  16. package/build/lib/index.js +2 -8
  17. package/build/lib/index.js.map +1 -1
  18. package/build/lib/index.mjs +1 -1
  19. package/build/lib/infiniteQueryBehavior.d.ts +3 -7
  20. package/build/lib/infiniteQueryBehavior.esm.js +52 -75
  21. package/build/lib/infiniteQueryBehavior.esm.js.map +1 -1
  22. package/build/lib/infiniteQueryBehavior.js +50 -77
  23. package/build/lib/infiniteQueryBehavior.js.map +1 -1
  24. package/build/lib/infiniteQueryBehavior.mjs +52 -75
  25. package/build/lib/infiniteQueryBehavior.mjs.map +1 -1
  26. package/build/lib/infiniteQueryObserver.d.ts +4 -4
  27. package/build/lib/infiniteQueryObserver.esm.js +18 -26
  28. package/build/lib/infiniteQueryObserver.esm.js.map +1 -1
  29. package/build/lib/infiniteQueryObserver.js +18 -28
  30. package/build/lib/infiniteQueryObserver.js.map +1 -1
  31. package/build/lib/infiniteQueryObserver.mjs +18 -26
  32. package/build/lib/infiniteQueryObserver.mjs.map +1 -1
  33. package/build/lib/mutation.d.ts +11 -22
  34. package/build/lib/mutation.esm.js +73 -105
  35. package/build/lib/mutation.esm.js.map +1 -1
  36. package/build/lib/mutation.js +73 -107
  37. package/build/lib/mutation.js.map +1 -1
  38. package/build/lib/mutation.mjs +73 -105
  39. package/build/lib/mutation.mjs.map +1 -1
  40. package/build/lib/mutationCache.d.ts +4 -6
  41. package/build/lib/mutationCache.esm.js +23 -32
  42. package/build/lib/mutationCache.esm.js.map +1 -1
  43. package/build/lib/mutationCache.js +23 -34
  44. package/build/lib/mutationCache.js.map +1 -1
  45. package/build/lib/mutationCache.mjs +23 -32
  46. package/build/lib/mutationCache.mjs.map +1 -1
  47. package/build/lib/mutationObserver.d.ts +4 -9
  48. package/build/lib/mutationObserver.esm.js +43 -72
  49. package/build/lib/mutationObserver.esm.js.map +1 -1
  50. package/build/lib/mutationObserver.js +43 -74
  51. package/build/lib/mutationObserver.js.map +1 -1
  52. package/build/lib/mutationObserver.mjs +43 -72
  53. package/build/lib/mutationObserver.mjs.map +1 -1
  54. package/build/lib/notifyManager.esm.js +7 -17
  55. package/build/lib/notifyManager.esm.js.map +1 -1
  56. package/build/lib/notifyManager.js +7 -19
  57. package/build/lib/notifyManager.js.map +1 -1
  58. package/build/lib/notifyManager.mjs +7 -17
  59. package/build/lib/notifyManager.mjs.map +1 -1
  60. package/build/lib/onlineManager.d.ts +1 -3
  61. package/build/lib/onlineManager.esm.js +16 -30
  62. package/build/lib/onlineManager.esm.js.map +1 -1
  63. package/build/lib/onlineManager.js +16 -32
  64. package/build/lib/onlineManager.js.map +1 -1
  65. package/build/lib/onlineManager.mjs +16 -30
  66. package/build/lib/onlineManager.mjs.map +1 -1
  67. package/build/lib/queriesObserver.d.ts +3 -10
  68. package/build/lib/queriesObserver.esm.js +47 -71
  69. package/build/lib/queriesObserver.esm.js.map +1 -1
  70. package/build/lib/queriesObserver.js +49 -75
  71. package/build/lib/queriesObserver.js.map +1 -1
  72. package/build/lib/queriesObserver.mjs +47 -71
  73. package/build/lib/queriesObserver.mjs.map +1 -1
  74. package/build/lib/query.d.ts +14 -21
  75. package/build/lib/query.esm.js +140 -194
  76. package/build/lib/query.esm.js.map +1 -1
  77. package/build/lib/query.js +139 -195
  78. package/build/lib/query.js.map +1 -1
  79. package/build/lib/query.mjs +140 -194
  80. package/build/lib/query.mjs.map +1 -1
  81. package/build/lib/queryCache.d.ts +12 -7
  82. package/build/lib/queryCache.esm.js +21 -45
  83. package/build/lib/queryCache.esm.js.map +1 -1
  84. package/build/lib/queryCache.js +20 -46
  85. package/build/lib/queryCache.js.map +1 -1
  86. package/build/lib/queryCache.mjs +21 -45
  87. package/build/lib/queryCache.mjs.map +1 -1
  88. package/build/lib/queryClient.d.ts +18 -46
  89. package/build/lib/queryClient.esm.js +137 -216
  90. package/build/lib/queryClient.esm.js.map +1 -1
  91. package/build/lib/queryClient.js +136 -217
  92. package/build/lib/queryClient.js.map +1 -1
  93. package/build/lib/queryClient.mjs +137 -216
  94. package/build/lib/queryClient.mjs.map +1 -1
  95. package/build/lib/queryObserver.d.ts +4 -29
  96. package/build/lib/queryObserver.esm.js +176 -258
  97. package/build/lib/queryObserver.esm.js.map +1 -1
  98. package/build/lib/queryObserver.js +176 -260
  99. package/build/lib/queryObserver.js.map +1 -1
  100. package/build/lib/queryObserver.mjs +176 -258
  101. package/build/lib/queryObserver.mjs.map +1 -1
  102. package/build/lib/removable.d.ts +3 -3
  103. package/build/lib/removable.esm.js +10 -14
  104. package/build/lib/removable.esm.js.map +1 -1
  105. package/build/lib/removable.js +10 -16
  106. package/build/lib/removable.js.map +1 -1
  107. package/build/lib/removable.mjs +10 -14
  108. package/build/lib/removable.mjs.map +1 -1
  109. package/build/lib/retryer.d.ts +5 -5
  110. package/build/lib/retryer.esm.js +27 -44
  111. package/build/lib/retryer.esm.js.map +1 -1
  112. package/build/lib/retryer.js +27 -46
  113. package/build/lib/retryer.js.map +1 -1
  114. package/build/lib/retryer.mjs +27 -44
  115. package/build/lib/retryer.mjs.map +1 -1
  116. package/build/lib/subscribable.esm.js +4 -7
  117. package/build/lib/subscribable.esm.js.map +1 -1
  118. package/build/lib/subscribable.js +4 -9
  119. package/build/lib/subscribable.js.map +1 -1
  120. package/build/lib/subscribable.mjs +4 -7
  121. package/build/lib/subscribable.mjs.map +1 -1
  122. package/build/lib/tests/utils.d.ts +3 -12
  123. package/build/lib/types.d.ts +111 -99
  124. package/build/lib/utils.d.ts +8 -18
  125. package/build/lib/utils.esm.js +39 -132
  126. package/build/lib/utils.esm.js.map +1 -1
  127. package/build/lib/utils.js +42 -144
  128. package/build/lib/utils.js.map +1 -1
  129. package/build/lib/utils.mjs +39 -132
  130. package/build/lib/utils.mjs.map +1 -1
  131. package/build/umd/index.development.js +868 -1398
  132. package/build/umd/index.development.js.map +1 -1
  133. package/build/umd/index.production.js +1 -1
  134. package/build/umd/index.production.js.map +1 -1
  135. package/package.json +1 -1
  136. package/src/focusManager.ts +17 -24
  137. package/src/index.ts +1 -11
  138. package/src/infiniteQueryBehavior.ts +54 -94
  139. package/src/infiniteQueryObserver.ts +10 -12
  140. package/src/mutation.ts +68 -92
  141. package/src/mutationCache.ts +27 -27
  142. package/src/mutationObserver.ts +60 -97
  143. package/src/onlineManager.ts +14 -14
  144. package/src/queriesObserver.ts +50 -54
  145. package/src/query.ts +106 -110
  146. package/src/queryCache.ts +42 -41
  147. package/src/queryClient.ts +155 -434
  148. package/src/queryObserver.ts +155 -192
  149. package/src/removable.ts +13 -13
  150. package/src/retryer.ts +5 -5
  151. package/src/tests/focusManager.test.tsx +25 -25
  152. package/src/tests/hydration.test.tsx +167 -81
  153. package/src/tests/infiniteQueryBehavior.test.tsx +209 -17
  154. package/src/tests/infiniteQueryObserver.test.tsx +6 -2
  155. package/src/tests/mutationCache.test.tsx +127 -127
  156. package/src/tests/mutationObserver.test.tsx +1 -31
  157. package/src/tests/mutations.test.tsx +62 -43
  158. package/src/tests/onlineManager.test.tsx +12 -4
  159. package/src/tests/queriesObserver.test.tsx +41 -77
  160. package/src/tests/query.test.tsx +175 -243
  161. package/src/tests/queryCache.test.tsx +170 -93
  162. package/src/tests/queryClient.test.tsx +229 -378
  163. package/src/tests/queryObserver.test.tsx +23 -147
  164. package/src/tests/utils.test.tsx +84 -29
  165. package/src/tests/utils.ts +9 -18
  166. package/src/types.ts +187 -140
  167. package/src/utils.ts +31 -124
  168. package/build/lib/logger.d.ts +0 -8
  169. package/build/lib/logger.esm.js +0 -4
  170. package/build/lib/logger.esm.js.map +0 -1
  171. package/build/lib/logger.js +0 -8
  172. package/build/lib/logger.js.map +0 -1
  173. package/build/lib/logger.mjs +0 -4
  174. package/build/lib/logger.mjs.map +0 -1
  175. package/build/lib/logger.native.d.ts +0 -6
  176. package/build/lib/logger.native.esm.js +0 -12
  177. package/build/lib/logger.native.esm.js.map +0 -1
  178. package/build/lib/logger.native.js +0 -16
  179. package/build/lib/logger.native.js.map +0 -1
  180. package/build/lib/logger.native.mjs +0 -12
  181. package/build/lib/logger.native.mjs.map +0 -1
  182. package/src/logger.native.ts +0 -11
  183. package/src/logger.ts +0 -9
package/src/mutation.ts CHANGED
@@ -1,8 +1,11 @@
1
- import type { MutationOptions, MutationStatus, MutationMeta } from './types'
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 = unknown,
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 LoadingAction<TVariables, TContext> {
46
- type: 'loading'
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
- | LoadingAction<TVariables, TContext>
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 = unknown,
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
- private observers: MutationObserver<TData, TError, TVariables, TContext>[]
96
- private mutationCache: MutationCache
97
- private logger: Logger
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.mutationCache = config.mutationCache
109
- this.logger = config.logger || defaultLogger
110
- this.observers = []
100
+ this.#mutationCache = config.mutationCache
101
+ this.#observers = []
111
102
  this.state = config.state || getDefaultState()
112
103
 
113
- this.updateCacheTime(this.options.cacheTime)
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.observers.indexOf(observer) === -1) {
127
- this.observers.push(observer)
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.mutationCache.notify({
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.observers = this.observers.filter((x) => x !== observer)
128
+ this.#observers = this.#observers.filter((x) => x !== observer)
142
129
 
143
130
  this.scheduleGc()
144
131
 
145
- this.mutationCache.notify({
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.observers.length) {
154
- if (this.state.status === 'loading') {
140
+ if (!this.#observers.length) {
141
+ if (this.state.status === 'pending') {
155
142
  this.scheduleGc()
156
143
  } else {
157
- this.mutationCache.remove(this)
144
+ this.#mutationCache.remove(this)
158
145
  }
159
146
  }
160
147
  }
161
148
 
162
149
  continue(): Promise<unknown> {
163
- return this.retryer?.continue() ?? this.execute()
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.retryer = createRetryer({
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(this.state.variables!)
164
+ return this.options.mutationFn(variables)
174
165
  },
175
166
  onFail: (failureCount, error) => {
176
- this.dispatch({ type: 'failed', failureCount, error })
167
+ this.#dispatch({ type: 'failed', failureCount, error })
177
168
  },
178
169
  onPause: () => {
179
- this.dispatch({ type: 'pause' })
170
+ this.#dispatch({ type: 'pause' })
180
171
  },
181
172
  onContinue: () => {
182
- this.dispatch({ type: 'continue' })
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.retryer.promise
180
+ return this.#retryer.promise
190
181
  }
191
182
 
192
- const restored = this.state.status === 'loading'
183
+ const restored = this.state.status === 'pending'
184
+
193
185
  try {
194
186
  if (!restored) {
195
- this.dispatch({ type: 'loading', variables: this.options.variables! })
187
+ this.#dispatch({ type: 'pending', variables })
196
188
  // Notify cache callback
197
- await this.mutationCache.config.onMutate?.(
198
- this.state.variables,
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?.(this.state.variables!)
193
+ const context = await this.options.onMutate?.(variables)
202
194
  if (context !== this.state.context) {
203
- this.dispatch({
204
- type: 'loading',
195
+ this.#dispatch({
196
+ type: 'pending',
205
197
  context,
206
- variables: this.state.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.mutationCache.config.onSuccess?.(
205
+ await this.#mutationCache.config.onSuccess?.(
214
206
  data,
215
- this.state.variables,
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.dispatch({ type: 'success', data })
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.mutationCache.config.onError?.(
221
+ await this.#mutationCache.config.onError?.(
239
222
  error,
240
- this.state.variables,
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
- this.state.variables!,
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
- this.state.variables!,
237
+ variables,
259
238
  this.state.context,
260
239
  )
261
240
  throw error
262
241
  } finally {
263
- this.dispatch({ type: 'error', error: error as TError })
242
+ this.#dispatch({ type: 'error', error: error as TError })
264
243
  }
265
244
  }
266
245
  }
267
246
 
268
- private dispatch(action: Action<TData, TError, TVariables, TContext>): void {
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 'loading':
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: 'loading',
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.observers.forEach((observer) => {
306
+ this.#observers.forEach((observer) => {
332
307
  observer.onMutationUpdate(action)
333
308
  })
334
- this.mutationCache.notify({
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
  }
@@ -1,5 +1,5 @@
1
1
  import type { MutationObserver } from './mutationObserver'
2
- import type { MutationOptions, NotifyEvent } from './types'
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
- config: MutationCacheConfig
78
+ #mutations: Mutation<any, any, any, any>[]
79
+ #mutationId: number
80
+ #resuming: Promise<unknown> | undefined
79
81
 
80
- private mutations: Mutation<any, any, any, any>[]
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.config = config || {}
87
- this.mutations = []
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
- logger: client.getLogger(),
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.mutations.push(mutation)
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.mutations = this.mutations.filter((x) => x !== mutation)
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.mutations.forEach((mutation) => {
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.mutations
124
+ return this.#mutations
132
125
  }
133
126
 
134
- find<TData = unknown, TError = unknown, TVariables = any, TContext = unknown>(
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.mutations.find((mutation) => matchMutation(filters, mutation))
139
+ return this.#mutations.find((mutation) => matchMutation(filters, mutation))
142
140
  }
143
141
 
144
- findAll(filters: MutationFilters): Mutation[] {
145
- return this.mutations.filter((mutation) => matchMutation(filters, mutation))
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.resuming = (this.resuming ?? Promise.resolve())
157
+ this.#resuming = (this.#resuming ?? Promise.resolve())
158
158
  .then(() => {
159
- const pausedMutations = this.mutations.filter((x) => x.state.isPaused)
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.resuming = undefined
169
+ this.#resuming = undefined
170
170
  })
171
171
 
172
- return this.resuming
172
+ return this.#resuming
173
173
  }
174
174
  }