@tanstack/query-core 5.62.9 → 5.62.15
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/legacy/mutationCache.cjs +51 -24
- package/build/legacy/mutationCache.cjs.map +1 -1
- package/build/legacy/mutationCache.js +51 -24
- package/build/legacy/mutationCache.js.map +1 -1
- package/build/modern/mutationCache.cjs +49 -21
- package/build/modern/mutationCache.cjs.map +1 -1
- package/build/modern/mutationCache.js +49 -21
- package/build/modern/mutationCache.js.map +1 -1
- package/package.json +1 -1
- package/src/mutationCache.ts +58 -31
|
@@ -53,15 +53,17 @@ var import_notifyManager = require("./notifyManager.cjs");
|
|
|
53
53
|
var import_mutation = require("./mutation.cjs");
|
|
54
54
|
var import_utils = require("./utils.cjs");
|
|
55
55
|
var import_subscribable = require("./subscribable.cjs");
|
|
56
|
-
var _mutations, _mutationId;
|
|
56
|
+
var _mutations, _scopes, _mutationId;
|
|
57
57
|
var MutationCache = class extends import_subscribable.Subscribable {
|
|
58
58
|
constructor(config = {}) {
|
|
59
59
|
super();
|
|
60
60
|
this.config = config;
|
|
61
61
|
__privateAdd(this, _mutations, void 0);
|
|
62
|
+
__privateAdd(this, _scopes, void 0);
|
|
62
63
|
__privateAdd(this, _mutationId, void 0);
|
|
63
|
-
__privateSet(this, _mutations, /* @__PURE__ */ new
|
|
64
|
-
__privateSet(this,
|
|
64
|
+
__privateSet(this, _mutations, /* @__PURE__ */ new Set());
|
|
65
|
+
__privateSet(this, _scopes, /* @__PURE__ */ new Map());
|
|
66
|
+
__privateSet(this, _mutationId, 0);
|
|
65
67
|
}
|
|
66
68
|
build(client, options, state) {
|
|
67
69
|
const mutation = new import_mutation.Mutation({
|
|
@@ -74,46 +76,70 @@ var MutationCache = class extends import_subscribable.Subscribable {
|
|
|
74
76
|
return mutation;
|
|
75
77
|
}
|
|
76
78
|
add(mutation) {
|
|
79
|
+
__privateGet(this, _mutations).add(mutation);
|
|
77
80
|
const scope = scopeFor(mutation);
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
+
if (typeof scope === "string") {
|
|
82
|
+
const scopedMutations = __privateGet(this, _scopes).get(scope);
|
|
83
|
+
if (scopedMutations) {
|
|
84
|
+
scopedMutations.push(mutation);
|
|
85
|
+
} else {
|
|
86
|
+
__privateGet(this, _scopes).set(scope, [mutation]);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
81
89
|
this.notify({ type: "added", mutation });
|
|
82
90
|
}
|
|
83
91
|
remove(mutation) {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
92
|
+
if (__privateGet(this, _mutations).delete(mutation)) {
|
|
93
|
+
const scope = scopeFor(mutation);
|
|
94
|
+
if (typeof scope === "string") {
|
|
95
|
+
const scopedMutations = __privateGet(this, _scopes).get(scope);
|
|
96
|
+
if (scopedMutations) {
|
|
97
|
+
if (scopedMutations.length > 1) {
|
|
98
|
+
const index = scopedMutations.indexOf(mutation);
|
|
99
|
+
if (index !== -1) {
|
|
100
|
+
scopedMutations.splice(index, 1);
|
|
101
|
+
}
|
|
102
|
+
} else if (scopedMutations[0] === mutation) {
|
|
103
|
+
__privateGet(this, _scopes).delete(scope);
|
|
104
|
+
}
|
|
93
105
|
}
|
|
94
106
|
}
|
|
95
107
|
}
|
|
96
108
|
this.notify({ type: "removed", mutation });
|
|
97
109
|
}
|
|
98
110
|
canRun(mutation) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
111
|
+
const scope = scopeFor(mutation);
|
|
112
|
+
if (typeof scope === "string") {
|
|
113
|
+
const mutationsWithSameScope = __privateGet(this, _scopes).get(scope);
|
|
114
|
+
const firstPendingMutation = mutationsWithSameScope == null ? void 0 : mutationsWithSameScope.find(
|
|
115
|
+
(m) => m.state.status === "pending"
|
|
116
|
+
);
|
|
117
|
+
return !firstPendingMutation || firstPendingMutation === mutation;
|
|
118
|
+
} else {
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
102
121
|
}
|
|
103
122
|
runNext(mutation) {
|
|
104
123
|
var _a;
|
|
105
|
-
const
|
|
106
|
-
|
|
124
|
+
const scope = scopeFor(mutation);
|
|
125
|
+
if (typeof scope === "string") {
|
|
126
|
+
const foundMutation = (_a = __privateGet(this, _scopes).get(scope)) == null ? void 0 : _a.find((m) => m !== mutation && m.state.isPaused);
|
|
127
|
+
return (foundMutation == null ? void 0 : foundMutation.continue()) ?? Promise.resolve();
|
|
128
|
+
} else {
|
|
129
|
+
return Promise.resolve();
|
|
130
|
+
}
|
|
107
131
|
}
|
|
108
132
|
clear() {
|
|
109
133
|
import_notifyManager.notifyManager.batch(() => {
|
|
110
|
-
this
|
|
111
|
-
this.
|
|
134
|
+
__privateGet(this, _mutations).forEach((mutation) => {
|
|
135
|
+
this.notify({ type: "removed", mutation });
|
|
112
136
|
});
|
|
137
|
+
__privateGet(this, _mutations).clear();
|
|
138
|
+
__privateGet(this, _scopes).clear();
|
|
113
139
|
});
|
|
114
140
|
}
|
|
115
141
|
getAll() {
|
|
116
|
-
return
|
|
142
|
+
return Array.from(__privateGet(this, _mutations));
|
|
117
143
|
}
|
|
118
144
|
find(filters) {
|
|
119
145
|
const defaultedFilters = { exact: true, ...filters };
|
|
@@ -141,10 +167,11 @@ var MutationCache = class extends import_subscribable.Subscribable {
|
|
|
141
167
|
}
|
|
142
168
|
};
|
|
143
169
|
_mutations = new WeakMap();
|
|
170
|
+
_scopes = new WeakMap();
|
|
144
171
|
_mutationId = new WeakMap();
|
|
145
172
|
function scopeFor(mutation) {
|
|
146
173
|
var _a;
|
|
147
|
-
return (
|
|
174
|
+
return (_a = mutation.options.scope) == null ? void 0 : _a.id;
|
|
148
175
|
}
|
|
149
176
|
// Annotate the CommonJS export names for ESM import in node:
|
|
150
177
|
0 && (module.exports = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"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 { DefaultError, 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: DefaultError,\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: DefaultError | 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\nexport type 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 #mutations: Map<string, Array<Mutation<any, any, any, any>>>\n #mutationId: number\n\n constructor(public config: MutationCacheConfig = {}) {\n super()\n this.#mutations = new Map()\n this.#mutationId = Date.now()\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 mutationId: ++this.#mutationId,\n options: client.defaultMutationOptions(options),\n state,\n })\n\n this.add(mutation)\n\n return mutation\n }\n\n add(mutation: Mutation<any, any, any, any>): void {\n const scope = scopeFor(mutation)\n const mutations = this.#mutations.get(scope) ?? []\n mutations.push(mutation)\n this.#mutations.set(scope, mutations)\n this.notify({ type: 'added', mutation })\n }\n\n remove(mutation: Mutation<any, any, any, any>): void {\n const scope = scopeFor(mutation)\n if (this.#mutations.has(scope)) {\n const mutations = this.#mutations\n .get(scope)\n ?.filter((x) => x !== mutation)\n if (mutations) {\n if (mutations.length === 0) {\n this.#mutations.delete(scope)\n } else {\n this.#mutations.set(scope, mutations)\n }\n }\n }\n\n this.notify({ type: 'removed', mutation })\n }\n\n canRun(mutation: Mutation<any, any, any, any>): boolean {\n const firstPendingMutation = this.#mutations\n .get(scopeFor(mutation))\n ?.find((m) => m.state.status === 'pending')\n\n // we can run if there is no current pending mutation (start use-case)\n // or if WE are the first pending mutation (continue use-case)\n return !firstPendingMutation || firstPendingMutation === mutation\n }\n\n runNext(mutation: Mutation<any, any, any, any>): Promise<unknown> {\n const foundMutation = this.#mutations\n .get(scopeFor(mutation))\n ?.find((m) => m !== mutation && m.state.isPaused)\n\n return foundMutation?.continue() ?? Promise.resolve()\n }\n\n clear(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((mutation) => {\n this.remove(mutation)\n })\n })\n }\n\n getAll(): Array<Mutation> {\n return [...this.#mutations.values()].flat()\n }\n\n find<\n TData = unknown,\n TError = DefaultError,\n TVariables = any,\n TContext = unknown,\n >(\n filters: MutationFilters,\n ): Mutation<TData, TError, TVariables, TContext> | undefined {\n const defaultedFilters = { exact: true, ...filters }\n\n return this.getAll().find((mutation) =>\n matchMutation(defaultedFilters, mutation),\n ) as Mutation<TData, TError, TVariables, TContext> | undefined\n }\n\n findAll(filters: MutationFilters = {}): Array<Mutation> {\n return this.getAll().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 const pausedMutations = this.getAll().filter((x) => x.state.isPaused)\n\n return notifyManager.batch(() =>\n Promise.all(\n pausedMutations.map((mutation) => mutation.continue().catch(noop)),\n ),\n )\n }\n}\n\nfunction scopeFor(mutation: Mutation<any, any, any, any>) {\n return mutation.options.scope?.id ?? String(mutation.mutationId)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,2BAA8B;AAC9B,sBAAyB;AACzB,mBAAoC;AACpC,0BAA6B;AAH7B;AAmFO,IAAM,gBAAN,cAA4B,iCAAoC;AAAA,EAIrE,YAAmB,SAA8B,CAAC,GAAG;AACnD,UAAM;AADW;AAHnB;AACA;AAIE,uBAAK,YAAa,oBAAI,IAAI;AAC1B,uBAAK,aAAc,KAAK,IAAI;AAAA,EAC9B;AAAA,EAEA,MACE,QACA,SACA,OAC+C;AAC/C,UAAM,WAAW,IAAI,yBAAS;AAAA,MAC5B,eAAe;AAAA,MACf,YAAmB,EAAL,uBAAK,aAAL;AAAA,MAC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,CAAC,aAAa;AAClC,aAAK,OAAO,QAAQ;AAAA,MACtB,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AAAA,EAEA,SAA0B;AACxB,WAAO,CAAC,GAAG,mBAAK,YAAW,OAAO,CAAC,EAAE,KAAK;AAAA,EAC5C;AAAA,EAEA,KAME,SAC2D;AAC3D,UAAM,mBAAmB,EAAE,OAAO,MAAM,GAAG,QAAQ;AAEnD,WAAO,KAAK,OAAO,EAAE;AAAA,MAAK,CAAC,iBACzB,4BAAc,kBAAkB,QAAQ;AAAA,IAC1C;AAAA,EACF;AAAA,EAEA,QAAQ,UAA2B,CAAC,GAAoB;AACtD,WAAO,KAAK,OAAO,EAAE,OAAO,CAAC,iBAAa,4BAAc,SAAS,QAAQ,CAAC;AAAA,EAC5E;AAAA,EAEA,OAAO,OAAiC;AACtC,uCAAc,MAAM,MAAM;AACxB,WAAK,UAAU,QAAQ,CAAC,aAAa;AACnC,iBAAS,KAAK;AAAA,MAChB,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AAAA,EAEA,wBAA0C;AACxC,UAAM,kBAAkB,KAAK,OAAO,EAAE,OAAO,CAAC,MAAM,EAAE,MAAM,QAAQ;AAEpE,WAAO,mCAAc;AAAA,MAAM,MACzB,QAAQ;AAAA,QACN,gBAAgB,IAAI,CAAC,aAAa,SAAS,SAAS,EAAE,MAAM,iBAAI,CAAC;AAAA,MACnE;AAAA,IACF;AAAA,EACF;AACF;AAtHE;AACA;AAuHF,SAAS,SAAS,UAAwC;AA5M1D;AA6ME,WAAO,cAAS,QAAQ,UAAjB,mBAAwB,OAAM,OAAO,SAAS,UAAU;AACjE;","names":[]}
|
|
1
|
+
{"version":3,"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 { DefaultError, 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: DefaultError,\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: DefaultError | 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\nexport type 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 #mutations: Set<Mutation<any, any, any, any>>\n #scopes: Map<string, Array<Mutation<any, any, any, any>>>\n #mutationId: number\n\n constructor(public config: MutationCacheConfig = {}) {\n super()\n this.#mutations = new Set()\n this.#scopes = new Map()\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 mutationId: ++this.#mutationId,\n options: client.defaultMutationOptions(options),\n state,\n })\n\n this.add(mutation)\n\n return mutation\n }\n\n add(mutation: Mutation<any, any, any, any>): void {\n this.#mutations.add(mutation)\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const scopedMutations = this.#scopes.get(scope)\n if (scopedMutations) {\n scopedMutations.push(mutation)\n } else {\n this.#scopes.set(scope, [mutation])\n }\n }\n this.notify({ type: 'added', mutation })\n }\n\n remove(mutation: Mutation<any, any, any, any>): void {\n if (this.#mutations.delete(mutation)) {\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const scopedMutations = this.#scopes.get(scope)\n if (scopedMutations) {\n if (scopedMutations.length > 1) {\n const index = scopedMutations.indexOf(mutation)\n if (index !== -1) {\n scopedMutations.splice(index, 1)\n }\n } else if (scopedMutations[0] === mutation) {\n this.#scopes.delete(scope)\n }\n }\n }\n }\n\n // Currently we notify the removal even if the mutation was already removed.\n // Consider making this an error or not notifying of the removal depending on the desired semantics.\n this.notify({ type: 'removed', mutation })\n }\n\n canRun(mutation: Mutation<any, any, any, any>): boolean {\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const mutationsWithSameScope = this.#scopes.get(scope)\n const firstPendingMutation = mutationsWithSameScope?.find(\n (m) => m.state.status === 'pending',\n )\n // we can run if there is no current pending mutation (start use-case)\n // or if WE are the first pending mutation (continue use-case)\n return !firstPendingMutation || firstPendingMutation === mutation\n } else {\n // For unscoped mutations there are never any pending mutations in front of the\n // current mutation\n return true\n }\n }\n\n runNext(mutation: Mutation<any, any, any, any>): Promise<unknown> {\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const foundMutation = this.#scopes\n .get(scope)\n ?.find((m) => m !== mutation && m.state.isPaused)\n\n return foundMutation?.continue() ?? Promise.resolve()\n } else {\n return Promise.resolve()\n }\n }\n\n clear(): void {\n notifyManager.batch(() => {\n this.#mutations.forEach((mutation) => {\n this.notify({ type: 'removed', mutation })\n })\n this.#mutations.clear()\n this.#scopes.clear()\n })\n }\n\n getAll(): Array<Mutation> {\n return Array.from(this.#mutations)\n }\n\n find<\n TData = unknown,\n TError = DefaultError,\n TVariables = any,\n TContext = unknown,\n >(\n filters: MutationFilters,\n ): Mutation<TData, TError, TVariables, TContext> | undefined {\n const defaultedFilters = { exact: true, ...filters }\n\n return this.getAll().find((mutation) =>\n matchMutation(defaultedFilters, mutation),\n ) as Mutation<TData, TError, TVariables, TContext> | undefined\n }\n\n findAll(filters: MutationFilters = {}): Array<Mutation> {\n return this.getAll().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 const pausedMutations = this.getAll().filter((x) => x.state.isPaused)\n\n return notifyManager.batch(() =>\n Promise.all(\n pausedMutations.map((mutation) => mutation.continue().catch(noop)),\n ),\n )\n }\n}\n\nfunction scopeFor(mutation: Mutation<any, any, any, any>) {\n return mutation.options.scope?.id\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,2BAA8B;AAC9B,sBAAyB;AACzB,mBAAoC;AACpC,0BAA6B;AAH7B;AAmFO,IAAM,gBAAN,cAA4B,iCAAoC;AAAA,EAKrE,YAAmB,SAA8B,CAAC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AAc,SAAS,QAAQ,CAAC;AAAA,EAC5E;AAAA,EAEA,OAAO,OAAiC;AACtC,uCAAc,MAAM,MAAM;AACxB,WAAK,UAAU,QAAQ,CAAC,aAAa;AACnC,iBAAS,KAAK;AAAA,MAChB,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AAAA,EAEA,wBAA0C;AACxC,UAAM,kBAAkB,KAAK,OAAO,EAAE,OAAO,CAAC,MAAM,EAAE,MAAM,QAAQ;AAEpE,WAAO,mCAAc;AAAA,MAAM,MACzB,QAAQ;AAAA,QACN,gBAAgB,IAAI,CAAC,aAAa,SAAS,SAAS,EAAE,MAAM,iBAAI,CAAC;AAAA,MACnE;AAAA,IACF;AAAA,EACF;AACF;AAjJE;AACA;AACA;AAiJF,SAAS,SAAS,UAAwC;AAvO1D;AAwOE,UAAO,cAAS,QAAQ,UAAjB,mBAAwB;AACjC;","names":[]}
|
|
@@ -10,15 +10,17 @@ import { notifyManager } from "./notifyManager.js";
|
|
|
10
10
|
import { Mutation } from "./mutation.js";
|
|
11
11
|
import { matchMutation, noop } from "./utils.js";
|
|
12
12
|
import { Subscribable } from "./subscribable.js";
|
|
13
|
-
var _mutations, _mutationId;
|
|
13
|
+
var _mutations, _scopes, _mutationId;
|
|
14
14
|
var MutationCache = class extends Subscribable {
|
|
15
15
|
constructor(config = {}) {
|
|
16
16
|
super();
|
|
17
17
|
this.config = config;
|
|
18
18
|
__privateAdd(this, _mutations, void 0);
|
|
19
|
+
__privateAdd(this, _scopes, void 0);
|
|
19
20
|
__privateAdd(this, _mutationId, void 0);
|
|
20
|
-
__privateSet(this, _mutations, /* @__PURE__ */ new
|
|
21
|
-
__privateSet(this,
|
|
21
|
+
__privateSet(this, _mutations, /* @__PURE__ */ new Set());
|
|
22
|
+
__privateSet(this, _scopes, /* @__PURE__ */ new Map());
|
|
23
|
+
__privateSet(this, _mutationId, 0);
|
|
22
24
|
}
|
|
23
25
|
build(client, options, state) {
|
|
24
26
|
const mutation = new Mutation({
|
|
@@ -31,46 +33,70 @@ var MutationCache = class extends Subscribable {
|
|
|
31
33
|
return mutation;
|
|
32
34
|
}
|
|
33
35
|
add(mutation) {
|
|
36
|
+
__privateGet(this, _mutations).add(mutation);
|
|
34
37
|
const scope = scopeFor(mutation);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
+
if (typeof scope === "string") {
|
|
39
|
+
const scopedMutations = __privateGet(this, _scopes).get(scope);
|
|
40
|
+
if (scopedMutations) {
|
|
41
|
+
scopedMutations.push(mutation);
|
|
42
|
+
} else {
|
|
43
|
+
__privateGet(this, _scopes).set(scope, [mutation]);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
38
46
|
this.notify({ type: "added", mutation });
|
|
39
47
|
}
|
|
40
48
|
remove(mutation) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
49
|
+
if (__privateGet(this, _mutations).delete(mutation)) {
|
|
50
|
+
const scope = scopeFor(mutation);
|
|
51
|
+
if (typeof scope === "string") {
|
|
52
|
+
const scopedMutations = __privateGet(this, _scopes).get(scope);
|
|
53
|
+
if (scopedMutations) {
|
|
54
|
+
if (scopedMutations.length > 1) {
|
|
55
|
+
const index = scopedMutations.indexOf(mutation);
|
|
56
|
+
if (index !== -1) {
|
|
57
|
+
scopedMutations.splice(index, 1);
|
|
58
|
+
}
|
|
59
|
+
} else if (scopedMutations[0] === mutation) {
|
|
60
|
+
__privateGet(this, _scopes).delete(scope);
|
|
61
|
+
}
|
|
50
62
|
}
|
|
51
63
|
}
|
|
52
64
|
}
|
|
53
65
|
this.notify({ type: "removed", mutation });
|
|
54
66
|
}
|
|
55
67
|
canRun(mutation) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
68
|
+
const scope = scopeFor(mutation);
|
|
69
|
+
if (typeof scope === "string") {
|
|
70
|
+
const mutationsWithSameScope = __privateGet(this, _scopes).get(scope);
|
|
71
|
+
const firstPendingMutation = mutationsWithSameScope == null ? void 0 : mutationsWithSameScope.find(
|
|
72
|
+
(m) => m.state.status === "pending"
|
|
73
|
+
);
|
|
74
|
+
return !firstPendingMutation || firstPendingMutation === mutation;
|
|
75
|
+
} else {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
59
78
|
}
|
|
60
79
|
runNext(mutation) {
|
|
61
80
|
var _a;
|
|
62
|
-
const
|
|
63
|
-
|
|
81
|
+
const scope = scopeFor(mutation);
|
|
82
|
+
if (typeof scope === "string") {
|
|
83
|
+
const foundMutation = (_a = __privateGet(this, _scopes).get(scope)) == null ? void 0 : _a.find((m) => m !== mutation && m.state.isPaused);
|
|
84
|
+
return (foundMutation == null ? void 0 : foundMutation.continue()) ?? Promise.resolve();
|
|
85
|
+
} else {
|
|
86
|
+
return Promise.resolve();
|
|
87
|
+
}
|
|
64
88
|
}
|
|
65
89
|
clear() {
|
|
66
90
|
notifyManager.batch(() => {
|
|
67
|
-
this
|
|
68
|
-
this.
|
|
91
|
+
__privateGet(this, _mutations).forEach((mutation) => {
|
|
92
|
+
this.notify({ type: "removed", mutation });
|
|
69
93
|
});
|
|
94
|
+
__privateGet(this, _mutations).clear();
|
|
95
|
+
__privateGet(this, _scopes).clear();
|
|
70
96
|
});
|
|
71
97
|
}
|
|
72
98
|
getAll() {
|
|
73
|
-
return
|
|
99
|
+
return Array.from(__privateGet(this, _mutations));
|
|
74
100
|
}
|
|
75
101
|
find(filters) {
|
|
76
102
|
const defaultedFilters = { exact: true, ...filters };
|
|
@@ -98,10 +124,11 @@ var MutationCache = class extends Subscribable {
|
|
|
98
124
|
}
|
|
99
125
|
};
|
|
100
126
|
_mutations = new WeakMap();
|
|
127
|
+
_scopes = new WeakMap();
|
|
101
128
|
_mutationId = new WeakMap();
|
|
102
129
|
function scopeFor(mutation) {
|
|
103
130
|
var _a;
|
|
104
|
-
return (
|
|
131
|
+
return (_a = mutation.options.scope) == null ? void 0 : _a.id;
|
|
105
132
|
}
|
|
106
133
|
export {
|
|
107
134
|
MutationCache
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"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 { DefaultError, 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: DefaultError,\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: DefaultError | 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\nexport type 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 #mutations: Map<string, Array<Mutation<any, any, any, any>>>\n #mutationId: number\n\n constructor(public config: MutationCacheConfig = {}) {\n super()\n this.#mutations = new Map()\n this.#mutationId = Date.now()\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 mutationId: ++this.#mutationId,\n options: client.defaultMutationOptions(options),\n state,\n })\n\n this.add(mutation)\n\n return mutation\n }\n\n add(mutation: Mutation<any, any, any, any>): void {\n const scope = scopeFor(mutation)\n const mutations = this.#mutations.get(scope) ?? []\n mutations.push(mutation)\n this.#mutations.set(scope, mutations)\n this.notify({ type: 'added', mutation })\n }\n\n remove(mutation: Mutation<any, any, any, any>): void {\n const scope = scopeFor(mutation)\n if (this.#mutations.has(scope)) {\n const mutations = this.#mutations\n .get(scope)\n ?.filter((x) => x !== mutation)\n if (mutations) {\n if (mutations.length === 0) {\n this.#mutations.delete(scope)\n } else {\n this.#mutations.set(scope, mutations)\n }\n }\n }\n\n this.notify({ type: 'removed', mutation })\n }\n\n canRun(mutation: Mutation<any, any, any, any>): boolean {\n const firstPendingMutation = this.#mutations\n .get(scopeFor(mutation))\n ?.find((m) => m.state.status === 'pending')\n\n // we can run if there is no current pending mutation (start use-case)\n // or if WE are the first pending mutation (continue use-case)\n return !firstPendingMutation || firstPendingMutation === mutation\n }\n\n runNext(mutation: Mutation<any, any, any, any>): Promise<unknown> {\n const foundMutation = this.#mutations\n .get(scopeFor(mutation))\n ?.find((m) => m !== mutation && m.state.isPaused)\n\n return foundMutation?.continue() ?? Promise.resolve()\n }\n\n clear(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((mutation) => {\n this.remove(mutation)\n })\n })\n }\n\n getAll(): Array<Mutation> {\n return [...this.#mutations.values()].flat()\n }\n\n find<\n TData = unknown,\n TError = DefaultError,\n TVariables = any,\n TContext = unknown,\n >(\n filters: MutationFilters,\n ): Mutation<TData, TError, TVariables, TContext> | undefined {\n const defaultedFilters = { exact: true, ...filters }\n\n return this.getAll().find((mutation) =>\n matchMutation(defaultedFilters, mutation),\n ) as Mutation<TData, TError, TVariables, TContext> | undefined\n }\n\n findAll(filters: MutationFilters = {}): Array<Mutation> {\n return this.getAll().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 const pausedMutations = this.getAll().filter((x) => x.state.isPaused)\n\n return notifyManager.batch(() =>\n Promise.all(\n pausedMutations.map((mutation) => mutation.continue().catch(noop)),\n ),\n )\n }\n}\n\nfunction scopeFor(mutation: Mutation<any, any, any, any>) {\n return mutation.options.scope?.id ?? String(mutation.mutationId)\n}\n"],"mappings":";;;;;;;;AAAA,SAAS,qBAAqB;AAC9B,SAAS,gBAAgB;AACzB,SAAS,eAAe,YAAY;AACpC,SAAS,oBAAoB;AAH7B;AAmFO,IAAM,gBAAN,cAA4B,aAAoC;AAAA,EAIrE,YAAmB,SAA8B,CAAC,GAAG;AACnD,UAAM;AADW;AAHnB;AACA;AAIE,uBAAK,YAAa,oBAAI,IAAI;AAC1B,uBAAK,aAAc,KAAK,IAAI;AAAA,EAC9B;AAAA,EAEA,MACE,QACA,SACA,OAC+C;AAC/C,UAAM,WAAW,IAAI,SAAS;AAAA,MAC5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AACZ,kBAAc,MAAM,MAAM;AACxB,WAAK,OAAO,EAAE,QAAQ,CAAC,aAAa;AAClC,aAAK,OAAO,QAAQ;AAAA,MACtB,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AAAA,EAEA,SAA0B;AACxB,WAAO,CAAC,GAAG,mBAAK,YAAW,OAAO,CAAC,EAAE,KAAK;AAAA,EAC5C;AAAA,EAEA,KAME,SAC2D;AAC3D,UAAM,mBAAmB,EAAE,OAAO,MAAM,GAAG,QAAQ;AAEnD,WAAO,KAAK,OAAO,EAAE;AAAA,MAAK,CAAC,aACzB,cAAc,kBAAkB,QAAQ;AAAA,IAC1C;AAAA,EACF;AAAA,EAEA,QAAQ,UAA2B,CAAC,GAAoB;AACtD,WAAO,KAAK,OAAO,EAAE,OAAO,CAAC,aAAa,cAAc,SAAS,QAAQ,CAAC;AAAA,EAC5E;AAAA,EAEA,OAAO,OAAiC;AACtC,kBAAc,MAAM,MAAM;AACxB,WAAK,UAAU,QAAQ,CAAC,aAAa;AACnC,iBAAS,KAAK;AAAA,MAChB,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AAAA,EAEA,wBAA0C;AACxC,UAAM,kBAAkB,KAAK,OAAO,EAAE,OAAO,CAAC,MAAM,EAAE,MAAM,QAAQ;AAEpE,WAAO,cAAc;AAAA,MAAM,MACzB,QAAQ;AAAA,QACN,gBAAgB,IAAI,CAAC,aAAa,SAAS,SAAS,EAAE,MAAM,IAAI,CAAC;AAAA,MACnE;AAAA,IACF;AAAA,EACF;AACF;AAtHE;AACA;AAuHF,SAAS,SAAS,UAAwC;AA5M1D;AA6ME,WAAO,cAAS,QAAQ,UAAjB,mBAAwB,OAAM,OAAO,SAAS,UAAU;AACjE;","names":[]}
|
|
1
|
+
{"version":3,"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 { DefaultError, 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: DefaultError,\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: DefaultError | 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\nexport type 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 #mutations: Set<Mutation<any, any, any, any>>\n #scopes: Map<string, Array<Mutation<any, any, any, any>>>\n #mutationId: number\n\n constructor(public config: MutationCacheConfig = {}) {\n super()\n this.#mutations = new Set()\n this.#scopes = new Map()\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 mutationId: ++this.#mutationId,\n options: client.defaultMutationOptions(options),\n state,\n })\n\n this.add(mutation)\n\n return mutation\n }\n\n add(mutation: Mutation<any, any, any, any>): void {\n this.#mutations.add(mutation)\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const scopedMutations = this.#scopes.get(scope)\n if (scopedMutations) {\n scopedMutations.push(mutation)\n } else {\n this.#scopes.set(scope, [mutation])\n }\n }\n this.notify({ type: 'added', mutation })\n }\n\n remove(mutation: Mutation<any, any, any, any>): void {\n if (this.#mutations.delete(mutation)) {\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const scopedMutations = this.#scopes.get(scope)\n if (scopedMutations) {\n if (scopedMutations.length > 1) {\n const index = scopedMutations.indexOf(mutation)\n if (index !== -1) {\n scopedMutations.splice(index, 1)\n }\n } else if (scopedMutations[0] === mutation) {\n this.#scopes.delete(scope)\n }\n }\n }\n }\n\n // Currently we notify the removal even if the mutation was already removed.\n // Consider making this an error or not notifying of the removal depending on the desired semantics.\n this.notify({ type: 'removed', mutation })\n }\n\n canRun(mutation: Mutation<any, any, any, any>): boolean {\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const mutationsWithSameScope = this.#scopes.get(scope)\n const firstPendingMutation = mutationsWithSameScope?.find(\n (m) => m.state.status === 'pending',\n )\n // we can run if there is no current pending mutation (start use-case)\n // or if WE are the first pending mutation (continue use-case)\n return !firstPendingMutation || firstPendingMutation === mutation\n } else {\n // For unscoped mutations there are never any pending mutations in front of the\n // current mutation\n return true\n }\n }\n\n runNext(mutation: Mutation<any, any, any, any>): Promise<unknown> {\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const foundMutation = this.#scopes\n .get(scope)\n ?.find((m) => m !== mutation && m.state.isPaused)\n\n return foundMutation?.continue() ?? Promise.resolve()\n } else {\n return Promise.resolve()\n }\n }\n\n clear(): void {\n notifyManager.batch(() => {\n this.#mutations.forEach((mutation) => {\n this.notify({ type: 'removed', mutation })\n })\n this.#mutations.clear()\n this.#scopes.clear()\n })\n }\n\n getAll(): Array<Mutation> {\n return Array.from(this.#mutations)\n }\n\n find<\n TData = unknown,\n TError = DefaultError,\n TVariables = any,\n TContext = unknown,\n >(\n filters: MutationFilters,\n ): Mutation<TData, TError, TVariables, TContext> | undefined {\n const defaultedFilters = { exact: true, ...filters }\n\n return this.getAll().find((mutation) =>\n matchMutation(defaultedFilters, mutation),\n ) as Mutation<TData, TError, TVariables, TContext> | undefined\n }\n\n findAll(filters: MutationFilters = {}): Array<Mutation> {\n return this.getAll().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 const pausedMutations = this.getAll().filter((x) => x.state.isPaused)\n\n return notifyManager.batch(() =>\n Promise.all(\n pausedMutations.map((mutation) => mutation.continue().catch(noop)),\n ),\n )\n }\n}\n\nfunction scopeFor(mutation: Mutation<any, any, any, any>) {\n return mutation.options.scope?.id\n}\n"],"mappings":";;;;;;;;AAAA,SAAS,qBAAqB;AAC9B,SAAS,gBAAgB;AACzB,SAAS,eAAe,YAAY;AACpC,SAAS,oBAAoB;AAH7B;AAmFO,IAAM,gBAAN,cAA4B,aAAoC;AAAA,EAKrE,YAAmB,SAA8B,CAAC,G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S,QAAQ,CAAC;AAAA,EAC5E;AAAA,EAEA,OAAO,OAAiC;AACtC,kBAAc,MAAM,MAAM;AACxB,WAAK,UAAU,QAAQ,CAAC,aAAa;AACnC,iBAAS,KAAK;AAAA,MAChB,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AAAA,EAEA,wBAA0C;AACxC,UAAM,kBAAkB,KAAK,OAAO,EAAE,OAAO,CAAC,MAAM,EAAE,MAAM,QAAQ;AAEpE,WAAO,cAAc;AAAA,MAAM,MACzB,QAAQ;AAAA,QACN,gBAAgB,IAAI,CAAC,aAAa,SAAS,SAAS,EAAE,MAAM,IAAI,CAAC;AAAA,MACnE;AAAA,IACF;AAAA,EACF;AACF;AAjJE;AACA;AACA;AAiJF,SAAS,SAAS,UAAwC;AAvO1D;AAwOE,UAAO,cAAS,QAAQ,UAAjB,mBAAwB;AACjC;","names":[]}
|
|
@@ -31,10 +31,12 @@ var MutationCache = class extends import_subscribable.Subscribable {
|
|
|
31
31
|
constructor(config = {}) {
|
|
32
32
|
super();
|
|
33
33
|
this.config = config;
|
|
34
|
-
this.#mutations = /* @__PURE__ */ new
|
|
35
|
-
this.#
|
|
34
|
+
this.#mutations = /* @__PURE__ */ new Set();
|
|
35
|
+
this.#scopes = /* @__PURE__ */ new Map();
|
|
36
|
+
this.#mutationId = 0;
|
|
36
37
|
}
|
|
37
38
|
#mutations;
|
|
39
|
+
#scopes;
|
|
38
40
|
#mutationId;
|
|
39
41
|
build(client, options, state) {
|
|
40
42
|
const mutation = new import_mutation.Mutation({
|
|
@@ -47,43 +49,69 @@ var MutationCache = class extends import_subscribable.Subscribable {
|
|
|
47
49
|
return mutation;
|
|
48
50
|
}
|
|
49
51
|
add(mutation) {
|
|
52
|
+
this.#mutations.add(mutation);
|
|
50
53
|
const scope = scopeFor(mutation);
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
+
if (typeof scope === "string") {
|
|
55
|
+
const scopedMutations = this.#scopes.get(scope);
|
|
56
|
+
if (scopedMutations) {
|
|
57
|
+
scopedMutations.push(mutation);
|
|
58
|
+
} else {
|
|
59
|
+
this.#scopes.set(scope, [mutation]);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
54
62
|
this.notify({ type: "added", mutation });
|
|
55
63
|
}
|
|
56
64
|
remove(mutation) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
if (
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
+
if (this.#mutations.delete(mutation)) {
|
|
66
|
+
const scope = scopeFor(mutation);
|
|
67
|
+
if (typeof scope === "string") {
|
|
68
|
+
const scopedMutations = this.#scopes.get(scope);
|
|
69
|
+
if (scopedMutations) {
|
|
70
|
+
if (scopedMutations.length > 1) {
|
|
71
|
+
const index = scopedMutations.indexOf(mutation);
|
|
72
|
+
if (index !== -1) {
|
|
73
|
+
scopedMutations.splice(index, 1);
|
|
74
|
+
}
|
|
75
|
+
} else if (scopedMutations[0] === mutation) {
|
|
76
|
+
this.#scopes.delete(scope);
|
|
77
|
+
}
|
|
65
78
|
}
|
|
66
79
|
}
|
|
67
80
|
}
|
|
68
81
|
this.notify({ type: "removed", mutation });
|
|
69
82
|
}
|
|
70
83
|
canRun(mutation) {
|
|
71
|
-
const
|
|
72
|
-
|
|
84
|
+
const scope = scopeFor(mutation);
|
|
85
|
+
if (typeof scope === "string") {
|
|
86
|
+
const mutationsWithSameScope = this.#scopes.get(scope);
|
|
87
|
+
const firstPendingMutation = mutationsWithSameScope?.find(
|
|
88
|
+
(m) => m.state.status === "pending"
|
|
89
|
+
);
|
|
90
|
+
return !firstPendingMutation || firstPendingMutation === mutation;
|
|
91
|
+
} else {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
73
94
|
}
|
|
74
95
|
runNext(mutation) {
|
|
75
|
-
const
|
|
76
|
-
|
|
96
|
+
const scope = scopeFor(mutation);
|
|
97
|
+
if (typeof scope === "string") {
|
|
98
|
+
const foundMutation = this.#scopes.get(scope)?.find((m) => m !== mutation && m.state.isPaused);
|
|
99
|
+
return foundMutation?.continue() ?? Promise.resolve();
|
|
100
|
+
} else {
|
|
101
|
+
return Promise.resolve();
|
|
102
|
+
}
|
|
77
103
|
}
|
|
78
104
|
clear() {
|
|
79
105
|
import_notifyManager.notifyManager.batch(() => {
|
|
80
|
-
this.
|
|
81
|
-
this.
|
|
106
|
+
this.#mutations.forEach((mutation) => {
|
|
107
|
+
this.notify({ type: "removed", mutation });
|
|
82
108
|
});
|
|
109
|
+
this.#mutations.clear();
|
|
110
|
+
this.#scopes.clear();
|
|
83
111
|
});
|
|
84
112
|
}
|
|
85
113
|
getAll() {
|
|
86
|
-
return
|
|
114
|
+
return Array.from(this.#mutations);
|
|
87
115
|
}
|
|
88
116
|
find(filters) {
|
|
89
117
|
const defaultedFilters = { exact: true, ...filters };
|
|
@@ -111,7 +139,7 @@ var MutationCache = class extends import_subscribable.Subscribable {
|
|
|
111
139
|
}
|
|
112
140
|
};
|
|
113
141
|
function scopeFor(mutation) {
|
|
114
|
-
return mutation.options.scope?.id
|
|
142
|
+
return mutation.options.scope?.id;
|
|
115
143
|
}
|
|
116
144
|
// Annotate the CommonJS export names for ESM import in node:
|
|
117
145
|
0 && (module.exports = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"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 { DefaultError, 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: DefaultError,\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: DefaultError | 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\nexport type 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 #mutations: Map<string, Array<Mutation<any, any, any, any>>>\n #mutationId: number\n\n constructor(public config: MutationCacheConfig = {}) {\n super()\n this.#mutations = new Map()\n this.#mutationId = Date.now()\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 mutationId: ++this.#mutationId,\n options: client.defaultMutationOptions(options),\n state,\n })\n\n this.add(mutation)\n\n return mutation\n }\n\n add(mutation: Mutation<any, any, any, any>): void {\n const scope = scopeFor(mutation)\n const mutations = this.#mutations.get(scope) ?? []\n mutations.push(mutation)\n this.#mutations.set(scope, mutations)\n this.notify({ type: 'added', mutation })\n }\n\n remove(mutation: Mutation<any, any, any, any>): void {\n const scope = scopeFor(mutation)\n if (this.#mutations.has(scope)) {\n const mutations = this.#mutations\n .get(scope)\n ?.filter((x) => x !== mutation)\n if (mutations) {\n if (mutations.length === 0) {\n this.#mutations.delete(scope)\n } else {\n this.#mutations.set(scope, mutations)\n }\n }\n }\n\n this.notify({ type: 'removed', mutation })\n }\n\n canRun(mutation: Mutation<any, any, any, any>): boolean {\n const firstPendingMutation = this.#mutations\n .get(scopeFor(mutation))\n ?.find((m) => m.state.status === 'pending')\n\n // we can run if there is no current pending mutation (start use-case)\n // or if WE are the first pending mutation (continue use-case)\n return !firstPendingMutation || firstPendingMutation === mutation\n }\n\n runNext(mutation: Mutation<any, any, any, any>): Promise<unknown> {\n const foundMutation = this.#mutations\n .get(scopeFor(mutation))\n ?.find((m) => m !== mutation && m.state.isPaused)\n\n return foundMutation?.continue() ?? Promise.resolve()\n }\n\n clear(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((mutation) => {\n this.remove(mutation)\n })\n })\n }\n\n getAll(): Array<Mutation> {\n return [...this.#mutations.values()].flat()\n }\n\n find<\n TData = unknown,\n TError = DefaultError,\n TVariables = any,\n TContext = unknown,\n >(\n filters: MutationFilters,\n ): Mutation<TData, TError, TVariables, TContext> | undefined {\n const defaultedFilters = { exact: true, ...filters }\n\n return this.getAll().find((mutation) =>\n matchMutation(defaultedFilters, mutation),\n ) as Mutation<TData, TError, TVariables, TContext> | undefined\n }\n\n findAll(filters: MutationFilters = {}): Array<Mutation> {\n return this.getAll().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 const pausedMutations = this.getAll().filter((x) => x.state.isPaused)\n\n return notifyManager.batch(() =>\n Promise.all(\n pausedMutations.map((mutation) => mutation.continue().catch(noop)),\n ),\n )\n }\n}\n\nfunction scopeFor(mutation: Mutation<any, any, any, any>) {\n return mutation.options.scope?.id ?? String(mutation.mutationId)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,2BAA8B;AAC9B,sBAAyB;AACzB,mBAAoC;AACpC,0BAA6B;AAgFtB,IAAM,gBAAN,cAA4B,iCAAoC;AAAA,EAIrE,YAAmB,SAA8B,CAAC,GAAG;AACnD,UAAM;AADW;AAEjB,SAAK,aAAa,oBAAI,IAAI;AAC1B,SAAK,cAAc,KAAK,IAAI;AAAA,EAC9B;AAAA,EAPA;AAAA,EACA;AAAA,EAQA,MACE,QACA,SACA,OAC+C;AAC/C,UAAM,WAAW,IAAI,yBAAS;AAAA,MAC5B,eAAe;AAAA,MACf,YAAY,EAAE,KAAK;AAAA,MACnB,SAAS,OAAO,uBAAuB,OAAO;AAAA,MAC9C;AAAA,IACF,CAAC;AAED,SAAK,IAAI,QAAQ;AAEjB,WAAO;AAAA,EACT;AAAA,EAEA,IAAI,UAA8C;AAChD,UAAM,QAAQ,SAAS,QAAQ;AAC/B,UAAM,YAAY,KAAK,WAAW,IAAI,KAAK,KAAK,CAAC;AACjD,cAAU,KAAK,QAAQ;AACvB,SAAK,WAAW,IAAI,OAAO,SAAS;AACpC,SAAK,OAAO,EAAE,MAAM,SAAS,SAAS,CAAC;AAAA,EACzC;AAAA,EAEA,OAAO,UAA8C;AACnD,UAAM,QAAQ,SAAS,QAAQ;AAC/B,QAAI,KAAK,WAAW,IAAI,KAAK,GAA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;AAAA,EAC5E;AAAA,EAEA,OAAO,OAAiC;AACtC,uCAAc,MAAM,MAAM;AACxB,WAAK,UAAU,QAAQ,CAAC,aAAa;AACnC,iBAAS,KAAK;AAAA,MAChB,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AAAA,EAEA,wBAA0C;AACxC,UAAM,kBAAkB,KAAK,OAAO,EAAE,OAAO,CAAC,MAAM,EAAE,MAAM,QAAQ;AAEpE,WAAO,mCAAc;AAAA,MAAM,MACzB,QAAQ;AAAA,QACN,gBAAgB,IAAI,CAAC,aAAa,SAAS,SAAS,EAAE,MAAM,iBAAI,CAAC;AAAA,MACnE;AAAA,IACF;AAAA,EACF;AACF;AAEA,SAAS,SAAS,UAAwC;AACxD,SAAO,SAAS,QAAQ,OAAO,MAAM,OAAO,SAAS,UAAU;AACjE;","names":[]}
|
|
1
|
+
{"version":3,"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 { DefaultError, 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: DefaultError,\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: DefaultError | 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\nexport type 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 #mutations: Set<Mutation<any, any, any, any>>\n #scopes: Map<string, Array<Mutation<any, any, any, any>>>\n #mutationId: number\n\n constructor(public config: MutationCacheConfig = {}) {\n super()\n this.#mutations = new Set()\n this.#scopes = new Map()\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 mutationId: ++this.#mutationId,\n options: client.defaultMutationOptions(options),\n state,\n })\n\n this.add(mutation)\n\n return mutation\n }\n\n add(mutation: Mutation<any, any, any, any>): void {\n this.#mutations.add(mutation)\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const scopedMutations = this.#scopes.get(scope)\n if (scopedMutations) {\n scopedMutations.push(mutation)\n } else {\n this.#scopes.set(scope, [mutation])\n }\n }\n this.notify({ type: 'added', mutation })\n }\n\n remove(mutation: Mutation<any, any, any, any>): void {\n if (this.#mutations.delete(mutation)) {\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const scopedMutations = this.#scopes.get(scope)\n if (scopedMutations) {\n if (scopedMutations.length > 1) {\n const index = scopedMutations.indexOf(mutation)\n if (index !== -1) {\n scopedMutations.splice(index, 1)\n }\n } else if (scopedMutations[0] === mutation) {\n this.#scopes.delete(scope)\n }\n }\n }\n }\n\n // Currently we notify the removal even if the mutation was already removed.\n // Consider making this an error or not notifying of the removal depending on the desired semantics.\n this.notify({ type: 'removed', mutation })\n }\n\n canRun(mutation: Mutation<any, any, any, any>): boolean {\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const mutationsWithSameScope = this.#scopes.get(scope)\n const firstPendingMutation = mutationsWithSameScope?.find(\n (m) => m.state.status === 'pending',\n )\n // we can run if there is no current pending mutation (start use-case)\n // or if WE are the first pending mutation (continue use-case)\n return !firstPendingMutation || firstPendingMutation === mutation\n } else {\n // For unscoped mutations there are never any pending mutations in front of the\n // current mutation\n return true\n }\n }\n\n runNext(mutation: Mutation<any, any, any, any>): Promise<unknown> {\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const foundMutation = this.#scopes\n .get(scope)\n ?.find((m) => m !== mutation && m.state.isPaused)\n\n return foundMutation?.continue() ?? Promise.resolve()\n } else {\n return Promise.resolve()\n }\n }\n\n clear(): void {\n notifyManager.batch(() => {\n this.#mutations.forEach((mutation) => {\n this.notify({ type: 'removed', mutation })\n })\n this.#mutations.clear()\n this.#scopes.clear()\n })\n }\n\n getAll(): Array<Mutation> {\n return Array.from(this.#mutations)\n }\n\n find<\n TData = unknown,\n TError = DefaultError,\n TVariables = any,\n TContext = unknown,\n >(\n filters: MutationFilters,\n ): Mutation<TData, TError, TVariables, TContext> | undefined {\n const defaultedFilters = { exact: true, ...filters }\n\n return this.getAll().find((mutation) =>\n matchMutation(defaultedFilters, mutation),\n ) as Mutation<TData, TError, TVariables, TContext> | undefined\n }\n\n findAll(filters: MutationFilters = {}): Array<Mutation> {\n return this.getAll().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 const pausedMutations = this.getAll().filter((x) => x.state.isPaused)\n\n return notifyManager.batch(() =>\n Promise.all(\n pausedMutations.map((mutation) => mutation.continue().catch(noop)),\n ),\n )\n }\n}\n\nfunction scopeFor(mutation: Mutation<any, any, any, any>) {\n return mutation.options.scope?.id\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,2BAA8B;AAC9B,sBAAyB;AACzB,mBAAoC;AACpC,0BAA6B;AAgFtB,IAAM,gBAAN,cAA4B,iCAAoC;AAAA,EAKrE,YAAmB,SAA8B,CAAC,GAAG;AACnD,UAAM;AADW;AAEjB,SAAK,aAAa,oBAAI,IAAI;AAC1B,SAAK,UAAU,oBAAI,IAAI;AACvB,SAAK,cAAc;AAAA,EACrB;AAAA,EATA;AAAA,EACA;AAAA,EACA;AAAA,EASA,MACE,QACA,SACA,OAC+C;AAC/C,UAAM,WAAW,IAAI,yBAAS;AAAA,MAC5B,eAAe;AAAA,MACf,YAAY,EAAE,KAAK;AAAA,MACnB,SAAS,OAAO,uBAAuB,OAAO;AAAA,MAC9C;AAAA,IACF,CAAC;AAED,SAAK,IAAI,QAAQ;AAEjB,WAAO;AAAA,EACT;AAAA,EAEA,IAAI,UAA8C;AAChD,SAAK,WAAW,IAAI,QAAQ;AAC5B,UAAM,QAAQ,SAAS,QAAQ;AAC/B,QAAI,OAAO,UAAU,UAAU;AAC7B,YAAM,kBAAkB,KAAK,QAAQ,IAAI,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Subscribable {
|
|
|
7
7
|
constructor(config = {}) {
|
|
8
8
|
super();
|
|
9
9
|
this.config = config;
|
|
10
|
-
this.#mutations = /* @__PURE__ */ new
|
|
11
|
-
this.#
|
|
10
|
+
this.#mutations = /* @__PURE__ */ new Set();
|
|
11
|
+
this.#scopes = /* @__PURE__ */ new Map();
|
|
12
|
+
this.#mutationId = 0;
|
|
12
13
|
}
|
|
13
14
|
#mutations;
|
|
15
|
+
#scopes;
|
|
14
16
|
#mutationId;
|
|
15
17
|
build(client, options, state) {
|
|
16
18
|
const mutation = new Mutation({
|
|
@@ -23,43 +25,69 @@ var MutationCache = class extends Subscribable {
|
|
|
23
25
|
return mutation;
|
|
24
26
|
}
|
|
25
27
|
add(mutation) {
|
|
28
|
+
this.#mutations.add(mutation);
|
|
26
29
|
const scope = scopeFor(mutation);
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
+
if (typeof scope === "string") {
|
|
31
|
+
const scopedMutations = this.#scopes.get(scope);
|
|
32
|
+
if (scopedMutations) {
|
|
33
|
+
scopedMutations.push(mutation);
|
|
34
|
+
} else {
|
|
35
|
+
this.#scopes.set(scope, [mutation]);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
30
38
|
this.notify({ type: "added", mutation });
|
|
31
39
|
}
|
|
32
40
|
remove(mutation) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
+
if (this.#mutations.delete(mutation)) {
|
|
42
|
+
const scope = scopeFor(mutation);
|
|
43
|
+
if (typeof scope === "string") {
|
|
44
|
+
const scopedMutations = this.#scopes.get(scope);
|
|
45
|
+
if (scopedMutations) {
|
|
46
|
+
if (scopedMutations.length > 1) {
|
|
47
|
+
const index = scopedMutations.indexOf(mutation);
|
|
48
|
+
if (index !== -1) {
|
|
49
|
+
scopedMutations.splice(index, 1);
|
|
50
|
+
}
|
|
51
|
+
} else if (scopedMutations[0] === mutation) {
|
|
52
|
+
this.#scopes.delete(scope);
|
|
53
|
+
}
|
|
41
54
|
}
|
|
42
55
|
}
|
|
43
56
|
}
|
|
44
57
|
this.notify({ type: "removed", mutation });
|
|
45
58
|
}
|
|
46
59
|
canRun(mutation) {
|
|
47
|
-
const
|
|
48
|
-
|
|
60
|
+
const scope = scopeFor(mutation);
|
|
61
|
+
if (typeof scope === "string") {
|
|
62
|
+
const mutationsWithSameScope = this.#scopes.get(scope);
|
|
63
|
+
const firstPendingMutation = mutationsWithSameScope?.find(
|
|
64
|
+
(m) => m.state.status === "pending"
|
|
65
|
+
);
|
|
66
|
+
return !firstPendingMutation || firstPendingMutation === mutation;
|
|
67
|
+
} else {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
49
70
|
}
|
|
50
71
|
runNext(mutation) {
|
|
51
|
-
const
|
|
52
|
-
|
|
72
|
+
const scope = scopeFor(mutation);
|
|
73
|
+
if (typeof scope === "string") {
|
|
74
|
+
const foundMutation = this.#scopes.get(scope)?.find((m) => m !== mutation && m.state.isPaused);
|
|
75
|
+
return foundMutation?.continue() ?? Promise.resolve();
|
|
76
|
+
} else {
|
|
77
|
+
return Promise.resolve();
|
|
78
|
+
}
|
|
53
79
|
}
|
|
54
80
|
clear() {
|
|
55
81
|
notifyManager.batch(() => {
|
|
56
|
-
this.
|
|
57
|
-
this.
|
|
82
|
+
this.#mutations.forEach((mutation) => {
|
|
83
|
+
this.notify({ type: "removed", mutation });
|
|
58
84
|
});
|
|
85
|
+
this.#mutations.clear();
|
|
86
|
+
this.#scopes.clear();
|
|
59
87
|
});
|
|
60
88
|
}
|
|
61
89
|
getAll() {
|
|
62
|
-
return
|
|
90
|
+
return Array.from(this.#mutations);
|
|
63
91
|
}
|
|
64
92
|
find(filters) {
|
|
65
93
|
const defaultedFilters = { exact: true, ...filters };
|
|
@@ -87,7 +115,7 @@ var MutationCache = class extends Subscribable {
|
|
|
87
115
|
}
|
|
88
116
|
};
|
|
89
117
|
function scopeFor(mutation) {
|
|
90
|
-
return mutation.options.scope?.id
|
|
118
|
+
return mutation.options.scope?.id;
|
|
91
119
|
}
|
|
92
120
|
export {
|
|
93
121
|
MutationCache
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"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 { DefaultError, 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: DefaultError,\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: DefaultError | 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\nexport type 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 #mutations: Map<string, Array<Mutation<any, any, any, any>>>\n #mutationId: number\n\n constructor(public config: MutationCacheConfig = {}) {\n super()\n this.#mutations = new
|
|
1
|
+
{"version":3,"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 { DefaultError, 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: DefaultError,\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: DefaultError | 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\nexport type 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 #mutations: Set<Mutation<any, any, any, any>>\n #scopes: Map<string, Array<Mutation<any, any, any, any>>>\n #mutationId: number\n\n constructor(public config: MutationCacheConfig = {}) {\n super()\n this.#mutations = new Set()\n this.#scopes = new Map()\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 mutationId: ++this.#mutationId,\n options: client.defaultMutationOptions(options),\n state,\n })\n\n this.add(mutation)\n\n return mutation\n }\n\n add(mutation: Mutation<any, any, any, any>): void {\n this.#mutations.add(mutation)\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const scopedMutations = this.#scopes.get(scope)\n if (scopedMutations) {\n scopedMutations.push(mutation)\n } else {\n this.#scopes.set(scope, [mutation])\n }\n }\n this.notify({ type: 'added', mutation })\n }\n\n remove(mutation: Mutation<any, any, any, any>): void {\n if (this.#mutations.delete(mutation)) {\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const scopedMutations = this.#scopes.get(scope)\n if (scopedMutations) {\n if (scopedMutations.length > 1) {\n const index = scopedMutations.indexOf(mutation)\n if (index !== -1) {\n scopedMutations.splice(index, 1)\n }\n } else if (scopedMutations[0] === mutation) {\n this.#scopes.delete(scope)\n }\n }\n }\n }\n\n // Currently we notify the removal even if the mutation was already removed.\n // Consider making this an error or not notifying of the removal depending on the desired semantics.\n this.notify({ type: 'removed', mutation })\n }\n\n canRun(mutation: Mutation<any, any, any, any>): boolean {\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const mutationsWithSameScope = this.#scopes.get(scope)\n const firstPendingMutation = mutationsWithSameScope?.find(\n (m) => m.state.status === 'pending',\n )\n // we can run if there is no current pending mutation (start use-case)\n // or if WE are the first pending mutation (continue use-case)\n return !firstPendingMutation || firstPendingMutation === mutation\n } else {\n // For unscoped mutations there are never any pending mutations in front of the\n // current mutation\n return true\n }\n }\n\n runNext(mutation: Mutation<any, any, any, any>): Promise<unknown> {\n const scope = scopeFor(mutation)\n if (typeof scope === 'string') {\n const foundMutation = this.#scopes\n .get(scope)\n ?.find((m) => m !== mutation && m.state.isPaused)\n\n return foundMutation?.continue() ?? Promise.resolve()\n } else {\n return Promise.resolve()\n }\n }\n\n clear(): void {\n notifyManager.batch(() => {\n this.#mutations.forEach((mutation) => {\n this.notify({ type: 'removed', mutation })\n })\n this.#mutations.clear()\n this.#scopes.clear()\n })\n }\n\n getAll(): Array<Mutation> {\n return Array.from(this.#mutations)\n }\n\n find<\n TData = unknown,\n TError = DefaultError,\n TVariables = any,\n TContext = unknown,\n >(\n filters: MutationFilters,\n ): Mutation<TData, TError, TVariables, TContext> | undefined {\n const defaultedFilters = { exact: true, ...filters }\n\n return this.getAll().find((mutation) =>\n matchMutation(defaultedFilters, mutation),\n ) as Mutation<TData, TError, TVariables, TContext> | undefined\n }\n\n findAll(filters: MutationFilters = {}): Array<Mutation> {\n return this.getAll().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 const pausedMutations = this.getAll().filter((x) => x.state.isPaused)\n\n return notifyManager.batch(() =>\n Promise.all(\n pausedMutations.map((mutation) => mutation.continue().catch(noop)),\n ),\n )\n }\n}\n\nfunction scopeFor(mutation: Mutation<any, any, any, any>) {\n return mutation.options.scope?.id\n}\n"],"mappings":";AAAA,SAAS,qBAAqB;AAC9B,SAAS,gBAAgB;AACzB,SAAS,eAAe,YAAY;AACpC,SAAS,oBAAoB;AAgFtB,IAAM,gBAAN,cAA4B,aAAoC;AAAA,EAKrE,YAAmB,SAA8B,CAAC,GAAG;AACnD,UAAM;AADW;AAEjB,SAAK,aAAa,oBAAI,IAAI;AAC1B,SAAK,UAAU,oBAAI,IAAI;AACvB,SAAK,cAAc;AAAA,EACrB;AAAA,EATA;AAAA,EACA;AAAA,EACA;AAAA,EASA,MACE,QACA,SACA,OAC+C;AAC/C,UAAM,WAAW,IAAI,SAAS;AAAA,MAC5B,eAAe;AAAA,MACf,YAAY,EAAE,KAAK;AAAA,MACnB,SAAS,OAAO,uBAAuB,OAAO;AAAA,MAC9C;AAAA,IACF,CAAC;AAED,SAAK,IAAI,QAAQ;AAEjB,WAAO;AAAA,EACT;AAAA,EAEA,IAAI,UAA8C;AAChD,SAAK,WAAW,IAAI,QAAQ;AAC5B,UAAM,QAAQ,SAAS,QAAQ;AAC/B,QAAI,OAAO,UAAU,UAAU;AAC7B,YAAM,kBAAkB,KAAK,QAAQ,IAAI,KAAK;AAC9C,UAAI,iBAAiB;AACnB,wBAAgB,KAAK,QAAQ;AAAA,MAC/B,OAAO;A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O,QAAQ,QAAQ;AAAA,IACzB;AAAA,EACF;AAAA,EAEA,QAAc;AACZ,kBAAc,MAAM,MAAM;AACxB,WAAK,WAAW,QAAQ,CAAC,aAAa;AACpC,aAAK,OAAO,EAAE,MAAM,WAAW,SAAS,CAAC;AAAA,MAC3C,CAAC;AACD,WAAK,WAAW,MAAM;AACtB,WAAK,QAAQ,MAAM;AAAA,IACrB,CAAC;AAAA,EACH;AAAA,EAEA,SAA0B;AACxB,WAAO,MAAM,KAAK,KAAK,UAAU;AAAA,EACnC;AAAA,EAEA,KAME,SAC2D;AAC3D,UAAM,mBAAmB,EAAE,OAAO,MAAM,GAAG,QAAQ;AAEnD,WAAO,KAAK,OAAO,EAAE;AAAA,MAAK,CAAC,aACzB,cAAc,kBAAkB,QAAQ;AAAA,IAC1C;AAAA,EACF;AAAA,EAEA,QAAQ,UAA2B,CAAC,GAAoB;AACtD,WAAO,KAAK,OAAO,EAAE,OAAO,CAAC,aAAa,cAAc,SAAS,QAAQ,CAAC;AAAA,EAC5E;AAAA,EAEA,OAAO,OAAiC;AACtC,kBAAc,MAAM,MAAM;AACxB,WAAK,UAAU,QAAQ,CAAC,aAAa;AACnC,iBAAS,KAAK;AAAA,MAChB,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AAAA,EAEA,wBAA0C;AACxC,UAAM,kBAAkB,KAAK,OAAO,EAAE,OAAO,CAAC,MAAM,EAAE,MAAM,QAAQ;AAEpE,WAAO,cAAc;AAAA,MAAM,MACzB,QAAQ;AAAA,QACN,gBAAgB,IAAI,CAAC,aAAa,SAAS,SAAS,EAAE,MAAM,IAAI,CAAC;AAAA,MACnE;AAAA,IACF;AAAA,EACF;AACF;AAEA,SAAS,SAAS,UAAwC;AACxD,SAAO,SAAS,QAAQ,OAAO;AACjC;","names":[]}
|
package/package.json
CHANGED
package/src/mutationCache.ts
CHANGED
|
@@ -82,13 +82,15 @@ type MutationCacheListener = (event: MutationCacheNotifyEvent) => void
|
|
|
82
82
|
// CLASS
|
|
83
83
|
|
|
84
84
|
export class MutationCache extends Subscribable<MutationCacheListener> {
|
|
85
|
-
#mutations:
|
|
85
|
+
#mutations: Set<Mutation<any, any, any, any>>
|
|
86
|
+
#scopes: Map<string, Array<Mutation<any, any, any, any>>>
|
|
86
87
|
#mutationId: number
|
|
87
88
|
|
|
88
89
|
constructor(public config: MutationCacheConfig = {}) {
|
|
89
90
|
super()
|
|
90
|
-
this.#mutations = new
|
|
91
|
-
this.#
|
|
91
|
+
this.#mutations = new Set()
|
|
92
|
+
this.#scopes = new Map()
|
|
93
|
+
this.#mutationId = 0
|
|
92
94
|
}
|
|
93
95
|
|
|
94
96
|
build<TData, TError, TVariables, TContext>(
|
|
@@ -109,59 +111,84 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
|
|
|
109
111
|
}
|
|
110
112
|
|
|
111
113
|
add(mutation: Mutation<any, any, any, any>): void {
|
|
114
|
+
this.#mutations.add(mutation)
|
|
112
115
|
const scope = scopeFor(mutation)
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
+
if (typeof scope === 'string') {
|
|
117
|
+
const scopedMutations = this.#scopes.get(scope)
|
|
118
|
+
if (scopedMutations) {
|
|
119
|
+
scopedMutations.push(mutation)
|
|
120
|
+
} else {
|
|
121
|
+
this.#scopes.set(scope, [mutation])
|
|
122
|
+
}
|
|
123
|
+
}
|
|
116
124
|
this.notify({ type: 'added', mutation })
|
|
117
125
|
}
|
|
118
126
|
|
|
119
127
|
remove(mutation: Mutation<any, any, any, any>): void {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
.get(scope)
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
128
|
+
if (this.#mutations.delete(mutation)) {
|
|
129
|
+
const scope = scopeFor(mutation)
|
|
130
|
+
if (typeof scope === 'string') {
|
|
131
|
+
const scopedMutations = this.#scopes.get(scope)
|
|
132
|
+
if (scopedMutations) {
|
|
133
|
+
if (scopedMutations.length > 1) {
|
|
134
|
+
const index = scopedMutations.indexOf(mutation)
|
|
135
|
+
if (index !== -1) {
|
|
136
|
+
scopedMutations.splice(index, 1)
|
|
137
|
+
}
|
|
138
|
+
} else if (scopedMutations[0] === mutation) {
|
|
139
|
+
this.#scopes.delete(scope)
|
|
140
|
+
}
|
|
130
141
|
}
|
|
131
142
|
}
|
|
132
143
|
}
|
|
133
144
|
|
|
145
|
+
// Currently we notify the removal even if the mutation was already removed.
|
|
146
|
+
// Consider making this an error or not notifying of the removal depending on the desired semantics.
|
|
134
147
|
this.notify({ type: 'removed', mutation })
|
|
135
148
|
}
|
|
136
149
|
|
|
137
150
|
canRun(mutation: Mutation<any, any, any, any>): boolean {
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
151
|
+
const scope = scopeFor(mutation)
|
|
152
|
+
if (typeof scope === 'string') {
|
|
153
|
+
const mutationsWithSameScope = this.#scopes.get(scope)
|
|
154
|
+
const firstPendingMutation = mutationsWithSameScope?.find(
|
|
155
|
+
(m) => m.state.status === 'pending',
|
|
156
|
+
)
|
|
157
|
+
// we can run if there is no current pending mutation (start use-case)
|
|
158
|
+
// or if WE are the first pending mutation (continue use-case)
|
|
159
|
+
return !firstPendingMutation || firstPendingMutation === mutation
|
|
160
|
+
} else {
|
|
161
|
+
// For unscoped mutations there are never any pending mutations in front of the
|
|
162
|
+
// current mutation
|
|
163
|
+
return true
|
|
164
|
+
}
|
|
145
165
|
}
|
|
146
166
|
|
|
147
167
|
runNext(mutation: Mutation<any, any, any, any>): Promise<unknown> {
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
168
|
+
const scope = scopeFor(mutation)
|
|
169
|
+
if (typeof scope === 'string') {
|
|
170
|
+
const foundMutation = this.#scopes
|
|
171
|
+
.get(scope)
|
|
172
|
+
?.find((m) => m !== mutation && m.state.isPaused)
|
|
151
173
|
|
|
152
|
-
|
|
174
|
+
return foundMutation?.continue() ?? Promise.resolve()
|
|
175
|
+
} else {
|
|
176
|
+
return Promise.resolve()
|
|
177
|
+
}
|
|
153
178
|
}
|
|
154
179
|
|
|
155
180
|
clear(): void {
|
|
156
181
|
notifyManager.batch(() => {
|
|
157
|
-
this.
|
|
158
|
-
this.
|
|
182
|
+
this.#mutations.forEach((mutation) => {
|
|
183
|
+
this.notify({ type: 'removed', mutation })
|
|
159
184
|
})
|
|
185
|
+
this.#mutations.clear()
|
|
186
|
+
this.#scopes.clear()
|
|
160
187
|
})
|
|
161
188
|
}
|
|
162
189
|
|
|
163
190
|
getAll(): Array<Mutation> {
|
|
164
|
-
return
|
|
191
|
+
return Array.from(this.#mutations)
|
|
165
192
|
}
|
|
166
193
|
|
|
167
194
|
find<
|
|
@@ -203,5 +230,5 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
|
|
|
203
230
|
}
|
|
204
231
|
|
|
205
232
|
function scopeFor(mutation: Mutation<any, any, any, any>) {
|
|
206
|
-
return mutation.options.scope?.id
|
|
233
|
+
return mutation.options.scope?.id
|
|
207
234
|
}
|