UNPKG

@tanstack/query-core

Version:

The framework agnostic core that powers TanStack Query

115 lines (114 loc) 3.42 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const require_utils = require("./utils.cjs"); const require_subscribable = require("./subscribable.cjs"); const require_notifyManager = require("./notifyManager.cjs"); const require_mutation = require("./mutation.cjs"); //#region src/mutationCache.ts var MutationCache = class extends require_subscribable.Subscribable { #mutations; #scopes; #mutationId; constructor(config = {}) { super(); this.config = config; this.#mutations = /* @__PURE__ */ new Set(); this.#scopes = /* @__PURE__ */ new Map(); this.#mutationId = 0; } build(client, options, state) { const mutation = new require_mutation.Mutation({ client, mutationCache: this, mutationId: ++this.#mutationId, options: client.defaultMutationOptions(options), state }); this.add(mutation); return mutation; } add(mutation) { this.#mutations.add(mutation); const scope = scopeFor(mutation); if (typeof scope === "string") { const scopedMutations = this.#scopes.get(scope); if (scopedMutations) scopedMutations.push(mutation); else this.#scopes.set(scope, [mutation]); } this.notify({ type: "added", mutation }); } remove(mutation) { if (this.#mutations.delete(mutation)) { const scope = scopeFor(mutation); if (typeof scope === "string") { const scopedMutations = this.#scopes.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) this.#scopes.delete(scope); } } } this.notify({ type: "removed", mutation }); } canRun(mutation) { const scope = scopeFor(mutation); if (typeof scope === "string") { const firstPendingMutation = this.#scopes.get(scope)?.find((m) => m.state.status === "pending"); return !firstPendingMutation || firstPendingMutation === mutation; } else return true; } runNext(mutation) { const scope = scopeFor(mutation); if (typeof scope === "string") return (this.#scopes.get(scope)?.find((m) => m !== mutation && m.state.isPaused))?.continue() ?? Promise.resolve(); else return Promise.resolve(); } clear() { require_notifyManager.notifyManager.batch(() => { this.#mutations.forEach((mutation) => { this.notify({ type: "removed", mutation }); }); this.#mutations.clear(); this.#scopes.clear(); }); } getAll() { return Array.from(this.#mutations); } find(filters) { const defaultedFilters = { exact: true, ...filters }; return this.getAll().find((mutation) => require_utils.matchMutation(defaultedFilters, mutation)); } findAll(filters = {}) { return this.getAll().filter((mutation) => require_utils.matchMutation(filters, mutation)); } notify(event) { require_notifyManager.notifyManager.batch(() => { this.listeners.forEach((listener) => { listener(event); }); }); } resumePausedMutations() { const pausedMutations = this.getAll().filter((x) => x.state.isPaused); return require_notifyManager.notifyManager.batch(() => Promise.all(pausedMutations.map((mutation) => mutation.continue().catch(require_utils.noop)))); } }; function scopeFor(mutation) { return mutation.options.scope?.id; } //#endregion exports.MutationCache = MutationCache; //# sourceMappingURL=mutationCache.cjs.map