UNPKG

react-query

Version:

Hooks for managing, caching and syncing asynchronous and remote data in React

138 lines (103 loc) 4.33 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.MutationObserver = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose")); var _mutation = require("./mutation"); var _notifyManager = require("./notifyManager"); var _subscribable = require("./subscribable"); // CLASS var MutationObserver = /*#__PURE__*/function (_Subscribable) { (0, _inheritsLoose2.default)(MutationObserver, _Subscribable); function MutationObserver(client, options) { var _this; _this = _Subscribable.call(this) || this; _this.client = client; _this.setOptions(options); _this.bindMethods(); _this.updateResult(); return _this; } var _proto = MutationObserver.prototype; _proto.bindMethods = function bindMethods() { this.mutate = this.mutate.bind(this); this.reset = this.reset.bind(this); }; _proto.setOptions = function setOptions(options) { this.options = this.client.defaultMutationOptions(options); }; _proto.onUnsubscribe = function onUnsubscribe() { if (!this.listeners.length) { var _this$currentMutation; (_this$currentMutation = this.currentMutation) == null ? void 0 : _this$currentMutation.removeObserver(this); } }; _proto.onMutationUpdate = function onMutationUpdate(action) { this.updateResult(); // Determine which callbacks to trigger var notifyOptions = { listeners: true }; if (action.type === 'success') { notifyOptions.onSuccess = true; } else if (action.type === 'error') { notifyOptions.onError = true; } this.notify(notifyOptions); }; _proto.getCurrentResult = function getCurrentResult() { return this.currentResult; }; _proto.reset = function reset() { this.currentMutation = undefined; this.updateResult(); this.notify({ listeners: true }); }; _proto.mutate = function mutate(variables, options) { this.mutateOptions = options; if (this.currentMutation) { this.currentMutation.removeObserver(this); } this.currentMutation = this.client.getMutationCache().build(this.client, (0, _extends2.default)({}, this.options, { variables: typeof variables !== 'undefined' ? variables : this.options.variables })); this.currentMutation.addObserver(this); return this.currentMutation.execute(); }; _proto.updateResult = function updateResult() { var state = this.currentMutation ? this.currentMutation.state : (0, _mutation.getDefaultState)(); var result = (0, _extends2.default)({}, state, { isLoading: state.status === 'loading', isSuccess: state.status === 'success', isError: state.status === 'error', isIdle: state.status === 'idle', mutate: this.mutate, reset: this.reset }); this.currentResult = result; }; _proto.notify = function notify(options) { var _this2 = this; _notifyManager.notifyManager.batch(function () { // First trigger the mutate callbacks if (_this2.mutateOptions) { if (options.onSuccess) { _this2.mutateOptions.onSuccess == null ? void 0 : _this2.mutateOptions.onSuccess(_this2.currentResult.data, _this2.currentResult.variables, _this2.currentResult.context); _this2.mutateOptions.onSettled == null ? void 0 : _this2.mutateOptions.onSettled(_this2.currentResult.data, null, _this2.currentResult.variables, _this2.currentResult.context); } else if (options.onError) { _this2.mutateOptions.onError == null ? void 0 : _this2.mutateOptions.onError(_this2.currentResult.error, _this2.currentResult.variables, _this2.currentResult.context); _this2.mutateOptions.onSettled == null ? void 0 : _this2.mutateOptions.onSettled(undefined, _this2.currentResult.error, _this2.currentResult.variables, _this2.currentResult.context); } } // Then trigger the listeners if (options.listeners) { _this2.listeners.forEach(function (listener) { listener(_this2.currentResult); }); } }); }; return MutationObserver; }(_subscribable.Subscribable); exports.MutationObserver = MutationObserver;