UNPKG

vue-use-query

Version:

vue use query

117 lines (116 loc) 5.51 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import { getDefaultState } from './mutation'; import { notifyManager } from './notifyManager'; import { Subscribable } from './subscribable'; // CLASS var MutationObserver = /** @class */ (function (_super) { __extends(MutationObserver, _super); function MutationObserver(client, options) { var _this = _super.call(this) || this; _this.client = client; _this.setOptions(options); _this.bindMethods(); _this.updateResult(); return _this; } MutationObserver.prototype.bindMethods = function () { this.mutate = this.mutate.bind(this); this.reset = this.reset.bind(this); }; MutationObserver.prototype.setOptions = function (options) { this.options = this.client.defaultMutationOptions(options); }; MutationObserver.prototype.onUnsubscribe = function () { var _a; if (!this.listeners.length) { (_a = this.currentMutation) === null || _a === void 0 ? void 0 : _a.removeObserver(this); } }; MutationObserver.prototype.onMutationUpdate = function (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); }; MutationObserver.prototype.getCurrentResult = function () { return this.currentResult; }; MutationObserver.prototype.reset = function () { this.currentMutation = undefined; this.updateResult(); this.notify({ listeners: true }); }; MutationObserver.prototype.mutate = function (variables, options) { this.mutateOptions = options; if (this.currentMutation) { this.currentMutation.removeObserver(this); } this.currentMutation = this.client.getMutationCache().build(this.client, __assign(__assign({}, this.options), { variables: typeof variables !== 'undefined' ? variables : this.options.variables })); this.currentMutation.addObserver(this); return this.currentMutation.execute(); }; MutationObserver.prototype.updateResult = function () { var state = this.currentMutation ? this.currentMutation.state : getDefaultState(); this.currentResult = __assign(__assign({}, state), { isLoading: state.status === 'loading', isSuccess: state.status === 'success', isError: state.status === 'error', isIdle: state.status === 'idle', mutate: this.mutate, reset: this.reset }); }; MutationObserver.prototype.notify = function (options) { var _this = this; notifyManager.batch(function () { var _a, _b, _c, _d, _e, _f, _g, _h; // First trigger the mutate callbacks if (_this.mutateOptions) { if (options.onSuccess) { (_b = (_a = _this.mutateOptions).onSuccess) === null || _b === void 0 ? void 0 : _b.call(_a, _this.currentResult.data, _this.currentResult.variables, _this.currentResult.context); (_d = (_c = _this.mutateOptions).onSettled) === null || _d === void 0 ? void 0 : _d.call(_c, _this.currentResult.data, null, _this.currentResult.variables, _this.currentResult.context); } else if (options.onError) { (_f = (_e = _this.mutateOptions).onError) === null || _f === void 0 ? void 0 : _f.call(_e, _this.currentResult.error, _this.currentResult.variables, _this.currentResult.context); (_h = (_g = _this.mutateOptions).onSettled) === null || _h === void 0 ? void 0 : _h.call(_g, undefined, _this.currentResult.error, _this.currentResult.variables, _this.currentResult.context); } } // Then trigger the listeners if (options.listeners) { _this.listeners.forEach(function (listener) { listener(_this.currentResult); }); } }); }; return MutationObserver; }(Subscribable)); export { MutationObserver };