UNPKG

@tanstack/query-core

Version:

The framework agnostic core that powers TanStack Query

149 lines (148 loc) 6.76 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __typeError = (msg) => { throw TypeError(msg); }; 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); var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg); var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj)); var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value); var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value); var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method); // src/mutationObserver.ts var mutationObserver_exports = {}; __export(mutationObserver_exports, { MutationObserver: () => MutationObserver }); module.exports = __toCommonJS(mutationObserver_exports); var import_mutation = require("./mutation.cjs"); var import_notifyManager = require("./notifyManager.cjs"); var import_subscribable = require("./subscribable.cjs"); var import_utils = require("./utils.cjs"); var _client, _currentResult, _currentMutation, _mutateOptions, _MutationObserver_instances, updateResult_fn, notify_fn; var MutationObserver = class extends import_subscribable.Subscribable { constructor(client, options) { super(); __privateAdd(this, _MutationObserver_instances); __privateAdd(this, _client); __privateAdd(this, _currentResult); __privateAdd(this, _currentMutation); __privateAdd(this, _mutateOptions); __privateSet(this, _client, client); this.setOptions(options); this.bindMethods(); __privateMethod(this, _MutationObserver_instances, updateResult_fn).call(this); } bindMethods() { this.mutate = this.mutate.bind(this); this.reset = this.reset.bind(this); } setOptions(options) { var _a; const prevOptions = this.options; this.options = __privateGet(this, _client).defaultMutationOptions(options); if (!(0, import_utils.shallowEqualObjects)(this.options, prevOptions)) { __privateGet(this, _client).getMutationCache().notify({ type: "observerOptionsUpdated", mutation: __privateGet(this, _currentMutation), observer: this }); } if ((prevOptions == null ? void 0 : prevOptions.mutationKey) && this.options.mutationKey && (0, import_utils.hashKey)(prevOptions.mutationKey) !== (0, import_utils.hashKey)(this.options.mutationKey)) { this.reset(); } else if (((_a = __privateGet(this, _currentMutation)) == null ? void 0 : _a.state.status) === "pending") { __privateGet(this, _currentMutation).setOptions(this.options); } } onUnsubscribe() { var _a; if (!this.hasListeners()) { (_a = __privateGet(this, _currentMutation)) == null ? void 0 : _a.removeObserver(this); } } onMutationUpdate(action) { __privateMethod(this, _MutationObserver_instances, updateResult_fn).call(this); __privateMethod(this, _MutationObserver_instances, notify_fn).call(this, action); } getCurrentResult() { return __privateGet(this, _currentResult); } reset() { var _a; (_a = __privateGet(this, _currentMutation)) == null ? void 0 : _a.removeObserver(this); __privateSet(this, _currentMutation, void 0); __privateMethod(this, _MutationObserver_instances, updateResult_fn).call(this); __privateMethod(this, _MutationObserver_instances, notify_fn).call(this); } mutate(variables, options) { var _a; __privateSet(this, _mutateOptions, options); (_a = __privateGet(this, _currentMutation)) == null ? void 0 : _a.removeObserver(this); __privateSet(this, _currentMutation, __privateGet(this, _client).getMutationCache().build(__privateGet(this, _client), this.options)); __privateGet(this, _currentMutation).addObserver(this); return __privateGet(this, _currentMutation).execute(variables); } }; _client = new WeakMap(); _currentResult = new WeakMap(); _currentMutation = new WeakMap(); _mutateOptions = new WeakMap(); _MutationObserver_instances = new WeakSet(); updateResult_fn = function() { var _a; const state = ((_a = __privateGet(this, _currentMutation)) == null ? void 0 : _a.state) ?? (0, import_mutation.getDefaultState)(); __privateSet(this, _currentResult, { ...state, isPending: state.status === "pending", isSuccess: state.status === "success", isError: state.status === "error", isIdle: state.status === "idle", mutate: this.mutate, reset: this.reset }); }; notify_fn = function(action) { import_notifyManager.notifyManager.batch(() => { var _a, _b, _c, _d, _e, _f, _g, _h; if (__privateGet(this, _mutateOptions) && this.hasListeners()) { const variables = __privateGet(this, _currentResult).variables; const context = __privateGet(this, _currentResult).context; if ((action == null ? void 0 : action.type) === "success") { (_b = (_a = __privateGet(this, _mutateOptions)).onSuccess) == null ? void 0 : _b.call(_a, action.data, variables, context); (_d = (_c = __privateGet(this, _mutateOptions)).onSettled) == null ? void 0 : _d.call(_c, action.data, null, variables, context); } else if ((action == null ? void 0 : action.type) === "error") { (_f = (_e = __privateGet(this, _mutateOptions)).onError) == null ? void 0 : _f.call(_e, action.error, variables, context); (_h = (_g = __privateGet(this, _mutateOptions)).onSettled) == null ? void 0 : _h.call( _g, void 0, action.error, variables, context ); } } this.listeners.forEach((listener) => { listener(__privateGet(this, _currentResult)); }); }); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { MutationObserver }); //# sourceMappingURL=mutationObserver.cjs.map