UNPKG

react-query

Version:

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

368 lines (284 loc) 12.4 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.QueryClient = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _utils = require("./utils"); var _queryCache = require("./queryCache"); var _mutationCache = require("./mutationCache"); var _focusManager = require("./focusManager"); var _onlineManager = require("./onlineManager"); var _notifyManager = require("./notifyManager"); var _infiniteQueryBehavior = require("./infiniteQueryBehavior"); // CLASS var QueryClient = /*#__PURE__*/function () { function QueryClient(config) { if (config === void 0) { config = {}; } this.queryCache = config.queryCache || new _queryCache.QueryCache(); this.mutationCache = config.mutationCache || new _mutationCache.MutationCache(); this.defaultOptions = config.defaultOptions || {}; this.queryDefaults = []; this.mutationDefaults = []; } var _proto = QueryClient.prototype; _proto.mount = function mount() { var _this = this; this.unsubscribeFocus = _focusManager.focusManager.subscribe(function () { if (_focusManager.focusManager.isFocused() && _onlineManager.onlineManager.isOnline()) { _this.mutationCache.onFocus(); _this.queryCache.onFocus(); } }); this.unsubscribeOnline = _onlineManager.onlineManager.subscribe(function () { if (_focusManager.focusManager.isFocused() && _onlineManager.onlineManager.isOnline()) { _this.mutationCache.onOnline(); _this.queryCache.onOnline(); } }); }; _proto.unmount = function unmount() { var _this$unsubscribeFocu, _this$unsubscribeOnli; (_this$unsubscribeFocu = this.unsubscribeFocus) == null ? void 0 : _this$unsubscribeFocu.call(this); (_this$unsubscribeOnli = this.unsubscribeOnline) == null ? void 0 : _this$unsubscribeOnli.call(this); }; _proto.isFetching = function isFetching(arg1, arg2) { var _parseFilterArgs = (0, _utils.parseFilterArgs)(arg1, arg2), filters = _parseFilterArgs[0]; filters.fetching = true; return this.queryCache.findAll(filters).length; }; _proto.isMutating = function isMutating(filters) { return this.mutationCache.findAll((0, _extends2.default)({}, filters, { fetching: true })).length; }; _proto.getQueryData = function getQueryData(queryKey, filters) { var _this$queryCache$find; return (_this$queryCache$find = this.queryCache.find(queryKey, filters)) == null ? void 0 : _this$queryCache$find.state.data; }; _proto.getQueriesData = function getQueriesData(queryKeyOrFilters) { return this.getQueryCache().findAll(queryKeyOrFilters).map(function (_ref) { var queryKey = _ref.queryKey, state = _ref.state; var data = state.data; return [queryKey, data]; }); }; _proto.setQueryData = function setQueryData(queryKey, updater, options) { var parsedOptions = (0, _utils.parseQueryArgs)(queryKey); var defaultedOptions = this.defaultQueryOptions(parsedOptions); return this.queryCache.build(this, defaultedOptions).setData(updater, options); }; _proto.setQueriesData = function setQueriesData(queryKeyOrFilters, updater, options) { var _this2 = this; return _notifyManager.notifyManager.batch(function () { return _this2.getQueryCache().findAll(queryKeyOrFilters).map(function (_ref2) { var queryKey = _ref2.queryKey; return [queryKey, _this2.setQueryData(queryKey, updater, options)]; }); }); }; _proto.getQueryState = function getQueryState(queryKey, filters) { var _this$queryCache$find2; return (_this$queryCache$find2 = this.queryCache.find(queryKey, filters)) == null ? void 0 : _this$queryCache$find2.state; }; _proto.removeQueries = function removeQueries(arg1, arg2) { var _parseFilterArgs2 = (0, _utils.parseFilterArgs)(arg1, arg2), filters = _parseFilterArgs2[0]; var queryCache = this.queryCache; _notifyManager.notifyManager.batch(function () { queryCache.findAll(filters).forEach(function (query) { queryCache.remove(query); }); }); }; _proto.resetQueries = function resetQueries(arg1, arg2, arg3) { var _this3 = this; var _parseFilterArgs3 = (0, _utils.parseFilterArgs)(arg1, arg2, arg3), filters = _parseFilterArgs3[0], options = _parseFilterArgs3[1]; var queryCache = this.queryCache; var refetchFilters = (0, _extends2.default)({}, filters, { active: true }); return _notifyManager.notifyManager.batch(function () { queryCache.findAll(filters).forEach(function (query) { query.reset(); }); return _this3.refetchQueries(refetchFilters, options); }); }; _proto.cancelQueries = function cancelQueries(arg1, arg2, arg3) { var _this4 = this; var _parseFilterArgs4 = (0, _utils.parseFilterArgs)(arg1, arg2, arg3), filters = _parseFilterArgs4[0], _parseFilterArgs4$ = _parseFilterArgs4[1], cancelOptions = _parseFilterArgs4$ === void 0 ? {} : _parseFilterArgs4$; if (typeof cancelOptions.revert === 'undefined') { cancelOptions.revert = true; } var promises = _notifyManager.notifyManager.batch(function () { return _this4.queryCache.findAll(filters).map(function (query) { return query.cancel(cancelOptions); }); }); return Promise.all(promises).then(_utils.noop).catch(_utils.noop); }; _proto.invalidateQueries = function invalidateQueries(arg1, arg2, arg3) { var _ref3, _filters$refetchActiv, _filters$refetchInact, _this5 = this; var _parseFilterArgs5 = (0, _utils.parseFilterArgs)(arg1, arg2, arg3), filters = _parseFilterArgs5[0], options = _parseFilterArgs5[1]; var refetchFilters = (0, _extends2.default)({}, filters, { // if filters.refetchActive is not provided and filters.active is explicitly false, // e.g. invalidateQueries({ active: false }), we don't want to refetch active queries active: (_ref3 = (_filters$refetchActiv = filters.refetchActive) != null ? _filters$refetchActiv : filters.active) != null ? _ref3 : true, inactive: (_filters$refetchInact = filters.refetchInactive) != null ? _filters$refetchInact : false }); return _notifyManager.notifyManager.batch(function () { _this5.queryCache.findAll(filters).forEach(function (query) { query.invalidate(); }); return _this5.refetchQueries(refetchFilters, options); }); }; _proto.refetchQueries = function refetchQueries(arg1, arg2, arg3) { var _this6 = this; var _parseFilterArgs6 = (0, _utils.parseFilterArgs)(arg1, arg2, arg3), filters = _parseFilterArgs6[0], options = _parseFilterArgs6[1]; var promises = _notifyManager.notifyManager.batch(function () { return _this6.queryCache.findAll(filters).map(function (query) { return query.fetch(undefined, (0, _extends2.default)({}, options, { meta: { refetchPage: filters == null ? void 0 : filters.refetchPage } })); }); }); var promise = Promise.all(promises).then(_utils.noop); if (!(options == null ? void 0 : options.throwOnError)) { promise = promise.catch(_utils.noop); } return promise; }; _proto.fetchQuery = function fetchQuery(arg1, arg2, arg3) { var parsedOptions = (0, _utils.parseQueryArgs)(arg1, arg2, arg3); var defaultedOptions = this.defaultQueryOptions(parsedOptions); // https://github.com/tannerlinsley/react-query/issues/652 if (typeof defaultedOptions.retry === 'undefined') { defaultedOptions.retry = false; } var query = this.queryCache.build(this, defaultedOptions); return query.isStaleByTime(defaultedOptions.staleTime) ? query.fetch(defaultedOptions) : Promise.resolve(query.state.data); }; _proto.prefetchQuery = function prefetchQuery(arg1, arg2, arg3) { return this.fetchQuery(arg1, arg2, arg3).then(_utils.noop).catch(_utils.noop); }; _proto.fetchInfiniteQuery = function fetchInfiniteQuery(arg1, arg2, arg3) { var parsedOptions = (0, _utils.parseQueryArgs)(arg1, arg2, arg3); parsedOptions.behavior = (0, _infiniteQueryBehavior.infiniteQueryBehavior)(); return this.fetchQuery(parsedOptions); }; _proto.prefetchInfiniteQuery = function prefetchInfiniteQuery(arg1, arg2, arg3) { return this.fetchInfiniteQuery(arg1, arg2, arg3).then(_utils.noop).catch(_utils.noop); }; _proto.cancelMutations = function cancelMutations() { var _this7 = this; var promises = _notifyManager.notifyManager.batch(function () { return _this7.mutationCache.getAll().map(function (mutation) { return mutation.cancel(); }); }); return Promise.all(promises).then(_utils.noop).catch(_utils.noop); }; _proto.resumePausedMutations = function resumePausedMutations() { return this.getMutationCache().resumePausedMutations(); }; _proto.executeMutation = function executeMutation(options) { return this.mutationCache.build(this, options).execute(); }; _proto.getQueryCache = function getQueryCache() { return this.queryCache; }; _proto.getMutationCache = function getMutationCache() { return this.mutationCache; }; _proto.getDefaultOptions = function getDefaultOptions() { return this.defaultOptions; }; _proto.setDefaultOptions = function setDefaultOptions(options) { this.defaultOptions = options; }; _proto.setQueryDefaults = function setQueryDefaults(queryKey, options) { var result = this.queryDefaults.find(function (x) { return (0, _utils.hashQueryKey)(queryKey) === (0, _utils.hashQueryKey)(x.queryKey); }); if (result) { result.defaultOptions = options; } else { this.queryDefaults.push({ queryKey: queryKey, defaultOptions: options }); } }; _proto.getQueryDefaults = function getQueryDefaults(queryKey) { var _this$queryDefaults$f; return queryKey ? (_this$queryDefaults$f = this.queryDefaults.find(function (x) { return (0, _utils.partialMatchKey)(queryKey, x.queryKey); })) == null ? void 0 : _this$queryDefaults$f.defaultOptions : undefined; }; _proto.setMutationDefaults = function setMutationDefaults(mutationKey, options) { var result = this.mutationDefaults.find(function (x) { return (0, _utils.hashQueryKey)(mutationKey) === (0, _utils.hashQueryKey)(x.mutationKey); }); if (result) { result.defaultOptions = options; } else { this.mutationDefaults.push({ mutationKey: mutationKey, defaultOptions: options }); } }; _proto.getMutationDefaults = function getMutationDefaults(mutationKey) { var _this$mutationDefault; return mutationKey ? (_this$mutationDefault = this.mutationDefaults.find(function (x) { return (0, _utils.partialMatchKey)(mutationKey, x.mutationKey); })) == null ? void 0 : _this$mutationDefault.defaultOptions : undefined; }; _proto.defaultQueryOptions = function defaultQueryOptions(options) { if (options == null ? void 0 : options._defaulted) { return options; } var defaultedOptions = (0, _extends2.default)({}, this.defaultOptions.queries, this.getQueryDefaults(options == null ? void 0 : options.queryKey), options, { _defaulted: true }); if (!defaultedOptions.queryHash && defaultedOptions.queryKey) { defaultedOptions.queryHash = (0, _utils.hashQueryKeyByOptions)(defaultedOptions.queryKey, defaultedOptions); } return defaultedOptions; }; _proto.defaultQueryObserverOptions = function defaultQueryObserverOptions(options) { return this.defaultQueryOptions(options); }; _proto.defaultMutationOptions = function defaultMutationOptions(options) { if (options == null ? void 0 : options._defaulted) { return options; } return (0, _extends2.default)({}, this.defaultOptions.mutations, this.getMutationDefaults(options == null ? void 0 : options.mutationKey), options, { _defaulted: true }); }; _proto.clear = function clear() { this.queryCache.clear(); this.mutationCache.clear(); }; return QueryClient; }(); exports.QueryClient = QueryClient;