UNPKG

react-query

Version:

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

161 lines (124 loc) 3.91 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.QueryCache = void 0; var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose")); var _utils = require("./utils"); var _query = require("./query"); var _notifyManager = require("./notifyManager"); var _subscribable = require("./subscribable"); // CLASS var QueryCache = /*#__PURE__*/function (_Subscribable) { (0, _inheritsLoose2.default)(QueryCache, _Subscribable); function QueryCache(config) { var _this; _this = _Subscribable.call(this) || this; _this.config = config || {}; _this.queries = []; _this.queriesMap = {}; return _this; } var _proto = QueryCache.prototype; _proto.build = function build(client, options, state) { var _options$queryHash; var queryKey = options.queryKey; var queryHash = (_options$queryHash = options.queryHash) != null ? _options$queryHash : (0, _utils.hashQueryKeyByOptions)(queryKey, options); var query = this.get(queryHash); if (!query) { query = new _query.Query({ cache: this, queryKey: queryKey, queryHash: queryHash, options: client.defaultQueryOptions(options), state: state, defaultOptions: client.getQueryDefaults(queryKey), meta: options.meta }); this.add(query); } return query; }; _proto.add = function add(query) { if (!this.queriesMap[query.queryHash]) { this.queriesMap[query.queryHash] = query; this.queries.push(query); this.notify({ type: 'queryAdded', query: query }); } }; _proto.remove = function remove(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 }); } }; _proto.clear = function clear() { var _this2 = this; _notifyManager.notifyManager.batch(function () { _this2.queries.forEach(function (query) { _this2.remove(query); }); }); }; _proto.get = function get(queryHash) { return this.queriesMap[queryHash]; }; _proto.getAll = function getAll() { return this.queries; }; _proto.find = function find(arg1, arg2) { var _parseFilterArgs = (0, _utils.parseFilterArgs)(arg1, arg2), filters = _parseFilterArgs[0]; if (typeof filters.exact === 'undefined') { filters.exact = true; } return this.queries.find(function (query) { return (0, _utils.matchQuery)(filters, query); }); }; _proto.findAll = function findAll(arg1, arg2) { var _parseFilterArgs2 = (0, _utils.parseFilterArgs)(arg1, arg2), filters = _parseFilterArgs2[0]; return Object.keys(filters).length > 0 ? this.queries.filter(function (query) { return (0, _utils.matchQuery)(filters, query); }) : this.queries; }; _proto.notify = function notify(event) { var _this3 = this; _notifyManager.notifyManager.batch(function () { _this3.listeners.forEach(function (listener) { listener(event); }); }); }; _proto.onFocus = function onFocus() { var _this4 = this; _notifyManager.notifyManager.batch(function () { _this4.queries.forEach(function (query) { query.onFocus(); }); }); }; _proto.onOnline = function onOnline() { var _this5 = this; _notifyManager.notifyManager.batch(function () { _this5.queries.forEach(function (query) { query.onOnline(); }); }); }; return QueryCache; }(_subscribable.Subscribable); exports.QueryCache = QueryCache;