react-query
Version:
Hooks for managing, caching and syncing asynchronous and remote data in React
351 lines (279 loc) • 11.6 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
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'; // TYPES
// CLASS
export var QueryClient = /*#__PURE__*/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 = [];
}
var _proto = QueryClient.prototype;
_proto.mount = function mount() {
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();
}
});
};
_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 = parseFilterArgs(arg1, arg2),
filters = _parseFilterArgs[0];
filters.fetching = true;
return this.queryCache.findAll(filters).length;
};
_proto.isMutating = function isMutating(filters) {
return this.mutationCache.findAll(_extends({}, 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 = 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.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 = parseFilterArgs(arg1, arg2),
filters = _parseFilterArgs2[0];
var queryCache = this.queryCache;
notifyManager.batch(function () {
queryCache.findAll(filters).forEach(function (query) {
queryCache.remove(query);
});
});
};
_proto.resetQueries = function resetQueries(arg1, arg2, arg3) {
var _this3 = this;
var _parseFilterArgs3 = parseFilterArgs(arg1, arg2, arg3),
filters = _parseFilterArgs3[0],
options = _parseFilterArgs3[1];
var queryCache = this.queryCache;
var refetchFilters = _extends({}, filters, {
active: true
});
return 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 = 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.batch(function () {
return _this4.queryCache.findAll(filters).map(function (query) {
return query.cancel(cancelOptions);
});
});
return Promise.all(promises).then(noop).catch(noop);
};
_proto.invalidateQueries = function invalidateQueries(arg1, arg2, arg3) {
var _ref3,
_filters$refetchActiv,
_filters$refetchInact,
_this5 = this;
var _parseFilterArgs5 = parseFilterArgs(arg1, arg2, arg3),
filters = _parseFilterArgs5[0],
options = _parseFilterArgs5[1];
var refetchFilters = _extends({}, 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.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 = parseFilterArgs(arg1, arg2, arg3),
filters = _parseFilterArgs6[0],
options = _parseFilterArgs6[1];
var promises = notifyManager.batch(function () {
return _this6.queryCache.findAll(filters).map(function (query) {
return query.fetch(undefined, {
meta: {
refetchPage: filters == null ? void 0 : filters.refetchPage
}
});
});
});
var promise = Promise.all(promises).then(noop);
if (!(options == null ? void 0 : options.throwOnError)) {
promise = promise.catch(noop);
}
return promise;
};
_proto.fetchQuery = function fetchQuery(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);
};
_proto.prefetchQuery = function prefetchQuery(arg1, arg2, arg3) {
return this.fetchQuery(arg1, arg2, arg3).then(noop).catch(noop);
};
_proto.fetchInfiniteQuery = function fetchInfiniteQuery(arg1, arg2, arg3) {
var parsedOptions = parseQueryArgs(arg1, arg2, arg3);
parsedOptions.behavior = infiniteQueryBehavior();
return this.fetchQuery(parsedOptions);
};
_proto.prefetchInfiniteQuery = function prefetchInfiniteQuery(arg1, arg2, arg3) {
return this.fetchInfiniteQuery(arg1, arg2, arg3).then(noop).catch(noop);
};
_proto.cancelMutations = function cancelMutations() {
var _this7 = this;
var promises = notifyManager.batch(function () {
return _this7.mutationCache.getAll().map(function (mutation) {
return mutation.cancel();
});
});
return Promise.all(promises).then(noop).catch(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 hashQueryKey(queryKey) === 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 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 hashQueryKey(mutationKey) === 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 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 = _extends({}, this.defaultOptions.queries, this.getQueryDefaults(options == null ? void 0 : options.queryKey), options, {
_defaulted: true
});
if (!defaultedOptions.queryHash && defaultedOptions.queryKey) {
defaultedOptions.queryHash = 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 _extends({}, 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;
}();