123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- import { parseFilterArgs, parseQueryArgs, functionalUpdate, noop, hashQueryKey, partialMatchKey, hashQueryKeyByOptions } from './utils.mjs';
- import { QueryCache } from './queryCache.mjs';
- import { MutationCache } from './mutationCache.mjs';
- import { focusManager } from './focusManager.mjs';
- import { onlineManager } from './onlineManager.mjs';
- import { notifyManager } from './notifyManager.mjs';
- import { infiniteQueryBehavior } from './infiniteQueryBehavior.mjs';
- import { defaultLogger } from './logger.mjs';
- // CLASS
- class QueryClient {
- constructor(config = {}) {
- this.queryCache = config.queryCache || new QueryCache();
- this.mutationCache = config.mutationCache || new MutationCache();
- this.logger = config.logger || defaultLogger;
- this.defaultOptions = config.defaultOptions || {};
- this.queryDefaults = [];
- this.mutationDefaults = [];
- this.mountCount = 0;
- if (process.env.NODE_ENV !== 'production' && config.logger) {
- this.logger.error("Passing a custom logger has been deprecated and will be removed in the next major version.");
- }
- }
- mount() {
- this.mountCount++;
- if (this.mountCount !== 1) return;
- this.unsubscribeFocus = focusManager.subscribe(() => {
- if (focusManager.isFocused()) {
- this.resumePausedMutations();
- this.queryCache.onFocus();
- }
- });
- this.unsubscribeOnline = onlineManager.subscribe(() => {
- if (onlineManager.isOnline()) {
- this.resumePausedMutations();
- this.queryCache.onOnline();
- }
- });
- }
- unmount() {
- var _this$unsubscribeFocu, _this$unsubscribeOnli;
- this.mountCount--;
- if (this.mountCount !== 0) return;
- (_this$unsubscribeFocu = this.unsubscribeFocus) == null ? void 0 : _this$unsubscribeFocu.call(this);
- this.unsubscribeFocus = undefined;
- (_this$unsubscribeOnli = this.unsubscribeOnline) == null ? void 0 : _this$unsubscribeOnli.call(this);
- this.unsubscribeOnline = undefined;
- }
- isFetching(arg1, arg2) {
- const [filters] = parseFilterArgs(arg1, arg2);
- filters.fetchStatus = 'fetching';
- return this.queryCache.findAll(filters).length;
- }
- isMutating(filters) {
- return this.mutationCache.findAll({ ...filters,
- fetching: true
- }).length;
- }
- getQueryData(queryKey, filters) {
- var _this$queryCache$find;
- return (_this$queryCache$find = this.queryCache.find(queryKey, filters)) == null ? void 0 : _this$queryCache$find.state.data;
- }
- ensureQueryData(arg1, arg2, arg3) {
- const parsedOptions = parseQueryArgs(arg1, arg2, arg3);
- const cachedData = this.getQueryData(parsedOptions.queryKey);
- return cachedData ? Promise.resolve(cachedData) : this.fetchQuery(parsedOptions);
- }
- getQueriesData(queryKeyOrFilters) {
- return this.getQueryCache().findAll(queryKeyOrFilters).map(({
- queryKey,
- state
- }) => {
- const data = state.data;
- return [queryKey, data];
- });
- }
- setQueryData(queryKey, updater, options) {
- const query = this.queryCache.find(queryKey);
- const prevData = query == null ? void 0 : query.state.data;
- const data = functionalUpdate(updater, prevData);
- if (typeof data === 'undefined') {
- return undefined;
- }
- const parsedOptions = parseQueryArgs(queryKey);
- const defaultedOptions = this.defaultQueryOptions(parsedOptions);
- return this.queryCache.build(this, defaultedOptions).setData(data, { ...options,
- manual: true
- });
- }
- setQueriesData(queryKeyOrFilters, updater, options) {
- return notifyManager.batch(() => this.getQueryCache().findAll(queryKeyOrFilters).map(({
- queryKey
- }) => [queryKey, this.setQueryData(queryKey, updater, options)]));
- }
- getQueryState(queryKey, filters) {
- var _this$queryCache$find2;
- return (_this$queryCache$find2 = this.queryCache.find(queryKey, filters)) == null ? void 0 : _this$queryCache$find2.state;
- }
- removeQueries(arg1, arg2) {
- const [filters] = parseFilterArgs(arg1, arg2);
- const queryCache = this.queryCache;
- notifyManager.batch(() => {
- queryCache.findAll(filters).forEach(query => {
- queryCache.remove(query);
- });
- });
- }
- resetQueries(arg1, arg2, arg3) {
- const [filters, options] = parseFilterArgs(arg1, arg2, arg3);
- const queryCache = this.queryCache;
- const refetchFilters = {
- type: 'active',
- ...filters
- };
- return notifyManager.batch(() => {
- queryCache.findAll(filters).forEach(query => {
- query.reset();
- });
- return this.refetchQueries(refetchFilters, options);
- });
- }
- cancelQueries(arg1, arg2, arg3) {
- const [filters, cancelOptions = {}] = parseFilterArgs(arg1, arg2, arg3);
- if (typeof cancelOptions.revert === 'undefined') {
- cancelOptions.revert = true;
- }
- const promises = notifyManager.batch(() => this.queryCache.findAll(filters).map(query => query.cancel(cancelOptions)));
- return Promise.all(promises).then(noop).catch(noop);
- }
- invalidateQueries(arg1, arg2, arg3) {
- const [filters, options] = parseFilterArgs(arg1, arg2, arg3);
- return notifyManager.batch(() => {
- var _ref, _filters$refetchType;
- this.queryCache.findAll(filters).forEach(query => {
- query.invalidate();
- });
- if (filters.refetchType === 'none') {
- return Promise.resolve();
- }
- const refetchFilters = { ...filters,
- type: (_ref = (_filters$refetchType = filters.refetchType) != null ? _filters$refetchType : filters.type) != null ? _ref : 'active'
- };
- return this.refetchQueries(refetchFilters, options);
- });
- }
- refetchQueries(arg1, arg2, arg3) {
- const [filters, options] = parseFilterArgs(arg1, arg2, arg3);
- const promises = notifyManager.batch(() => this.queryCache.findAll(filters).filter(query => !query.isDisabled()).map(query => {
- var _options$cancelRefetc;
- return query.fetch(undefined, { ...options,
- cancelRefetch: (_options$cancelRefetc = options == null ? void 0 : options.cancelRefetch) != null ? _options$cancelRefetc : true,
- meta: {
- refetchPage: filters.refetchPage
- }
- });
- }));
- let promise = Promise.all(promises).then(noop);
- if (!(options != null && options.throwOnError)) {
- promise = promise.catch(noop);
- }
- return promise;
- }
- fetchQuery(arg1, arg2, arg3) {
- const parsedOptions = parseQueryArgs(arg1, arg2, arg3);
- const defaultedOptions = this.defaultQueryOptions(parsedOptions); // https://github.com/tannerlinsley/react-query/issues/652
- if (typeof defaultedOptions.retry === 'undefined') {
- defaultedOptions.retry = false;
- }
- const query = this.queryCache.build(this, defaultedOptions);
- return query.isStaleByTime(defaultedOptions.staleTime) ? query.fetch(defaultedOptions) : Promise.resolve(query.state.data);
- }
- prefetchQuery(arg1, arg2, arg3) {
- return this.fetchQuery(arg1, arg2, arg3).then(noop).catch(noop);
- }
- fetchInfiniteQuery(arg1, arg2, arg3) {
- const parsedOptions = parseQueryArgs(arg1, arg2, arg3);
- parsedOptions.behavior = infiniteQueryBehavior();
- return this.fetchQuery(parsedOptions);
- }
- prefetchInfiniteQuery(arg1, arg2, arg3) {
- return this.fetchInfiniteQuery(arg1, arg2, arg3).then(noop).catch(noop);
- }
- resumePausedMutations() {
- return this.mutationCache.resumePausedMutations();
- }
- getQueryCache() {
- return this.queryCache;
- }
- getMutationCache() {
- return this.mutationCache;
- }
- getLogger() {
- return this.logger;
- }
- getDefaultOptions() {
- return this.defaultOptions;
- }
- setDefaultOptions(options) {
- this.defaultOptions = options;
- }
- setQueryDefaults(queryKey, options) {
- const result = this.queryDefaults.find(x => hashQueryKey(queryKey) === hashQueryKey(x.queryKey));
- if (result) {
- result.defaultOptions = options;
- } else {
- this.queryDefaults.push({
- queryKey,
- defaultOptions: options
- });
- }
- }
- getQueryDefaults(queryKey) {
- if (!queryKey) {
- return undefined;
- } // Get the first matching defaults
- const firstMatchingDefaults = this.queryDefaults.find(x => partialMatchKey(queryKey, x.queryKey)); // Additional checks and error in dev mode
- if (process.env.NODE_ENV !== 'production') {
- // Retrieve all matching defaults for the given key
- const matchingDefaults = this.queryDefaults.filter(x => partialMatchKey(queryKey, x.queryKey)); // It is ok not having defaults, but it is error prone to have more than 1 default for a given key
- if (matchingDefaults.length > 1) {
- this.logger.error("[QueryClient] Several query defaults match with key '" + JSON.stringify(queryKey) + "'. The first matching query defaults are used. Please check how query defaults are registered. Order does matter here. cf. https://react-query.tanstack.com/reference/QueryClient#queryclientsetquerydefaults.");
- }
- }
- return firstMatchingDefaults == null ? void 0 : firstMatchingDefaults.defaultOptions;
- }
- setMutationDefaults(mutationKey, options) {
- const result = this.mutationDefaults.find(x => hashQueryKey(mutationKey) === hashQueryKey(x.mutationKey));
- if (result) {
- result.defaultOptions = options;
- } else {
- this.mutationDefaults.push({
- mutationKey,
- defaultOptions: options
- });
- }
- }
- getMutationDefaults(mutationKey) {
- if (!mutationKey) {
- return undefined;
- } // Get the first matching defaults
- const firstMatchingDefaults = this.mutationDefaults.find(x => partialMatchKey(mutationKey, x.mutationKey)); // Additional checks and error in dev mode
- if (process.env.NODE_ENV !== 'production') {
- // Retrieve all matching defaults for the given key
- const matchingDefaults = this.mutationDefaults.filter(x => partialMatchKey(mutationKey, x.mutationKey)); // It is ok not having defaults, but it is error prone to have more than 1 default for a given key
- if (matchingDefaults.length > 1) {
- this.logger.error("[QueryClient] Several mutation defaults match with key '" + JSON.stringify(mutationKey) + "'. The first matching mutation defaults are used. Please check how mutation defaults are registered. Order does matter here. cf. https://react-query.tanstack.com/reference/QueryClient#queryclientsetmutationdefaults.");
- }
- }
- return firstMatchingDefaults == null ? void 0 : firstMatchingDefaults.defaultOptions;
- }
- defaultQueryOptions(options) {
- if (options != null && options._defaulted) {
- return options;
- }
- const defaultedOptions = { ...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);
- } // dependent default values
- if (typeof defaultedOptions.refetchOnReconnect === 'undefined') {
- defaultedOptions.refetchOnReconnect = defaultedOptions.networkMode !== 'always';
- }
- if (typeof defaultedOptions.useErrorBoundary === 'undefined') {
- defaultedOptions.useErrorBoundary = !!defaultedOptions.suspense;
- }
- return defaultedOptions;
- }
- defaultMutationOptions(options) {
- if (options != null && options._defaulted) {
- return options;
- }
- return { ...this.defaultOptions.mutations,
- ...this.getMutationDefaults(options == null ? void 0 : options.mutationKey),
- ...options,
- _defaulted: true
- };
- }
- clear() {
- this.queryCache.clear();
- this.mutationCache.clear();
- }
- }
- export { QueryClient };
- //# sourceMappingURL=queryClient.mjs.map
|