UNPKG

@tanstack/query-core

Version:

The framework agnostic core that powers TanStack Query

133 lines (132 loc) 4.81 kB
import { __privateAdd, __privateGet, __privateMethod, __privateSet } from "./chunk-WPSKCR32.js"; // src/mutationObserver.ts import { getDefaultState } from "./mutation.js"; import { notifyManager } from "./notifyManager.js"; import { Subscribable } from "./subscribable.js"; import { shallowEqualObjects } from "./utils.js"; var _client, _currentResult, _currentMutation, _mutateOptions, _updateResult, updateResult_fn, _notify, notify_fn; var MutationObserver = class extends Subscribable { constructor(client, options) { super(); __privateAdd(this, _updateResult); __privateAdd(this, _notify); __privateAdd(this, _client, void 0); __privateAdd(this, _currentResult, void 0); __privateAdd(this, _currentMutation, void 0); __privateAdd(this, _mutateOptions, void 0); __privateSet(this, _client, client); this.setOptions(options); this.bindMethods(); __privateMethod(this, _updateResult, 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 (!shallowEqualObjects(prevOptions, this.options)) { __privateGet(this, _client).getMutationCache().notify({ type: "observerOptionsUpdated", mutation: __privateGet(this, _currentMutation), observer: this }); } (_a = __privateGet(this, _currentMutation)) == null ? void 0 : _a.setOptions(this.options); } onUnsubscribe() { var _a; if (!this.hasListeners()) { (_a = __privateGet(this, _currentMutation)) == null ? void 0 : _a.removeObserver(this); } } onMutationUpdate(action) { __privateMethod(this, _updateResult, updateResult_fn).call(this); __privateMethod(this, _notify, notify_fn).call(this, action); } getCurrentResult() { return __privateGet(this, _currentResult); } reset() { __privateSet(this, _currentMutation, void 0); __privateMethod(this, _updateResult, updateResult_fn).call(this); __privateMethod(this, _notify, 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(); _updateResult = new WeakSet(); updateResult_fn = function() { var _a; const state = ((_a = __privateGet(this, _currentMutation)) == null ? void 0 : _a.state) ?? 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 = new WeakSet(); notify_fn = function(action) { notifyManager.batch(() => { var _a, _b, _c, _d, _e, _f, _g, _h; if (__privateGet(this, _mutateOptions) && this.hasListeners()) { if ((action == null ? void 0 : action.type) === "success") { (_b = (_a = __privateGet(this, _mutateOptions)).onSuccess) == null ? void 0 : _b.call( _a, action.data, __privateGet(this, _currentResult).variables, __privateGet(this, _currentResult).context ); (_d = (_c = __privateGet(this, _mutateOptions)).onSettled) == null ? void 0 : _d.call( _c, action.data, null, __privateGet(this, _currentResult).variables, __privateGet(this, _currentResult).context ); } else if ((action == null ? void 0 : action.type) === "error") { (_f = (_e = __privateGet(this, _mutateOptions)).onError) == null ? void 0 : _f.call( _e, action.error, __privateGet(this, _currentResult).variables, __privateGet(this, _currentResult).context ); (_h = (_g = __privateGet(this, _mutateOptions)).onSettled) == null ? void 0 : _h.call( _g, void 0, action.error, __privateGet(this, _currentResult).variables, __privateGet(this, _currentResult).context ); } } this.listeners.forEach((listener) => { listener(__privateGet(this, _currentResult)); }); }); }; export { MutationObserver }; //# sourceMappingURL=mutationObserver.js.map