"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/mutationCache.ts var mutationCache_exports = {}; __export(mutationCache_exports, { MutationCache: () => MutationCache }); module.exports = __toCommonJS(mutationCache_exports); var import_notifyManager = require("./notifyManager.cjs"); var import_mutation = require("./mutation.cjs"); var import_utils = require("./utils.cjs"); var import_subscribable = require("./subscribable.cjs"); var MutationCache = class extends import_subscribable.Subscribable { constructor(config = {}) { super(); this.config = config; this.#mutations = /* @__PURE__ */ new Map(); this.#mutationId = Date.now(); } #mutations; #mutationId; build(client, options, state) { const mutation = new import_mutation.Mutation({ mutationCache: this, mutationId: ++this.#mutationId, options: client.defaultMutationOptions(options), state }); this.add(mutation); return mutation; } add(mutation) { const scope = scopeFor(mutation); const mutations = this.#mutations.get(scope) ?? []; mutations.push(mutation); this.#mutations.set(scope, mutations); this.notify({ type: "added", mutation }); } remove(mutation) { const scope = scopeFor(mutation); if (this.#mutations.has(scope)) { const mutations = this.#mutations.get(scope)?.filter((x) => x !== mutation); if (mutations) { if (mutations.length === 0) { this.#mutations.delete(scope); } else { this.#mutations.set(scope, mutations); } } } this.notify({ type: "removed", mutation }); } canRun(mutation) { const firstPendingMutation = this.#mutations.get(scopeFor(mutation))?.find((m) => m.state.status === "pending"); return !firstPendingMutation || firstPendingMutation === mutation; } runNext(mutation) { const foundMutation = this.#mutations.get(scopeFor(mutation))?.find((m) => m !== mutation && m.state.isPaused); return foundMutation?.continue() ?? Promise.resolve(); } clear() { import_notifyManager.notifyManager.batch(() => { this.getAll().forEach((mutation) => { this.remove(mutation); }); }); } getAll() { return [...this.#mutations.values()].flat(); } find(filters) { const defaultedFilters = { exact: true, ...filters }; return this.getAll().find( (mutation) => (0, import_utils.matchMutation)(defaultedFilters, mutation) ); } findAll(filters = {}) { return this.getAll().filter((mutation) => (0, import_utils.matchMutation)(filters, mutation)); } notify(event) { import_notifyManager.notifyManager.batch(() => { this.listeners.forEach((listener) => { listener(event); }); }); } resumePausedMutations() { const pausedMutations = this.getAll().filter((x) => x.state.isPaused); return import_notifyManager.notifyManager.batch( () => Promise.all( pausedMutations.map((mutation) => mutation.continue().catch(import_utils.noop)) ) ); } }; function scopeFor(mutation) { return mutation.options.scope?.id ?? String(mutation.mutationId); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { MutationCache }); //# sourceMappingURL=mutationCache.cjs.map