UNPKG

@tanstack/query-core

Version:

The framework agnostic core that powers TanStack Query

268 lines 8.44 kB
// src/queryClient.ts import { functionalUpdate, hashKey, hashQueryKeyByOptions, noop, partialMatchKey } from "./utils.js"; import { QueryCache } from "./queryCache.js"; import { MutationCache } from "./mutationCache.js"; import { focusManager } from "./focusManager.js"; import { onlineManager } from "./onlineManager.js"; import { notifyManager } from "./notifyManager.js"; import { infiniteQueryBehavior } from "./infiniteQueryBehavior.js"; var QueryClient = class { #queryCache; #mutationCache; #defaultOptions; #queryDefaults; #mutationDefaults; #mountCount; #unsubscribeFocus; #unsubscribeOnline; constructor(config = {}) { this.#queryCache = config.queryCache || new QueryCache(); this.#mutationCache = config.mutationCache || new MutationCache(); this.#defaultOptions = config.defaultOptions || {}; this.#queryDefaults = /* @__PURE__ */ new Map(); this.#mutationDefaults = /* @__PURE__ */ new Map(); this.#mountCount = 0; } 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() { this.#mountCount--; if (this.#mountCount !== 0) return; this.#unsubscribeFocus?.(); this.#unsubscribeFocus = void 0; this.#unsubscribeOnline?.(); this.#unsubscribeOnline = void 0; } isFetching(filters) { return this.#queryCache.findAll({ ...filters, fetchStatus: "fetching" }).length; } isMutating(filters) { return this.#mutationCache.findAll({ ...filters, status: "pending" }).length; } getQueryData(queryKey) { return this.#queryCache.find({ queryKey })?.state.data; } ensureQueryData(options) { const cachedData = this.getQueryData(options.queryKey); return cachedData !== void 0 ? Promise.resolve(cachedData) : this.fetchQuery(options); } getQueriesData(filters) { return this.getQueryCache().findAll(filters).map(({ queryKey, state }) => { const data = state.data; return [queryKey, data]; }); } setQueryData(queryKey, updater, options) { const query = this.#queryCache.find({ queryKey }); const prevData = query?.state.data; const data = functionalUpdate(updater, prevData); if (typeof data === "undefined") { return void 0; } const defaultedOptions = this.defaultQueryOptions({ queryKey }); return this.#queryCache.build(this, defaultedOptions).setData(data, { ...options, manual: true }); } setQueriesData(filters, updater, options) { return notifyManager.batch( () => this.getQueryCache().findAll(filters).map(({ queryKey }) => [ queryKey, this.setQueryData(queryKey, updater, options) ]) ); } getQueryState(queryKey) { return this.#queryCache.find({ queryKey })?.state; } removeQueries(filters) { const queryCache = this.#queryCache; notifyManager.batch(() => { queryCache.findAll(filters).forEach((query) => { queryCache.remove(query); }); }); } resetQueries(filters, options) { 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(filters = {}, cancelOptions = {}) { const defaultedCancelOptions = { revert: true, ...cancelOptions }; const promises = notifyManager.batch( () => this.#queryCache.findAll(filters).map((query) => query.cancel(defaultedCancelOptions)) ); return Promise.all(promises).then(noop).catch(noop); } invalidateQueries(filters = {}, options = {}) { return notifyManager.batch(() => { this.#queryCache.findAll(filters).forEach((query) => { query.invalidate(); }); if (filters.refetchType === "none") { return Promise.resolve(); } const refetchFilters = { ...filters, type: filters.refetchType ?? filters.type ?? "active" }; return this.refetchQueries(refetchFilters, options); }); } refetchQueries(filters = {}, options) { const fetchOptions = { ...options, cancelRefetch: options?.cancelRefetch ?? true }; const promises = notifyManager.batch( () => this.#queryCache.findAll(filters).filter((query) => !query.isDisabled()).map((query) => { let promise = query.fetch(void 0, fetchOptions); if (!fetchOptions.throwOnError) { promise = promise.catch(noop); } return query.state.fetchStatus === "paused" ? Promise.resolve() : promise; }) ); return Promise.all(promises).then(noop); } fetchQuery(options) { const defaultedOptions = this.defaultQueryOptions(options); 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(options) { return this.fetchQuery(options).then(noop).catch(noop); } fetchInfiniteQuery(options) { options.behavior = infiniteQueryBehavior(options.pages); return this.fetchQuery(options); } prefetchInfiniteQuery(options) { return this.fetchInfiniteQuery(options).then(noop).catch(noop); } resumePausedMutations() { return this.#mutationCache.resumePausedMutations(); } getQueryCache() { return this.#queryCache; } getMutationCache() { return this.#mutationCache; } getDefaultOptions() { return this.#defaultOptions; } setDefaultOptions(options) { this.#defaultOptions = options; } setQueryDefaults(queryKey, options) { this.#queryDefaults.set(hashKey(queryKey), { queryKey, defaultOptions: options }); } getQueryDefaults(queryKey) { const defaults = [...this.#queryDefaults.values()]; let result = {}; defaults.forEach((queryDefault) => { if (partialMatchKey(queryKey, queryDefault.queryKey)) { result = { ...result, ...queryDefault.defaultOptions }; } }); return result; } setMutationDefaults(mutationKey, options) { this.#mutationDefaults.set(hashKey(mutationKey), { mutationKey, defaultOptions: options }); } getMutationDefaults(mutationKey) { const defaults = [...this.#mutationDefaults.values()]; let result = {}; defaults.forEach((queryDefault) => { if (partialMatchKey(mutationKey, queryDefault.mutationKey)) { result = { ...result, ...queryDefault.defaultOptions }; } }); return result; } defaultQueryOptions(options) { if (options?._defaulted) { return options; } const defaultedOptions = { ...this.#defaultOptions.queries, ...options?.queryKey && this.getQueryDefaults(options.queryKey), ...options, _defaulted: true }; if (!defaultedOptions.queryHash) { defaultedOptions.queryHash = hashQueryKeyByOptions( defaultedOptions.queryKey, defaultedOptions ); } if (typeof defaultedOptions.refetchOnReconnect === "undefined") { defaultedOptions.refetchOnReconnect = defaultedOptions.networkMode !== "always"; } if (typeof defaultedOptions.throwOnError === "undefined") { defaultedOptions.throwOnError = !!defaultedOptions.suspense; } if (typeof defaultedOptions.networkMode === "undefined" && defaultedOptions.persister) { defaultedOptions.networkMode = "offlineFirst"; } return defaultedOptions; } defaultMutationOptions(options) { if (options?._defaulted) { return options; } return { ...this.#defaultOptions.mutations, ...options?.mutationKey && this.getMutationDefaults(options.mutationKey), ...options, _defaulted: true }; } clear() { this.#queryCache.clear(); this.#mutationCache.clear(); } }; export { QueryClient }; //# sourceMappingURL=queryClient.js.map