UNPKG

vue-use-query

Version:

vue use query

123 lines (122 loc) 4.63 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 { hashQueryKeyByOptions, matchQuery, parseFilterArgs, } from './utils'; import { Query } from './query'; import { notifyManager } from './notifyManager'; import { Subscribable } from './subscribable'; // CLASS var QueryCache = /** @class */ (function (_super) { __extends(QueryCache, _super); function QueryCache(config) { var _this = _super.call(this) || this; _this.config = config || {}; _this.queries = []; _this.queriesMap = {}; return _this; } QueryCache.prototype.build = function (client, options, state) { var _a; var queryKey = options.queryKey; var queryHash = (_a = options.queryHash) !== null && _a !== void 0 ? _a : hashQueryKeyByOptions(queryKey, options); var query = this.get(queryHash); if (!query) { query = new Query({ cache: this, queryKey: queryKey, queryHash: queryHash, options: client.defaultQueryOptions(options), state: state, defaultOptions: client.getQueryDefaults(queryKey), }); this.add(query); } return query; }; QueryCache.prototype.add = function (query) { if (!this.queriesMap[query.queryHash]) { this.queriesMap[query.queryHash] = query; this.queries.push(query); this.notify({ type: 'queryAdded', query: query, }); } }; QueryCache.prototype.remove = function (query) { var queryInMap = this.queriesMap[query.queryHash]; if (queryInMap) { query.destroy(); this.queries = this.queries.filter(function (x) { return x !== query; }); if (queryInMap === query) { delete this.queriesMap[query.queryHash]; } this.notify({ type: 'queryRemoved', query: query }); } }; QueryCache.prototype.clear = function () { var _this = this; notifyManager.batch(function () { _this.queries.forEach(function (query) { _this.remove(query); }); }); }; QueryCache.prototype.get = function (queryHash) { return this.queriesMap[queryHash]; }; QueryCache.prototype.getAll = function () { return this.queries; }; QueryCache.prototype.find = function (arg1, arg2) { var filters = parseFilterArgs(arg1, arg2)[0]; if (typeof filters.exact === 'undefined') { filters.exact = true; } return this.queries.find(function (query) { return matchQuery(filters, query); }); }; QueryCache.prototype.findAll = function (arg1, arg2) { var filters = parseFilterArgs(arg1, arg2)[0]; return filters ? this.queries.filter(function (query) { return matchQuery(filters, query); }) : this.queries; }; QueryCache.prototype.notify = function (event) { var _this = this; notifyManager.batch(function () { _this.listeners.forEach(function (listener) { listener(event); }); }); }; QueryCache.prototype.onFocus = function () { var _this = this; notifyManager.batch(function () { _this.queries.forEach(function (query) { query.onFocus(); }); }); }; QueryCache.prototype.onOnline = function () { var _this = this; notifyManager.batch(function () { _this.queries.forEach(function (query) { query.onOnline(); }); }); }; return QueryCache; }(Subscribable)); export { QueryCache };