@tanstack/query-core 4.3.6 → 4.3.7

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 (43) hide show
  1. package/build/lib/focusManager.esm.js +86 -0
  2. package/build/lib/focusManager.esm.js.map +1 -0
  3. package/build/lib/hydration.esm.js +97 -0
  4. package/build/lib/hydration.esm.js.map +1 -0
  5. package/build/lib/index.esm.js +14 -0
  6. package/build/lib/index.esm.js.map +1 -0
  7. package/build/lib/infiniteQueryBehavior.esm.js +142 -0
  8. package/build/lib/infiniteQueryBehavior.esm.js.map +1 -0
  9. package/build/lib/infiniteQueryObserver.esm.js +78 -0
  10. package/build/lib/infiniteQueryObserver.esm.js.map +1 -0
  11. package/build/lib/logger.esm.js +4 -0
  12. package/build/lib/logger.esm.js.map +1 -0
  13. package/build/lib/logger.native.esm.js +12 -0
  14. package/build/lib/logger.native.esm.js.map +1 -0
  15. package/build/lib/mutation.esm.js +243 -0
  16. package/build/lib/mutation.esm.js.map +1 -0
  17. package/build/lib/mutationCache.esm.js +85 -0
  18. package/build/lib/mutationCache.esm.js.map +1 -0
  19. package/build/lib/mutationObserver.esm.js +126 -0
  20. package/build/lib/mutationObserver.esm.js.map +1 -0
  21. package/build/lib/notifyManager.esm.js +99 -0
  22. package/build/lib/notifyManager.esm.js.map +1 -0
  23. package/build/lib/onlineManager.esm.js +85 -0
  24. package/build/lib/onlineManager.esm.js.map +1 -0
  25. package/build/lib/queriesObserver.esm.js +156 -0
  26. package/build/lib/queriesObserver.esm.js.map +1 -0
  27. package/build/lib/query.esm.js +460 -0
  28. package/build/lib/query.esm.js.map +1 -0
  29. package/build/lib/queryCache.esm.js +126 -0
  30. package/build/lib/queryCache.esm.js.map +1 -0
  31. package/build/lib/queryClient.esm.js +339 -0
  32. package/build/lib/queryClient.esm.js.map +1 -0
  33. package/build/lib/queryObserver.esm.js +515 -0
  34. package/build/lib/queryObserver.esm.js.map +1 -0
  35. package/build/lib/removable.esm.js +33 -0
  36. package/build/lib/removable.esm.js.map +1 -0
  37. package/build/lib/retryer.esm.js +160 -0
  38. package/build/lib/retryer.esm.js.map +1 -0
  39. package/build/lib/subscribable.esm.js +29 -0
  40. package/build/lib/subscribable.esm.js.map +1 -0
  41. package/build/lib/utils.esm.js +318 -0
  42. package/build/lib/utils.esm.js.map +1 -0
  43. package/package.json +2 -1
@@ -0,0 +1,243 @@
1
+ import { defaultLogger } from './logger.esm.js';
2
+ import { notifyManager } from './notifyManager.esm.js';
3
+ import { Removable } from './removable.esm.js';
4
+ import { createRetryer, canFetch } from './retryer.esm.js';
5
+
6
+ // CLASS
7
+ class Mutation extends Removable {
8
+ constructor(config) {
9
+ super();
10
+ this.options = { ...config.defaultOptions,
11
+ ...config.options
12
+ };
13
+ this.mutationId = config.mutationId;
14
+ this.mutationCache = config.mutationCache;
15
+ this.logger = config.logger || defaultLogger;
16
+ this.observers = [];
17
+ this.state = config.state || getDefaultState();
18
+ this.meta = config.meta;
19
+ this.updateCacheTime(this.options.cacheTime);
20
+ this.scheduleGc();
21
+ }
22
+
23
+ setState(state) {
24
+ this.dispatch({
25
+ type: 'setState',
26
+ state
27
+ });
28
+ }
29
+
30
+ addObserver(observer) {
31
+ if (this.observers.indexOf(observer) === -1) {
32
+ this.observers.push(observer); // Stop the mutation from being garbage collected
33
+
34
+ this.clearGcTimeout();
35
+ this.mutationCache.notify({
36
+ type: 'observerAdded',
37
+ mutation: this,
38
+ observer
39
+ });
40
+ }
41
+ }
42
+
43
+ removeObserver(observer) {
44
+ this.observers = this.observers.filter(x => x !== observer);
45
+ this.scheduleGc();
46
+ this.mutationCache.notify({
47
+ type: 'observerRemoved',
48
+ mutation: this,
49
+ observer
50
+ });
51
+ }
52
+
53
+ optionalRemove() {
54
+ if (!this.observers.length) {
55
+ if (this.state.status === 'loading') {
56
+ this.scheduleGc();
57
+ } else {
58
+ this.mutationCache.remove(this);
59
+ }
60
+ }
61
+ }
62
+
63
+ continue() {
64
+ if (this.retryer) {
65
+ this.retryer.continue();
66
+ return this.retryer.promise;
67
+ }
68
+
69
+ return this.execute();
70
+ }
71
+
72
+ async execute() {
73
+ const executeMutation = () => {
74
+ var _this$options$retry;
75
+
76
+ this.retryer = createRetryer({
77
+ fn: () => {
78
+ if (!this.options.mutationFn) {
79
+ return Promise.reject('No mutationFn found');
80
+ }
81
+
82
+ return this.options.mutationFn(this.state.variables);
83
+ },
84
+ onFail: () => {
85
+ this.dispatch({
86
+ type: 'failed'
87
+ });
88
+ },
89
+ onPause: () => {
90
+ this.dispatch({
91
+ type: 'pause'
92
+ });
93
+ },
94
+ onContinue: () => {
95
+ this.dispatch({
96
+ type: 'continue'
97
+ });
98
+ },
99
+ retry: (_this$options$retry = this.options.retry) != null ? _this$options$retry : 0,
100
+ retryDelay: this.options.retryDelay,
101
+ networkMode: this.options.networkMode
102
+ });
103
+ return this.retryer.promise;
104
+ };
105
+
106
+ const restored = this.state.status === 'loading';
107
+
108
+ try {
109
+ var _this$mutationCache$c3, _this$mutationCache$c4, _this$options$onSucce, _this$options2, _this$options$onSettl, _this$options3;
110
+
111
+ if (!restored) {
112
+ var _this$mutationCache$c, _this$mutationCache$c2, _this$options$onMutat, _this$options;
113
+
114
+ this.dispatch({
115
+ type: 'loading',
116
+ variables: this.options.variables
117
+ }); // Notify cache callback
118
+
119
+ (_this$mutationCache$c = (_this$mutationCache$c2 = this.mutationCache.config).onMutate) == null ? void 0 : _this$mutationCache$c.call(_this$mutationCache$c2, this.state.variables, this);
120
+ const context = await ((_this$options$onMutat = (_this$options = this.options).onMutate) == null ? void 0 : _this$options$onMutat.call(_this$options, this.state.variables));
121
+
122
+ if (context !== this.state.context) {
123
+ this.dispatch({
124
+ type: 'loading',
125
+ context,
126
+ variables: this.state.variables
127
+ });
128
+ }
129
+ }
130
+
131
+ const data = await executeMutation(); // Notify cache callback
132
+
133
+ (_this$mutationCache$c3 = (_this$mutationCache$c4 = this.mutationCache.config).onSuccess) == null ? void 0 : _this$mutationCache$c3.call(_this$mutationCache$c4, data, this.state.variables, this.state.context, this);
134
+ await ((_this$options$onSucce = (_this$options2 = this.options).onSuccess) == null ? void 0 : _this$options$onSucce.call(_this$options2, data, this.state.variables, this.state.context));
135
+ await ((_this$options$onSettl = (_this$options3 = this.options).onSettled) == null ? void 0 : _this$options$onSettl.call(_this$options3, data, null, this.state.variables, this.state.context));
136
+ this.dispatch({
137
+ type: 'success',
138
+ data
139
+ });
140
+ return data;
141
+ } catch (error) {
142
+ try {
143
+ var _this$mutationCache$c5, _this$mutationCache$c6, _this$options$onError, _this$options4, _this$options$onSettl2, _this$options5;
144
+
145
+ // Notify cache callback
146
+ (_this$mutationCache$c5 = (_this$mutationCache$c6 = this.mutationCache.config).onError) == null ? void 0 : _this$mutationCache$c5.call(_this$mutationCache$c6, error, this.state.variables, this.state.context, this);
147
+
148
+ if (process.env.NODE_ENV !== 'production') {
149
+ this.logger.error(error);
150
+ }
151
+
152
+ await ((_this$options$onError = (_this$options4 = this.options).onError) == null ? void 0 : _this$options$onError.call(_this$options4, error, this.state.variables, this.state.context));
153
+ await ((_this$options$onSettl2 = (_this$options5 = this.options).onSettled) == null ? void 0 : _this$options$onSettl2.call(_this$options5, undefined, error, this.state.variables, this.state.context));
154
+ throw error;
155
+ } finally {
156
+ this.dispatch({
157
+ type: 'error',
158
+ error: error
159
+ });
160
+ }
161
+ }
162
+ }
163
+
164
+ dispatch(action) {
165
+ const reducer = state => {
166
+ switch (action.type) {
167
+ case 'failed':
168
+ return { ...state,
169
+ failureCount: state.failureCount + 1
170
+ };
171
+
172
+ case 'pause':
173
+ return { ...state,
174
+ isPaused: true
175
+ };
176
+
177
+ case 'continue':
178
+ return { ...state,
179
+ isPaused: false
180
+ };
181
+
182
+ case 'loading':
183
+ return { ...state,
184
+ context: action.context,
185
+ data: undefined,
186
+ error: null,
187
+ isPaused: !canFetch(this.options.networkMode),
188
+ status: 'loading',
189
+ variables: action.variables
190
+ };
191
+
192
+ case 'success':
193
+ return { ...state,
194
+ data: action.data,
195
+ error: null,
196
+ status: 'success',
197
+ isPaused: false
198
+ };
199
+
200
+ case 'error':
201
+ return { ...state,
202
+ data: undefined,
203
+ error: action.error,
204
+ failureCount: state.failureCount + 1,
205
+ isPaused: false,
206
+ status: 'error'
207
+ };
208
+
209
+ case 'setState':
210
+ return { ...state,
211
+ ...action.state
212
+ };
213
+ }
214
+ };
215
+
216
+ this.state = reducer(this.state);
217
+ notifyManager.batch(() => {
218
+ this.observers.forEach(observer => {
219
+ observer.onMutationUpdate(action);
220
+ });
221
+ this.mutationCache.notify({
222
+ mutation: this,
223
+ type: 'updated',
224
+ action
225
+ });
226
+ });
227
+ }
228
+
229
+ }
230
+ function getDefaultState() {
231
+ return {
232
+ context: undefined,
233
+ data: undefined,
234
+ error: null,
235
+ failureCount: 0,
236
+ isPaused: false,
237
+ status: 'idle',
238
+ variables: undefined
239
+ };
240
+ }
241
+
242
+ export { Mutation, getDefaultState };
243
+ //# sourceMappingURL=mutation.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mutation.esm.js","sources":["../../src/mutation.ts"],"sourcesContent":["import type { MutationOptions, MutationStatus, MutationMeta } from './types'\nimport type { MutationCache } from './mutationCache'\nimport type { MutationObserver } from './mutationObserver'\nimport { defaultLogger, Logger } from './logger'\nimport { notifyManager } from './notifyManager'\nimport { Removable } from './removable'\nimport { canFetch, Retryer, createRetryer } from './retryer'\n\n// TYPES\n\ninterface MutationConfig<TData, TError, TVariables, TContext> {\n mutationId: number\n mutationCache: MutationCache\n options: MutationOptions<TData, TError, TVariables, TContext>\n logger?: Logger\n defaultOptions?: MutationOptions<TData, TError, TVariables, TContext>\n state?: MutationState<TData, TError, TVariables, TContext>\n meta?: MutationMeta\n}\n\nexport interface MutationState<\n TData = unknown,\n TError = unknown,\n TVariables = void,\n TContext = unknown,\n> {\n context: TContext | undefined\n data: TData | undefined\n error: TError | null\n failureCount: number\n isPaused: boolean\n status: MutationStatus\n variables: TVariables | undefined\n}\n\ninterface FailedAction {\n type: 'failed'\n}\n\ninterface LoadingAction<TVariables, TContext> {\n type: 'loading'\n variables?: TVariables\n context?: TContext\n}\n\ninterface SuccessAction<TData> {\n type: 'success'\n data: TData\n}\n\ninterface ErrorAction<TError> {\n type: 'error'\n error: TError\n}\n\ninterface PauseAction {\n type: 'pause'\n}\n\ninterface ContinueAction {\n type: 'continue'\n}\n\ninterface SetStateAction<TData, TError, TVariables, TContext> {\n type: 'setState'\n state: MutationState<TData, TError, TVariables, TContext>\n}\n\nexport type Action<TData, TError, TVariables, TContext> =\n | ContinueAction\n | ErrorAction<TError>\n | FailedAction\n | LoadingAction<TVariables, TContext>\n | PauseAction\n | SetStateAction<TData, TError, TVariables, TContext>\n | SuccessAction<TData>\n\n// CLASS\n\nexport class Mutation<\n TData = unknown,\n TError = unknown,\n TVariables = void,\n TContext = unknown,\n> extends Removable {\n state: MutationState<TData, TError, TVariables, TContext>\n options: MutationOptions<TData, TError, TVariables, TContext>\n mutationId: number\n meta: MutationMeta | undefined\n\n private observers: MutationObserver<TData, TError, TVariables, TContext>[]\n private mutationCache: MutationCache\n private logger: Logger\n private retryer?: Retryer<TData>\n\n constructor(config: MutationConfig<TData, TError, TVariables, TContext>) {\n super()\n\n this.options = {\n ...config.defaultOptions,\n ...config.options,\n }\n this.mutationId = config.mutationId\n this.mutationCache = config.mutationCache\n this.logger = config.logger || defaultLogger\n this.observers = []\n this.state = config.state || getDefaultState()\n this.meta = config.meta\n\n this.updateCacheTime(this.options.cacheTime)\n this.scheduleGc()\n }\n\n setState(state: MutationState<TData, TError, TVariables, TContext>): void {\n this.dispatch({ type: 'setState', state })\n }\n\n addObserver(observer: MutationObserver<any, any, any, any>): void {\n if (this.observers.indexOf(observer) === -1) {\n this.observers.push(observer)\n\n // Stop the mutation from being garbage collected\n this.clearGcTimeout()\n\n this.mutationCache.notify({\n type: 'observerAdded',\n mutation: this,\n observer,\n })\n }\n }\n\n removeObserver(observer: MutationObserver<any, any, any, any>): void {\n this.observers = this.observers.filter((x) => x !== observer)\n\n this.scheduleGc()\n\n this.mutationCache.notify({\n type: 'observerRemoved',\n mutation: this,\n observer,\n })\n }\n\n protected optionalRemove() {\n if (!this.observers.length) {\n if (this.state.status === 'loading') {\n this.scheduleGc()\n } else {\n this.mutationCache.remove(this)\n }\n }\n }\n\n continue(): Promise<TData> {\n if (this.retryer) {\n this.retryer.continue()\n return this.retryer.promise\n }\n return this.execute()\n }\n\n async execute(): Promise<TData> {\n const executeMutation = () => {\n this.retryer = createRetryer({\n fn: () => {\n if (!this.options.mutationFn) {\n return Promise.reject('No mutationFn found')\n }\n return this.options.mutationFn(this.state.variables!)\n },\n onFail: () => {\n this.dispatch({ type: 'failed' })\n },\n onPause: () => {\n this.dispatch({ type: 'pause' })\n },\n onContinue: () => {\n this.dispatch({ type: 'continue' })\n },\n retry: this.options.retry ?? 0,\n retryDelay: this.options.retryDelay,\n networkMode: this.options.networkMode,\n })\n\n return this.retryer.promise\n }\n\n const restored = this.state.status === 'loading'\n try {\n if (!restored) {\n this.dispatch({ type: 'loading', variables: this.options.variables! })\n // Notify cache callback\n this.mutationCache.config.onMutate?.(\n this.state.variables,\n this as Mutation<unknown, unknown, unknown, unknown>,\n )\n const context = await this.options.onMutate?.(this.state.variables!)\n if (context !== this.state.context) {\n this.dispatch({\n type: 'loading',\n context,\n variables: this.state.variables,\n })\n }\n }\n const data = await executeMutation()\n\n // Notify cache callback\n this.mutationCache.config.onSuccess?.(\n data,\n this.state.variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n )\n\n await this.options.onSuccess?.(\n data,\n this.state.variables!,\n this.state.context!,\n )\n\n await this.options.onSettled?.(\n data,\n null,\n this.state.variables!,\n this.state.context,\n )\n\n this.dispatch({ type: 'success', data })\n return data\n } catch (error) {\n try {\n // Notify cache callback\n this.mutationCache.config.onError?.(\n error,\n this.state.variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n )\n\n if (process.env.NODE_ENV !== 'production') {\n this.logger.error(error)\n }\n\n await this.options.onError?.(\n error as TError,\n this.state.variables!,\n this.state.context,\n )\n\n await this.options.onSettled?.(\n undefined,\n error as TError,\n this.state.variables!,\n this.state.context,\n )\n throw error\n } finally {\n this.dispatch({ type: 'error', error: error as TError })\n }\n }\n }\n\n private dispatch(action: Action<TData, TError, TVariables, TContext>): void {\n const reducer = (\n state: MutationState<TData, TError, TVariables, TContext>,\n ): MutationState<TData, TError, TVariables, TContext> => {\n switch (action.type) {\n case 'failed':\n return {\n ...state,\n failureCount: state.failureCount + 1,\n }\n case 'pause':\n return {\n ...state,\n isPaused: true,\n }\n case 'continue':\n return {\n ...state,\n isPaused: false,\n }\n case 'loading':\n return {\n ...state,\n context: action.context,\n data: undefined,\n error: null,\n isPaused: !canFetch(this.options.networkMode),\n status: 'loading',\n variables: action.variables,\n }\n case 'success':\n return {\n ...state,\n data: action.data,\n error: null,\n status: 'success',\n isPaused: false,\n }\n case 'error':\n return {\n ...state,\n data: undefined,\n error: action.error,\n failureCount: state.failureCount + 1,\n isPaused: false,\n status: 'error',\n }\n case 'setState':\n return {\n ...state,\n ...action.state,\n }\n }\n }\n this.state = reducer(this.state)\n\n notifyManager.batch(() => {\n this.observers.forEach((observer) => {\n observer.onMutationUpdate(action)\n })\n this.mutationCache.notify({\n mutation: this,\n type: 'updated',\n action,\n })\n })\n }\n}\n\nexport function getDefaultState<\n TData,\n TError,\n TVariables,\n TContext,\n>(): MutationState<TData, TError, TVariables, TContext> {\n return {\n context: undefined,\n data: undefined,\n error: null,\n failureCount: 0,\n isPaused: false,\n status: 'idle',\n variables: undefined,\n }\n}\n"],"names":["Mutation","Removable","constructor","config","options","defaultOptions","mutationId","mutationCache","logger","defaultLogger","observers","state","getDefaultState","meta","updateCacheTime","cacheTime","scheduleGc","setState","dispatch","type","addObserver","observer","indexOf","push","clearGcTimeout","notify","mutation","removeObserver","filter","x","optionalRemove","length","status","remove","continue","retryer","promise","execute","executeMutation","createRetryer","fn","mutationFn","Promise","reject","variables","onFail","onPause","onContinue","retry","retryDelay","networkMode","restored","onMutate","context","data","onSuccess","onSettled","error","onError","process","env","NODE_ENV","undefined","action","reducer","failureCount","isPaused","canFetch","notifyManager","batch","forEach","onMutationUpdate"],"mappings":";;;;;AA6EA;AAEO,MAAMA,QAAN,SAKGC,SALH,CAKa;EAWlBC,WAAW,CAACC,MAAD,EAA8D;AACvE,IAAA,KAAA,EAAA,CAAA;AAEA,IAAA,IAAA,CAAKC,OAAL,GAAe,EACb,GAAGD,MAAM,CAACE,cADG;AAEb,MAAA,GAAGF,MAAM,CAACC,OAAAA;KAFZ,CAAA;AAIA,IAAA,IAAA,CAAKE,UAAL,GAAkBH,MAAM,CAACG,UAAzB,CAAA;AACA,IAAA,IAAA,CAAKC,aAAL,GAAqBJ,MAAM,CAACI,aAA5B,CAAA;AACA,IAAA,IAAA,CAAKC,MAAL,GAAcL,MAAM,CAACK,MAAP,IAAiBC,aAA/B,CAAA;IACA,IAAKC,CAAAA,SAAL,GAAiB,EAAjB,CAAA;AACA,IAAA,IAAA,CAAKC,KAAL,GAAaR,MAAM,CAACQ,KAAP,IAAgBC,eAAe,EAA5C,CAAA;AACA,IAAA,IAAA,CAAKC,IAAL,GAAYV,MAAM,CAACU,IAAnB,CAAA;AAEA,IAAA,IAAA,CAAKC,eAAL,CAAqB,IAAKV,CAAAA,OAAL,CAAaW,SAAlC,CAAA,CAAA;AACA,IAAA,IAAA,CAAKC,UAAL,EAAA,CAAA;AACD,GAAA;;EAEDC,QAAQ,CAACN,KAAD,EAAkE;AACxE,IAAA,IAAA,CAAKO,QAAL,CAAc;AAAEC,MAAAA,IAAI,EAAE,UAAR;AAAoBR,MAAAA,KAAAA;KAAlC,CAAA,CAAA;AACD,GAAA;;EAEDS,WAAW,CAACC,QAAD,EAAuD;IAChE,IAAI,IAAA,CAAKX,SAAL,CAAeY,OAAf,CAAuBD,QAAvB,CAAA,KAAqC,CAAC,CAA1C,EAA6C;AAC3C,MAAA,IAAA,CA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AGA,IAAKxD,CAAAA,aAAL,CAAmBkB,MAAnB,CAA0B;AACxBC,QAAAA,QAAQ,EAAE,IADc;AAExBP,QAAAA,IAAI,EAAE,SAFkB;AAGxB4C,QAAAA,MAAAA;OAHF,CAAA,CAAA;KAJF,CAAA,CAAA;AAUD,GAAA;;AAtPiB,CAAA;AAyPb,SAASnD,eAAT,GAKiD;EACtD,OAAO;AACLyC,IAAAA,OAAO,EAAES,SADJ;AAELR,IAAAA,IAAI,EAAEQ,SAFD;AAGLL,IAAAA,KAAK,EAAE,IAHF;AAILQ,IAAAA,YAAY,EAAE,CAJT;AAKLC,IAAAA,QAAQ,EAAE,KALL;AAMLlC,IAAAA,MAAM,EAAE,MANH;AAOLY,IAAAA,SAAS,EAAEkB,SAAAA;GAPb,CAAA;AASD;;;;"}
@@ -0,0 +1,85 @@
1
+ import { notifyManager } from './notifyManager.esm.js';
2
+ import { Mutation } from './mutation.esm.js';
3
+ import { matchMutation, noop } from './utils.esm.js';
4
+ import { Subscribable } from './subscribable.esm.js';
5
+
6
+ // CLASS
7
+ class MutationCache extends Subscribable {
8
+ constructor(config) {
9
+ super();
10
+ this.config = config || {};
11
+ this.mutations = [];
12
+ this.mutationId = 0;
13
+ }
14
+
15
+ build(client, options, state) {
16
+ const mutation = new Mutation({
17
+ mutationCache: this,
18
+ logger: client.getLogger(),
19
+ mutationId: ++this.mutationId,
20
+ options: client.defaultMutationOptions(options),
21
+ state,
22
+ defaultOptions: options.mutationKey ? client.getMutationDefaults(options.mutationKey) : undefined,
23
+ meta: options.meta
24
+ });
25
+ this.add(mutation);
26
+ return mutation;
27
+ }
28
+
29
+ add(mutation) {
30
+ this.mutations.push(mutation);
31
+ this.notify({
32
+ type: 'added',
33
+ mutation
34
+ });
35
+ }
36
+
37
+ remove(mutation) {
38
+ this.mutations = this.mutations.filter(x => x !== mutation);
39
+ this.notify({
40
+ type: 'removed',
41
+ mutation
42
+ });
43
+ }
44
+
45
+ clear() {
46
+ notifyManager.batch(() => {
47
+ this.mutations.forEach(mutation => {
48
+ this.remove(mutation);
49
+ });
50
+ });
51
+ }
52
+
53
+ getAll() {
54
+ return this.mutations;
55
+ }
56
+
57
+ find(filters) {
58
+ if (typeof filters.exact === 'undefined') {
59
+ filters.exact = true;
60
+ }
61
+
62
+ return this.mutations.find(mutation => matchMutation(filters, mutation));
63
+ }
64
+
65
+ findAll(filters) {
66
+ return this.mutations.filter(mutation => matchMutation(filters, mutation));
67
+ }
68
+
69
+ notify(event) {
70
+ notifyManager.batch(() => {
71
+ this.listeners.forEach(listener => {
72
+ listener(event);
73
+ });
74
+ });
75
+ }
76
+
77
+ resumePausedMutations() {
78
+ const pausedMutations = this.mutations.filter(x => x.state.isPaused);
79
+ return notifyManager.batch(() => pausedMutations.reduce((promise, mutation) => promise.then(() => mutation.continue().catch(noop)), Promise.resolve()));
80
+ }
81
+
82
+ }
83
+
84
+ export { MutationCache };
85
+ //# sourceMappingURL=mutationCache.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mutationCache.esm.js","sources":["../../src/mutationCache.ts"],"sourcesContent":["import { MutationObserver } from './mutationObserver'\nimport type { MutationOptions } from './types'\nimport type { QueryClient } from './queryClient'\nimport { notifyManager } from './notifyManager'\nimport { Action, Mutation, MutationState } from './mutation'\nimport { matchMutation, MutationFilters, noop } from './utils'\nimport { Subscribable } from './subscribable'\n\n// TYPES\n\ninterface MutationCacheConfig {\n onError?: (\n error: unknown,\n variables: unknown,\n context: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n ) => void\n onSuccess?: (\n data: unknown,\n variables: unknown,\n context: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n ) => void\n onMutate?: (\n variables: unknown,\n mutation: Mutation<unknown, unknown, unknown, unknown>,\n ) => void\n}\n\ninterface NotifyEventMutationAdded {\n type: 'added'\n mutation: Mutation<any, any, any, any>\n}\ninterface NotifyEventMutationRemoved {\n type: 'removed'\n mutation: Mutation<any, any, any, any>\n}\n\ninterface NotifyEventMutationObserverAdded {\n type: 'observerAdded'\n mutation: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any>\n}\n\ninterface NotifyEventMutationObserverRemoved {\n type: 'observerRemoved'\n mutation: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any>\n}\n\ninterface NotifyEventMutationObserverOptionsUpdated {\n type: 'observerOptionsUpdated'\n mutation?: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any, any>\n}\n\ninterface NotifyEventMutationUpdated {\n type: 'updated'\n mutation: Mutation<any, any, any, any>\n action: Action<any, any, any, any>\n}\n\ntype MutationCacheNotifyEvent =\n | NotifyEventMutationAdded\n | NotifyEventMutationRemoved\n | NotifyEventMutationObserverAdded\n | NotifyEventMutationObserverRemoved\n | NotifyEventMutationObserverOptionsUpdated\n | NotifyEventMutationUpdated\n\ntype MutationCacheListener = (event: MutationCacheNotifyEvent) => void\n\n// CLASS\n\nexport class MutationCache extends Subscribable<MutationCacheListener> {\n config: MutationCacheConfig\n\n private mutations: Mutation<any, any, any, any>[]\n private mutationId: number\n\n constructor(config?: MutationCacheConfig) {\n super()\n this.config = config || {}\n this.mutations = []\n this.mutationId = 0\n }\n\n build<TData, TError, TVariables, TContext>(\n client: QueryClient,\n options: MutationOptions<TData, TError, TVariables, TContext>,\n state?: MutationState<TData, TError, TVariables, TContext>,\n ): Mutation<TData, TError, TVariables, TContext> {\n const mutation = new Mutation({\n mutationCache: this,\n logger: client.getLogger(),\n mutationId: ++this.mutationId,\n options: client.defaultMutationOptions(options),\n state,\n defaultOptions: options.mutationKey\n ? client.getMutationDefaults(options.mutationKey)\n : undefined,\n meta: options.meta,\n })\n\n this.add(mutation)\n\n return mutation\n }\n\n add(mutation: Mutation<any, any, any, any>): void {\n this.mutations.push(mutation)\n this.notify({ type: 'added', mutation })\n }\n\n remove(mutation: Mutation<any, any, any, any>): void {\n this.mutations = this.mutations.filter((x) => x !== mutation)\n this.notify({ type: 'removed', mutation })\n }\n\n clear(): void {\n notifyManager.batch(() => {\n this.mutations.forEach((mutation) => {\n this.remove(mutation)\n })\n })\n }\n\n getAll(): Mutation[] {\n return this.mutations\n }\n\n find<TData = unknown, TError = unknown, TVariables = any, TContext = unknown>(\n filters: MutationFilters,\n ): Mutation<TData, TError, TVariables, TContext> | undefined {\n if (typeof filters.exact === 'undefined') {\n filters.exact = true\n }\n\n return this.mutations.find((mutation) => matchMutation(filters, mutation))\n }\n\n findAll(filters: MutationFilters): Mutation[] {\n return this.mutations.filter((mutation) => matchMutation(filters, mutation))\n }\n\n notify(event: MutationCacheNotifyEvent) {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(event)\n })\n })\n }\n\n resumePausedMutations(): Promise<void> {\n const pausedMutations = this.mutations.filter((x) => x.state.isPaused)\n return notifyManager.batch(() =>\n pausedMutations.reduce(\n (promise, mutation) =>\n promise.then(() => mutation.continue().catch(noop)),\n Promise.resolve(),\n ),\n )\n }\n}\n"],"names":["MutationCache","Subscribable","constructor","config","mutations","mutationId","build","client","options","state","mutation","Mutation","mutationCache","logger","getLogger","defaultMutationOptions","defaultOptions","mutationKey","getMutationDefaults","undefined","meta","add","push","notify","type","remove","filter","x","clear","notifyManager","batch","forEach","getAll","find","filters","exact","matchMutation","findAll","event","listeners","listener","resumePausedMutations","pausedMutations","isPaused","reduce","promise","then","continue","catch","noop","Promise","resolve"],"mappings":";;;;;AAwEA;AAEO,MAAMA,aAAN,SAA4BC,YAA5B,CAAgE;EAMrEC,WAAW,CAACC,MAAD,EAA+B;AACxC,IAAA,KAAA,EAAA,CAAA;AACA,IAAA,IAAA,CAAKA,MAAL,GAAcA,MAAM,IAAI,EAAxB,CAAA;IACA,IAAKC,CAAAA,SAAL,GAAiB,EAAjB,CAAA;IACA,IAAKC,CAAAA,UAAL,GAAkB,CAAlB,CAAA;AACD,GAAA;;AAEDC,EAAAA,KAAK,CACHC,MADG,EAEHC,OAFG,EAGHC,KAHG,EAI4C;AAC/C,IAAA,MAAMC,QAAQ,GAAG,IAAIC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AAxFoE;;;;"}
@@ -0,0 +1,126 @@
1
+ import { getDefaultState } from './mutation.esm.js';
2
+ import { notifyManager } from './notifyManager.esm.js';
3
+ import { Subscribable } from './subscribable.esm.js';
4
+ import { shallowEqualObjects } from './utils.esm.js';
5
+
6
+ // CLASS
7
+ class MutationObserver extends Subscribable {
8
+ constructor(client, options) {
9
+ super();
10
+ this.client = client;
11
+ this.setOptions(options);
12
+ this.bindMethods();
13
+ this.updateResult();
14
+ }
15
+
16
+ bindMethods() {
17
+ this.mutate = this.mutate.bind(this);
18
+ this.reset = this.reset.bind(this);
19
+ }
20
+
21
+ setOptions(options) {
22
+ const prevOptions = this.options;
23
+ this.options = this.client.defaultMutationOptions(options);
24
+
25
+ if (!shallowEqualObjects(prevOptions, this.options)) {
26
+ this.client.getMutationCache().notify({
27
+ type: 'observerOptionsUpdated',
28
+ mutation: this.currentMutation,
29
+ observer: this
30
+ });
31
+ }
32
+ }
33
+
34
+ onUnsubscribe() {
35
+ if (!this.listeners.length) {
36
+ var _this$currentMutation;
37
+
38
+ (_this$currentMutation = this.currentMutation) == null ? void 0 : _this$currentMutation.removeObserver(this);
39
+ }
40
+ }
41
+
42
+ onMutationUpdate(action) {
43
+ this.updateResult(); // Determine which callbacks to trigger
44
+
45
+ const notifyOptions = {
46
+ listeners: true
47
+ };
48
+
49
+ if (action.type === 'success') {
50
+ notifyOptions.onSuccess = true;
51
+ } else if (action.type === 'error') {
52
+ notifyOptions.onError = true;
53
+ }
54
+
55
+ this.notify(notifyOptions);
56
+ }
57
+
58
+ getCurrentResult() {
59
+ return this.currentResult;
60
+ }
61
+
62
+ reset() {
63
+ this.currentMutation = undefined;
64
+ this.updateResult();
65
+ this.notify({
66
+ listeners: true
67
+ });
68
+ }
69
+
70
+ mutate(variables, options) {
71
+ this.mutateOptions = options;
72
+
73
+ if (this.currentMutation) {
74
+ this.currentMutation.removeObserver(this);
75
+ }
76
+
77
+ this.currentMutation = this.client.getMutationCache().build(this.client, { ...this.options,
78
+ variables: typeof variables !== 'undefined' ? variables : this.options.variables
79
+ });
80
+ this.currentMutation.addObserver(this);
81
+ return this.currentMutation.execute();
82
+ }
83
+
84
+ updateResult() {
85
+ const state = this.currentMutation ? this.currentMutation.state : getDefaultState();
86
+ const result = { ...state,
87
+ isLoading: state.status === 'loading',
88
+ isSuccess: state.status === 'success',
89
+ isError: state.status === 'error',
90
+ isIdle: state.status === 'idle',
91
+ mutate: this.mutate,
92
+ reset: this.reset
93
+ };
94
+ this.currentResult = result;
95
+ }
96
+
97
+ notify(options) {
98
+ notifyManager.batch(() => {
99
+ // First trigger the mutate callbacks
100
+ if (this.mutateOptions) {
101
+ if (options.onSuccess) {
102
+ var _this$mutateOptions$o, _this$mutateOptions, _this$mutateOptions$o2, _this$mutateOptions2;
103
+
104
+ (_this$mutateOptions$o = (_this$mutateOptions = this.mutateOptions).onSuccess) == null ? void 0 : _this$mutateOptions$o.call(_this$mutateOptions, this.currentResult.data, this.currentResult.variables, this.currentResult.context);
105
+ (_this$mutateOptions$o2 = (_this$mutateOptions2 = this.mutateOptions).onSettled) == null ? void 0 : _this$mutateOptions$o2.call(_this$mutateOptions2, this.currentResult.data, null, this.currentResult.variables, this.currentResult.context);
106
+ } else if (options.onError) {
107
+ var _this$mutateOptions$o3, _this$mutateOptions3, _this$mutateOptions$o4, _this$mutateOptions4;
108
+
109
+ (_this$mutateOptions$o3 = (_this$mutateOptions3 = this.mutateOptions).onError) == null ? void 0 : _this$mutateOptions$o3.call(_this$mutateOptions3, this.currentResult.error, this.currentResult.variables, this.currentResult.context);
110
+ (_this$mutateOptions$o4 = (_this$mutateOptions4 = this.mutateOptions).onSettled) == null ? void 0 : _this$mutateOptions$o4.call(_this$mutateOptions4, undefined, this.currentResult.error, this.currentResult.variables, this.currentResult.context);
111
+ }
112
+ } // Then trigger the listeners
113
+
114
+
115
+ if (options.listeners) {
116
+ this.listeners.forEach(listener => {
117
+ listener(this.currentResult);
118
+ });
119
+ }
120
+ });
121
+ }
122
+
123
+ }
124
+
125
+ export { MutationObserver };
126
+ //# sourceMappingURL=mutationObserver.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mutationObserver.esm.js","sources":["../../src/mutationObserver.ts"],"sourcesContent":["import { Action, getDefaultState, Mutation } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport type { QueryClient } from './queryClient'\nimport { Subscribable } from './subscribable'\nimport type {\n MutateOptions,\n MutationObserverBaseResult,\n MutationObserverResult,\n MutationObserverOptions,\n} from './types'\nimport { shallowEqualObjects } from './utils'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TContext> = (\n result: MutationObserverResult<TData, TError, TVariables, TContext>,\n) => void\n\ninterface NotifyOptions {\n listeners?: boolean\n onError?: boolean\n onSuccess?: boolean\n}\n\n// CLASS\n\nexport class MutationObserver<\n TData = unknown,\n TError = unknown,\n TVariables = void,\n TContext = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TContext>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TContext>\n\n private client: QueryClient\n private currentResult!: MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n >\n private currentMutation?: Mutation<TData, TError, TVariables, TContext>\n private mutateOptions?: MutateOptions<TData, TError, TVariables, TContext>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n super()\n\n this.client = client\n this.setOptions(options)\n this.bindMethods()\n this.updateResult()\n }\n\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n setOptions(\n options?: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n const prevOptions = this.options\n this.options = this.client.defaultMutationOptions(options)\n if (!shallowEqualObjects(prevOptions, this.options)) {\n this.client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.currentMutation,\n observer: this,\n })\n }\n }\n\n protected onUnsubscribe(): void {\n if (!this.listeners.length) {\n this.currentMutation?.removeObserver(this)\n }\n }\n\n onMutationUpdate(action: Action<TData, TError, TVariables, TContext>): void {\n this.updateResult()\n\n // Determine which callbacks to trigger\n const notifyOptions: NotifyOptions = {\n listeners: true,\n }\n\n if (action.type === 'success') {\n notifyOptions.onSuccess = true\n } else if (action.type === 'error') {\n notifyOptions.onError = true\n }\n\n this.notify(notifyOptions)\n }\n\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n > {\n return this.currentResult\n }\n\n reset(): void {\n this.currentMutation = undefined\n this.updateResult()\n this.notify({ listeners: true })\n }\n\n mutate(\n variables?: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TContext>,\n ): Promise<TData> {\n this.mutateOptions = options\n\n if (this.currentMutation) {\n this.currentMutation.removeObserver(this)\n }\n\n this.currentMutation = this.client.getMutationCache().build(this.client, {\n ...this.options,\n variables:\n typeof variables !== 'undefined' ? variables : this.options.variables,\n })\n\n this.currentMutation.addObserver(this)\n\n return this.currentMutation.execute()\n }\n\n private updateResult(): void {\n const state = this.currentMutation\n ? this.currentMutation.state\n : getDefaultState<TData, TError, TVariables, TContext>()\n\n const result: MutationObserverBaseResult<\n TData,\n TError,\n TVariables,\n TContext\n > = {\n ...state,\n isLoading: state.status === 'loading',\n isSuccess: state.status === 'success',\n isError: state.status === 'error',\n isIdle: state.status === 'idle',\n mutate: this.mutate,\n reset: this.reset,\n }\n\n this.currentResult = result as MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n >\n }\n\n private notify(options: NotifyOptions) {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n if (this.mutateOptions) {\n if (options.onSuccess) {\n this.mutateOptions.onSuccess?.(\n this.currentResult.data!,\n this.currentResult.variables!,\n this.currentResult.context!,\n )\n this.mutateOptions.onSettled?.(\n this.currentResult.data!,\n null,\n this.currentResult.variables!,\n this.currentResult.context,\n )\n } else if (options.onError) {\n this.mutateOptions.onError?.(\n this.currentResult.error!,\n this.currentResult.variables!,\n this.currentResult.context,\n )\n this.mutateOptions.onSettled?.(\n undefined,\n this.currentResult.error,\n this.currentResult.variables!,\n this.currentResult.context,\n )\n }\n }\n\n // Then trigger the listeners\n if (options.listeners) {\n this.listeners.forEach((listener) => {\n listener(this.currentResult)\n })\n }\n })\n }\n}\n"],"names":["MutationObserver","Subscribable","constructor","client","options","setOptions","bindMethods","updateResult","mutate","bind","reset","prevOptions","defaultMutationOptions","shallowEqualObjects","getMutationCache","notify","type","mutation","currentMutation","observer","onUnsubscribe","listeners","length","removeObserver","onMutationUpdate","action","notifyOptions","onSuccess","onError","getCurrentResult","currentResult","undefined","variables","mutateOptions","build","addObserver","execute","state","getDefaultState","result","isLoading","status","isSuccess","isError","isIdle","notifyManager","batch","data","context","onSettled","error","forEach","listener"],"mappings":";;;;;AAwBA;AAEO,MAAMA,gBAAN,SAKGC,YALH,CAOL;AAaAC,EAAAA,WAAW,CACTC,MADS,EAETC,OAFS,EAGT;AACA,IAAA,KAAA,EAAA,CAAA;IAEA,IAAKD,CAAAA,MAAL,GAAcA,MAAd,CAAA;IACA,IAAKE,CAAAA,UAAL,CAAgBD,OAAhB,CAAA,CAAA;AACA,IAAA,IAAA,CAAKE,WAAL,EAAA,CAAA;AACA,IAAA,IAAA,CAAKC,YAAL,EAAA,CAAA;AACD,GAAA;;AAESD,EAAAA,WAAW,GAAS;IAC5B,IAAKE,CAAAA,MAAL,GAAc,IAAKA,CAAAA,MAAL,CAAYC,IAAZ,CAAiB,IAAjB,CAAd,CAAA;IACA,IAAKC,CAAAA,KAAL,GAAa,IAAKA,CAAAA,KAAL,CAAWD,IAAX,CAAgB,IAAhB,CAAb,CAAA;AACD,GAAA;;EAEDJ,UAAU,CACRD,OADQ,EAER;IACA,MAAMO,WAAW,GAAG,IAAA,CAAKP,OAAzB,CAAA;IACA,IAAKA,CAAAA,OAAL,GAAe,IAAKD,CAAAA,MAAL,CAAYS,sBAAZ,CAAmCR,OAAnC,CAAf,CAAA;;IACA,IAAI,CAACS,mBAAmB,CAACF,WAAD,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B,OAHrB,CAAA,CAAA;AAKA,UAAA,CAAA,sBAAA,GAAA,CAAA,oBAAA,GAAA,IAAA,CAAKf,aAAL,EAAmBgB,SAAnB,uEACElB,SADF,EAEE,KAAKD,aAAL,CAAmBoB,KAFrB,EAGE,IAAA,CAAKpB,aAAL,CAAmBE,SAHrB,EAIE,IAAKF,CAAAA,aAAL,CAAmBkB,OAJrB,CAAA,CAAA;AAMD,SAAA;AACF,OA5BuB;;;MA+BxB,IAAI5C,OAAO,CAACiB,SAAZ,EAAuB;AACrB,QAAA,IAAA,CAAKA,SAAL,CAAe8B,OAAf,CAAwBC,QAAD,IAAc;UACnCA,QAAQ,CAAC,IAAKtB,CAAAA,aAAN,CAAR,CAAA;SADF,CAAA,CAAA;AAGD,OAAA;KAnCH,CAAA,CAAA;AAqCD,GAAA;;AAzKD;;;;"}
@@ -0,0 +1,99 @@
1
+ import { scheduleMicrotask } from './utils.esm.js';
2
+
3
+ function createNotifyManager() {
4
+ let queue = [];
5
+ let transactions = 0;
6
+
7
+ let notifyFn = callback => {
8
+ callback();
9
+ };
10
+
11
+ let batchNotifyFn = callback => {
12
+ callback();
13
+ };
14
+
15
+ const batch = callback => {
16
+ let result;
17
+ transactions++;
18
+
19
+ try {
20
+ result = callback();
21
+ } finally {
22
+ transactions--;
23
+
24
+ if (!transactions) {
25
+ flush();
26
+ }
27
+ }
28
+
29
+ return result;
30
+ };
31
+
32
+ const schedule = callback => {
33
+ if (transactions) {
34
+ queue.push(callback);
35
+ } else {
36
+ scheduleMicrotask(() => {
37
+ notifyFn(callback);
38
+ });
39
+ }
40
+ };
41
+ /**
42
+ * All calls to the wrapped function will be batched.
43
+ */
44
+
45
+
46
+ const batchCalls = callback => {
47
+ return (...args) => {
48
+ schedule(() => {
49
+ callback(...args);
50
+ });
51
+ };
52
+ };
53
+
54
+ const flush = () => {
55
+ const originalQueue = queue;
56
+ queue = [];
57
+
58
+ if (originalQueue.length) {
59
+ scheduleMicrotask(() => {
60
+ batchNotifyFn(() => {
61
+ originalQueue.forEach(callback => {
62
+ notifyFn(callback);
63
+ });
64
+ });
65
+ });
66
+ }
67
+ };
68
+ /**
69
+ * Use this method to set a custom notify function.
70
+ * This can be used to for example wrap notifications with `React.act` while running tests.
71
+ */
72
+
73
+
74
+ const setNotifyFunction = fn => {
75
+ notifyFn = fn;
76
+ };
77
+ /**
78
+ * Use this method to set a custom function to batch notifications together into a single tick.
79
+ * By default React Query will use the batch function provided by ReactDOM or React Native.
80
+ */
81
+
82
+
83
+ const setBatchNotifyFunction = fn => {
84
+ batchNotifyFn = fn;
85
+ };
86
+
87
+ return {
88
+ batch,
89
+ batchCalls,
90
+ schedule,
91
+ setNotifyFunction,
92
+ setBatchNotifyFunction
93
+ };
94
+ } // SINGLETON
95
+
96
+ const notifyManager = createNotifyManager();
97
+
98
+ export { createNotifyManager, notifyManager };
99
+ //# sourceMappingURL=notifyManager.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"notifyManager.esm.js","sources":["../../src/notifyManager.ts"],"sourcesContent":["import { scheduleMicrotask } from './utils'\n\n// TYPES\n\ntype NotifyCallback = () => void\n\ntype NotifyFunction = (callback: () => void) => void\n\ntype BatchNotifyFunction = (callback: () => void) => void\n\nexport function createNotifyManager() {\n let queue: NotifyCallback[] = []\n let transactions = 0\n let notifyFn: NotifyFunction = (callback) => {\n callback()\n }\n let batchNotifyFn: BatchNotifyFunction = (callback: () => void) => {\n callback()\n }\n\n const batch = <T>(callback: () => T): T => {\n let result\n transactions++\n try {\n result = callback()\n } finally {\n transactions--\n if (!transactions) {\n flush()\n }\n }\n return result\n }\n\n const schedule = (callback: NotifyCallback): void => {\n if (transactions) {\n queue.push(callback)\n } else {\n scheduleMicrotask(() => {\n notifyFn(callback)\n })\n }\n }\n\n /**\n * All calls to the wrapped function will be batched.\n */\n const batchCalls = <T extends Function>(callback: T): T => {\n return ((...args: any[]) => {\n schedule(() => {\n callback(...args)\n })\n }) as any\n }\n\n const flush = (): void => {\n const originalQueue = queue\n queue = []\n if (originalQueue.length) {\n scheduleMicrotask(() => {\n batchNotifyFn(() => {\n originalQueue.forEach((callback) => {\n notifyFn(callback)\n })\n })\n })\n }\n }\n\n /**\n * Use this method to set a custom notify function.\n * This can be used to for example wrap notifications with `React.act` while running tests.\n */\n const setNotifyFunction = (fn: NotifyFunction) => {\n notifyFn = fn\n }\n\n /**\n * Use this method to set a custom function to batch notifications together into a single tick.\n * By default React Query will use the batch function provided by ReactDOM or React Native.\n */\n const setBatchNotifyFunction = (fn: BatchNotifyFunction) => {\n batchNotifyFn = fn\n }\n\n return {\n batch,\n batchCalls,\n schedule,\n setNotifyFunction,\n setBatchNotifyFunction,\n } as const\n}\n\n// SINGLETON\nexport const notifyManager = createNotifyManager()\n"],"names":["createNotifyManager","queue","transactions","notifyFn","callback","batchNotifyFn","batch","result","flush","schedule","push","scheduleMicrotask","batchCalls","args","originalQueue","length","forEach","setNotifyFunction","fn","setBatchNotifyFunction","notifyManager"],"mappings":";;AAUO,SAASA,mBAAT,GAA+B;EACpC,IAAIC,KAAuB,GAAG,EAA9B,CAAA;EACA,IAAIC,YAAY,GAAG,CAAnB,CAAA;;EACA,IAAIC,QAAwB,GAAIC,QAAD,IAAc;IAC3CA,QAAQ,EAAA,CAAA;GADV,CAAA;;EAGA,IAAIC,aAAkC,GAAID,QAAD,IAA0B;IACjEA,QAAQ,EAAA,CAAA;GADV,CAAA;;EAIA,MAAME,KAAK,GAAOF,QAAJ,IAA6B;AACzC,IAAA,IAAIG,MAAJ,CAAA;IACAL,YAAY,EAAA,CAAA;;IACZ,IAAI;MACFK,MAAM,GAAGH,QAAQ,EAAjB,CAAA;AACD,KAFD,SAEU;MACRF,YAAY,EAAA,CAAA;;MACZ,IAAI,CAACA,YAAL,EAAmB;QACjBM,KAAK,EAAA,CAAA;AACN,OAAA;AACF,KAAA;;AACD,IAAA,OAAOD,MAAP,CAAA;GAXF,CAAA;;EAcA,MAAME,QAAQ,GAAIL,QAAD,IAAoC;AACnD,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