UNPKG

vue-use-query

Version:

vue use query

97 lines (96 loc) 3.91 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 __()); }; })(); import { notifyManager } from './notifyManager'; import { Mutation } from './mutation'; import { matchMutation, noop } from './utils'; import { Subscribable } from './subscribable'; // CLASS var MutationCache = /** @class */ (function (_super) { __extends(MutationCache, _super); function MutationCache(config) { var _this = _super.call(this) || this; _this.config = config || {}; _this.mutations = []; _this.mutationId = 0; return _this; } MutationCache.prototype.build = function (client, options, state) { var mutation = new Mutation({ mutationCache: this, mutationId: ++this.mutationId, options: client.defaultMutationOptions(options), state: state, defaultOptions: options.mutationKey ? client.getMutationDefaults(options.mutationKey) : undefined, }); this.add(mutation); return mutation; }; MutationCache.prototype.add = function (mutation) { this.mutations.push(mutation); this.notify(mutation); }; MutationCache.prototype.remove = function (mutation) { this.mutations = this.mutations.filter(function (x) { return x !== mutation; }); mutation.cancel(); this.notify(mutation); }; MutationCache.prototype.clear = function () { var _this = this; notifyManager.batch(function () { _this.mutations.forEach(function (mutation) { _this.remove(mutation); }); }); }; MutationCache.prototype.getAll = function () { return this.mutations; }; MutationCache.prototype.find = function (filters) { if (typeof filters.exact === 'undefined') { filters.exact = true; } return this.mutations.find(function (mutation) { return matchMutation(filters, mutation); }); }; MutationCache.prototype.findAll = function (filters) { return this.mutations.filter(function (mutation) { return matchMutation(filters, mutation); }); }; MutationCache.prototype.notify = function (mutation) { var _this = this; notifyManager.batch(function () { _this.listeners.forEach(function (listener) { listener(mutation); }); }); }; MutationCache.prototype.onFocus = function () { this.resumePausedMutations(); }; MutationCache.prototype.onOnline = function () { this.resumePausedMutations(); }; MutationCache.prototype.resumePausedMutations = function () { var pausedMutations = this.mutations.filter(function (x) { return x.state.isPaused; }); return notifyManager.batch(function () { return pausedMutations.reduce(function (promise, mutation) { return promise.then(function () { return mutation.continue().catch(noop); }); }, Promise.resolve()); }); }; return MutationCache; }(Subscribable)); export { MutationCache };