UNPKG

vue-use-query

Version:

vue use query

257 lines (256 loc) 12.1 kB
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 { hashQueryKey, noop, parseFilterArgs, parseQueryArgs, partialMatchKey, hashQueryKeyByOptions, } from './utils'; import { QueryCache } from './queryCache'; import { MutationCache } from './mutationCache'; import { focusManager } from './focusManager'; import { onlineManager } from './onlineManager'; import { notifyManager } from './notifyManager'; import { infiniteQueryBehavior } from './infiniteQueryBehavior'; // CLASS var QueryClient = /** @class */ (function () { function QueryClient(config) { if (config === void 0) { config = {}; } this.queryCache = config.queryCache || new QueryCache(); this.mutationCache = config.mutationCache || new MutationCache(); this.defaultOptions = config.defaultOptions || {}; this.queryDefaults = []; this.mutationDefaults = []; } // 挂载 QueryClient.prototype.mount = function () { var _this = this; this.unsubscribeFocus = focusManager.subscribe(function () { if (focusManager.isFocused() && onlineManager.isOnline()) { _this.mutationCache.onFocus(); _this.queryCache.onFocus(); } }); this.unsubscribeOnline = onlineManager.subscribe(function () { if (focusManager.isFocused() && onlineManager.isOnline()) { _this.mutationCache.onOnline(); _this.queryCache.onOnline(); } }); }; // 卸载 QueryClient.prototype.unmount = function () { var _a, _b; (_a = this.unsubscribeFocus) === null || _a === void 0 ? void 0 : _a.call(this); (_b = this.unsubscribeOnline) === null || _b === void 0 ? void 0 : _b.call(this); }; QueryClient.prototype.isFetching = function (arg1, arg2) { var filters = parseFilterArgs(arg1, arg2)[0]; filters.fetching = true; return this.queryCache.findAll(filters).length; }; QueryClient.prototype.isMutating = function (filters) { return this.mutationCache.findAll(__assign(__assign({}, filters), { fetching: true })).length; }; QueryClient.prototype.getQueryData = function (queryKey, filters) { var _a; return (_a = this.queryCache.find(queryKey, filters)) === null || _a === void 0 ? void 0 : _a.state.data; }; QueryClient.prototype.setQueryData = function (queryKey, updater, options) { var parsedOptions = parseQueryArgs(queryKey); var defaultedOptions = this.defaultQueryOptions(parsedOptions); return this.queryCache .build(this, defaultedOptions) .setData(updater, options); }; QueryClient.prototype.setQueriesData = function (queryKeyOrFilters, updater, options) { var _this = this; return notifyManager.batch(function () { return _this.getQueryCache() .findAll(queryKeyOrFilters) .map(function (_a) { var queryKey = _a.queryKey; return [ queryKey, _this.setQueryData(queryKey, updater, options), ]; }); }); }; QueryClient.prototype.getQueryState = function (queryKey, filters) { var _a; return (_a = this.queryCache.find(queryKey, filters)) === null || _a === void 0 ? void 0 : _a.state; }; QueryClient.prototype.removeQueries = function (arg1, arg2) { var filters = parseFilterArgs(arg1, arg2)[0]; var queryCache = this.queryCache; notifyManager.batch(function () { queryCache.findAll(filters).forEach(function (query) { queryCache.remove(query); }); }); }; QueryClient.prototype.resetQueries = function (arg1, arg2, arg3) { var _this = this; var _a = parseFilterArgs(arg1, arg2, arg3), filters = _a[0], options = _a[1]; var queryCache = this.queryCache; var refetchFilters = __assign(__assign({}, filters), { active: true }); return notifyManager.batch(function () { queryCache.findAll(filters).forEach(function (query) { query.reset(); }); return _this.refetchQueries(refetchFilters, options); }); }; QueryClient.prototype.cancelQueries = function (arg1, arg2, arg3) { var _this = this; var _a = parseFilterArgs(arg1, arg2, arg3), filters = _a[0], _b = _a[1], cancelOptions = _b === void 0 ? {} : _b; if (typeof cancelOptions.revert === 'undefined') { cancelOptions.revert = true; } var promises = notifyManager.batch(function () { return _this.queryCache.findAll(filters).map(function (query) { return query.cancel(cancelOptions); }); }); return Promise.all(promises).then(noop).catch(noop); }; QueryClient.prototype.invalidateQueries = function (arg1, arg2, arg3) { var _this = this; var _a, _b, _c; var _d = parseFilterArgs(arg1, arg2, arg3), filters = _d[0], options = _d[1]; var refetchFilters = __assign(__assign({}, 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: (_b = (_a = filters.refetchActive) !== null && _a !== void 0 ? _a : filters.active) !== null && _b !== void 0 ? _b : true, inactive: (_c = filters.refetchInactive) !== null && _c !== void 0 ? _c : false }); return notifyManager.batch(function () { _this.queryCache.findAll(filters).forEach(function (query) { query.invalidate(); }); return _this.refetchQueries(refetchFilters, options); }); }; QueryClient.prototype.refetchQueries = function (arg1, arg2, arg3) { var _this = this; var _a = parseFilterArgs(arg1, arg2, arg3), filters = _a[0], options = _a[1]; var promises = notifyManager.batch(function () { return _this.queryCache.findAll(filters).map(function (query) { return query.fetch(); }); }); var promise = Promise.all(promises).then(noop); if (!(options === null || options === void 0 ? void 0 : options.throwOnError)) { promise = promise.catch(noop); } return promise; }; QueryClient.prototype.fetchQuery = function (arg1, arg2, arg3) { var parsedOptions = 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); }; QueryClient.prototype.prefetchQuery = function (arg1, arg2, arg3) { return this.fetchQuery(arg1, arg2, arg3) .then(noop) .catch(noop); }; QueryClient.prototype.fetchInfiniteQuery = function (arg1, arg2, arg3) { var parsedOptions = parseQueryArgs(arg1, arg2, arg3); parsedOptions.behavior = infiniteQueryBehavior(); return this.fetchQuery(parsedOptions); }; QueryClient.prototype.prefetchInfiniteQuery = function (arg1, arg2, arg3) { return this.fetchInfiniteQuery(arg1, arg2, arg3) .then(noop) .catch(noop); }; QueryClient.prototype.cancelMutations = function () { var _this = this; var promises = notifyManager.batch(function () { return _this.mutationCache.getAll().map(function (mutation) { return mutation.cancel(); }); }); return Promise.all(promises).then(noop).catch(noop); }; QueryClient.prototype.resumePausedMutations = function () { return this.getMutationCache().resumePausedMutations(); }; QueryClient.prototype.executeMutation = function (options) { return this.mutationCache.build(this, options).execute(); }; QueryClient.prototype.getQueryCache = function () { return this.queryCache; }; QueryClient.prototype.getMutationCache = function () { return this.mutationCache; }; QueryClient.prototype.getDefaultOptions = function () { return this.defaultOptions; }; QueryClient.prototype.setDefaultOptions = function (options) { this.defaultOptions = options; }; QueryClient.prototype.setQueryDefaults = function (queryKey, options) { var result = this.queryDefaults.find(function (x) { return hashQueryKey(queryKey) === hashQueryKey(x.queryKey); }); if (result) { result.defaultOptions = options; } else { this.queryDefaults.push({ queryKey: queryKey, defaultOptions: options }); } }; QueryClient.prototype.getQueryDefaults = function (queryKey) { var _a; return queryKey ? (_a = this.queryDefaults.find(function (x) { return partialMatchKey(queryKey, x.queryKey); })) === null || _a === void 0 ? void 0 : _a.defaultOptions : undefined; }; QueryClient.prototype.setMutationDefaults = function (mutationKey, options) { var result = this.mutationDefaults.find(function (x) { return hashQueryKey(mutationKey) === hashQueryKey(x.mutationKey); }); if (result) { result.defaultOptions = options; } else { this.mutationDefaults.push({ mutationKey: mutationKey, defaultOptions: options }); } }; QueryClient.prototype.getMutationDefaults = function (mutationKey) { var _a; return mutationKey ? (_a = this.mutationDefaults.find(function (x) { return partialMatchKey(mutationKey, x.mutationKey); })) === null || _a === void 0 ? void 0 : _a.defaultOptions : undefined; }; QueryClient.prototype.defaultQueryOptions = function (options) { if (options === null || options === void 0 ? void 0 : options._defaulted) { return options; } var defaultedOptions = __assign(__assign(__assign(__assign({}, this.defaultOptions.queries), this.getQueryDefaults(options === null || options === void 0 ? void 0 : options.queryKey)), options), { _defaulted: true }); if (!defaultedOptions.queryHash && defaultedOptions.queryKey) { defaultedOptions.queryHash = hashQueryKeyByOptions(defaultedOptions.queryKey, defaultedOptions); } return defaultedOptions; }; QueryClient.prototype.defaultQueryObserverOptions = function (options) { return this.defaultQueryOptions(options); }; QueryClient.prototype.defaultMutationOptions = function (options) { if (options === null || options === void 0 ? void 0 : options._defaulted) { return options; } return __assign(__assign(__assign(__assign({}, this.defaultOptions.mutations), this.getMutationDefaults(options === null || options === void 0 ? void 0 : options.mutationKey)), options), { _defaulted: true }); }; QueryClient.prototype.clear = function () { this.queryCache.clear(); this.mutationCache.clear(); }; return QueryClient; }()); export { QueryClient };