UNPKG

@tanstack/query-core

Version:

The framework agnostic core that powers TanStack Query

124 lines (123 loc) 4.6 kB
import { i as _classPrivateFieldInitSpec, n as _classPrivateFieldGet2, t as _classPrivateFieldSet2 } from "./classPrivateFieldSet2-CV7wyte-.js"; import { matchMutation, noop } from "./utils.js"; import { Subscribable } from "./subscribable.js"; import { notifyManager } from "./notifyManager.js"; import { Mutation } from "./mutation.js"; //#region src/mutationCache.ts var _mutations = /* @__PURE__ */ new WeakMap(); var _scopes = /* @__PURE__ */ new WeakMap(); var _mutationId = /* @__PURE__ */ new WeakMap(); var MutationCache = class extends Subscribable { constructor(config = {}) { super(); this.config = config; _classPrivateFieldInitSpec(this, _mutations, void 0); _classPrivateFieldInitSpec(this, _scopes, void 0); _classPrivateFieldInitSpec(this, _mutationId, void 0); _classPrivateFieldSet2(_mutations, this, /* @__PURE__ */ new Set()); _classPrivateFieldSet2(_scopes, this, /* @__PURE__ */ new Map()); _classPrivateFieldSet2(_mutationId, this, 0); } build(client, options, state) { var _this$mutationId; const mutation = new Mutation({ client, mutationCache: this, mutationId: _classPrivateFieldSet2(_mutationId, this, (_this$mutationId = _classPrivateFieldGet2(_mutationId, this), ++_this$mutationId)), options: client.defaultMutationOptions(options), state }); this.add(mutation); return mutation; } add(mutation) { _classPrivateFieldGet2(_mutations, this).add(mutation); const scope = scopeFor(mutation); if (typeof scope === "string") { const scopedMutations = _classPrivateFieldGet2(_scopes, this).get(scope); if (scopedMutations) scopedMutations.push(mutation); else _classPrivateFieldGet2(_scopes, this).set(scope, [mutation]); } this.notify({ type: "added", mutation }); } remove(mutation) { if (_classPrivateFieldGet2(_mutations, this).delete(mutation)) { const scope = scopeFor(mutation); if (typeof scope === "string") { const scopedMutations = _classPrivateFieldGet2(_scopes, this).get(scope); if (scopedMutations) { if (scopedMutations.length > 1) { const index = scopedMutations.indexOf(mutation); if (index !== -1) scopedMutations.splice(index, 1); } else if (scopedMutations[0] === mutation) _classPrivateFieldGet2(_scopes, this).delete(scope); } } } this.notify({ type: "removed", mutation }); } canRun(mutation) { const scope = scopeFor(mutation); if (typeof scope === "string") { const mutationsWithSameScope = _classPrivateFieldGet2(_scopes, this).get(scope); const firstPendingMutation = mutationsWithSameScope === null || mutationsWithSameScope === void 0 ? void 0 : mutationsWithSameScope.find((m) => m.state.status === "pending"); return !firstPendingMutation || firstPendingMutation === mutation; } else return true; } runNext(mutation) { const scope = scopeFor(mutation); if (typeof scope === "string") { var _classPrivateFieldGet2$1; const foundMutation = (_classPrivateFieldGet2$1 = _classPrivateFieldGet2(_scopes, this).get(scope)) === null || _classPrivateFieldGet2$1 === void 0 ? void 0 : _classPrivateFieldGet2$1.find((m) => m !== mutation && m.state.isPaused); return (foundMutation === null || foundMutation === void 0 ? void 0 : foundMutation.continue()) ?? Promise.resolve(); } else return Promise.resolve(); } clear() { notifyManager.batch(() => { _classPrivateFieldGet2(_mutations, this).forEach((mutation) => { this.notify({ type: "removed", mutation }); }); _classPrivateFieldGet2(_mutations, this).clear(); _classPrivateFieldGet2(_scopes, this).clear(); }); } getAll() { return Array.from(_classPrivateFieldGet2(_mutations, this)); } find(filters) { const defaultedFilters = { exact: true, ...filters }; return this.getAll().find((mutation) => matchMutation(defaultedFilters, mutation)); } findAll(filters = {}) { return this.getAll().filter((mutation) => matchMutation(filters, mutation)); } notify(event) { notifyManager.batch(() => { this.listeners.forEach((listener) => { listener(event); }); }); } resumePausedMutations() { const pausedMutations = this.getAll().filter((x) => x.state.isPaused); return notifyManager.batch(() => Promise.all(pausedMutations.map((mutation) => mutation.continue().catch(noop)))); } }; function scopeFor(mutation) { var _mutation$options$sco; return (_mutation$options$sco = mutation.options.scope) === null || _mutation$options$sco === void 0 ? void 0 : _mutation$options$sco.id; } //#endregion export { MutationCache }; //# sourceMappingURL=mutationCache.js.map