{"ast":null,"code":"// src/mutationCache.ts\nimport { notifyManager } from \"./notifyManager.js\";\nimport { Mutation } from \"./mutation.js\";\nimport { matchMutation, noop } from \"./utils.js\";\nimport { Subscribable } from \"./subscribable.js\";\nvar MutationCache = class extends Subscribable {\n constructor(config = {}) {\n super();\n this.config = config;\n this.#mutations = /* @__PURE__ */new Map();\n this.#mutationId = Date.now();\n }\n #mutations;\n #mutationId;\n build(client, options, state) {\n const mutation = new Mutation({\n mutationCache: this,\n mutationId: ++this.#mutationId,\n options: client.defaultMutationOptions(options),\n state\n });\n this.add(mutation);\n return mutation;\n }\n add(mutation) {\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({\n type: \"added\",\n mutation\n });\n }\n remove(mutation) {\n const scope = scopeFor(mutation);\n if (this.#mutations.has(scope)) {\n const mutations = this.#mutations.get(scope)?.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 this.notify({\n type: \"removed\",\n mutation\n });\n }\n canRun(mutation) {\n const firstPendingMutation = this.#mutations.get(scopeFor(mutation))?.find(m => m.state.status === \"pending\");\n return !firstPendingMutation || firstPendingMutation === mutation;\n }\n runNext(mutation) {\n const foundMutation = this.#mutations.get(scopeFor(mutation))?.find(m => m !== mutation && m.state.isPaused);\n return foundMutation?.continue() ?? Promise.resolve();\n }\n clear() {\n notifyManager.batch(() => {\n this.getAll().forEach(mutation => {\n this.remove(mutation);\n });\n });\n }\n getAll() {\n return [...this.#mutations.values()].flat();\n }\n find(filters) {\n const defaultedFilters = {\n exact: true,\n ...filters\n };\n return this.getAll().find(mutation => matchMutation(defaultedFilters, mutation));\n }\n findAll(filters = {}) {\n return this.getAll().filter(mutation => matchMutation(filters, mutation));\n }\n notify(event) {\n notifyManager.batch(() => {\n this.listeners.forEach(listener => {\n listener(event);\n });\n });\n }\n resumePausedMutations() {\n const pausedMutations = this.getAll().filter(x => x.state.isPaused);\n return notifyManager.batch(() => Promise.all(pausedMutations.map(mutation => mutation.continue().catch(noop))));\n }\n};\nfunction scopeFor(mutation) {\n return mutation.options.scope?.id ?? String(mutation.mutationId);\n}\nexport { MutationCache };","map":{"version":3,"names":["notifyManager","Mutation","matchMutation","noop","Subscribable","MutationCache","constructor","config","mutations","Map","mutationId","Date","now","build","client","options","state","mutation","mutationCache","defaultMutationOptions","add","scope","scopeFor","get","push","set","notify","type","remove","has","filter","x","length","delete","canRun","firstPendingMutation","find","m","status","runNext","foundMutation","isPaused","continue","Promise","resolve","clear","batch","getAll","forEach","values","flat","filters","defaultedFilters","exact","findAll","event","listeners","listener","resumePausedMutations","pausedMutations","all","map","catch","id","String"],"sources":["/Users/shoofle/Projects/the-forest/node_modules/@tanstack/query-core/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,\n ) => Promise | unknown\n onSuccess?: (\n data: unknown,\n variables: unknown,\n context: unknown,\n mutation: Mutation,\n ) => Promise | unknown\n onMutate?: (\n variables: unknown,\n mutation: Mutation,\n ) => Promise | unknown\n onSettled?: (\n data: unknown | undefined,\n error: DefaultError | null,\n variables: unknown,\n context: unknown,\n mutation: Mutation,\n ) => Promise | unknown\n}\n\ninterface NotifyEventMutationAdded extends NotifyEvent {\n type: 'added'\n mutation: Mutation\n}\ninterface NotifyEventMutationRemoved extends NotifyEvent {\n type: 'removed'\n mutation: Mutation\n}\n\ninterface NotifyEventMutationObserverAdded extends NotifyEvent {\n type: 'observerAdded'\n mutation: Mutation\n observer: MutationObserver\n}\n\ninterface NotifyEventMutationObserverRemoved extends NotifyEvent {\n type: 'observerRemoved'\n mutation: Mutation\n observer: MutationObserver\n}\n\ninterface NotifyEventMutationObserverOptionsUpdated extends NotifyEvent {\n type: 'observerOptionsUpdated'\n mutation?: Mutation\n observer: MutationObserver\n}\n\ninterface NotifyEventMutationUpdated extends NotifyEvent {\n type: 'updated'\n mutation: Mutation\n action: Action\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 {\n #mutations: Map>>\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(\n client: QueryClient,\n options: MutationOptions,\n state?: MutationState,\n ): Mutation {\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): 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): 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): 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): Promise {\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 {\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 | undefined {\n const defaultedFilters = { exact: true, ...filters }\n\n return this.getAll().find((mutation) =>\n matchMutation(defaultedFilters, mutation),\n ) as Mutation | undefined\n }\n\n findAll(filters: MutationFilters = {}): Array {\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 {\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) {\n return mutation.options.scope?.id ?? String(mutation.mutationId)\n}\n"],"mappings":";AAAA,SAASA,aAAA,QAAqB;AAC9B,SAASC,QAAA,QAAgB;AACzB,SAASC,aAAA,EAAeC,IAAA,QAAY;AACpC,SAASC,YAAA,QAAoB;AAgFtB,IAAMC,aAAA,GAAN,cAA4BD,YAAA,CAAoC;EAIrEE,YAAmBC,MAAA,GAA8B,CAAC,GAAG;IACnD,MAAM;IADW,KAAAA,MAAA,GAAAA,MAAA;IAEjB,KAAK,CAAAC,SAAA,GAAa,mBAAIC,GAAA,CAAI;IAC1B,KAAK,CAAAC,UAAA,GAAcC,IAAA,CAAKC,G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