@tanstack/query-core 4.39.0 → 4.39.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.esm.js +91 -0
- package/build/lib/focusManager.esm.js.map +1 -0
- package/build/lib/focusManager.js +96 -0
- package/build/lib/focusManager.js.map +1 -0
- package/build/lib/focusManager.mjs +91 -0
- package/build/lib/focusManager.mjs.map +1 -0
- package/build/lib/hydration.esm.js +109 -0
- package/build/lib/hydration.esm.js.map +1 -0
- package/build/lib/hydration.js +116 -0
- package/build/lib/hydration.js.map +1 -0
- package/build/lib/hydration.mjs +109 -0
- package/build/lib/hydration.mjs.map +1 -0
- package/build/lib/index.esm.js +15 -0
- package/build/lib/index.esm.js.map +1 -0
- package/build/lib/index.js +48 -0
- package/build/lib/index.js.map +1 -0
- package/build/lib/index.mjs +15 -0
- package/build/lib/index.mjs.map +1 -0
- package/build/lib/infiniteQueryBehavior.esm.js +146 -0
- package/build/lib/infiniteQueryBehavior.esm.js.map +1 -0
- package/build/lib/infiniteQueryBehavior.js +154 -0
- package/build/lib/infiniteQueryBehavior.js.map +1 -0
- package/build/lib/infiniteQueryBehavior.mjs +146 -0
- package/build/lib/infiniteQueryBehavior.mjs.map +1 -0
- package/build/lib/infiniteQueryObserver.esm.js +85 -0
- package/build/lib/infiniteQueryObserver.esm.js.map +1 -0
- package/build/lib/infiniteQueryObserver.js +89 -0
- package/build/lib/infiniteQueryObserver.js.map +1 -0
- package/build/lib/infiniteQueryObserver.mjs +85 -0
- package/build/lib/infiniteQueryObserver.mjs.map +1 -0
- package/build/lib/logger.esm.js +4 -0
- package/build/lib/logger.esm.js.map +1 -0
- package/build/lib/logger.js +8 -0
- package/build/lib/logger.js.map +1 -0
- package/build/lib/logger.mjs +4 -0
- package/build/lib/logger.mjs.map +1 -0
- package/build/lib/logger.native.esm.js +12 -0
- package/build/lib/logger.native.esm.js.map +1 -0
- package/build/lib/logger.native.js +16 -0
- package/build/lib/logger.native.js.map +1 -0
- package/build/lib/logger.native.mjs +12 -0
- package/build/lib/logger.native.mjs.map +1 -0
- package/build/lib/mutation.esm.js +261 -0
- package/build/lib/mutation.esm.js.map +1 -0
- package/build/lib/mutation.js +266 -0
- package/build/lib/mutation.js.map +1 -0
- package/build/lib/mutation.mjs +261 -0
- package/build/lib/mutation.mjs.map +1 -0
- package/build/lib/mutationCache.esm.js +93 -0
- package/build/lib/mutationCache.esm.js.map +1 -0
- package/build/lib/mutationCache.js +97 -0
- package/build/lib/mutationCache.js.map +1 -0
- package/build/lib/mutationCache.mjs +93 -0
- package/build/lib/mutationCache.mjs.map +1 -0
- package/build/lib/mutationObserver.esm.js +134 -0
- package/build/lib/mutationObserver.esm.js.map +1 -0
- package/build/lib/mutationObserver.js +138 -0
- package/build/lib/mutationObserver.js.map +1 -0
- package/build/lib/mutationObserver.mjs +134 -0
- package/build/lib/mutationObserver.mjs.map +1 -0
- package/build/lib/notifyManager.esm.js +99 -0
- package/build/lib/notifyManager.esm.js.map +1 -0
- package/build/lib/notifyManager.js +104 -0
- package/build/lib/notifyManager.js.map +1 -0
- package/build/lib/notifyManager.mjs +99 -0
- package/build/lib/notifyManager.mjs.map +1 -0
- package/build/lib/onlineManager.esm.js +93 -0
- package/build/lib/onlineManager.esm.js.map +1 -0
- package/build/lib/onlineManager.js +98 -0
- package/build/lib/onlineManager.js.map +1 -0
- package/build/lib/onlineManager.mjs +93 -0
- package/build/lib/onlineManager.mjs.map +1 -0
- package/build/lib/queriesObserver.esm.js +168 -0
- package/build/lib/queriesObserver.esm.js.map +1 -0
- package/build/lib/queriesObserver.js +172 -0
- package/build/lib/queriesObserver.js.map +1 -0
- package/build/lib/queriesObserver.mjs +168 -0
- package/build/lib/queriesObserver.mjs.map +1 -0
- package/build/lib/query.esm.js +475 -0
- package/build/lib/query.esm.js.map +1 -0
- package/build/lib/query.js +479 -0
- package/build/lib/query.js.map +1 -0
- package/build/lib/query.mjs +475 -0
- package/build/lib/query.mjs.map +1 -0
- package/build/lib/queryCache.esm.js +133 -0
- package/build/lib/queryCache.esm.js.map +1 -0
- package/build/lib/queryCache.js +137 -0
- package/build/lib/queryCache.js.map +1 -0
- package/build/lib/queryCache.mjs +133 -0
- package/build/lib/queryCache.mjs.map +1 -0
- package/build/lib/queryClient.esm.js +402 -0
- package/build/lib/queryClient.esm.js.map +1 -0
- package/build/lib/queryClient.js +406 -0
- package/build/lib/queryClient.js.map +1 -0
- package/build/lib/queryClient.mjs +402 -0
- package/build/lib/queryClient.mjs.map +1 -0
- package/build/lib/queryObserver.esm.js +580 -0
- package/build/lib/queryObserver.esm.js.map +1 -0
- package/build/lib/queryObserver.js +584 -0
- package/build/lib/queryObserver.js.map +1 -0
- package/build/lib/queryObserver.mjs +580 -0
- package/build/lib/queryObserver.mjs.map +1 -0
- package/build/lib/removable.esm.js +33 -0
- package/build/lib/removable.esm.js.map +1 -0
- package/build/lib/removable.js +37 -0
- package/build/lib/removable.js.map +1 -0
- package/build/lib/removable.mjs +33 -0
- package/build/lib/removable.mjs.map +1 -0
- package/build/lib/retryer.esm.js +167 -0
- package/build/lib/retryer.esm.js.map +1 -0
- package/build/lib/retryer.js +174 -0
- package/build/lib/retryer.js.map +1 -0
- package/build/lib/retryer.mjs +167 -0
- package/build/lib/retryer.mjs.map +1 -0
- package/build/lib/subscribable.esm.js +32 -0
- package/build/lib/subscribable.esm.js.map +1 -0
- package/build/lib/subscribable.js +36 -0
- package/build/lib/subscribable.js.map +1 -0
- package/build/lib/subscribable.mjs +32 -0
- package/build/lib/subscribable.mjs.map +1 -0
- package/build/lib/utils.esm.js +320 -0
- package/build/lib/utils.esm.js.map +1 -0
- package/build/lib/utils.js +350 -0
- package/build/lib/utils.js.map +1 -0
- package/build/lib/utils.mjs +320 -0
- package/build/lib/utils.mjs.map +1 -0
- package/build/umd/index.development.js +3361 -0
- package/build/umd/index.development.js.map +1 -0
- package/build/umd/index.production.js +2 -0
- package/build/umd/index.production.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { notifyManager } from './notifyManager.mjs';
|
|
2
|
+
import { Mutation } from './mutation.mjs';
|
|
3
|
+
import { matchMutation, noop } from './utils.mjs';
|
|
4
|
+
import { Subscribable } from './subscribable.mjs';
|
|
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
|
+
});
|
|
24
|
+
this.add(mutation);
|
|
25
|
+
return mutation;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
add(mutation) {
|
|
29
|
+
this.mutations.push(mutation);
|
|
30
|
+
this.notify({
|
|
31
|
+
type: 'added',
|
|
32
|
+
mutation
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
remove(mutation) {
|
|
37
|
+
this.mutations = this.mutations.filter(x => x !== mutation);
|
|
38
|
+
this.notify({
|
|
39
|
+
type: 'removed',
|
|
40
|
+
mutation
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
clear() {
|
|
45
|
+
notifyManager.batch(() => {
|
|
46
|
+
this.mutations.forEach(mutation => {
|
|
47
|
+
this.remove(mutation);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
getAll() {
|
|
53
|
+
return this.mutations;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
find(filters) {
|
|
57
|
+
if (typeof filters.exact === 'undefined') {
|
|
58
|
+
filters.exact = true;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return this.mutations.find(mutation => matchMutation(filters, mutation));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
findAll(filters) {
|
|
65
|
+
return this.mutations.filter(mutation => matchMutation(filters, mutation));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
notify(event) {
|
|
69
|
+
notifyManager.batch(() => {
|
|
70
|
+
this.listeners.forEach(({
|
|
71
|
+
listener
|
|
72
|
+
}) => {
|
|
73
|
+
listener(event);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
resumePausedMutations() {
|
|
79
|
+
var _this$resuming;
|
|
80
|
+
|
|
81
|
+
this.resuming = ((_this$resuming = this.resuming) != null ? _this$resuming : Promise.resolve()).then(() => {
|
|
82
|
+
const pausedMutations = this.mutations.filter(x => x.state.isPaused);
|
|
83
|
+
return notifyManager.batch(() => pausedMutations.reduce((promise, mutation) => promise.then(() => mutation.continue().catch(noop)), Promise.resolve()));
|
|
84
|
+
}).then(() => {
|
|
85
|
+
this.resuming = undefined;
|
|
86
|
+
});
|
|
87
|
+
return this.resuming;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export { MutationCache };
|
|
93
|
+
//# sourceMappingURL=mutationCache.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mutationCache.mjs","sources":["../../src/mutationCache.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport { Mutation } from './mutation'\nimport { matchMutation, noop } from './utils'\nimport { Subscribable } from './subscribable'\nimport type { MutationObserver } from './mutationObserver'\nimport type { MutationOptions, NotifyEvent } from './types'\nimport type { QueryClient } from './queryClient'\nimport type { Action, MutationState } from './mutation'\nimport type { MutationFilters } from './utils'\n\n// TYPES\n\ninterface MutationCacheConfig {\n onError?: (\n error: unknown,\n variables: unknown,\n context: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n ) => Promise<unknown> | unknown\n onSuccess?: (\n data: unknown,\n variables: unknown,\n context: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n ) => Promise<unknown> | unknown\n onMutate?: (\n variables: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n ) => Promise<unknown> | unknown\n onSettled?: (\n data: unknown | undefined,\n error: unknown | null,\n variables: unknown,\n context: unknown,\n mutation: Mutation<unknown, unknown, unknown>,\n ) => Promise<unknown> | unknown\n}\n\ninterface NotifyEventMutationAdded extends NotifyEvent {\n type: 'added'\n mutation: Mutation<any, any, any, any>\n}\ninterface NotifyEventMutationRemoved extends NotifyEvent {\n type: 'removed'\n mutation: Mutation<any, any, any, any>\n}\n\ninterface NotifyEventMutationObserverAdded extends NotifyEvent {\n type: 'observerAdded'\n mutation: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any>\n}\n\ninterface NotifyEventMutationObserverRemoved extends NotifyEvent {\n type: 'observerRemoved'\n mutation: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any>\n}\n\ninterface NotifyEventMutationObserverOptionsUpdated extends NotifyEvent {\n type: 'observerOptionsUpdated'\n mutation?: Mutation<any, any, any, any>\n observer: MutationObserver<any, any, any, any>\n}\n\ninterface NotifyEventMutationUpdated extends NotifyEvent {\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 private resuming: Promise<unknown> | undefined\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 })\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<unknown> {\n this.resuming = (this.resuming ?? Promise.resolve())\n .then(() => {\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() as Promise<unknown>,\n ),\n )\n })\n .then(() => {\n this.resuming = undefined\n })\n\n return this.resuming\n }\n}\n"],"names":["MutationCache","Subscribable","constructor","config","mutations","mutationId","build","client","options","state","mutation","Mutation","mutationCache","logger","getLogger","defaultMutationOptions","defaultOptions","mutationKey","getMutationDefaults","undefined","add","push","notify","type","remove","filter","x","clear","notifyManager","batch","forEach","getAll","find","filters","exact","matchMutation","findAll","event","listeners","listener","resumePausedMutations","resuming","Promise","resolve","then","pausedMutations","isPaused","reduce","promise","continue","catch","noop"],"mappings":";;;;;AAiFA;AAEO,MAAMA,aAAN,SAA4BC,YAA5B,CAAgE;EAOrEC,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,QAAJ,CAAa;AAC5BC,MAAAA,aAAa,EAAE,IADa;AAE5BC,MAAAA,MAAM,EAAEN,MAAM,CAACO,SAAP,EAFoB;MAG5BT,UAAU,EAAE,EAAE,IAAA,CAAKA,UAHS;AAI5BG,MAAAA,OAAO,EAAED,MAAM,CAACQ,sBAAP,CAA8BP,OAA9B,CAJmB;MAK5BC,KAL4B;AAM5BO,MAAAA,cAAc,EAAER,OAAO,CAACS,WAAR,GACZV,MAAM,CAACW,mBAAP,CAA2BV,OAAO,CAACS,WAAnC,CADY,GAEZE,SAAAA;AARwB,KAAb,CAAjB,CAAA;IAWA,IAAKC,CAAAA,GAAL,CAASV,QAAT,CAAA,CAAA;AAEA,IAAA,OAAOA,QAAP,CAAA;AACD,GAAA;;EAEDU,GAAG,CAACV,QAAD,EAA+C;AAChD,IAAA,IAAA,CAAKN,SAAL,CAAeiB,IAAf,C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ACe,KAAD,EAAkC;IACtCT,aAAa,CAACC,KAAd,CAAoB,MAAM;AACxB,MAAA,IAAA,CAAKS,SAAL,CAAeR,OAAf,CAAuB,CAAC;AAAES,QAAAA,QAAAA;AAAF,OAAD,KAAkB;QACvCA,QAAQ,CAACF,KAAD,CAAR,CAAA;OADF,CAAA,CAAA;KADF,CAAA,CAAA;AAKD,GAAA;;AAEDG,EAAAA,qBAAqB,GAAqB;AAAA,IAAA,IAAA,cAAA,CAAA;;AACxC,IAAA,IAAA,CAAKC,QAAL,GAAgB,CAAC,CAAA,cAAA,GAAA,IAAA,CAAKA,QAAN,KAAA,IAAA,GAAA,cAAA,GAAkBC,OAAO,CAACC,OAAR,EAAlB,EACbC,IADa,CACR,MAAM;AACV,MAAA,MAAMC,eAAe,GAAG,IAAKzC,CAAAA,SAAL,CAAeqB,MAAf,CAAuBC,CAAD,IAAOA,CAAC,CAACjB,KAAF,CAAQqC,QAArC,CAAxB,CAAA;AACA,MAAA,OAAOlB,aAAa,CAACC,KAAd,CAAoB,MACzBgB,eAAe,CAACE,MAAhB,CACE,CAACC,OAAD,EAAUtC,QAAV,KACEsC,OAAO,CAACJ,IAAR,CAAa,MAAMlC,QAAQ,CAACuC,QAAT,EAAA,CAAoBC,KAApB,CAA0BC,IAA1B,CAAnB,CAFJ,EAGET,OAAO,CAACC,OAAR,EAHF,CADK,CAAP,CAAA;KAHY,CAAA,CAWbC,IAXa,CAWR,MAAM;MACV,IAAKH,CAAAA,QAAL,GAAgBtB,SAAhB,CAAA;AACD,KAba,CAAhB,CAAA;AAeA,IAAA,OAAO,KAAKsB,QAAZ,CAAA;AACD,GAAA;;AAhGoE;;;;"}
|
|
@@ -0,0 +1,134 @@
|
|
|
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
|
+
var _this$currentMutation;
|
|
23
|
+
|
|
24
|
+
const prevOptions = this.options;
|
|
25
|
+
this.options = this.client.defaultMutationOptions(options);
|
|
26
|
+
|
|
27
|
+
if (!shallowEqualObjects(prevOptions, this.options)) {
|
|
28
|
+
this.client.getMutationCache().notify({
|
|
29
|
+
type: 'observerOptionsUpdated',
|
|
30
|
+
mutation: this.currentMutation,
|
|
31
|
+
observer: this
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
(_this$currentMutation = this.currentMutation) == null ? void 0 : _this$currentMutation.setOptions(this.options);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
onUnsubscribe() {
|
|
39
|
+
if (!this.hasListeners()) {
|
|
40
|
+
var _this$currentMutation2;
|
|
41
|
+
|
|
42
|
+
(_this$currentMutation2 = this.currentMutation) == null ? void 0 : _this$currentMutation2.removeObserver(this);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
onMutationUpdate(action) {
|
|
47
|
+
this.updateResult(); // Determine which callbacks to trigger
|
|
48
|
+
|
|
49
|
+
const notifyOptions = {
|
|
50
|
+
listeners: true
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
if (action.type === 'success') {
|
|
54
|
+
notifyOptions.onSuccess = true;
|
|
55
|
+
} else if (action.type === 'error') {
|
|
56
|
+
notifyOptions.onError = true;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
this.notify(notifyOptions);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
getCurrentResult() {
|
|
63
|
+
return this.currentResult;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
reset() {
|
|
67
|
+
this.currentMutation = undefined;
|
|
68
|
+
this.updateResult();
|
|
69
|
+
this.notify({
|
|
70
|
+
listeners: true
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
mutate(variables, options) {
|
|
75
|
+
this.mutateOptions = options;
|
|
76
|
+
|
|
77
|
+
if (this.currentMutation) {
|
|
78
|
+
this.currentMutation.removeObserver(this);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
this.currentMutation = this.client.getMutationCache().build(this.client, { ...this.options,
|
|
82
|
+
variables: typeof variables !== 'undefined' ? variables : this.options.variables
|
|
83
|
+
});
|
|
84
|
+
this.currentMutation.addObserver(this);
|
|
85
|
+
return this.currentMutation.execute();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
updateResult() {
|
|
89
|
+
const state = this.currentMutation ? this.currentMutation.state : getDefaultState();
|
|
90
|
+
const isLoading = state.status === 'loading';
|
|
91
|
+
const result = { ...state,
|
|
92
|
+
isLoading,
|
|
93
|
+
isPending: isLoading,
|
|
94
|
+
isSuccess: state.status === 'success',
|
|
95
|
+
isError: state.status === 'error',
|
|
96
|
+
isIdle: state.status === 'idle',
|
|
97
|
+
mutate: this.mutate,
|
|
98
|
+
reset: this.reset
|
|
99
|
+
};
|
|
100
|
+
this.currentResult = result;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
notify(options) {
|
|
104
|
+
notifyManager.batch(() => {
|
|
105
|
+
// First trigger the mutate callbacks
|
|
106
|
+
if (this.mutateOptions && this.hasListeners()) {
|
|
107
|
+
if (options.onSuccess) {
|
|
108
|
+
var _this$mutateOptions$o, _this$mutateOptions, _this$mutateOptions$o2, _this$mutateOptions2;
|
|
109
|
+
|
|
110
|
+
(_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);
|
|
111
|
+
(_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);
|
|
112
|
+
} else if (options.onError) {
|
|
113
|
+
var _this$mutateOptions$o3, _this$mutateOptions3, _this$mutateOptions$o4, _this$mutateOptions4;
|
|
114
|
+
|
|
115
|
+
(_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);
|
|
116
|
+
(_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);
|
|
117
|
+
}
|
|
118
|
+
} // Then trigger the listeners
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
if (options.listeners) {
|
|
122
|
+
this.listeners.forEach(({
|
|
123
|
+
listener
|
|
124
|
+
}) => {
|
|
125
|
+
listener(this.currentResult);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export { MutationObserver };
|
|
134
|
+
//# sourceMappingURL=mutationObserver.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mutationObserver.esm.js","sources":["../../src/mutationObserver.ts"],"sourcesContent":["import { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport { shallowEqualObjects } from './utils'\nimport type { QueryClient } from './queryClient'\nimport type {\n MutateOptions,\n MutationObserverBaseResult,\n MutationObserverOptions,\n MutationObserverResult,\n} from './types'\nimport type { Action, Mutation } from './mutation'\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 this.currentMutation?.setOptions(this.options)\n }\n\n protected onUnsubscribe(): void {\n if (!this.hasListeners()) {\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 isLoading = state.status === 'loading'\n const result: MutationObserverBaseResult<\n TData,\n TError,\n TVariables,\n TContext\n > = {\n ...state,\n isLoading,\n isPending: isLoading,\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 && this.hasListeners()) {\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","hasListeners","removeObserver","onMutationUpdate","action","notifyOptions","listeners","onSuccess","onError","getCurrentResult","currentResult","undefined","variables","mutateOptions","build","addObserver","execute","state","getDefaultState","isLoading","status","result","isPending","isSuccess","isError","isIdle","notifyManager","batch","data","context","onSettled","error","forEach","listener"],"mappings":";;;;;AAyBA;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;AAAA,IAAA,IAAA,qBAAA,CAAA;;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,EAAc,IAAKP,CAAAA,OAAnB,CAAxB,EAAqD;AACnD,MAAA,IAAA,CAAKD,MAAL,CAAYW,gBAAZ,EAAA,CAA+BC,MAA/B,CAAsC;AACpCC,QAAAA,IAAI,EAAE,wBAD8B;QAEpCC,QAAQ,EAAE,KAAKC,eAFqB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AAmBE,SAFrB,EAGE,IAAKF,CAAAA,aAAL,CAAmBmB,OAHrB,CAAA,CAAA;AAKA,UAAA,CAAA,sBAAA,GAAA,CAAA,oBAAA,GAAA,IAAA,CAAKhB,aAAL,EAAmBiB,SAAnB,uEACEnB,SADF,EAEE,KAAKD,aAAL,CAAmBqB,KAFrB,EAGE,IAAA,CAAKrB,aAAL,CAAmBE,SAHrB,EAIE,IAAKF,CAAAA,aAAL,CAAmBmB,OAJrB,CAAA,CAAA;AAMD,SAAA;AACF,OA5BuB;;;MA+BxB,IAAI7C,OAAO,CAACsB,SAAZ,EAAuB;AACrB,QAAA,IAAA,CAAKA,SAAL,CAAe0B,OAAf,CAAuB,CAAC;AAAEC,UAAAA,QAAAA;AAAF,SAAD,KAAkB;UACvCA,QAAQ,CAAC,IAAKvB,CAAAA,aAAN,CAAR,CAAA;SADF,CAAA,CAAA;AAGD,OAAA;KAnCH,CAAA,CAAA;AAqCD,GAAA;;AA5KD;;;;"}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var mutation = require('./mutation.js');
|
|
6
|
+
var notifyManager = require('./notifyManager.js');
|
|
7
|
+
var subscribable = require('./subscribable.js');
|
|
8
|
+
var utils = require('./utils.js');
|
|
9
|
+
|
|
10
|
+
// CLASS
|
|
11
|
+
class MutationObserver extends subscribable.Subscribable {
|
|
12
|
+
constructor(client, options) {
|
|
13
|
+
super();
|
|
14
|
+
this.client = client;
|
|
15
|
+
this.setOptions(options);
|
|
16
|
+
this.bindMethods();
|
|
17
|
+
this.updateResult();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
bindMethods() {
|
|
21
|
+
this.mutate = this.mutate.bind(this);
|
|
22
|
+
this.reset = this.reset.bind(this);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
setOptions(options) {
|
|
26
|
+
var _this$currentMutation;
|
|
27
|
+
|
|
28
|
+
const prevOptions = this.options;
|
|
29
|
+
this.options = this.client.defaultMutationOptions(options);
|
|
30
|
+
|
|
31
|
+
if (!utils.shallowEqualObjects(prevOptions, this.options)) {
|
|
32
|
+
this.client.getMutationCache().notify({
|
|
33
|
+
type: 'observerOptionsUpdated',
|
|
34
|
+
mutation: this.currentMutation,
|
|
35
|
+
observer: this
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
(_this$currentMutation = this.currentMutation) == null ? void 0 : _this$currentMutation.setOptions(this.options);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
onUnsubscribe() {
|
|
43
|
+
if (!this.hasListeners()) {
|
|
44
|
+
var _this$currentMutation2;
|
|
45
|
+
|
|
46
|
+
(_this$currentMutation2 = this.currentMutation) == null ? void 0 : _this$currentMutation2.removeObserver(this);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
onMutationUpdate(action) {
|
|
51
|
+
this.updateResult(); // Determine which callbacks to trigger
|
|
52
|
+
|
|
53
|
+
const notifyOptions = {
|
|
54
|
+
listeners: true
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
if (action.type === 'success') {
|
|
58
|
+
notifyOptions.onSuccess = true;
|
|
59
|
+
} else if (action.type === 'error') {
|
|
60
|
+
notifyOptions.onError = true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
this.notify(notifyOptions);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
getCurrentResult() {
|
|
67
|
+
return this.currentResult;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
reset() {
|
|
71
|
+
this.currentMutation = undefined;
|
|
72
|
+
this.updateResult();
|
|
73
|
+
this.notify({
|
|
74
|
+
listeners: true
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
mutate(variables, options) {
|
|
79
|
+
this.mutateOptions = options;
|
|
80
|
+
|
|
81
|
+
if (this.currentMutation) {
|
|
82
|
+
this.currentMutation.removeObserver(this);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
this.currentMutation = this.client.getMutationCache().build(this.client, { ...this.options,
|
|
86
|
+
variables: typeof variables !== 'undefined' ? variables : this.options.variables
|
|
87
|
+
});
|
|
88
|
+
this.currentMutation.addObserver(this);
|
|
89
|
+
return this.currentMutation.execute();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
updateResult() {
|
|
93
|
+
const state = this.currentMutation ? this.currentMutation.state : mutation.getDefaultState();
|
|
94
|
+
const isLoading = state.status === 'loading';
|
|
95
|
+
const result = { ...state,
|
|
96
|
+
isLoading,
|
|
97
|
+
isPending: isLoading,
|
|
98
|
+
isSuccess: state.status === 'success',
|
|
99
|
+
isError: state.status === 'error',
|
|
100
|
+
isIdle: state.status === 'idle',
|
|
101
|
+
mutate: this.mutate,
|
|
102
|
+
reset: this.reset
|
|
103
|
+
};
|
|
104
|
+
this.currentResult = result;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
notify(options) {
|
|
108
|
+
notifyManager.notifyManager.batch(() => {
|
|
109
|
+
// First trigger the mutate callbacks
|
|
110
|
+
if (this.mutateOptions && this.hasListeners()) {
|
|
111
|
+
if (options.onSuccess) {
|
|
112
|
+
var _this$mutateOptions$o, _this$mutateOptions, _this$mutateOptions$o2, _this$mutateOptions2;
|
|
113
|
+
|
|
114
|
+
(_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);
|
|
115
|
+
(_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);
|
|
116
|
+
} else if (options.onError) {
|
|
117
|
+
var _this$mutateOptions$o3, _this$mutateOptions3, _this$mutateOptions$o4, _this$mutateOptions4;
|
|
118
|
+
|
|
119
|
+
(_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);
|
|
120
|
+
(_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);
|
|
121
|
+
}
|
|
122
|
+
} // Then trigger the listeners
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
if (options.listeners) {
|
|
126
|
+
this.listeners.forEach(({
|
|
127
|
+
listener
|
|
128
|
+
}) => {
|
|
129
|
+
listener(this.currentResult);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
exports.MutationObserver = MutationObserver;
|
|
138
|
+
//# sourceMappingURL=mutationObserver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mutationObserver.js","sources":["../../src/mutationObserver.ts"],"sourcesContent":["import { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport { shallowEqualObjects } from './utils'\nimport type { QueryClient } from './queryClient'\nimport type {\n MutateOptions,\n MutationObserverBaseResult,\n MutationObserverOptions,\n MutationObserverResult,\n} from './types'\nimport type { Action, Mutation } from './mutation'\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 this.currentMutation?.setOptions(this.options)\n }\n\n protected onUnsubscribe(): void {\n if (!this.hasListeners()) {\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 isLoading = state.status === 'loading'\n const result: MutationObserverBaseResult<\n TData,\n TError,\n TVariables,\n TContext\n > = {\n ...state,\n isLoading,\n isPending: isLoading,\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 && this.hasListeners()) {\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","hasListeners","removeObserver","onMutationUpdate","action","notifyOptions","listeners","onSuccess","onError","getCurrentResult","currentResult","undefined","variables","mutateOptions","build","addObserver","execute","state","getDefaultState","isLoading","status","result","isPending","isSuccess","isError","isIdle","notifyManager","batch","data","context","onSettled","error","forEach","listener"],"mappings":";;;;;;;;;AAyBA;AAEO,MAAMA,gBAAN,SAKGC,yBALH,CAOL;AAaAC,EAAAA,WAAW,CACTC,MADS,EAETC,OA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xB,IAAI7C,OAAO,CAACsB,SAAZ,EAAuB;AACrB,QAAA,IAAA,CAAKA,SAAL,CAAe0B,OAAf,CAAuB,CAAC;AAAEC,UAAAA,QAAAA;AAAF,SAAD,KAAkB;UACvCA,QAAQ,CAAC,IAAKvB,CAAAA,aAAN,CAAR,CAAA;SADF,CAAA,CAAA;AAGD,OAAA;KAnCH,CAAA,CAAA;AAqCD,GAAA;;AA5KD;;;;"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { getDefaultState } from './mutation.mjs';
|
|
2
|
+
import { notifyManager } from './notifyManager.mjs';
|
|
3
|
+
import { Subscribable } from './subscribable.mjs';
|
|
4
|
+
import { shallowEqualObjects } from './utils.mjs';
|
|
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
|
+
var _this$currentMutation;
|
|
23
|
+
|
|
24
|
+
const prevOptions = this.options;
|
|
25
|
+
this.options = this.client.defaultMutationOptions(options);
|
|
26
|
+
|
|
27
|
+
if (!shallowEqualObjects(prevOptions, this.options)) {
|
|
28
|
+
this.client.getMutationCache().notify({
|
|
29
|
+
type: 'observerOptionsUpdated',
|
|
30
|
+
mutation: this.currentMutation,
|
|
31
|
+
observer: this
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
(_this$currentMutation = this.currentMutation) == null ? void 0 : _this$currentMutation.setOptions(this.options);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
onUnsubscribe() {
|
|
39
|
+
if (!this.hasListeners()) {
|
|
40
|
+
var _this$currentMutation2;
|
|
41
|
+
|
|
42
|
+
(_this$currentMutation2 = this.currentMutation) == null ? void 0 : _this$currentMutation2.removeObserver(this);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
onMutationUpdate(action) {
|
|
47
|
+
this.updateResult(); // Determine which callbacks to trigger
|
|
48
|
+
|
|
49
|
+
const notifyOptions = {
|
|
50
|
+
listeners: true
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
if (action.type === 'success') {
|
|
54
|
+
notifyOptions.onSuccess = true;
|
|
55
|
+
} else if (action.type === 'error') {
|
|
56
|
+
notifyOptions.onError = true;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
this.notify(notifyOptions);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
getCurrentResult() {
|
|
63
|
+
return this.currentResult;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
reset() {
|
|
67
|
+
this.currentMutation = undefined;
|
|
68
|
+
this.updateResult();
|
|
69
|
+
this.notify({
|
|
70
|
+
listeners: true
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
mutate(variables, options) {
|
|
75
|
+
this.mutateOptions = options;
|
|
76
|
+
|
|
77
|
+
if (this.currentMutation) {
|
|
78
|
+
this.currentMutation.removeObserver(this);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
this.currentMutation = this.client.getMutationCache().build(this.client, { ...this.options,
|
|
82
|
+
variables: typeof variables !== 'undefined' ? variables : this.options.variables
|
|
83
|
+
});
|
|
84
|
+
this.currentMutation.addObserver(this);
|
|
85
|
+
return this.currentMutation.execute();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
updateResult() {
|
|
89
|
+
const state = this.currentMutation ? this.currentMutation.state : getDefaultState();
|
|
90
|
+
const isLoading = state.status === 'loading';
|
|
91
|
+
const result = { ...state,
|
|
92
|
+
isLoading,
|
|
93
|
+
isPending: isLoading,
|
|
94
|
+
isSuccess: state.status === 'success',
|
|
95
|
+
isError: state.status === 'error',
|
|
96
|
+
isIdle: state.status === 'idle',
|
|
97
|
+
mutate: this.mutate,
|
|
98
|
+
reset: this.reset
|
|
99
|
+
};
|
|
100
|
+
this.currentResult = result;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
notify(options) {
|
|
104
|
+
notifyManager.batch(() => {
|
|
105
|
+
// First trigger the mutate callbacks
|
|
106
|
+
if (this.mutateOptions && this.hasListeners()) {
|
|
107
|
+
if (options.onSuccess) {
|
|
108
|
+
var _this$mutateOptions$o, _this$mutateOptions, _this$mutateOptions$o2, _this$mutateOptions2;
|
|
109
|
+
|
|
110
|
+
(_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);
|
|
111
|
+
(_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);
|
|
112
|
+
} else if (options.onError) {
|
|
113
|
+
var _this$mutateOptions$o3, _this$mutateOptions3, _this$mutateOptions$o4, _this$mutateOptions4;
|
|
114
|
+
|
|
115
|
+
(_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);
|
|
116
|
+
(_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);
|
|
117
|
+
}
|
|
118
|
+
} // Then trigger the listeners
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
if (options.listeners) {
|
|
122
|
+
this.listeners.forEach(({
|
|
123
|
+
listener
|
|
124
|
+
}) => {
|
|
125
|
+
listener(this.currentResult);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export { MutationObserver };
|
|
134
|
+
//# sourceMappingURL=mutationObserver.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mutationObserver.mjs","sources":["../../src/mutationObserver.ts"],"sourcesContent":["import { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport { shallowEqualObjects } from './utils'\nimport type { QueryClient } from './queryClient'\nimport type {\n MutateOptions,\n MutationObserverBaseResult,\n MutationObserverOptions,\n MutationObserverResult,\n} from './types'\nimport type { Action, Mutation } from './mutation'\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 this.currentMutation?.setOptions(this.options)\n }\n\n protected onUnsubscribe(): void {\n if (!this.hasListeners()) {\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 isLoading = state.status === 'loading'\n const result: MutationObserverBaseResult<\n TData,\n TError,\n TVariables,\n TContext\n > = {\n ...state,\n isLoading,\n isPending: isLoading,\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 && this.hasListeners()) {\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","hasListeners","removeObserver","onMutationUpdate","action","notifyOptions","listeners","onSuccess","onError","getCurrentResult","currentResult","undefined","variables","mutateOptions","build","addObserver","execute","state","getDefaultState","isLoading","status","result","isPending","isSuccess","isError","isIdle","notifyManager","batch","data","context","onSettled","error","forEach","listener"],"mappings":";;;;;AAyBA;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,IAAh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