@tanstack/query-core 5.102.7 → 5.103.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/legacy/environmentManager.cjs +15 -1
- package/build/legacy/environmentManager.cjs.map +1 -1
- package/build/legacy/environmentManager.d.cts +15 -1
- package/build/legacy/environmentManager.d.cts.map +1 -1
- package/build/legacy/environmentManager.d.ts +15 -1
- package/build/legacy/environmentManager.d.ts.map +1 -1
- package/build/legacy/environmentManager.js +15 -1
- package/build/legacy/environmentManager.js.map +1 -1
- package/build/legacy/focusManager.cjs +57 -0
- package/build/legacy/focusManager.cjs.map +1 -1
- package/build/legacy/focusManager.d.cts +57 -0
- package/build/legacy/focusManager.d.cts.map +1 -1
- package/build/legacy/focusManager.d.ts +57 -0
- package/build/legacy/focusManager.d.ts.map +1 -1
- package/build/legacy/focusManager.js +57 -0
- package/build/legacy/focusManager.js.map +1 -1
- package/build/legacy/{hydration-Bjs0MSgg.d.ts → hydration-Cq7QYAzB.d.ts} +1424 -7
- package/build/legacy/hydration-Cq7QYAzB.d.ts.map +1 -0
- package/build/legacy/{hydration-DWGr3cFC.d.cts → hydration-DwR10Hi-.d.cts} +1424 -7
- package/build/legacy/hydration-DwR10Hi-.d.cts.map +1 -0
- package/build/legacy/hydration.cjs +57 -4
- package/build/legacy/hydration.cjs.map +1 -1
- package/build/legacy/hydration.d.cts +1 -1
- package/build/legacy/hydration.d.ts +1 -1
- package/build/legacy/hydration.js +57 -4
- package/build/legacy/hydration.js.map +1 -1
- package/build/legacy/index.cjs.map +1 -1
- package/build/legacy/index.d.cts +5 -5
- package/build/legacy/index.d.ts +5 -5
- package/build/legacy/index.js.map +1 -1
- package/build/legacy/infiniteQueryBehavior.d.cts +1 -1
- package/build/legacy/infiniteQueryBehavior.d.ts +1 -1
- package/build/legacy/infiniteQueryObserver.cjs +67 -0
- package/build/legacy/infiniteQueryObserver.cjs.map +1 -1
- package/build/legacy/infiniteQueryObserver.d.cts +68 -1
- package/build/legacy/infiniteQueryObserver.d.cts.map +1 -1
- package/build/legacy/infiniteQueryObserver.d.ts +68 -1
- package/build/legacy/infiniteQueryObserver.d.ts.map +1 -1
- package/build/legacy/infiniteQueryObserver.js +67 -0
- package/build/legacy/infiniteQueryObserver.js.map +1 -1
- package/build/legacy/mutation.cjs +74 -0
- package/build/legacy/mutation.cjs.map +1 -1
- package/build/legacy/mutation.d.cts +1 -1
- package/build/legacy/mutation.d.ts +1 -1
- package/build/legacy/mutation.js +74 -0
- package/build/legacy/mutation.js.map +1 -1
- package/build/legacy/mutationCache.cjs +74 -0
- package/build/legacy/mutationCache.cjs.map +1 -1
- package/build/legacy/mutationCache.d.cts +1 -1
- package/build/legacy/mutationCache.d.ts +1 -1
- package/build/legacy/mutationCache.js +74 -0
- package/build/legacy/mutationCache.js.map +1 -1
- package/build/legacy/mutationObserver.cjs +71 -0
- package/build/legacy/mutationObserver.cjs.map +1 -1
- package/build/legacy/mutationObserver.d.cts +1 -1
- package/build/legacy/mutationObserver.d.ts +1 -1
- package/build/legacy/mutationObserver.js +71 -0
- package/build/legacy/mutationObserver.js.map +1 -1
- package/build/legacy/notifyManager.cjs +45 -1
- package/build/legacy/notifyManager.cjs.map +1 -1
- package/build/legacy/notifyManager.d.cts +83 -2
- package/build/legacy/notifyManager.d.cts.map +1 -1
- package/build/legacy/notifyManager.d.ts +83 -2
- package/build/legacy/notifyManager.d.ts.map +1 -1
- package/build/legacy/notifyManager.js +45 -1
- package/build/legacy/notifyManager.js.map +1 -1
- package/build/legacy/onlineManager.cjs +47 -0
- package/build/legacy/onlineManager.cjs.map +1 -1
- package/build/legacy/onlineManager.d.cts +47 -0
- package/build/legacy/onlineManager.d.cts.map +1 -1
- package/build/legacy/onlineManager.d.ts +47 -0
- package/build/legacy/onlineManager.d.ts.map +1 -1
- package/build/legacy/onlineManager.js +47 -0
- package/build/legacy/onlineManager.js.map +1 -1
- package/build/legacy/queriesObserver.cjs +64 -0
- package/build/legacy/queriesObserver.cjs.map +1 -1
- package/build/legacy/queriesObserver.d.cts +73 -1
- package/build/legacy/queriesObserver.d.cts.map +1 -1
- package/build/legacy/queriesObserver.d.ts +73 -1
- package/build/legacy/queriesObserver.d.ts.map +1 -1
- package/build/legacy/queriesObserver.js +64 -0
- package/build/legacy/queriesObserver.js.map +1 -1
- package/build/legacy/query.cjs +141 -0
- package/build/legacy/query.cjs.map +1 -1
- package/build/legacy/query.d.cts +1 -1
- package/build/legacy/query.d.ts +1 -1
- package/build/legacy/query.js +141 -0
- package/build/legacy/query.js.map +1 -1
- package/build/legacy/queryCache.cjs +120 -0
- package/build/legacy/queryCache.cjs.map +1 -1
- package/build/legacy/queryCache.d.cts +1 -1
- package/build/legacy/queryCache.d.ts +1 -1
- package/build/legacy/queryCache.js +120 -0
- package/build/legacy/queryCache.js.map +1 -1
- package/build/legacy/queryClient.cjs +363 -0
- package/build/legacy/queryClient.cjs.map +1 -1
- package/build/legacy/queryClient.d.cts +1 -1
- package/build/legacy/queryClient.d.ts +1 -1
- package/build/legacy/queryClient.js +363 -0
- package/build/legacy/queryClient.js.map +1 -1
- package/build/legacy/queryObserver.cjs +116 -2
- package/build/legacy/queryObserver.cjs.map +1 -1
- package/build/legacy/queryObserver.d.cts +1 -1
- package/build/legacy/queryObserver.d.ts +1 -1
- package/build/legacy/queryObserver.js +116 -2
- package/build/legacy/queryObserver.js.map +1 -1
- package/build/legacy/retryer.cjs +18 -0
- package/build/legacy/retryer.cjs.map +1 -1
- package/build/legacy/retryer.d.cts +1 -1
- package/build/legacy/retryer.d.ts +1 -1
- package/build/legacy/retryer.js +18 -0
- package/build/legacy/retryer.js.map +1 -1
- package/build/legacy/streamedQuery.cjs +10 -1
- package/build/legacy/streamedQuery.cjs.map +1 -1
- package/build/legacy/streamedQuery.d.cts +26 -2
- package/build/legacy/streamedQuery.d.cts.map +1 -1
- package/build/legacy/streamedQuery.d.ts +26 -2
- package/build/legacy/streamedQuery.d.ts.map +1 -1
- package/build/legacy/streamedQuery.js +10 -1
- package/build/legacy/streamedQuery.js.map +1 -1
- package/build/legacy/timeoutManager.cjs +99 -0
- package/build/legacy/timeoutManager.cjs.map +1 -1
- package/build/legacy/timeoutManager.d.cts +105 -0
- package/build/legacy/timeoutManager.d.cts.map +1 -1
- package/build/legacy/timeoutManager.d.ts +105 -0
- package/build/legacy/timeoutManager.d.ts.map +1 -1
- package/build/legacy/timeoutManager.js +99 -0
- package/build/legacy/timeoutManager.js.map +1 -1
- package/build/legacy/types.cjs.map +1 -1
- package/build/legacy/types.d.cts +1 -1
- package/build/legacy/types.d.ts +1 -1
- package/build/legacy/types.js.map +1 -1
- package/build/legacy/utils.cjs +76 -0
- package/build/legacy/utils.cjs.map +1 -1
- package/build/legacy/utils.d.cts +1 -1
- package/build/legacy/utils.d.ts +1 -1
- package/build/legacy/utils.js +76 -0
- package/build/legacy/utils.js.map +1 -1
- package/build/modern/environmentManager.cjs +15 -1
- package/build/modern/environmentManager.cjs.map +1 -1
- package/build/modern/environmentManager.d.cts +15 -1
- package/build/modern/environmentManager.d.cts.map +1 -1
- package/build/modern/environmentManager.d.ts +15 -1
- package/build/modern/environmentManager.d.ts.map +1 -1
- package/build/modern/environmentManager.js +15 -1
- package/build/modern/environmentManager.js.map +1 -1
- package/build/modern/focusManager.cjs +57 -0
- package/build/modern/focusManager.cjs.map +1 -1
- package/build/modern/focusManager.d.cts +57 -0
- package/build/modern/focusManager.d.cts.map +1 -1
- package/build/modern/focusManager.d.ts +57 -0
- package/build/modern/focusManager.d.ts.map +1 -1
- package/build/modern/focusManager.js +57 -0
- package/build/modern/focusManager.js.map +1 -1
- package/build/modern/{hydration-Bjs0MSgg.d.ts → hydration-Cq7QYAzB.d.ts} +1424 -7
- package/build/modern/hydration-Cq7QYAzB.d.ts.map +1 -0
- package/build/modern/{hydration-DWGr3cFC.d.cts → hydration-DwR10Hi-.d.cts} +1424 -7
- package/build/modern/hydration-DwR10Hi-.d.cts.map +1 -0
- package/build/modern/hydration.cjs +57 -4
- package/build/modern/hydration.cjs.map +1 -1
- package/build/modern/hydration.d.cts +1 -1
- package/build/modern/hydration.d.ts +1 -1
- package/build/modern/hydration.js +57 -4
- package/build/modern/hydration.js.map +1 -1
- package/build/modern/index.cjs.map +1 -1
- package/build/modern/index.d.cts +5 -5
- package/build/modern/index.d.ts +5 -5
- package/build/modern/index.js.map +1 -1
- package/build/modern/infiniteQueryBehavior.d.cts +1 -1
- package/build/modern/infiniteQueryBehavior.d.ts +1 -1
- package/build/modern/infiniteQueryObserver.cjs +67 -0
- package/build/modern/infiniteQueryObserver.cjs.map +1 -1
- package/build/modern/infiniteQueryObserver.d.cts +68 -1
- package/build/modern/infiniteQueryObserver.d.cts.map +1 -1
- package/build/modern/infiniteQueryObserver.d.ts +68 -1
- package/build/modern/infiniteQueryObserver.d.ts.map +1 -1
- package/build/modern/infiniteQueryObserver.js +67 -0
- package/build/modern/infiniteQueryObserver.js.map +1 -1
- package/build/modern/mutation.cjs +74 -0
- package/build/modern/mutation.cjs.map +1 -1
- package/build/modern/mutation.d.cts +1 -1
- package/build/modern/mutation.d.ts +1 -1
- package/build/modern/mutation.js +74 -0
- package/build/modern/mutation.js.map +1 -1
- package/build/modern/mutationCache.cjs +74 -0
- package/build/modern/mutationCache.cjs.map +1 -1
- package/build/modern/mutationCache.d.cts +1 -1
- package/build/modern/mutationCache.d.ts +1 -1
- package/build/modern/mutationCache.js +74 -0
- package/build/modern/mutationCache.js.map +1 -1
- package/build/modern/mutationObserver.cjs +71 -0
- package/build/modern/mutationObserver.cjs.map +1 -1
- package/build/modern/mutationObserver.d.cts +1 -1
- package/build/modern/mutationObserver.d.ts +1 -1
- package/build/modern/mutationObserver.js +71 -0
- package/build/modern/mutationObserver.js.map +1 -1
- package/build/modern/notifyManager.cjs +45 -1
- package/build/modern/notifyManager.cjs.map +1 -1
- package/build/modern/notifyManager.d.cts +83 -2
- package/build/modern/notifyManager.d.cts.map +1 -1
- package/build/modern/notifyManager.d.ts +83 -2
- package/build/modern/notifyManager.d.ts.map +1 -1
- package/build/modern/notifyManager.js +45 -1
- package/build/modern/notifyManager.js.map +1 -1
- package/build/modern/onlineManager.cjs +47 -0
- package/build/modern/onlineManager.cjs.map +1 -1
- package/build/modern/onlineManager.d.cts +47 -0
- package/build/modern/onlineManager.d.cts.map +1 -1
- package/build/modern/onlineManager.d.ts +47 -0
- package/build/modern/onlineManager.d.ts.map +1 -1
- package/build/modern/onlineManager.js +47 -0
- package/build/modern/onlineManager.js.map +1 -1
- package/build/modern/queriesObserver.cjs +64 -0
- package/build/modern/queriesObserver.cjs.map +1 -1
- package/build/modern/queriesObserver.d.cts +73 -1
- package/build/modern/queriesObserver.d.cts.map +1 -1
- package/build/modern/queriesObserver.d.ts +73 -1
- package/build/modern/queriesObserver.d.ts.map +1 -1
- package/build/modern/queriesObserver.js +64 -0
- package/build/modern/queriesObserver.js.map +1 -1
- package/build/modern/query.cjs +141 -0
- package/build/modern/query.cjs.map +1 -1
- package/build/modern/query.d.cts +1 -1
- package/build/modern/query.d.ts +1 -1
- package/build/modern/query.js +141 -0
- package/build/modern/query.js.map +1 -1
- package/build/modern/queryCache.cjs +120 -0
- package/build/modern/queryCache.cjs.map +1 -1
- package/build/modern/queryCache.d.cts +1 -1
- package/build/modern/queryCache.d.ts +1 -1
- package/build/modern/queryCache.js +120 -0
- package/build/modern/queryCache.js.map +1 -1
- package/build/modern/queryClient.cjs +363 -0
- package/build/modern/queryClient.cjs.map +1 -1
- package/build/modern/queryClient.d.cts +1 -1
- package/build/modern/queryClient.d.ts +1 -1
- package/build/modern/queryClient.js +363 -0
- package/build/modern/queryClient.js.map +1 -1
- package/build/modern/queryObserver.cjs +116 -2
- package/build/modern/queryObserver.cjs.map +1 -1
- package/build/modern/queryObserver.d.cts +1 -1
- package/build/modern/queryObserver.d.ts +1 -1
- package/build/modern/queryObserver.js +116 -2
- package/build/modern/queryObserver.js.map +1 -1
- package/build/modern/retryer.cjs +18 -0
- package/build/modern/retryer.cjs.map +1 -1
- package/build/modern/retryer.d.cts +1 -1
- package/build/modern/retryer.d.ts +1 -1
- package/build/modern/retryer.js +18 -0
- package/build/modern/retryer.js.map +1 -1
- package/build/modern/streamedQuery.cjs +10 -1
- package/build/modern/streamedQuery.cjs.map +1 -1
- package/build/modern/streamedQuery.d.cts +26 -2
- package/build/modern/streamedQuery.d.cts.map +1 -1
- package/build/modern/streamedQuery.d.ts +26 -2
- package/build/modern/streamedQuery.d.ts.map +1 -1
- package/build/modern/streamedQuery.js +10 -1
- package/build/modern/streamedQuery.js.map +1 -1
- package/build/modern/timeoutManager.cjs +99 -0
- package/build/modern/timeoutManager.cjs.map +1 -1
- package/build/modern/timeoutManager.d.cts +105 -0
- package/build/modern/timeoutManager.d.cts.map +1 -1
- package/build/modern/timeoutManager.d.ts +105 -0
- package/build/modern/timeoutManager.d.ts.map +1 -1
- package/build/modern/timeoutManager.js +99 -0
- package/build/modern/timeoutManager.js.map +1 -1
- package/build/modern/types.cjs.map +1 -1
- package/build/modern/types.d.cts +1 -1
- package/build/modern/types.d.ts +1 -1
- package/build/modern/types.js.map +1 -1
- package/build/modern/utils.cjs +76 -0
- package/build/modern/utils.cjs.map +1 -1
- package/build/modern/utils.d.cts +1 -1
- package/build/modern/utils.d.ts +1 -1
- package/build/modern/utils.js +76 -0
- package/build/modern/utils.js.map +1 -1
- package/package.json +9 -9
- package/src/environmentManager.ts +15 -1
- package/src/focusManager.ts +57 -0
- package/src/hydration.ts +82 -16
- package/src/index.ts +3 -0
- package/src/infiniteQueryObserver.ts +67 -0
- package/src/mutation.ts +108 -0
- package/src/mutationCache.ts +91 -0
- package/src/mutationObserver.ts +71 -0
- package/src/notifyManager.ts +46 -1
- package/src/onlineManager.ts +47 -0
- package/src/queriesObserver.ts +72 -0
- package/src/query.ts +193 -0
- package/src/queryCache.ts +134 -0
- package/src/queryClient.ts +369 -0
- package/src/queryObserver.ts +117 -4
- package/src/retryer.ts +18 -0
- package/src/streamedQuery.ts +25 -1
- package/src/timeoutManager.ts +105 -0
- package/src/types.ts +147 -5
- package/src/utils.ts +107 -0
- package/build/legacy/hydration-Bjs0MSgg.d.ts.map +0 -1
- package/build/legacy/hydration-DWGr3cFC.d.cts.map +0 -1
- package/build/modern/hydration-Bjs0MSgg.d.ts.map +0 -1
- package/build/modern/hydration-DWGr3cFC.d.cts.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","names":[],"sources":["../../src/utils.ts"],"sourcesContent":["import { timeoutManager } from './timeoutManager'\nimport type {\n DefaultError,\n FetchStatus,\n MutationKey,\n MutationStatus,\n QueryFunction,\n QueryKey,\n QueryOptions,\n} from './types'\nimport type { Mutation } from './mutation'\nimport type { FetchOptions, Query } from './query'\n\n// TYPES\n\ntype DropLast<T extends ReadonlyArray<unknown>> = T extends readonly [\n ...infer R,\n unknown,\n]\n ? readonly [...R]\n : never\n\ntype TuplePrefixes<T extends ReadonlyArray<unknown>> = T extends readonly []\n ? readonly []\n : TuplePrefixes<DropLast<T>> | T\n\nexport interface QueryFilters<TQueryKey extends QueryKey = QueryKey> {\n /**\n * Filter to active queries, inactive queries or all queries\n */\n type?: QueryTypeFilter\n /**\n * Match query key exactly\n */\n exact?: boolean\n /**\n * Include queries matching this predicate function\n */\n predicate?: (query: Query) => boolean\n /**\n * Include queries matching this query key\n */\n queryKey?: TQueryKey | TuplePrefixes<TQueryKey>\n /**\n * Include or exclude stale queries\n */\n stale?: boolean\n /**\n * Include queries matching their fetchStatus\n */\n fetchStatus?: FetchStatus\n}\n\nexport interface MutationFilters<\n TData = unknown,\n TError = DefaultError,\n TVariables = unknown,\n TOnMutateResult = unknown,\n> {\n /**\n * Match mutation key exactly\n */\n exact?: boolean\n /**\n * Include mutations matching this predicate function\n */\n predicate?: (\n mutation: Mutation<TData, TError, TVariables, TOnMutateResult>,\n ) => boolean\n /**\n * Include mutations matching this mutation key\n */\n mutationKey?: TuplePrefixes<MutationKey>\n /**\n * Filter by mutation status\n */\n status?: MutationStatus\n}\n\nexport type Updater<TInput, TOutput> = TOutput | ((input: TInput) => TOutput)\n\nexport type QueryTypeFilter = 'all' | 'active' | 'inactive'\n\n// UTILS\n\n/** @deprecated\n * use `environmentManager.isServer()` instead.\n */\nexport const isServer = typeof window === 'undefined' || 'Deno' in globalThis\n\nexport function noop(): void\nexport function noop(): undefined\nexport function noop() {}\n\nexport function functionalUpdate<TInput, TOutput>(\n updater: Updater<TInput, TOutput>,\n input: TInput,\n): TOutput {\n return typeof updater === 'function'\n ? (updater as (_: TInput) => TOutput)(input)\n : updater\n}\n\nexport function isValidTimeout(value: unknown): value is number {\n return typeof value === 'number' && value >= 0 && value !== Infinity\n}\n\nexport function timeUntilStale(updatedAt: number, staleTime?: number): number {\n return Math.max(updatedAt + (staleTime || 0) - Date.now(), 0)\n}\n\nexport function resolveQueryValue<\n TValue,\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n value:\n | undefined\n | TValue\n | ((query: Query<TQueryFnData, TError, TData, TQueryKey>) => TValue),\n query: Query<TQueryFnData, TError, TData, TQueryKey>,\n): TValue | undefined {\n return typeof value === 'function'\n ? (\n value as (\n query: Query<TQueryFnData, TError, TData, TQueryKey>,\n ) => TValue\n )(query)\n : value\n}\n\nexport function matchQuery(\n filters: QueryFilters,\n query: Query<any, any, any, any>,\n): boolean {\n const {\n type = 'all',\n exact,\n fetchStatus,\n predicate,\n queryKey,\n stale,\n } = filters\n\n if (queryKey) {\n if (exact) {\n if (query.queryHash !== hashQueryKeyByOptions(queryKey, query.options)) {\n return false\n }\n } else if (!partialMatchKey(query.queryKey, queryKey)) {\n return false\n }\n }\n\n if (type !== 'all') {\n const isActive = query.isActive()\n if (type === 'active' && !isActive) {\n return false\n }\n if (type === 'inactive' && isActive) {\n return false\n }\n }\n\n if (typeof stale === 'boolean' && query.isStale() !== stale) {\n return false\n }\n\n if (fetchStatus && fetchStatus !== query.state.fetchStatus) {\n return false\n }\n\n if (predicate && !predicate(query)) {\n return false\n }\n\n return true\n}\n\nexport function matchMutation(\n filters: MutationFilters,\n mutation: Mutation<any, any>,\n): boolean {\n const { exact, status, predicate, mutationKey } = filters\n if (mutationKey) {\n if (!mutation.options.mutationKey) {\n return false\n }\n if (exact) {\n if (hashKey(mutation.options.mutationKey) !== hashKey(mutationKey)) {\n return false\n }\n } else if (!partialMatchKey(mutation.options.mutationKey, mutationKey)) {\n return false\n }\n }\n\n if (status && mutation.state.status !== status) {\n return false\n }\n\n if (predicate && !predicate(mutation)) {\n return false\n }\n\n return true\n}\n\nexport function hashQueryKeyByOptions<TQueryKey extends QueryKey = QueryKey>(\n queryKey: TQueryKey,\n options?: Pick<QueryOptions<any, any, any, any>, 'queryKeyHashFn'>,\n): string {\n const hashFn = options?.queryKeyHashFn || hashKey\n return hashFn(queryKey)\n}\n\n/**\n * Default query & mutation keys hash function.\n * Hashes the value into a stable hash.\n */\nexport function hashKey(queryKey: QueryKey | MutationKey): string {\n return JSON.stringify(queryKey, (_, val) =>\n isPlainObject(val)\n ? Object.keys(val)\n .sort()\n .reduce((result, key) => {\n result[key] = val[key]\n return result\n }, {} as any)\n : val,\n )\n}\n\n/**\n * Checks if key `b` partially matches with key `a`.\n */\nexport function partialMatchKey(a: QueryKey, b: QueryKey): boolean\nexport function partialMatchKey(a: any, b: any): boolean {\n if (a === b) {\n return true\n }\n\n if (typeof a !== typeof b) {\n return false\n }\n\n if (a && b && typeof a === 'object' && typeof b === 'object') {\n if (Array.isArray(a) && Array.isArray(b)) {\n for (let i = 0; i < b.length; i++) {\n if (!partialMatchKey(a[i], b[i])) {\n return false\n }\n }\n return true\n }\n\n const bKeys = Object.keys(b)\n for (const key of bKeys) {\n if (!partialMatchKey(a[key], b[key])) {\n return false\n }\n }\n return true\n }\n\n return false\n}\n\nconst hasOwn = Object.prototype.hasOwnProperty\n\n/**\n * This function returns `a` if `b` is deeply equal.\n * If not, it will replace any deeply equal children of `b` with those of `a`.\n * This can be used for structural sharing between JSON values for example.\n */\nexport function replaceEqualDeep<T>(a: unknown, b: T, depth?: number): T\nexport function replaceEqualDeep(a: any, b: any, depth = 0): any {\n if (a === b) {\n return a\n }\n\n if (depth > 500) return b\n\n const array = isPlainArray(a) && isPlainArray(b)\n\n if (!array && !(isPlainObject(a) && isPlainObject(b))) return b\n\n const aItems = array ? a : Object.keys(a)\n const aSize = aItems.length\n const bItems = array ? b : Object.keys(b)\n const bSize = bItems.length\n const copy: any = array ? new Array(bSize) : {}\n\n let equalItems = 0\n\n for (let i = 0; i < bSize; i++) {\n const key: any = array ? i : bItems[i]\n const aItem = a[key]\n const bItem = b[key]\n\n if (aItem === bItem) {\n copy[key] = aItem\n if (array ? i < aSize : hasOwn.call(a, key)) equalItems++\n continue\n }\n\n if (\n aItem === null ||\n bItem === null ||\n typeof aItem !== 'object' ||\n typeof bItem !== 'object'\n ) {\n copy[key] = bItem\n continue\n }\n\n const v = replaceEqualDeep(aItem, bItem, depth + 1)\n copy[key] = v\n if (v === aItem) equalItems++\n }\n\n return aSize === bSize && equalItems === aSize ? a : copy\n}\n\n/**\n * Shallow compare objects.\n */\nexport function shallowEqualObjects<T extends Record<string, any>>(\n a: T,\n b: T | undefined,\n): boolean {\n if (!b || Object.keys(a).length !== Object.keys(b).length) {\n return false\n }\n\n for (const key in a) {\n if (a[key] !== b[key]) {\n return false\n }\n }\n\n return true\n}\n\nexport function isPlainArray(value: unknown): value is Array<unknown> {\n return Array.isArray(value) && value.length === Object.keys(value).length\n}\n\n// Copied from: https://github.com/jonschlinkert/is-plain-object\nexport function isPlainObject(o: any): o is Record<PropertyKey, unknown> {\n if (!hasObjectPrototype(o)) {\n return false\n }\n\n // If has no constructor\n const ctor = o.constructor\n if (ctor === undefined) {\n return true\n }\n\n // If has modified prototype\n const prot = ctor.prototype\n if (!hasObjectPrototype(prot)) {\n return false\n }\n\n // If constructor does not have an Object-specific method\n if (!prot.hasOwnProperty('isPrototypeOf')) {\n return false\n }\n\n // Handles Objects created by Object.create(<arbitrary prototype>)\n if (Object.getPrototypeOf(o) !== Object.prototype) {\n return false\n }\n\n // Most likely a plain Object\n return true\n}\n\nfunction hasObjectPrototype(o: any): boolean {\n return Object.prototype.toString.call(o) === '[object Object]'\n}\n\nexport function sleep(timeout: number): Promise<void> {\n return new Promise((resolve) => {\n timeoutManager.setTimeout(resolve, timeout)\n })\n}\n\nexport function replaceData<\n TData,\n TOptions extends QueryOptions<any, any, any, any>,\n>(prevData: TData | undefined, data: TData, options: TOptions): TData {\n if (typeof options.structuralSharing === 'function') {\n return options.structuralSharing(prevData, data) as TData\n } else if (options.structuralSharing !== false) {\n if (process.env.NODE_ENV !== 'production') {\n try {\n return replaceEqualDeep(prevData, data)\n } catch (error) {\n console.error(\n `Structural sharing requires data to be JSON serializable. To fix this, turn off structuralSharing or return JSON-serializable data from your queryFn. [${options.queryHash}]: ${error}`,\n )\n\n // Prevent the replaceEqualDeep from being called again down below.\n throw error\n }\n }\n // Structurally share data between prev and new data if needed\n return replaceEqualDeep(prevData, data)\n }\n return data\n}\n\nexport function keepPreviousData<T>(\n previousData: T | undefined,\n): T | undefined {\n return previousData\n}\n\nexport function addToEnd<T>(items: Array<T>, item: T, max = 0): Array<T> {\n const newItems = [...items, item]\n return max && newItems.length > max ? newItems.slice(1) : newItems\n}\n\nexport function addToStart<T>(items: Array<T>, item: T, max = 0): Array<T> {\n const newItems = [item, ...items]\n return max && newItems.length > max ? newItems.slice(0, -1) : newItems\n}\n\nexport const skipToken = Symbol()\nexport type SkipToken = typeof skipToken\n\nexport function ensureQueryFn<\n TQueryFnData = unknown,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: {\n queryFn?: QueryFunction<TQueryFnData, TQueryKey> | SkipToken\n queryHash?: string\n },\n fetchOptions?: FetchOptions<TQueryFnData>,\n): QueryFunction<TQueryFnData, TQueryKey> {\n if (process.env.NODE_ENV !== 'production') {\n if (options.queryFn === skipToken) {\n console.error(\n `Attempted to invoke queryFn when set to skipToken. This is likely a configuration error. Query hash: '${options.queryHash}'`,\n )\n }\n }\n\n // if we attempt to retry a fetch that was triggered from an initialPromise\n // when we don't have a queryFn yet, we can't retry, so we just return the already rejected initialPromise\n // if an observer has already mounted, we will be able to retry with that queryFn\n if (!options.queryFn && fetchOptions?.initialPromise) {\n return () => fetchOptions.initialPromise!\n }\n\n if (!options.queryFn || options.queryFn === skipToken) {\n return () =>\n Promise.reject(new Error(`Missing queryFn: '${options.queryHash}'`))\n }\n\n return options.queryFn\n}\n\nexport function shouldThrowError<T extends (...args: Array<any>) => boolean>(\n throwOnError: boolean | T | undefined,\n params: Parameters<T>,\n): boolean {\n // Allow throwOnError function to override throwing behavior on a per-error basis\n if (typeof throwOnError === 'function') {\n return throwOnError(...params)\n }\n\n return !!throwOnError\n}\n\nexport function addConsumeAwareSignal<T>(\n object: T,\n getSignal: () => AbortSignal,\n onCancelled: VoidFunction,\n): T & { signal: AbortSignal } {\n let consumed = false\n let signal: AbortSignal | undefined\n\n Object.defineProperty(object, 'signal', {\n enumerable: true,\n get: () => {\n signal ??= getSignal()\n if (consumed) {\n return signal\n }\n\n consumed = true\n if (signal.aborted) {\n onCancelled()\n } else {\n signal.addEventListener('abort', onCancelled, { once: true })\n }\n\n return signal\n },\n })\n\n return object as T & { signal: AbortSignal }\n}\n"],"mappings":";;;;;AAwFA,MAAa,WAAW,OAAO,WAAW,eAAe,UAAU;AAInE,SAAgB,OAAO,CAAC;AAExB,SAAgB,iBACd,SACA,OACS;CACT,OAAO,OAAO,YAAY,aACrB,QAAmC,KAAK,IACzC;AACN;AAEA,SAAgB,eAAe,OAAiC;CAC9D,OAAO,OAAO,UAAU,YAAY,SAAS,KAAK,UAAU;AAC9D;AAEA,SAAgB,eAAe,WAAmB,WAA4B;CAC5E,OAAO,KAAK,IAAI,aAAa,aAAa,KAAK,KAAK,IAAI,GAAG,CAAC;AAC9D;AAEA,SAAgB,kBAOd,OAIA,OACoB;CACpB,OAAO,OAAO,UAAU,aAElB,MAGA,KAAK,IAC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,WACA,aAC6B;CAC7B,IAAI,WAAW;CACf,IAAI;CAEJ,OAAO,eAAe,QAAQ,UAAU;EACtC,YAAY;EACZ,WAAW;GACT,WAAA,SAAW,UAAU;GACrB,IAAI,UACF,OAAO;GAGT,WAAW;GACX,IAAI,OAAO,SACT,YAAY;QAEZ,OAAO,iBAAiB,SAAS,aAAa,EAAE,MAAM,KAAK,CAAC;GAG9D,OAAO;EACT;CACF,CAAC;CAED,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"utils.js","names":[],"sources":["../../src/utils.ts"],"sourcesContent":["import { timeoutManager } from './timeoutManager'\nimport type {\n DefaultError,\n FetchStatus,\n MutationKey,\n MutationStatus,\n QueryFunction,\n QueryKey,\n QueryOptions,\n} from './types'\nimport type { Mutation } from './mutation'\nimport type { FetchOptions, Query } from './query'\n\n// TYPES\n\ntype DropLast<T extends ReadonlyArray<unknown>> = T extends readonly [\n ...infer R,\n unknown,\n]\n ? readonly [...R]\n : never\n\ntype TuplePrefixes<T extends ReadonlyArray<unknown>> = T extends readonly []\n ? readonly []\n : TuplePrefixes<DropLast<T>> | T\n\n/**\n * Filters used to select queries, for example in `queryClient.getQueriesData` or `queryClient.invalidateQueries`.\n * All provided filters must match; filters that are left unspecified are ignored.\n */\nexport interface QueryFilters<TQueryKey extends QueryKey = QueryKey> {\n /**\n * Filter to active queries, inactive queries or all queries\n *\n * Defaults to `'all'`.\n */\n type?: QueryTypeFilter\n /**\n * Match query key exactly\n */\n exact?: boolean\n /**\n * Include queries matching this predicate function\n */\n predicate?: (query: Query) => boolean\n /**\n * Include queries matching this query key\n */\n queryKey?: TQueryKey | TuplePrefixes<TQueryKey>\n /**\n * Include or exclude stale queries\n */\n stale?: boolean\n /**\n * Include queries matching their fetchStatus\n */\n fetchStatus?: FetchStatus\n}\n\n/**\n * Filters used to select mutations, for example in `mutationCache.findAll` or `queryClient.isMutating`.\n * All provided filters must match; filters that are left unspecified are ignored.\n */\nexport interface MutationFilters<\n TData = unknown,\n TError = DefaultError,\n TVariables = unknown,\n TOnMutateResult = unknown,\n> {\n /**\n * Match mutation key exactly\n */\n exact?: boolean\n /**\n * Include mutations matching this predicate function\n */\n predicate?: (\n mutation: Mutation<TData, TError, TVariables, TOnMutateResult>,\n ) => boolean\n /**\n * Include mutations matching this mutation key\n */\n mutationKey?: TuplePrefixes<MutationKey>\n /**\n * Filter by mutation status\n */\n status?: MutationStatus\n}\n\n/**\n * Either a plain value of type `TOutput`, or a function that receives `TInput` and returns `TOutput`.\n * Used for example by `setQueryData`-style updaters, which accept either the new data directly or a\n * function that computes it from the previous data. See {@link functionalUpdate}.\n *\n * @example\n * ```ts\n * queryClient.setQueryData(['posts'], newPosts)\n *\n * // Or, using an updater function that receives the current data:\n * queryClient.setQueryData(['posts'], (oldPosts) =>\n * oldPosts ? [...oldPosts, newPost] : oldPosts,\n * )\n * ```\n */\nexport type Updater<TInput, TOutput> = TOutput | ((input: TInput) => TOutput)\n\nexport type QueryTypeFilter = 'all' | 'active' | 'inactive'\n\n// UTILS\n\n/** @deprecated\n * use `environmentManager.isServer()` instead.\n */\nexport const isServer = typeof window === 'undefined' || 'Deno' in globalThis\n\n/**\n * A function that does nothing.\n */\nexport function noop(): void\nexport function noop(): undefined\nexport function noop() {}\n\nexport function functionalUpdate<TInput, TOutput>(\n updater: Updater<TInput, TOutput>,\n input: TInput,\n): TOutput {\n return typeof updater === 'function'\n ? (updater as (_: TInput) => TOutput)(input)\n : updater\n}\n\nexport function isValidTimeout(value: unknown): value is number {\n return typeof value === 'number' && value >= 0 && value !== Infinity\n}\n\nexport function timeUntilStale(updatedAt: number, staleTime?: number): number {\n return Math.max(updatedAt + (staleTime || 0) - Date.now(), 0)\n}\n\nexport function resolveQueryValue<\n TValue,\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n value:\n | undefined\n | TValue\n | ((query: Query<TQueryFnData, TError, TData, TQueryKey>) => TValue),\n query: Query<TQueryFnData, TError, TData, TQueryKey>,\n): TValue | undefined {\n return typeof value === 'function'\n ? (\n value as (\n query: Query<TQueryFnData, TError, TData, TQueryKey>,\n ) => TValue\n )(query)\n : value\n}\n\n/**\n * Checks whether a query matches the given {@link QueryFilters}.\n * Every filter that is specified must match; filters that are left unspecified are ignored.\n *\n * @example\n * ```ts\n * const queryCache = queryClient.getQueryCache()\n *\n * const matchingQueries = queryCache\n * .getAll()\n * .filter((query) => matchQuery({ queryKey: ['posts'] }, query))\n * ```\n */\nexport function matchQuery(\n filters: QueryFilters,\n query: Query<any, any, any, any>,\n): boolean {\n const {\n type = 'all',\n exact,\n fetchStatus,\n predicate,\n queryKey,\n stale,\n } = filters\n\n if (queryKey) {\n if (exact) {\n if (query.queryHash !== hashQueryKeyByOptions(queryKey, query.options)) {\n return false\n }\n } else if (!partialMatchKey(query.queryKey, queryKey)) {\n return false\n }\n }\n\n if (type !== 'all') {\n const isActive = query.isActive()\n if (type === 'active' && !isActive) {\n return false\n }\n if (type === 'inactive' && isActive) {\n return false\n }\n }\n\n if (typeof stale === 'boolean' && query.isStale() !== stale) {\n return false\n }\n\n if (fetchStatus && fetchStatus !== query.state.fetchStatus) {\n return false\n }\n\n if (predicate && !predicate(query)) {\n return false\n }\n\n return true\n}\n\n/**\n * Checks whether a mutation matches the given {@link MutationFilters}.\n * Every filter that is specified must match; filters that are left unspecified are ignored.\n * If a `mutationKey` filter is provided but the mutation has no `mutationKey` of its own, it does not match.\n *\n * @example\n * ```ts\n * const mutationCache = queryClient.getMutationCache()\n *\n * const matchingMutations = mutationCache\n * .getAll()\n * .filter((mutation) => matchMutation({ mutationKey: ['addPost'] }, mutation))\n * ```\n */\nexport function matchMutation(\n filters: MutationFilters,\n mutation: Mutation<any, any>,\n): boolean {\n const { exact, status, predicate, mutationKey } = filters\n if (mutationKey) {\n if (!mutation.options.mutationKey) {\n return false\n }\n if (exact) {\n if (hashKey(mutation.options.mutationKey) !== hashKey(mutationKey)) {\n return false\n }\n } else if (!partialMatchKey(mutation.options.mutationKey, mutationKey)) {\n return false\n }\n }\n\n if (status && mutation.state.status !== status) {\n return false\n }\n\n if (predicate && !predicate(mutation)) {\n return false\n }\n\n return true\n}\n\nexport function hashQueryKeyByOptions<TQueryKey extends QueryKey = QueryKey>(\n queryKey: TQueryKey,\n options?: Pick<QueryOptions<any, any, any, any>, 'queryKeyHashFn'>,\n): string {\n const hashFn = options?.queryKeyHashFn || hashKey\n return hashFn(queryKey)\n}\n\n/**\n * Default query & mutation keys hash function.\n * Hashes the value into a stable hash.\n *\n * @example\n * ```ts\n * // Object keys are sorted, so key order doesn't affect the hash:\n * hashKey(['todos', { page: 1, filter: 'done' }]) // === '[\"todos\",{\"filter\":\"done\",\"page\":1}]'\n * ```\n */\nexport function hashKey(queryKey: QueryKey | MutationKey): string {\n return JSON.stringify(queryKey, (_, val) =>\n isPlainObject(val)\n ? Object.keys(val)\n .sort()\n .reduce((result, key) => {\n result[key] = val[key]\n return result\n }, {} as any)\n : val,\n )\n}\n\n/**\n * Checks if key `b` partially matches with key `a`.\n */\nexport function partialMatchKey(a: QueryKey, b: QueryKey): boolean\nexport function partialMatchKey(a: any, b: any): boolean {\n if (a === b) {\n return true\n }\n\n if (typeof a !== typeof b) {\n return false\n }\n\n if (a && b && typeof a === 'object' && typeof b === 'object') {\n if (Array.isArray(a) && Array.isArray(b)) {\n if (b.length > a.length) return false\n for (let i = 0; i < b.length; i++) {\n if (!partialMatchKey(a[i], b[i])) {\n return false\n }\n }\n return true\n }\n\n const bKeys = Object.keys(b)\n for (const key of bKeys) {\n if (!partialMatchKey(a[key], b[key])) {\n return false\n }\n }\n return true\n }\n\n return false\n}\n\nconst hasOwn = Object.prototype.hasOwnProperty\n\n/**\n * This function returns `a` if `b` is deeply equal.\n * If not, it will replace any deeply equal children of `b` with those of `a`.\n * This can be used for structural sharing between JSON values for example.\n */\nexport function replaceEqualDeep<T>(a: unknown, b: T, depth?: number): T\nexport function replaceEqualDeep(a: any, b: any, depth = 0): any {\n if (a === b) {\n return a\n }\n\n if (depth > 500) return b\n\n const array = isPlainArray(a) && isPlainArray(b)\n\n if (!array && !(isPlainObject(a) && isPlainObject(b))) return b\n\n const aItems = array ? a : Object.keys(a)\n const aSize = aItems.length\n const bItems = array ? b : Object.keys(b)\n const bSize = bItems.length\n const copy: any = array ? new Array(bSize) : {}\n\n let equalItems = 0\n\n for (let i = 0; i < bSize; i++) {\n const key: any = array ? i : bItems[i]\n const aItem = a[key]\n const bItem = b[key]\n\n if (aItem === bItem) {\n copy[key] = aItem\n if (array ? i < aSize : hasOwn.call(a, key)) equalItems++\n continue\n }\n\n if (\n aItem === null ||\n bItem === null ||\n typeof aItem !== 'object' ||\n typeof bItem !== 'object'\n ) {\n copy[key] = bItem\n continue\n }\n\n const v = replaceEqualDeep(aItem, bItem, depth + 1)\n copy[key] = v\n if (v === aItem) equalItems++\n }\n\n return aSize === bSize && equalItems === aSize ? a : copy\n}\n\n/**\n * Shallow compare objects.\n */\nexport function shallowEqualObjects<T extends Record<string, any>>(\n a: T,\n b: T | undefined,\n): boolean {\n if (!b || Object.keys(a).length !== Object.keys(b).length) {\n return false\n }\n\n for (const key in a) {\n if (a[key] !== b[key]) {\n return false\n }\n }\n\n return true\n}\n\nexport function isPlainArray(value: unknown): value is Array<unknown> {\n return Array.isArray(value) && value.length === Object.keys(value).length\n}\n\n// Copied from: https://github.com/jonschlinkert/is-plain-object\nexport function isPlainObject(o: any): o is Record<PropertyKey, unknown> {\n if (!hasObjectPrototype(o)) {\n return false\n }\n\n // If has no constructor\n const ctor = o.constructor\n if (ctor === undefined) {\n return true\n }\n\n // If has modified prototype\n const prot = ctor.prototype\n if (!hasObjectPrototype(prot)) {\n return false\n }\n\n // If constructor does not have an Object-specific method\n if (!prot.hasOwnProperty('isPrototypeOf')) {\n return false\n }\n\n // Handles Objects created by Object.create(<arbitrary prototype>)\n if (Object.getPrototypeOf(o) !== Object.prototype) {\n return false\n }\n\n // Most likely a plain Object\n return true\n}\n\nfunction hasObjectPrototype(o: any): boolean {\n return Object.prototype.toString.call(o) === '[object Object]'\n}\n\nexport function sleep(timeout: number): Promise<void> {\n return new Promise((resolve) => {\n timeoutManager.setTimeout(resolve, timeout)\n })\n}\n\nexport function replaceData<\n TData,\n TOptions extends QueryOptions<any, any, any, any>,\n>(prevData: TData | undefined, data: TData, options: TOptions): TData {\n if (typeof options.structuralSharing === 'function') {\n return options.structuralSharing(prevData, data) as TData\n } else if (options.structuralSharing !== false) {\n if (process.env.NODE_ENV !== 'production') {\n try {\n return replaceEqualDeep(prevData, data)\n } catch (error) {\n console.error(\n `Structural sharing requires data to be JSON serializable. To fix this, turn off structuralSharing or return JSON-serializable data from your queryFn. [${options.queryHash}]: ${error}`,\n )\n\n // Prevent the replaceEqualDeep from being called again down below.\n throw error\n }\n }\n // Structurally share data between prev and new data if needed\n return replaceEqualDeep(prevData, data)\n }\n return data\n}\n\n/**\n * Intended to be passed as a query's `placeholderData` option, for example\n * `placeholderData: keepPreviousData`. Instead of resetting the query's data to `undefined` while a new\n * query key is fetching, it keeps displaying the previously fetched data until the new data arrives.\n *\n * @example\n * ```ts\n * new QueryObserver(queryClient, {\n * queryKey: ['posts', page],\n * queryFn: () => fetchPosts(page),\n * placeholderData: keepPreviousData,\n * })\n * ```\n */\nexport function keepPreviousData<T>(\n previousData: T | undefined,\n): T | undefined {\n return previousData\n}\n\nexport function addToEnd<T>(items: Array<T>, item: T, max = 0): Array<T> {\n const newItems = [...items, item]\n return max && newItems.length > max ? newItems.slice(1) : newItems\n}\n\nexport function addToStart<T>(items: Array<T>, item: T, max = 0): Array<T> {\n const newItems = [item, ...items]\n return max && newItems.length > max ? newItems.slice(0, -1) : newItems\n}\n\n/**\n * Sentinel value that can be passed as a query's `queryFn` to conditionally disable the query (equivalent\n * to `enabled: false`) while preserving full type inference for the query's data. Unlike `enabled: false`,\n * a query disabled via `skipToken` cannot be triggered with `refetch`.\n *\n * @example\n * ```ts\n * new QueryObserver(queryClient, {\n * queryKey: ['post', postId],\n * queryFn: postId != null ? () => fetchPost(postId) : skipToken,\n * })\n * ```\n */\nexport const skipToken = Symbol()\n/**\n * The type of the {@link skipToken} sentinel value.\n */\nexport type SkipToken = typeof skipToken\n\nexport function ensureQueryFn<\n TQueryFnData = unknown,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: {\n queryFn?: QueryFunction<TQueryFnData, TQueryKey> | SkipToken\n queryHash?: string\n },\n fetchOptions?: FetchOptions<TQueryFnData>,\n): QueryFunction<TQueryFnData, TQueryKey> {\n if (process.env.NODE_ENV !== 'production') {\n if (options.queryFn === skipToken) {\n console.error(\n `Attempted to invoke queryFn when set to skipToken. This is likely a configuration error. Query hash: '${options.queryHash}'`,\n )\n }\n }\n\n // if we attempt to retry a fetch that was triggered from an initialPromise\n // when we don't have a queryFn yet, we can't retry, so we just return the already rejected initialPromise\n // if an observer has already mounted, we will be able to retry with that queryFn\n if (!options.queryFn && fetchOptions?.initialPromise) {\n return () => fetchOptions.initialPromise!\n }\n\n if (!options.queryFn || options.queryFn === skipToken) {\n return () =>\n Promise.reject(new Error(`Missing queryFn: '${options.queryHash}'`))\n }\n\n return options.queryFn\n}\n\n/**\n * Resolves a `throwOnError` option to a boolean.\n * If `throwOnError` is a function, it is called with `params` (e.g. the error and, depending on the caller,\n * additional context such as the query or mutation) and its result is returned, allowing the throwing\n * behavior to be decided per error. Otherwise, `throwOnError` itself is coerced to a boolean (`undefined`\n * resolves to `false`).\n *\n * @example\n * ```ts\n * const throwOnError =\n * query.state.error && typeof options.throwOnError === 'function'\n * ? shouldThrowError(options.throwOnError, [query.state.error, query])\n * : options.throwOnError\n * ```\n */\nexport function shouldThrowError<T extends (...args: Array<any>) => boolean>(\n throwOnError: boolean | T | undefined,\n params: Parameters<T>,\n): boolean {\n // Allow throwOnError function to override throwing behavior on a per-error basis\n if (typeof throwOnError === 'function') {\n return throwOnError(...params)\n }\n\n return !!throwOnError\n}\n\nexport function addConsumeAwareSignal<T>(\n object: T,\n getSignal: () => AbortSignal,\n onCancelled: VoidFunction,\n): T & { signal: AbortSignal } {\n let consumed = false\n let signal: AbortSignal | undefined\n\n Object.defineProperty(object, 'signal', {\n enumerable: true,\n get: () => {\n signal ??= getSignal()\n if (consumed) {\n return signal\n }\n\n consumed = true\n if (signal.aborted) {\n onCancelled()\n } else {\n signal.addEventListener('abort', onCancelled, { once: true })\n }\n\n return signal\n },\n })\n\n return object as T & { signal: AbortSignal }\n}\n"],"mappings":";;;;;AAiHA,MAAa,WAAW,OAAO,WAAW,eAAe,UAAU;AAOnE,SAAgB,OAAO,CAAC;AAExB,SAAgB,iBACd,SACA,OACS;CACT,OAAO,OAAO,YAAY,aACrB,QAAmC,KAAK,IACzC;AACN;AAEA,SAAgB,eAAe,OAAiC;CAC9D,OAAO,OAAO,UAAU,YAAY,SAAS,KAAK,UAAU;AAC9D;AAEA,SAAgB,eAAe,WAAmB,WAA4B;CAC5E,OAAO,KAAK,IAAI,aAAa,aAAa,KAAK,KAAK,IAAI,GAAG,CAAC;AAC9D;AAEA,SAAgB,kBAOd,OAIA,OACoB;CACpB,OAAO,OAAO,UAAU,aAElB,MAGA,KAAK,IACP;AACN;;;;;;;;;;;;;;AAeA,SAAgB,WACd,SACA,OACS;CACT,MAAM,EACJ,OAAO,OACP,OACA,aACA,WACA,UACA,UACE;CAEJ,IAAI,UAAU;EACZ,IAAI,OACE;OAAA,MAAM,cAAc,sBAAsB,UAAU,MAAM,OAAO,GACnE,OAAO;EAAA,OAEJ,IAAI,CAAC,gBAAgB,MAAM,UAAU,QAAQ,GAClD,OAAO;CAEX;CAEA,IAAI,SAAS,OAAO;EAClB,MAAM,WAAW,MAAM,SAAS;EAChC,IAAI,SAAS,YAAY,CAAC,UACxB,OAAO;EAET,IAAI,SAAS,cAAc,UACzB,OAAO;CAEX;CAEA,IAAI,OAAO,UAAU,aAAa,MAAM,QAAQ,MAAM,OACpD,OAAO;CAGT,IAAI,eAAe,gBAAgB,MAAM,MAAM,aAC7C,OAAO;CA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();
|
|
9
9
|
/**
|
|
10
|
-
* Manages
|
|
10
|
+
* Manages how TanStack Query detects whether the current runtime should be treated as
|
|
11
|
+
* server-side, which disables scheduling refetch timers and changes the default `retry` count
|
|
12
|
+
* and `gcTime`. By default, the detection treats a missing `window` (or the presence of a
|
|
13
|
+
* `Deno` global) as server.
|
|
14
|
+
*
|
|
15
|
+
* Override this for runtimes where that default detection would give the wrong answer — for
|
|
16
|
+
* example, a Service Worker, where `window` is undefined even though the environment should
|
|
17
|
+
* behave like a client.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { environmentManager } from '@tanstack/query-core'
|
|
22
|
+
*
|
|
23
|
+
* environmentManager.setIsServer(() => false)
|
|
24
|
+
* ```
|
|
11
25
|
*/
|
|
12
26
|
const environmentManager = {
|
|
13
27
|
isServer,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"environmentManager.cjs","names":["defaultIsServer"],"sources":["../../src/environmentManager.ts"],"sourcesContent":["import { isServer as defaultIsServer } from './utils'\n\nexport type IsServerValue = () => boolean\n\nlet isServerFn: IsServerValue = () => defaultIsServer\n\n/**\n * Returns whether the current runtime should be treated as a server environment.\n */\nexport const isServer = (): boolean => isServerFn()\n\n/**\n * Manages
|
|
1
|
+
{"version":3,"file":"environmentManager.cjs","names":["defaultIsServer"],"sources":["../../src/environmentManager.ts"],"sourcesContent":["import { isServer as defaultIsServer } from './utils'\n\nexport type IsServerValue = () => boolean\n\nlet isServerFn: IsServerValue = () => defaultIsServer\n\n/**\n * Returns whether the current runtime should be treated as a server environment.\n */\nexport const isServer = (): boolean => isServerFn()\n\n/**\n * Manages how TanStack Query detects whether the current runtime should be treated as\n * server-side, which disables scheduling refetch timers and changes the default `retry` count\n * and `gcTime`. By default, the detection treats a missing `window` (or the presence of a\n * `Deno` global) as server.\n *\n * Override this for runtimes where that default detection would give the wrong answer — for\n * example, a Service Worker, where `window` is undefined even though the environment should\n * behave like a client.\n *\n * @example\n * ```ts\n * import { environmentManager } from '@tanstack/query-core'\n *\n * environmentManager.setIsServer(() => false)\n * ```\n */\nexport const environmentManager = {\n isServer,\n /**\n * Overrides the server check globally.\n */\n setIsServer(isServerValue: IsServerValue): void {\n isServerFn = isServerValue\n },\n}\n"],"mappings":";;;AAIA,IAAI,mBAAkCA,cAAAA;;;;AAKtC,MAAa,iBAA0B,WAAW;;;;;;;;;;;;;;;;;;AAmBlD,MAAa,qBAAqB;CAChC;;;;CAIA,YAAY,eAAoC;EAC9C,aAAa;CACf;AACF"}
|
|
@@ -5,7 +5,21 @@ type IsServerValue = () => boolean;
|
|
|
5
5
|
*/
|
|
6
6
|
declare const isServer: () => boolean;
|
|
7
7
|
/**
|
|
8
|
-
* Manages
|
|
8
|
+
* Manages how TanStack Query detects whether the current runtime should be treated as
|
|
9
|
+
* server-side, which disables scheduling refetch timers and changes the default `retry` count
|
|
10
|
+
* and `gcTime`. By default, the detection treats a missing `window` (or the presence of a
|
|
11
|
+
* `Deno` global) as server.
|
|
12
|
+
*
|
|
13
|
+
* Override this for runtimes where that default detection would give the wrong answer — for
|
|
14
|
+
* example, a Service Worker, where `window` is undefined even though the environment should
|
|
15
|
+
* behave like a client.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { environmentManager } from '@tanstack/query-core'
|
|
20
|
+
*
|
|
21
|
+
* environmentManager.setIsServer(() => false)
|
|
22
|
+
* ```
|
|
9
23
|
*/
|
|
10
24
|
declare const environmentManager: {
|
|
11
25
|
isServer: () => boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"environmentManager.d.cts","names":[],"sources":["../../src/environmentManager.ts"],"mappings":";KAEY;;;;cAOC
|
|
1
|
+
{"version":3,"file":"environmentManager.d.cts","names":[],"sources":["../../src/environmentManager.ts"],"mappings":";KAEY;;;;cAOC;;;;;;;;;;;;;;;;;;cAmBA;EAnBe;;;;EAwBC,YAAA,eAAA"}
|
|
@@ -5,7 +5,21 @@ type IsServerValue = () => boolean;
|
|
|
5
5
|
*/
|
|
6
6
|
declare const isServer: () => boolean;
|
|
7
7
|
/**
|
|
8
|
-
* Manages
|
|
8
|
+
* Manages how TanStack Query detects whether the current runtime should be treated as
|
|
9
|
+
* server-side, which disables scheduling refetch timers and changes the default `retry` count
|
|
10
|
+
* and `gcTime`. By default, the detection treats a missing `window` (or the presence of a
|
|
11
|
+
* `Deno` global) as server.
|
|
12
|
+
*
|
|
13
|
+
* Override this for runtimes where that default detection would give the wrong answer — for
|
|
14
|
+
* example, a Service Worker, where `window` is undefined even though the environment should
|
|
15
|
+
* behave like a client.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { environmentManager } from '@tanstack/query-core'
|
|
20
|
+
*
|
|
21
|
+
* environmentManager.setIsServer(() => false)
|
|
22
|
+
* ```
|
|
9
23
|
*/
|
|
10
24
|
declare const environmentManager: {
|
|
11
25
|
isServer: () => boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"environmentManager.d.ts","names":[],"sources":["../../src/environmentManager.ts"],"mappings":";KAEY;;;;cAOC
|
|
1
|
+
{"version":3,"file":"environmentManager.d.ts","names":[],"sources":["../../src/environmentManager.ts"],"mappings":";KAEY;;;;cAOC;;;;;;;;;;;;;;;;;;cAmBA;EAnBe;;;;EAwBC,YAAA,eAAA"}
|
|
@@ -6,7 +6,21 @@ let isServerFn = () => isServer$1;
|
|
|
6
6
|
*/
|
|
7
7
|
const isServer = () => isServerFn();
|
|
8
8
|
/**
|
|
9
|
-
* Manages
|
|
9
|
+
* Manages how TanStack Query detects whether the current runtime should be treated as
|
|
10
|
+
* server-side, which disables scheduling refetch timers and changes the default `retry` count
|
|
11
|
+
* and `gcTime`. By default, the detection treats a missing `window` (or the presence of a
|
|
12
|
+
* `Deno` global) as server.
|
|
13
|
+
*
|
|
14
|
+
* Override this for runtimes where that default detection would give the wrong answer — for
|
|
15
|
+
* example, a Service Worker, where `window` is undefined even though the environment should
|
|
16
|
+
* behave like a client.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { environmentManager } from '@tanstack/query-core'
|
|
21
|
+
*
|
|
22
|
+
* environmentManager.setIsServer(() => false)
|
|
23
|
+
* ```
|
|
10
24
|
*/
|
|
11
25
|
const environmentManager = {
|
|
12
26
|
isServer,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"environmentManager.js","names":["defaultIsServer"],"sources":["../../src/environmentManager.ts"],"sourcesContent":["import { isServer as defaultIsServer } from './utils'\n\nexport type IsServerValue = () => boolean\n\nlet isServerFn: IsServerValue = () => defaultIsServer\n\n/**\n * Returns whether the current runtime should be treated as a server environment.\n */\nexport const isServer = (): boolean => isServerFn()\n\n/**\n * Manages
|
|
1
|
+
{"version":3,"file":"environmentManager.js","names":["defaultIsServer"],"sources":["../../src/environmentManager.ts"],"sourcesContent":["import { isServer as defaultIsServer } from './utils'\n\nexport type IsServerValue = () => boolean\n\nlet isServerFn: IsServerValue = () => defaultIsServer\n\n/**\n * Returns whether the current runtime should be treated as a server environment.\n */\nexport const isServer = (): boolean => isServerFn()\n\n/**\n * Manages how TanStack Query detects whether the current runtime should be treated as\n * server-side, which disables scheduling refetch timers and changes the default `retry` count\n * and `gcTime`. By default, the detection treats a missing `window` (or the presence of a\n * `Deno` global) as server.\n *\n * Override this for runtimes where that default detection would give the wrong answer — for\n * example, a Service Worker, where `window` is undefined even though the environment should\n * behave like a client.\n *\n * @example\n * ```ts\n * import { environmentManager } from '@tanstack/query-core'\n *\n * environmentManager.setIsServer(() => false)\n * ```\n */\nexport const environmentManager = {\n isServer,\n /**\n * Overrides the server check globally.\n */\n setIsServer(isServerValue: IsServerValue): void {\n isServerFn = isServerValue\n },\n}\n"],"mappings":";;AAIA,IAAI,mBAAkCA;;;;AAKtC,MAAa,iBAA0B,WAAW;;;;;;;;;;;;;;;;;;AAmBlD,MAAa,qBAAqB;CAChC;;;;CAIA,YAAY,eAAoC;EAC9C,aAAa;CACf;AACF"}
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_subscribable = require("./subscribable.cjs");
|
|
3
3
|
//#region src/focusManager.ts
|
|
4
|
+
/**
|
|
5
|
+
* The `FocusManager` manages the focus state within TanStack Query.
|
|
6
|
+
*
|
|
7
|
+
* It can be used to change the default event listeners or to manually change the focus state.
|
|
8
|
+
*/
|
|
4
9
|
var FocusManager = class extends require_subscribable.Subscribable {
|
|
5
10
|
#focused;
|
|
6
11
|
#cleanup;
|
|
@@ -26,6 +31,31 @@ var FocusManager = class extends require_subscribable.Subscribable {
|
|
|
26
31
|
this.#cleanup = void 0;
|
|
27
32
|
}
|
|
28
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* `setEventListener` can be used to set a custom event listener that will
|
|
36
|
+
* be used to determine the focus state. The provided `setup` function
|
|
37
|
+
* receives a `setFocused` callback: call it with a `boolean` to manually
|
|
38
|
+
* set the focus state, or with no arguments to re-evaluate the current
|
|
39
|
+
* focus state and notify subscribers.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* import { focusManager } from '@tanstack/query-core'
|
|
44
|
+
*
|
|
45
|
+
* focusManager.setEventListener((handleFocus) => {
|
|
46
|
+
* const listener = () => handleFocus()
|
|
47
|
+
* // Listen to visibilitychange
|
|
48
|
+
* if (typeof window !== 'undefined' && window.addEventListener) {
|
|
49
|
+
* window.addEventListener('visibilitychange', listener, false)
|
|
50
|
+
* }
|
|
51
|
+
*
|
|
52
|
+
* return () => {
|
|
53
|
+
* // Be sure to unsubscribe if a new handler is set
|
|
54
|
+
* window.removeEventListener('visibilitychange', listener)
|
|
55
|
+
* }
|
|
56
|
+
* })
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
29
59
|
setEventListener(setup) {
|
|
30
60
|
this.#setup = setup;
|
|
31
61
|
this.#cleanup?.();
|
|
@@ -34,23 +64,50 @@ var FocusManager = class extends require_subscribable.Subscribable {
|
|
|
34
64
|
else this.onFocus();
|
|
35
65
|
});
|
|
36
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* `setFocused` can be used to manually set the focus state. Set `undefined`
|
|
69
|
+
* to fall back to the default focus check.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* import { focusManager } from '@tanstack/query-core'
|
|
74
|
+
*
|
|
75
|
+
* // Set focused
|
|
76
|
+
* focusManager.setFocused(true)
|
|
77
|
+
*
|
|
78
|
+
* // Set unfocused
|
|
79
|
+
* focusManager.setFocused(false)
|
|
80
|
+
*
|
|
81
|
+
* // Fallback to the default focus check
|
|
82
|
+
* focusManager.setFocused(undefined)
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
37
85
|
setFocused(focused) {
|
|
38
86
|
if (this.#focused !== focused) {
|
|
39
87
|
this.#focused = focused;
|
|
40
88
|
this.onFocus();
|
|
41
89
|
}
|
|
42
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* `onFocus` notifies all subscribed listeners with the current focus state.
|
|
93
|
+
*/
|
|
43
94
|
onFocus() {
|
|
44
95
|
const isFocused = this.isFocused();
|
|
45
96
|
this.listeners.forEach((listener) => {
|
|
46
97
|
listener(isFocused);
|
|
47
98
|
});
|
|
48
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* `isFocused` can be used to get the current focus state.
|
|
102
|
+
*/
|
|
49
103
|
isFocused() {
|
|
50
104
|
if (typeof this.#focused === "boolean") return this.#focused;
|
|
51
105
|
return globalThis.document?.visibilityState !== "hidden";
|
|
52
106
|
}
|
|
53
107
|
};
|
|
108
|
+
/**
|
|
109
|
+
* Singleton instance of {@link FocusManager}, used to manage and observe the focus state within TanStack Query.
|
|
110
|
+
*/
|
|
54
111
|
const focusManager = new FocusManager();
|
|
55
112
|
//#endregion
|
|
56
113
|
exports.FocusManager = FocusManager;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"focusManager.cjs","names":["Subscribable","#setup","#cleanup","#focused"],"sources":["../../src/focusManager.ts"],"sourcesContent":["import { Subscribable } from './subscribable'\n\ntype Listener = (focused: boolean) => void\n\ntype SetupFn = (\n setFocused: (focused?: boolean) => void,\n) => (() => void) | undefined\n\nexport class FocusManager extends Subscribable<Listener> {\n #focused?: boolean\n #cleanup?: () => void\n\n #setup: SetupFn\n\n constructor() {\n super()\n this.#setup = (onFocus) => {\n // addEventListener does not exist in React Native, but window does\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (typeof window !== 'undefined' && window.addEventListener) {\n const listener = () => onFocus()\n // Listen to visibilitychange\n window.addEventListener('visibilitychange', listener, false)\n\n return () => {\n // Be sure to unsubscribe if a new handler is set\n window.removeEventListener('visibilitychange', listener)\n }\n }\n return\n }\n }\n\n protected onSubscribe(): void {\n if (!this.#cleanup) {\n this.setEventListener(this.#setup)\n }\n }\n\n protected onUnsubscribe() {\n if (!this.hasListeners()) {\n this.#cleanup?.()\n this.#cleanup = undefined\n }\n }\n\n setEventListener(setup: SetupFn): void {\n this.#setup = setup\n this.#cleanup?.()\n this.#cleanup = setup((focused) => {\n if (typeof focused === 'boolean') {\n this.setFocused(focused)\n } else {\n this.onFocus()\n }\n })\n }\n\n setFocused(focused?: boolean): void {\n const changed = this.#focused !== focused\n if (changed) {\n this.#focused = focused\n this.onFocus()\n }\n }\n\n onFocus(): void {\n const isFocused = this.isFocused()\n this.listeners.forEach((listener) => {\n listener(isFocused)\n })\n }\n\n isFocused(): boolean {\n if (typeof this.#focused === 'boolean') {\n return this.#focused\n }\n\n // document global can be unavailable in react native\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n return globalThis.document?.visibilityState !== 'hidden'\n }\n}\n\nexport const focusManager = new FocusManager()\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"focusManager.cjs","names":["Subscribable","#setup","#cleanup","#focused"],"sources":["../../src/focusManager.ts"],"sourcesContent":["import { Subscribable } from './subscribable'\n\ntype Listener = (focused: boolean) => void\n\ntype SetupFn = (\n setFocused: (focused?: boolean) => void,\n) => (() => void) | undefined\n\n/**\n * The `FocusManager` manages the focus state within TanStack Query.\n *\n * It can be used to change the default event listeners or to manually change the focus state.\n */\nexport class FocusManager extends Subscribable<Listener> {\n #focused?: boolean\n #cleanup?: () => void\n\n #setup: SetupFn\n\n constructor() {\n super()\n this.#setup = (onFocus) => {\n // addEventListener does not exist in React Native, but window does\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (typeof window !== 'undefined' && window.addEventListener) {\n const listener = () => onFocus()\n // Listen to visibilitychange\n window.addEventListener('visibilitychange', listener, false)\n\n return () => {\n // Be sure to unsubscribe if a new handler is set\n window.removeEventListener('visibilitychange', listener)\n }\n }\n return\n }\n }\n\n protected onSubscribe(): void {\n if (!this.#cleanup) {\n this.setEventListener(this.#setup)\n }\n }\n\n protected onUnsubscribe() {\n if (!this.hasListeners()) {\n this.#cleanup?.()\n this.#cleanup = undefined\n }\n }\n\n /**\n * `setEventListener` can be used to set a custom event listener that will\n * be used to determine the focus state. The provided `setup` function\n * receives a `setFocused` callback: call it with a `boolean` to manually\n * set the focus state, or with no arguments to re-evaluate the current\n * focus state and notify subscribers.\n *\n * @example\n * ```ts\n * import { focusManager } from '@tanstack/query-core'\n *\n * focusManager.setEventListener((handleFocus) => {\n * const listener = () => handleFocus()\n * // Listen to visibilitychange\n * if (typeof window !== 'undefined' && window.addEventListener) {\n * window.addEventListener('visibilitychange', listener, false)\n * }\n *\n * return () => {\n * // Be sure to unsubscribe if a new handler is set\n * window.removeEventListener('visibilitychange', listener)\n * }\n * })\n * ```\n */\n setEventListener(setup: SetupFn): void {\n this.#setup = setup\n this.#cleanup?.()\n this.#cleanup = setup((focused) => {\n if (typeof focused === 'boolean') {\n this.setFocused(focused)\n } else {\n this.onFocus()\n }\n })\n }\n\n /**\n * `setFocused` can be used to manually set the focus state. Set `undefined`\n * to fall back to the default focus check.\n *\n * @example\n * ```ts\n * import { focusManager } from '@tanstack/query-core'\n *\n * // Set focused\n * focusManager.setFocused(true)\n *\n * // Set unfocused\n * focusManager.setFocused(false)\n *\n * // Fallback to the default focus check\n * focusManager.setFocused(undefined)\n * ```\n */\n setFocused(focused?: boolean): void {\n const changed = this.#focused !== focused\n if (changed) {\n this.#focused = focused\n this.onFocus()\n }\n }\n\n /**\n * `onFocus` notifies all subscribed listeners with the current focus state.\n */\n onFocus(): void {\n const isFocused = this.isFocused()\n this.listeners.forEach((listener) => {\n listener(isFocused)\n })\n }\n\n /**\n * `isFocused` can be used to get the current focus state.\n */\n isFocused(): boolean {\n if (typeof this.#focused === 'boolean') {\n return this.#focused\n }\n\n // document global can be unavailable in react native\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n return globalThis.document?.visibilityState !== 'hidden'\n }\n}\n\n/**\n * Singleton instance of {@link FocusManager}, used to manage and observe the focus state within TanStack Query.\n */\nexport const focusManager = new FocusManager()\n"],"mappings":";;;;;;;;AAaA,IAAa,eAAb,cAAkCA,qBAAAA,aAAuB;CACvD;CACA;CAEA;CAEA,cAAc;EACZ,MAAM;EACN,KAAKC,UAAU,YAAY;GAGzB,IAAI,OAAO,WAAW,eAAe,OAAO,kBAAkB;IAC5D,MAAM,iBAAiB,QAAQ;IAE/B,OAAO,iBAAiB,oBAAoB,UAAU,KAAK;IAE3D,aAAa;KAEX,OAAO,oBAAoB,oBAAoB,QAAQ;IACzD;GACF;EAEF;CACF;CAEA,cAA8B;EAC5B,IAAI,CAAC,KAAKC,UACR,KAAK,iBAAiB,KAAKD,MAAM;CAErC;CAEA,gBAA0B;EACxB,IAAI,CAAC,KAAK,aAAa,GAAG;GACxB,KAAKC,WAAW;GAChB,KAAKA,WAAW,KAAA;EAClB;CACF;;;;;;;;;;;;;;;;;;;;;;;;;;CA2BA,iBAAiB,OAAsB;EACrC,KAAKD,SAAS;EACd,KAAKC,WAAW;EAChB,KAAKA,WAAW,OAAO,YAAY;GACjC,IAAI,OAAO,YAAY,WACrB,KAAK,WAAW,OAAO;QAEvB,KAAK,QAAQ;EAEjB,CAAC;CACH;;;;;;;;;;;;;;;;;;;CAoBA,WAAW,SAAyB;EAElC,IADgB,KAAKC,aAAa,SACrB;GACX,KAAKA,WAAW;GAChB,KAAK,QAAQ;EACf;CACF;;;;CAKA,UAAgB;EACd,MAAM,YAAY,KAAK,UAAU;EACjC,KAAK,UAAU,SAAS,aAAa;GACnC,SAAS,SAAS;EACpB,CAAC;CACH;;;;CAKA,YAAqB;EACnB,IAAI,OAAO,KAAKA,aAAa,WAC3B,OAAO,KAAKA;EAKd,OAAO,WAAW,UAAU,oBAAoB;CAClD;AACF;;;;AAKA,MAAa,eAAe,IAAI,aAAa"}
|
|
@@ -2,16 +2,73 @@ import { t as Subscribable } from "./subscribable-CbifVTKz.cjs";
|
|
|
2
2
|
//#region src/focusManager.d.ts
|
|
3
3
|
type Listener = (focused: boolean) => void;
|
|
4
4
|
type SetupFn = (setFocused: (focused?: boolean) => void) => (() => void) | undefined;
|
|
5
|
+
/**
|
|
6
|
+
* The `FocusManager` manages the focus state within TanStack Query.
|
|
7
|
+
*
|
|
8
|
+
* It can be used to change the default event listeners or to manually change the focus state.
|
|
9
|
+
*/
|
|
5
10
|
declare class FocusManager extends Subscribable<Listener> {
|
|
6
11
|
#private;
|
|
7
12
|
constructor();
|
|
8
13
|
protected onSubscribe(): void;
|
|
9
14
|
protected onUnsubscribe(): void;
|
|
15
|
+
/**
|
|
16
|
+
* `setEventListener` can be used to set a custom event listener that will
|
|
17
|
+
* be used to determine the focus state. The provided `setup` function
|
|
18
|
+
* receives a `setFocused` callback: call it with a `boolean` to manually
|
|
19
|
+
* set the focus state, or with no arguments to re-evaluate the current
|
|
20
|
+
* focus state and notify subscribers.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { focusManager } from '@tanstack/query-core'
|
|
25
|
+
*
|
|
26
|
+
* focusManager.setEventListener((handleFocus) => {
|
|
27
|
+
* const listener = () => handleFocus()
|
|
28
|
+
* // Listen to visibilitychange
|
|
29
|
+
* if (typeof window !== 'undefined' && window.addEventListener) {
|
|
30
|
+
* window.addEventListener('visibilitychange', listener, false)
|
|
31
|
+
* }
|
|
32
|
+
*
|
|
33
|
+
* return () => {
|
|
34
|
+
* // Be sure to unsubscribe if a new handler is set
|
|
35
|
+
* window.removeEventListener('visibilitychange', listener)
|
|
36
|
+
* }
|
|
37
|
+
* })
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
10
40
|
setEventListener(setup: SetupFn): void;
|
|
41
|
+
/**
|
|
42
|
+
* `setFocused` can be used to manually set the focus state. Set `undefined`
|
|
43
|
+
* to fall back to the default focus check.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* import { focusManager } from '@tanstack/query-core'
|
|
48
|
+
*
|
|
49
|
+
* // Set focused
|
|
50
|
+
* focusManager.setFocused(true)
|
|
51
|
+
*
|
|
52
|
+
* // Set unfocused
|
|
53
|
+
* focusManager.setFocused(false)
|
|
54
|
+
*
|
|
55
|
+
* // Fallback to the default focus check
|
|
56
|
+
* focusManager.setFocused(undefined)
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
11
59
|
setFocused(focused?: boolean): void;
|
|
60
|
+
/**
|
|
61
|
+
* `onFocus` notifies all subscribed listeners with the current focus state.
|
|
62
|
+
*/
|
|
12
63
|
onFocus(): void;
|
|
64
|
+
/**
|
|
65
|
+
* `isFocused` can be used to get the current focus state.
|
|
66
|
+
*/
|
|
13
67
|
isFocused(): boolean;
|
|
14
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Singleton instance of {@link FocusManager}, used to manage and observe the focus state within TanStack Query.
|
|
71
|
+
*/
|
|
15
72
|
declare const focusManager: FocusManager;
|
|
16
73
|
//#endregion
|
|
17
74
|
export { FocusManager, focusManager };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"focusManager.d.cts","names":[],"sources":["../../src/focusManager.ts"],"mappings":";;KAEK,YAAY;KAEZ,WACH,aAAa
|
|
1
|
+
{"version":3,"file":"focusManager.d.cts","names":[],"sources":["../../src/focusManager.ts"],"mappings":";;KAEK,YAAY;KAEZ,WACH,aAAa;;;;;;cAQF,qBAAqB,aAAa;;;YAyBnC;YAMA;;;;;;;;;;;;;;;;;;;;;;;;;;EAgCV,iBAAiB,OAAO;;;;;;;;;;;;;;;;;;;EA8BxB,WAAW;;;;EAWX;;;;EAUA;;;;;cAcW,cAAY"}
|
|
@@ -2,16 +2,73 @@ import { t as Subscribable } from "./subscribable-CbifVTKz.js";
|
|
|
2
2
|
//#region src/focusManager.d.ts
|
|
3
3
|
type Listener = (focused: boolean) => void;
|
|
4
4
|
type SetupFn = (setFocused: (focused?: boolean) => void) => (() => void) | undefined;
|
|
5
|
+
/**
|
|
6
|
+
* The `FocusManager` manages the focus state within TanStack Query.
|
|
7
|
+
*
|
|
8
|
+
* It can be used to change the default event listeners or to manually change the focus state.
|
|
9
|
+
*/
|
|
5
10
|
declare class FocusManager extends Subscribable<Listener> {
|
|
6
11
|
#private;
|
|
7
12
|
constructor();
|
|
8
13
|
protected onSubscribe(): void;
|
|
9
14
|
protected onUnsubscribe(): void;
|
|
15
|
+
/**
|
|
16
|
+
* `setEventListener` can be used to set a custom event listener that will
|
|
17
|
+
* be used to determine the focus state. The provided `setup` function
|
|
18
|
+
* receives a `setFocused` callback: call it with a `boolean` to manually
|
|
19
|
+
* set the focus state, or with no arguments to re-evaluate the current
|
|
20
|
+
* focus state and notify subscribers.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { focusManager } from '@tanstack/query-core'
|
|
25
|
+
*
|
|
26
|
+
* focusManager.setEventListener((handleFocus) => {
|
|
27
|
+
* const listener = () => handleFocus()
|
|
28
|
+
* // Listen to visibilitychange
|
|
29
|
+
* if (typeof window !== 'undefined' && window.addEventListener) {
|
|
30
|
+
* window.addEventListener('visibilitychange', listener, false)
|
|
31
|
+
* }
|
|
32
|
+
*
|
|
33
|
+
* return () => {
|
|
34
|
+
* // Be sure to unsubscribe if a new handler is set
|
|
35
|
+
* window.removeEventListener('visibilitychange', listener)
|
|
36
|
+
* }
|
|
37
|
+
* })
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
10
40
|
setEventListener(setup: SetupFn): void;
|
|
41
|
+
/**
|
|
42
|
+
* `setFocused` can be used to manually set the focus state. Set `undefined`
|
|
43
|
+
* to fall back to the default focus check.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* import { focusManager } from '@tanstack/query-core'
|
|
48
|
+
*
|
|
49
|
+
* // Set focused
|
|
50
|
+
* focusManager.setFocused(true)
|
|
51
|
+
*
|
|
52
|
+
* // Set unfocused
|
|
53
|
+
* focusManager.setFocused(false)
|
|
54
|
+
*
|
|
55
|
+
* // Fallback to the default focus check
|
|
56
|
+
* focusManager.setFocused(undefined)
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
11
59
|
setFocused(focused?: boolean): void;
|
|
60
|
+
/**
|
|
61
|
+
* `onFocus` notifies all subscribed listeners with the current focus state.
|
|
62
|
+
*/
|
|
12
63
|
onFocus(): void;
|
|
64
|
+
/**
|
|
65
|
+
* `isFocused` can be used to get the current focus state.
|
|
66
|
+
*/
|
|
13
67
|
isFocused(): boolean;
|
|
14
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Singleton instance of {@link FocusManager}, used to manage and observe the focus state within TanStack Query.
|
|
71
|
+
*/
|
|
15
72
|
declare const focusManager: FocusManager;
|
|
16
73
|
//#endregion
|
|
17
74
|
export { FocusManager, focusManager };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"focusManager.d.ts","names":[],"sources":["../../src/focusManager.ts"],"mappings":";;KAEK,YAAY;KAEZ,WACH,aAAa
|
|
1
|
+
{"version":3,"file":"focusManager.d.ts","names":[],"sources":["../../src/focusManager.ts"],"mappings":";;KAEK,YAAY;KAEZ,WACH,aAAa;;;;;;cAQF,qBAAqB,aAAa;;;YAyBnC;YAMA;;;;;;;;;;;;;;;;;;;;;;;;;;EAgCV,iBAAiB,OAAO;;;;;;;;;;;;;;;;;;;EA8BxB,WAAW;;;;EAWX;;;;EAUA;;;;;cAcW,cAAY"}
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { Subscribable } from "./subscribable.js";
|
|
2
2
|
//#region src/focusManager.ts
|
|
3
|
+
/**
|
|
4
|
+
* The `FocusManager` manages the focus state within TanStack Query.
|
|
5
|
+
*
|
|
6
|
+
* It can be used to change the default event listeners or to manually change the focus state.
|
|
7
|
+
*/
|
|
3
8
|
var FocusManager = class extends Subscribable {
|
|
4
9
|
#focused;
|
|
5
10
|
#cleanup;
|
|
@@ -25,6 +30,31 @@ var FocusManager = class extends Subscribable {
|
|
|
25
30
|
this.#cleanup = void 0;
|
|
26
31
|
}
|
|
27
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* `setEventListener` can be used to set a custom event listener that will
|
|
35
|
+
* be used to determine the focus state. The provided `setup` function
|
|
36
|
+
* receives a `setFocused` callback: call it with a `boolean` to manually
|
|
37
|
+
* set the focus state, or with no arguments to re-evaluate the current
|
|
38
|
+
* focus state and notify subscribers.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* import { focusManager } from '@tanstack/query-core'
|
|
43
|
+
*
|
|
44
|
+
* focusManager.setEventListener((handleFocus) => {
|
|
45
|
+
* const listener = () => handleFocus()
|
|
46
|
+
* // Listen to visibilitychange
|
|
47
|
+
* if (typeof window !== 'undefined' && window.addEventListener) {
|
|
48
|
+
* window.addEventListener('visibilitychange', listener, false)
|
|
49
|
+
* }
|
|
50
|
+
*
|
|
51
|
+
* return () => {
|
|
52
|
+
* // Be sure to unsubscribe if a new handler is set
|
|
53
|
+
* window.removeEventListener('visibilitychange', listener)
|
|
54
|
+
* }
|
|
55
|
+
* })
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
28
58
|
setEventListener(setup) {
|
|
29
59
|
this.#setup = setup;
|
|
30
60
|
this.#cleanup?.();
|
|
@@ -33,23 +63,50 @@ var FocusManager = class extends Subscribable {
|
|
|
33
63
|
else this.onFocus();
|
|
34
64
|
});
|
|
35
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* `setFocused` can be used to manually set the focus state. Set `undefined`
|
|
68
|
+
* to fall back to the default focus check.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* import { focusManager } from '@tanstack/query-core'
|
|
73
|
+
*
|
|
74
|
+
* // Set focused
|
|
75
|
+
* focusManager.setFocused(true)
|
|
76
|
+
*
|
|
77
|
+
* // Set unfocused
|
|
78
|
+
* focusManager.setFocused(false)
|
|
79
|
+
*
|
|
80
|
+
* // Fallback to the default focus check
|
|
81
|
+
* focusManager.setFocused(undefined)
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
36
84
|
setFocused(focused) {
|
|
37
85
|
if (this.#focused !== focused) {
|
|
38
86
|
this.#focused = focused;
|
|
39
87
|
this.onFocus();
|
|
40
88
|
}
|
|
41
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* `onFocus` notifies all subscribed listeners with the current focus state.
|
|
92
|
+
*/
|
|
42
93
|
onFocus() {
|
|
43
94
|
const isFocused = this.isFocused();
|
|
44
95
|
this.listeners.forEach((listener) => {
|
|
45
96
|
listener(isFocused);
|
|
46
97
|
});
|
|
47
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
* `isFocused` can be used to get the current focus state.
|
|
101
|
+
*/
|
|
48
102
|
isFocused() {
|
|
49
103
|
if (typeof this.#focused === "boolean") return this.#focused;
|
|
50
104
|
return globalThis.document?.visibilityState !== "hidden";
|
|
51
105
|
}
|
|
52
106
|
};
|
|
107
|
+
/**
|
|
108
|
+
* Singleton instance of {@link FocusManager}, used to manage and observe the focus state within TanStack Query.
|
|
109
|
+
*/
|
|
53
110
|
const focusManager = new FocusManager();
|
|
54
111
|
//#endregion
|
|
55
112
|
export { FocusManager, focusManager };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"focusManager.js","names":["#setup","#cleanup","#focused"],"sources":["../../src/focusManager.ts"],"sourcesContent":["import { Subscribable } from './subscribable'\n\ntype Listener = (focused: boolean) => void\n\ntype SetupFn = (\n setFocused: (focused?: boolean) => void,\n) => (() => void) | undefined\n\nexport class FocusManager extends Subscribable<Listener> {\n #focused?: boolean\n #cleanup?: () => void\n\n #setup: SetupFn\n\n constructor() {\n super()\n this.#setup = (onFocus) => {\n // addEventListener does not exist in React Native, but window does\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (typeof window !== 'undefined' && window.addEventListener) {\n const listener = () => onFocus()\n // Listen to visibilitychange\n window.addEventListener('visibilitychange', listener, false)\n\n return () => {\n // Be sure to unsubscribe if a new handler is set\n window.removeEventListener('visibilitychange', listener)\n }\n }\n return\n }\n }\n\n protected onSubscribe(): void {\n if (!this.#cleanup) {\n this.setEventListener(this.#setup)\n }\n }\n\n protected onUnsubscribe() {\n if (!this.hasListeners()) {\n this.#cleanup?.()\n this.#cleanup = undefined\n }\n }\n\n setEventListener(setup: SetupFn): void {\n this.#setup = setup\n this.#cleanup?.()\n this.#cleanup = setup((focused) => {\n if (typeof focused === 'boolean') {\n this.setFocused(focused)\n } else {\n this.onFocus()\n }\n })\n }\n\n setFocused(focused?: boolean): void {\n const changed = this.#focused !== focused\n if (changed) {\n this.#focused = focused\n this.onFocus()\n }\n }\n\n onFocus(): void {\n const isFocused = this.isFocused()\n this.listeners.forEach((listener) => {\n listener(isFocused)\n })\n }\n\n isFocused(): boolean {\n if (typeof this.#focused === 'boolean') {\n return this.#focused\n }\n\n // document global can be unavailable in react native\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n return globalThis.document?.visibilityState !== 'hidden'\n }\n}\n\nexport const focusManager = new FocusManager()\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"focusManager.js","names":["#setup","#cleanup","#focused"],"sources":["../../src/focusManager.ts"],"sourcesContent":["import { Subscribable } from './subscribable'\n\ntype Listener = (focused: boolean) => void\n\ntype SetupFn = (\n setFocused: (focused?: boolean) => void,\n) => (() => void) | undefined\n\n/**\n * The `FocusManager` manages the focus state within TanStack Query.\n *\n * It can be used to change the default event listeners or to manually change the focus state.\n */\nexport class FocusManager extends Subscribable<Listener> {\n #focused?: boolean\n #cleanup?: () => void\n\n #setup: SetupFn\n\n constructor() {\n super()\n this.#setup = (onFocus) => {\n // addEventListener does not exist in React Native, but window does\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (typeof window !== 'undefined' && window.addEventListener) {\n const listener = () => onFocus()\n // Listen to visibilitychange\n window.addEventListener('visibilitychange', listener, false)\n\n return () => {\n // Be sure to unsubscribe if a new handler is set\n window.removeEventListener('visibilitychange', listener)\n }\n }\n return\n }\n }\n\n protected onSubscribe(): void {\n if (!this.#cleanup) {\n this.setEventListener(this.#setup)\n }\n }\n\n protected onUnsubscribe() {\n if (!this.hasListeners()) {\n this.#cleanup?.()\n this.#cleanup = undefined\n }\n }\n\n /**\n * `setEventListener` can be used to set a custom event listener that will\n * be used to determine the focus state. The provided `setup` function\n * receives a `setFocused` callback: call it with a `boolean` to manually\n * set the focus state, or with no arguments to re-evaluate the current\n * focus state and notify subscribers.\n *\n * @example\n * ```ts\n * import { focusManager } from '@tanstack/query-core'\n *\n * focusManager.setEventListener((handleFocus) => {\n * const listener = () => handleFocus()\n * // Listen to visibilitychange\n * if (typeof window !== 'undefined' && window.addEventListener) {\n * window.addEventListener('visibilitychange', listener, false)\n * }\n *\n * return () => {\n * // Be sure to unsubscribe if a new handler is set\n * window.removeEventListener('visibilitychange', listener)\n * }\n * })\n * ```\n */\n setEventListener(setup: SetupFn): void {\n this.#setup = setup\n this.#cleanup?.()\n this.#cleanup = setup((focused) => {\n if (typeof focused === 'boolean') {\n this.setFocused(focused)\n } else {\n this.onFocus()\n }\n })\n }\n\n /**\n * `setFocused` can be used to manually set the focus state. Set `undefined`\n * to fall back to the default focus check.\n *\n * @example\n * ```ts\n * import { focusManager } from '@tanstack/query-core'\n *\n * // Set focused\n * focusManager.setFocused(true)\n *\n * // Set unfocused\n * focusManager.setFocused(false)\n *\n * // Fallback to the default focus check\n * focusManager.setFocused(undefined)\n * ```\n */\n setFocused(focused?: boolean): void {\n const changed = this.#focused !== focused\n if (changed) {\n this.#focused = focused\n this.onFocus()\n }\n }\n\n /**\n * `onFocus` notifies all subscribed listeners with the current focus state.\n */\n onFocus(): void {\n const isFocused = this.isFocused()\n this.listeners.forEach((listener) => {\n listener(isFocused)\n })\n }\n\n /**\n * `isFocused` can be used to get the current focus state.\n */\n isFocused(): boolean {\n if (typeof this.#focused === 'boolean') {\n return this.#focused\n }\n\n // document global can be unavailable in react native\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n return globalThis.document?.visibilityState !== 'hidden'\n }\n}\n\n/**\n * Singleton instance of {@link FocusManager}, used to manage and observe the focus state within TanStack Query.\n */\nexport const focusManager = new FocusManager()\n"],"mappings":";;;;;;;AAaA,IAAa,eAAb,cAAkC,aAAuB;CACvD;CACA;CAEA;CAEA,cAAc;EACZ,MAAM;EACN,KAAKA,UAAU,YAAY;GAGzB,IAAI,OAAO,WAAW,eAAe,OAAO,kBAAkB;IAC5D,MAAM,iBAAiB,QAAQ;IAE/B,OAAO,iBAAiB,oBAAoB,UAAU,KAAK;IAE3D,aAAa;KAEX,OAAO,oBAAoB,oBAAoB,QAAQ;IACzD;GACF;EAEF;CACF;CAEA,cAA8B;EAC5B,IAAI,CAAC,KAAKC,UACR,KAAK,iBAAiB,KAAKD,MAAM;CAErC;CAEA,gBAA0B;EACxB,IAAI,CAAC,KAAK,aAAa,GAAG;GACxB,KAAKC,WAAW;GAChB,KAAKA,WAAW,KAAA;EAClB;CACF;;;;;;;;;;;;;;;;;;;;;;;;;;CA2BA,iBAAiB,OAAsB;EACrC,KAAKD,SAAS;EACd,KAAKC,WAAW;EAChB,KAAKA,WAAW,OAAO,YAAY;GACjC,IAAI,OAAO,YAAY,WACrB,KAAK,WAAW,OAAO;QAEvB,KAAK,QAAQ;EAEjB,CAAC;CACH;;;;;;;;;;;;;;;;;;;CAoBA,WAAW,SAAyB;EAElC,IADgB,KAAKC,aAAa,SACrB;GACX,KAAKA,WAAW;GAChB,KAAK,QAAQ;EACf;CACF;;;;CAKA,UAAgB;EACd,MAAM,YAAY,KAAK,UAAU;EACjC,KAAK,UAAU,SAAS,aAAa;GACnC,SAAS,SAAS;EACpB,CAAC;CACH;;;;CAKA,YAAqB;EACnB,IAAI,OAAO,KAAKA,aAAa,WAC3B,OAAO,KAAKA;EAKd,OAAO,WAAW,UAAU,oBAAoB;CAClD;AACF;;;;AAKA,MAAa,eAAe,IAAI,aAAa"}
|