@tanstack/query-core 5.90.20 → 5.91.2
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/_tsup-dts-rollup.d.cts +2229 -0
- package/build/legacy/_tsup-dts-rollup.d.ts +2229 -0
- package/build/legacy/environmentManager.cjs +48 -0
- package/build/legacy/environmentManager.cjs.map +1 -0
- package/build/legacy/environmentManager.d.cts +2 -0
- package/build/legacy/environmentManager.d.ts +2 -0
- package/build/legacy/environmentManager.js +25 -0
- package/build/legacy/environmentManager.js.map +1 -0
- package/build/legacy/focusManager.cjs +1 -2
- package/build/legacy/focusManager.cjs.map +1 -1
- package/build/legacy/focusManager.d.cts +2 -17
- package/build/legacy/focusManager.d.ts +2 -17
- package/build/legacy/focusManager.js +1 -2
- package/build/legacy/focusManager.js.map +1 -1
- package/build/legacy/hydration.d.cts +7 -3
- package/build/legacy/hydration.d.ts +7 -3
- package/build/legacy/index.cjs +3 -0
- package/build/legacy/index.cjs.map +1 -1
- package/build/legacy/index.d.cts +142 -10
- package/build/legacy/index.d.ts +142 -10
- package/build/legacy/index.js +2 -0
- package/build/legacy/index.js.map +1 -1
- package/build/legacy/infiniteQueryBehavior.d.cts +3 -15
- package/build/legacy/infiniteQueryBehavior.d.ts +3 -15
- package/build/legacy/infiniteQueryObserver.d.cts +1 -20
- package/build/legacy/infiniteQueryObserver.d.ts +1 -20
- package/build/legacy/mutation.d.cts +4 -3
- package/build/legacy/mutation.d.ts +4 -3
- package/build/legacy/mutationCache.d.cts +2 -3
- package/build/legacy/mutationCache.d.ts +2 -3
- package/build/legacy/mutationObserver.d.cts +1 -3
- package/build/legacy/mutationObserver.d.ts +1 -3
- package/build/legacy/notifyManager.d.cts +3 -46
- package/build/legacy/notifyManager.d.ts +3 -46
- package/build/legacy/onlineManager.cjs +1 -2
- package/build/legacy/onlineManager.cjs.map +1 -1
- package/build/legacy/onlineManager.d.cts +2 -16
- package/build/legacy/onlineManager.d.ts +2 -16
- package/build/legacy/onlineManager.js +1 -2
- package/build/legacy/onlineManager.js.map +1 -1
- package/build/legacy/queriesObserver.d.cts +2 -27
- package/build/legacy/queriesObserver.d.ts +2 -27
- package/build/legacy/query.cjs +13 -4
- package/build/legacy/query.cjs.map +1 -1
- package/build/legacy/query.d.cts +10 -3
- package/build/legacy/query.d.ts +10 -3
- package/build/legacy/query.js +13 -4
- package/build/legacy/query.js.map +1 -1
- package/build/legacy/queryCache.d.cts +3 -3
- package/build/legacy/queryCache.d.ts +3 -3
- package/build/legacy/queryClient.d.cts +1 -3
- package/build/legacy/queryClient.d.ts +1 -3
- package/build/legacy/queryObserver.cjs +4 -3
- package/build/legacy/queryObserver.cjs.map +1 -1
- package/build/legacy/queryObserver.d.cts +1 -3
- package/build/legacy/queryObserver.d.ts +1 -3
- package/build/legacy/queryObserver.js +4 -4
- package/build/legacy/queryObserver.js.map +1 -1
- package/build/legacy/removable.cjs +2 -1
- package/build/legacy/removable.cjs.map +1 -1
- package/build/legacy/removable.d.cts +1 -11
- package/build/legacy/removable.d.ts +1 -11
- package/build/legacy/removable.js +3 -2
- package/build/legacy/removable.js.map +1 -1
- package/build/legacy/retryer.cjs +2 -1
- package/build/legacy/retryer.cjs.map +1 -1
- package/build/legacy/retryer.d.cts +7 -3
- package/build/legacy/retryer.d.ts +7 -3
- package/build/legacy/retryer.js +3 -2
- package/build/legacy/retryer.js.map +1 -1
- package/build/legacy/streamedQuery.cjs +2 -4
- package/build/legacy/streamedQuery.cjs.map +1 -1
- package/build/legacy/streamedQuery.d.cts +1 -34
- package/build/legacy/streamedQuery.d.ts +1 -34
- package/build/legacy/streamedQuery.js +2 -4
- package/build/legacy/streamedQuery.js.map +1 -1
- package/build/legacy/subscribable.d.cts +1 -10
- package/build/legacy/subscribable.d.ts +1 -10
- package/build/legacy/thenable.cjs.map +1 -1
- package/build/legacy/thenable.d.cts +6 -47
- package/build/legacy/thenable.d.ts +6 -47
- package/build/legacy/thenable.js.map +1 -1
- package/build/legacy/timeoutManager.d.cts +7 -58
- package/build/legacy/timeoutManager.d.ts +7 -58
- package/build/legacy/types.d.cts +95 -3
- package/build/legacy/types.d.ts +95 -3
- package/build/legacy/utils.cjs.map +1 -1
- package/build/legacy/utils.d.cts +30 -3
- package/build/legacy/utils.d.ts +30 -3
- package/build/legacy/utils.js.map +1 -1
- package/build/modern/_tsup-dts-rollup.d.cts +2229 -0
- package/build/modern/_tsup-dts-rollup.d.ts +2229 -0
- package/build/modern/environmentManager.cjs +48 -0
- package/build/modern/environmentManager.cjs.map +1 -0
- package/build/modern/environmentManager.d.cts +2 -0
- package/build/modern/environmentManager.d.ts +2 -0
- package/build/modern/environmentManager.js +23 -0
- package/build/modern/environmentManager.js.map +1 -0
- package/build/modern/focusManager.cjs +1 -2
- package/build/modern/focusManager.cjs.map +1 -1
- package/build/modern/focusManager.d.cts +2 -17
- package/build/modern/focusManager.d.ts +2 -17
- package/build/modern/focusManager.js +1 -2
- package/build/modern/focusManager.js.map +1 -1
- package/build/modern/hydration.d.cts +7 -3
- package/build/modern/hydration.d.ts +7 -3
- package/build/modern/index.cjs +3 -0
- package/build/modern/index.cjs.map +1 -1
- package/build/modern/index.d.cts +142 -10
- package/build/modern/index.d.ts +142 -10
- package/build/modern/index.js +2 -0
- package/build/modern/index.js.map +1 -1
- package/build/modern/infiniteQueryBehavior.d.cts +3 -15
- package/build/modern/infiniteQueryBehavior.d.ts +3 -15
- package/build/modern/infiniteQueryObserver.d.cts +1 -20
- package/build/modern/infiniteQueryObserver.d.ts +1 -20
- package/build/modern/mutation.d.cts +4 -3
- package/build/modern/mutation.d.ts +4 -3
- package/build/modern/mutationCache.d.cts +2 -3
- package/build/modern/mutationCache.d.ts +2 -3
- package/build/modern/mutationObserver.d.cts +1 -3
- package/build/modern/mutationObserver.d.ts +1 -3
- package/build/modern/notifyManager.d.cts +3 -46
- package/build/modern/notifyManager.d.ts +3 -46
- package/build/modern/onlineManager.cjs +1 -2
- package/build/modern/onlineManager.cjs.map +1 -1
- package/build/modern/onlineManager.d.cts +2 -16
- package/build/modern/onlineManager.d.ts +2 -16
- package/build/modern/onlineManager.js +1 -2
- package/build/modern/onlineManager.js.map +1 -1
- package/build/modern/queriesObserver.d.cts +2 -27
- package/build/modern/queriesObserver.d.ts +2 -27
- package/build/modern/query.cjs +12 -3
- package/build/modern/query.cjs.map +1 -1
- package/build/modern/query.d.cts +10 -3
- package/build/modern/query.d.ts +10 -3
- package/build/modern/query.js +12 -3
- package/build/modern/query.js.map +1 -1
- package/build/modern/queryCache.d.cts +3 -3
- package/build/modern/queryCache.d.ts +3 -3
- package/build/modern/queryClient.d.cts +1 -3
- package/build/modern/queryClient.d.ts +1 -3
- package/build/modern/queryObserver.cjs +4 -3
- package/build/modern/queryObserver.cjs.map +1 -1
- package/build/modern/queryObserver.d.cts +1 -3
- package/build/modern/queryObserver.d.ts +1 -3
- package/build/modern/queryObserver.js +4 -4
- package/build/modern/queryObserver.js.map +1 -1
- package/build/modern/removable.cjs +2 -1
- package/build/modern/removable.cjs.map +1 -1
- package/build/modern/removable.d.cts +1 -11
- package/build/modern/removable.d.ts +1 -11
- package/build/modern/removable.js +3 -2
- package/build/modern/removable.js.map +1 -1
- package/build/modern/retryer.cjs +2 -1
- package/build/modern/retryer.cjs.map +1 -1
- package/build/modern/retryer.d.cts +7 -3
- package/build/modern/retryer.d.ts +7 -3
- package/build/modern/retryer.js +3 -2
- package/build/modern/retryer.js.map +1 -1
- package/build/modern/streamedQuery.cjs +2 -4
- package/build/modern/streamedQuery.cjs.map +1 -1
- package/build/modern/streamedQuery.d.cts +1 -34
- package/build/modern/streamedQuery.d.ts +1 -34
- package/build/modern/streamedQuery.js +2 -4
- package/build/modern/streamedQuery.js.map +1 -1
- package/build/modern/subscribable.d.cts +1 -10
- package/build/modern/subscribable.d.ts +1 -10
- package/build/modern/thenable.cjs.map +1 -1
- package/build/modern/thenable.d.cts +6 -47
- package/build/modern/thenable.d.ts +6 -47
- package/build/modern/thenable.js.map +1 -1
- package/build/modern/timeoutManager.d.cts +7 -58
- package/build/modern/timeoutManager.d.ts +7 -58
- package/build/modern/types.d.cts +95 -3
- package/build/modern/types.d.ts +95 -3
- package/build/modern/utils.cjs.map +1 -1
- package/build/modern/utils.d.cts +30 -3
- package/build/modern/utils.d.ts +30 -3
- package/build/modern/utils.js.map +1 -1
- package/package.json +4 -5
- package/src/environmentManager.ts +25 -0
- package/src/focusManager.ts +1 -2
- package/src/index.ts +1 -0
- package/src/onlineManager.ts +1 -2
- package/src/query.ts +17 -6
- package/src/queryObserver.ts +8 -4
- package/src/removable.ts +3 -2
- package/src/retryer.ts +3 -2
- package/src/streamedQuery.ts +2 -4
- package/src/thenable.ts +1 -1
- package/src/utils.ts +3 -0
- package/build/legacy/hydration-BlEVG2Lp.d.ts +0 -1384
- package/build/legacy/hydration-uphG6M-i.d.cts +0 -1384
- package/build/modern/hydration-BlEVG2Lp.d.ts +0 -1384
- package/build/modern/hydration-uphG6M-i.d.cts +0 -1384
package/build/modern/utils.d.cts
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
export { noop_alias_1 as noop } from './_tsup-dts-rollup.cjs';
|
|
2
|
+
export { functionalUpdate } from './_tsup-dts-rollup.cjs';
|
|
3
|
+
export { isValidTimeout } from './_tsup-dts-rollup.cjs';
|
|
4
|
+
export { timeUntilStale } from './_tsup-dts-rollup.cjs';
|
|
5
|
+
export { resolveStaleTime } from './_tsup-dts-rollup.cjs';
|
|
6
|
+
export { resolveEnabled } from './_tsup-dts-rollup.cjs';
|
|
7
|
+
export { matchQuery_alias_1 as matchQuery } from './_tsup-dts-rollup.cjs';
|
|
8
|
+
export { matchMutation_alias_1 as matchMutation } from './_tsup-dts-rollup.cjs';
|
|
9
|
+
export { hashQueryKeyByOptions } from './_tsup-dts-rollup.cjs';
|
|
10
|
+
export { hashKey_alias_1 as hashKey } from './_tsup-dts-rollup.cjs';
|
|
11
|
+
export { partialMatchKey_alias_1 as partialMatchKey } from './_tsup-dts-rollup.cjs';
|
|
12
|
+
export { replaceEqualDeep_alias_1 as replaceEqualDeep } from './_tsup-dts-rollup.cjs';
|
|
13
|
+
export { shallowEqualObjects } from './_tsup-dts-rollup.cjs';
|
|
14
|
+
export { isPlainArray } from './_tsup-dts-rollup.cjs';
|
|
15
|
+
export { isPlainObject } from './_tsup-dts-rollup.cjs';
|
|
16
|
+
export { sleep } from './_tsup-dts-rollup.cjs';
|
|
17
|
+
export { replaceData } from './_tsup-dts-rollup.cjs';
|
|
18
|
+
export { keepPreviousData_alias_1 as keepPreviousData } from './_tsup-dts-rollup.cjs';
|
|
19
|
+
export { addToEnd } from './_tsup-dts-rollup.cjs';
|
|
20
|
+
export { addToStart } from './_tsup-dts-rollup.cjs';
|
|
21
|
+
export { ensureQueryFn } from './_tsup-dts-rollup.cjs';
|
|
22
|
+
export { shouldThrowError_alias_1 as shouldThrowError } from './_tsup-dts-rollup.cjs';
|
|
23
|
+
export { addConsumeAwareSignal } from './_tsup-dts-rollup.cjs';
|
|
24
|
+
export { QueryFilters_alias_1 as QueryFilters } from './_tsup-dts-rollup.cjs';
|
|
25
|
+
export { MutationFilters_alias_1 as MutationFilters } from './_tsup-dts-rollup.cjs';
|
|
26
|
+
export { Updater_alias_1 as Updater } from './_tsup-dts-rollup.cjs';
|
|
27
|
+
export { QueryTypeFilter } from './_tsup-dts-rollup.cjs';
|
|
28
|
+
export { isServer_alias_1 as isServer } from './_tsup-dts-rollup.cjs';
|
|
29
|
+
export { skipToken_alias_1 as skipToken } from './_tsup-dts-rollup.cjs';
|
|
30
|
+
export { SkipToken_alias_1 as SkipToken } from './_tsup-dts-rollup.cjs';
|
package/build/modern/utils.d.ts
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
export { noop_alias_1 as noop } from './_tsup-dts-rollup.js';
|
|
2
|
+
export { functionalUpdate } from './_tsup-dts-rollup.js';
|
|
3
|
+
export { isValidTimeout } from './_tsup-dts-rollup.js';
|
|
4
|
+
export { timeUntilStale } from './_tsup-dts-rollup.js';
|
|
5
|
+
export { resolveStaleTime } from './_tsup-dts-rollup.js';
|
|
6
|
+
export { resolveEnabled } from './_tsup-dts-rollup.js';
|
|
7
|
+
export { matchQuery_alias_1 as matchQuery } from './_tsup-dts-rollup.js';
|
|
8
|
+
export { matchMutation_alias_1 as matchMutation } from './_tsup-dts-rollup.js';
|
|
9
|
+
export { hashQueryKeyByOptions } from './_tsup-dts-rollup.js';
|
|
10
|
+
export { hashKey_alias_1 as hashKey } from './_tsup-dts-rollup.js';
|
|
11
|
+
export { partialMatchKey_alias_1 as partialMatchKey } from './_tsup-dts-rollup.js';
|
|
12
|
+
export { replaceEqualDeep_alias_1 as replaceEqualDeep } from './_tsup-dts-rollup.js';
|
|
13
|
+
export { shallowEqualObjects } from './_tsup-dts-rollup.js';
|
|
14
|
+
export { isPlainArray } from './_tsup-dts-rollup.js';
|
|
15
|
+
export { isPlainObject } from './_tsup-dts-rollup.js';
|
|
16
|
+
export { sleep } from './_tsup-dts-rollup.js';
|
|
17
|
+
export { replaceData } from './_tsup-dts-rollup.js';
|
|
18
|
+
export { keepPreviousData_alias_1 as keepPreviousData } from './_tsup-dts-rollup.js';
|
|
19
|
+
export { addToEnd } from './_tsup-dts-rollup.js';
|
|
20
|
+
export { addToStart } from './_tsup-dts-rollup.js';
|
|
21
|
+
export { ensureQueryFn } from './_tsup-dts-rollup.js';
|
|
22
|
+
export { shouldThrowError_alias_1 as shouldThrowError } from './_tsup-dts-rollup.js';
|
|
23
|
+
export { addConsumeAwareSignal } from './_tsup-dts-rollup.js';
|
|
24
|
+
export { QueryFilters_alias_1 as QueryFilters } from './_tsup-dts-rollup.js';
|
|
25
|
+
export { MutationFilters_alias_1 as MutationFilters } from './_tsup-dts-rollup.js';
|
|
26
|
+
export { Updater_alias_1 as Updater } from './_tsup-dts-rollup.js';
|
|
27
|
+
export { QueryTypeFilter } from './_tsup-dts-rollup.js';
|
|
28
|
+
export { isServer_alias_1 as isServer } from './_tsup-dts-rollup.js';
|
|
29
|
+
export { skipToken_alias_1 as skipToken } from './_tsup-dts-rollup.js';
|
|
30
|
+
export { SkipToken_alias_1 as SkipToken } from './_tsup-dts-rollup.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils.ts"],"sourcesContent":["import { timeoutManager } from './timeoutManager'\nimport type {\n DefaultError,\n Enabled,\n FetchStatus,\n MutationKey,\n MutationStatus,\n QueryFunction,\n QueryKey,\n QueryOptions,\n StaleTime,\n StaleTimeFunction,\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\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 resolveStaleTime<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n staleTime:\n | undefined\n | StaleTimeFunction<TQueryFnData, TError, TData, TQueryKey>,\n query: Query<TQueryFnData, TError, TData, TQueryKey>,\n): StaleTime | undefined {\n return typeof staleTime === 'function' ? staleTime(query) : staleTime\n}\n\nexport function resolveEnabled<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n enabled: undefined | Enabled<TQueryFnData, TError, TData, TQueryKey>,\n query: Query<TQueryFnData, TError, TData, TQueryKey>,\n): boolean | undefined {\n return typeof enabled === 'function' ? enabled(query) : enabled\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 return Object.keys(b).every((key) => partialMatchKey(a[key], b[key]))\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":";AAAA,SAAS,sBAAsB;AAwFxB,IAAM,WAAW,OAAO,WAAW,eAAe,UAAU;AAI5D,SAAS,OAAO;AAAC;AAEjB,SAAS,iBACd,SACA,OACS;AACT,SAAO,OAAO,YAAY,aACrB,QAAmC,KAAK,IACzC;AACN;AAEO,SAAS,eAAe,OAAiC;AAC9D,SAAO,OAAO,UAAU,YAAY,SAAS,KAAK,UAAU;AAC9D;AAEO,SAAS,eAAe,WAAmB,WAA4B;AAC5E,SAAO,KAAK,IAAI,aAAa,aAAa,KAAK,KAAK,IAAI,GAAG,CAAC;AAC9D;AAEO,SAAS,iBAMd,WAGA,OACuB;AACvB,SAAO,OAAO,cAAc,aAAa,UAAU,KAAK,IAAI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,YAAY,WAAW;AACjC,cAAQ;AAAA,QACN,yGAAyG,QAAQ,SAAS;AAAA,MAC5H;AAAA,IACF;AAAA,EACF;AAKA,MAAI,CAAC,QAAQ,WAAW,cAAc,gBAAgB;AACpD,WAAO,MAAM,aAAa;AAAA,EAC5B;AAEA,MAAI,CAAC,QAAQ,WAAW,QAAQ,YAAY,WAAW;AACrD,WAAO,MACL,QAAQ,OAAO,IAAI,MAAM,qBAAqB,QAAQ,SAAS,GAAG,CAAC;AAAA,EACvE;AAEA,SAAO,QAAQ;AACjB;AAEO,SAAS,iBACd,cACA,QACS;AAET,MAAI,OAAO,iBAAiB,YAAY;AACtC,WAAO,aAAa,GAAG,MAAM;AAAA,EAC/B;AAEA,SAAO,CAAC,CAAC;AACX;AAEO,SAAS,sBACd,QACA,WACA,aAC6B;AAC7B,MAAI,WAAW;AACf,MAAI;AAEJ,SAAO,eAAe,QAAQ,UAAU;AAAA,IACtC,YAAY;AAAA,IACZ,KAAK,MAAM;AACT,iBAAW,UAAU;AACrB,UAAI,UAAU;AACZ,eAAO;AAAA,MACT;AAEA,iBAAW;AACX,UAAI,OAAO,SAAS;AAClB,oBAAY;AAAA,MACd,OAAO;AACL,eAAO,iBAAiB,SAAS,aAAa,EAAE,MAAM,KAAK,CAAC;AAAA,MAC9D;AAEA,aAAO;AAAA,IACT;AAAA,EACF,CAAC;AAED,SAAO;AACT;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/utils.ts"],"sourcesContent":["import { timeoutManager } from './timeoutManager'\nimport type {\n DefaultError,\n Enabled,\n FetchStatus,\n MutationKey,\n MutationStatus,\n QueryFunction,\n QueryKey,\n QueryOptions,\n StaleTime,\n StaleTimeFunction,\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 resolveStaleTime<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n staleTime:\n | undefined\n | StaleTimeFunction<TQueryFnData, TError, TData, TQueryKey>,\n query: Query<TQueryFnData, TError, TData, TQueryKey>,\n): StaleTime | undefined {\n return typeof staleTime === 'function' ? staleTime(query) : staleTime\n}\n\nexport function resolveEnabled<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n enabled: undefined | Enabled<TQueryFnData, TError, TData, TQueryKey>,\n query: Query<TQueryFnData, TError, TData, TQueryKey>,\n): boolean | undefined {\n return typeof enabled === 'function' ? enabled(query) : enabled\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 return Object.keys(b).every((key) => partialMatchKey(a[key], b[key]))\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":";AAAA,SAAS,sBAAsB;AA2FxB,IAAM,WAAW,OAAO,WAAW,eAAe,UAAU;AAI5D,SAAS,OAAO;AAAC;AAEjB,SAAS,iBACd,SACA,OACS;AACT,SAAO,OAAO,YAAY,aACrB,QAAmC,KAAK,IACzC;AACN;AAEO,SAAS,eAAe,OAAiC;AAC9D,SAAO,OAAO,UAAU,YAAY,SAAS,KAAK,UAAU;AAC9D;AAEO,SAAS,eAAe,WAAmB,WAA4B;AAC5E,SAAO,KAAK,IAAI,aAAa,aAAa,KAAK,KAAK,IAAI,GAAG,CAAC;AAC9D;AAEO,SAAS,iBAMd,WAGA,OACuB;AACvB,SAAO,OAAO,cAAc,aAAa,UAAU,KAAK,IAAI;AAC9D;AAEO,SAAS,eAMd,SACA,OACqB;AACrB,SAAO,OAAO,YAAY,aAAa,QAAQ,KAAK,IAAI;AAC1D;AAEO,SAAS,WACd,SACA,OACS;AACT,QAAM;AAAA,IACJ,OAAO;AAAA,IACP;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI;AAEJ,MAAI,UAAU;AACZ,QAAI,OAAO;AACT,UAAI,MAAM,cAAc,sBAAsB,UAAU,MAAM,OAAO,GAAG;AACtE,eAAO;AAAA,MACT;AAAA,IACF,WAAW,CAAC,gBAAgB,MAAM,UAAU,QAAQ,GAAG;AACrD,aAAO;AAAA,IACT;AAAA,EACF;AAEA,MAAI,SAAS,OAAO;AAClB,UAAM,WAAW,MAAM,SAAS;AAChC,QAAI,SAAS,YAAY,CAAC,UAAU;AAClC,aAAO;AAAA,IACT;AACA,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,SAAO,eAAe,QAAQ,UAAU;AAAA,IACtC,YAAY;AAAA,IACZ,KAAK,MAAM;AACT,iBAAW,UAAU;AACrB,UAAI,UAAU;AACZ,eAAO;AAAA,MACT;AAEA,iBAAW;AACX,UAAI,OAAO,SAAS;AAClB,oBAAY;AAAA,MACd,OAAO;AACL,eAAO,iBAAiB,SAAS,aAAa,EAAE,MAAM,KAAK,CAAC;AAAA,MAC9D;AAEA,aAAO;AAAA,IACT;AAAA,EACF,CAAC;AAED,SAAO;AACT;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/query-core",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.91.2",
|
|
4
4
|
"description": "The framework agnostic core that powers TanStack Query",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -48,15 +48,14 @@
|
|
|
48
48
|
"compile": "tsc --build",
|
|
49
49
|
"test:eslint": "eslint --concurrency=auto ./src",
|
|
50
50
|
"test:types": "npm-run-all --serial test:types:*",
|
|
51
|
-
"test:types:ts50": "node ../../node_modules/typescript50/lib/tsc.js --build tsconfig.legacy.json",
|
|
52
|
-
"test:types:ts51": "node ../../node_modules/typescript51/lib/tsc.js --build tsconfig.legacy.json",
|
|
53
|
-
"test:types:ts52": "node ../../node_modules/typescript52/lib/tsc.js --build tsconfig.legacy.json",
|
|
54
|
-
"test:types:ts53": "node ../../node_modules/typescript53/lib/tsc.js --build tsconfig.legacy.json",
|
|
55
51
|
"test:types:ts54": "node ../../node_modules/typescript54/lib/tsc.js --build tsconfig.legacy.json",
|
|
56
52
|
"test:types:ts55": "node ../../node_modules/typescript55/lib/tsc.js --build tsconfig.legacy.json",
|
|
57
53
|
"test:types:ts56": "node ../../node_modules/typescript56/lib/tsc.js --build tsconfig.legacy.json",
|
|
58
54
|
"test:types:ts57": "node ../../node_modules/typescript57/lib/tsc.js --build tsconfig.legacy.json",
|
|
55
|
+
"test:types:ts58": "node ../../node_modules/typescript58/lib/tsc.js --build tsconfig.legacy.json",
|
|
56
|
+
"test:types:ts59": "node ../../node_modules/typescript59/lib/tsc.js --build tsconfig.legacy.json",
|
|
59
57
|
"test:types:tscurrent": "tsc --build",
|
|
58
|
+
"test:types:ts60": "node ../../node_modules/typescript60/lib/tsc.js --build tsconfig.legacy.json",
|
|
60
59
|
"test:lib": "vitest",
|
|
61
60
|
"test:lib:dev": "pnpm run test:lib --watch",
|
|
62
61
|
"test:build": "publint --strict && attw --pack",
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { isServer } from './utils'
|
|
2
|
+
|
|
3
|
+
export type IsServerValue = () => boolean
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Manages environment detection used by TanStack Query internals.
|
|
7
|
+
*/
|
|
8
|
+
export const environmentManager = (() => {
|
|
9
|
+
let isServerFn: IsServerValue = () => isServer
|
|
10
|
+
|
|
11
|
+
return {
|
|
12
|
+
/**
|
|
13
|
+
* Returns whether the current runtime should be treated as a server environment.
|
|
14
|
+
*/
|
|
15
|
+
isServer(): boolean {
|
|
16
|
+
return isServerFn()
|
|
17
|
+
},
|
|
18
|
+
/**
|
|
19
|
+
* Overrides the server check globally.
|
|
20
|
+
*/
|
|
21
|
+
setIsServer(isServerValue: IsServerValue): void {
|
|
22
|
+
isServerFn = isServerValue
|
|
23
|
+
},
|
|
24
|
+
}
|
|
25
|
+
})()
|
package/src/focusManager.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Subscribable } from './subscribable'
|
|
2
|
-
import { isServer } from './utils'
|
|
3
2
|
|
|
4
3
|
type Listener = (focused: boolean) => void
|
|
5
4
|
|
|
@@ -18,7 +17,7 @@ export class FocusManager extends Subscribable<Listener> {
|
|
|
18
17
|
this.#setup = (onFocus) => {
|
|
19
18
|
// addEventListener does not exist in React Native, but window does
|
|
20
19
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
21
|
-
if (
|
|
20
|
+
if (typeof window !== 'undefined' && window.addEventListener) {
|
|
22
21
|
const listener = () => onFocus()
|
|
23
22
|
// Listen to visibilitychange
|
|
24
23
|
window.addEventListener('visibilitychange', listener, false)
|
package/src/index.ts
CHANGED
package/src/onlineManager.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Subscribable } from './subscribable'
|
|
2
|
-
import { isServer } from './utils'
|
|
3
2
|
|
|
4
3
|
type Listener = (online: boolean) => void
|
|
5
4
|
type SetupFn = (setOnline: Listener) => (() => void) | undefined
|
|
@@ -15,7 +14,7 @@ export class OnlineManager extends Subscribable<Listener> {
|
|
|
15
14
|
this.#setup = (onOnline) => {
|
|
16
15
|
// addEventListener does not exist in React Native, but window does
|
|
17
16
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
18
|
-
if (
|
|
17
|
+
if (typeof window !== 'undefined' && window.addEventListener) {
|
|
19
18
|
const onlineListener = () => onOnline(true)
|
|
20
19
|
const offlineListener = () => onOnline(false)
|
|
21
20
|
// Listen to online
|
package/src/query.ts
CHANGED
|
@@ -260,9 +260,13 @@ export class Query<
|
|
|
260
260
|
this.cancel({ silent: true })
|
|
261
261
|
}
|
|
262
262
|
|
|
263
|
+
get resetState(): QueryState<TData, TError> {
|
|
264
|
+
return this.#initialState
|
|
265
|
+
}
|
|
266
|
+
|
|
263
267
|
reset(): void {
|
|
264
268
|
this.destroy()
|
|
265
|
-
this.setState(this
|
|
269
|
+
this.setState(this.resetState)
|
|
266
270
|
}
|
|
267
271
|
|
|
268
272
|
isActive(): boolean {
|
|
@@ -276,10 +280,11 @@ export class Query<
|
|
|
276
280
|
return !this.isActive()
|
|
277
281
|
}
|
|
278
282
|
// if a query has no observers, it should still be considered disabled if it never attempted a fetch
|
|
279
|
-
return (
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
+
return this.options.queryFn === skipToken || !this.isFetched()
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
isFetched() {
|
|
287
|
+
return this.state.dataUpdateCount + this.state.errorUpdateCount > 0
|
|
283
288
|
}
|
|
284
289
|
|
|
285
290
|
isStatic(): boolean {
|
|
@@ -359,7 +364,7 @@ export class Query<
|
|
|
359
364
|
// If the transport layer does not support cancellation
|
|
360
365
|
// we'll let the query continue so the result can be cached
|
|
361
366
|
if (this.#retryer) {
|
|
362
|
-
if (this.#abortSignalConsumed) {
|
|
367
|
+
if (this.#abortSignalConsumed || this.#isInitialPausedFetch()) {
|
|
363
368
|
this.#retryer.cancel({ revert: true })
|
|
364
369
|
} else {
|
|
365
370
|
this.#retryer.cancelRetry()
|
|
@@ -377,6 +382,12 @@ export class Query<
|
|
|
377
382
|
return this.observers.length
|
|
378
383
|
}
|
|
379
384
|
|
|
385
|
+
#isInitialPausedFetch(): boolean {
|
|
386
|
+
return (
|
|
387
|
+
this.state.fetchStatus === 'paused' && this.state.status === 'pending'
|
|
388
|
+
)
|
|
389
|
+
}
|
|
390
|
+
|
|
380
391
|
invalidate(): void {
|
|
381
392
|
if (!this.state.isInvalidated) {
|
|
382
393
|
this.#dispatch({ type: 'invalidate' })
|
package/src/queryObserver.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { focusManager } from './focusManager'
|
|
2
|
+
import { environmentManager } from './environmentManager'
|
|
2
3
|
import { notifyManager } from './notifyManager'
|
|
3
4
|
import { fetchState } from './query'
|
|
4
5
|
import { Subscribable } from './subscribable'
|
|
5
6
|
import { pendingThenable } from './thenable'
|
|
6
7
|
import {
|
|
7
|
-
isServer,
|
|
8
8
|
isValidTimeout,
|
|
9
9
|
noop,
|
|
10
10
|
replaceData,
|
|
@@ -358,7 +358,11 @@ export class QueryObserver<
|
|
|
358
358
|
this.#currentQuery,
|
|
359
359
|
)
|
|
360
360
|
|
|
361
|
-
if (
|
|
361
|
+
if (
|
|
362
|
+
environmentManager.isServer() ||
|
|
363
|
+
this.#currentResult.isStale ||
|
|
364
|
+
!isValidTimeout(staleTime)
|
|
365
|
+
) {
|
|
362
366
|
return
|
|
363
367
|
}
|
|
364
368
|
|
|
@@ -389,7 +393,7 @@ export class QueryObserver<
|
|
|
389
393
|
this.#currentRefetchInterval = nextInterval
|
|
390
394
|
|
|
391
395
|
if (
|
|
392
|
-
isServer ||
|
|
396
|
+
environmentManager.isServer() ||
|
|
393
397
|
resolveEnabled(this.options.enabled, this.#currentQuery) === false ||
|
|
394
398
|
!isValidTimeout(this.#currentRefetchInterval) ||
|
|
395
399
|
this.#currentRefetchInterval === 0
|
|
@@ -572,7 +576,7 @@ export class QueryObserver<
|
|
|
572
576
|
failureCount: newState.fetchFailureCount,
|
|
573
577
|
failureReason: newState.fetchFailureReason,
|
|
574
578
|
errorUpdateCount: newState.errorUpdateCount,
|
|
575
|
-
isFetched:
|
|
579
|
+
isFetched: query.isFetched(),
|
|
576
580
|
isFetchedAfterMount:
|
|
577
581
|
newState.dataUpdateCount > queryInitialState.dataUpdateCount ||
|
|
578
582
|
newState.errorUpdateCount > queryInitialState.errorUpdateCount,
|
package/src/removable.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { timeoutManager } from './timeoutManager'
|
|
2
|
-
import {
|
|
2
|
+
import { environmentManager } from './environmentManager'
|
|
3
|
+
import { isValidTimeout } from './utils'
|
|
3
4
|
import type { ManagedTimerId } from './timeoutManager'
|
|
4
5
|
|
|
5
6
|
export abstract class Removable {
|
|
@@ -24,7 +25,7 @@ export abstract class Removable {
|
|
|
24
25
|
// Default to 5 minutes (Infinity for server-side) if no gcTime is set
|
|
25
26
|
this.gcTime = Math.max(
|
|
26
27
|
this.gcTime || 0,
|
|
27
|
-
newGcTime ?? (isServer ? Infinity : 5 * 60 * 1000),
|
|
28
|
+
newGcTime ?? (environmentManager.isServer() ? Infinity : 5 * 60 * 1000),
|
|
28
29
|
)
|
|
29
30
|
}
|
|
30
31
|
|
package/src/retryer.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { focusManager } from './focusManager'
|
|
2
2
|
import { onlineManager } from './onlineManager'
|
|
3
3
|
import { pendingThenable } from './thenable'
|
|
4
|
-
import {
|
|
4
|
+
import { environmentManager } from './environmentManager'
|
|
5
|
+
import { sleep } from './utils'
|
|
5
6
|
import type { Thenable } from './thenable'
|
|
6
7
|
import type { CancelOptions, DefaultError, NetworkMode } from './types'
|
|
7
8
|
|
|
@@ -166,7 +167,7 @@ export function createRetryer<TData = unknown, TError = DefaultError>(
|
|
|
166
167
|
}
|
|
167
168
|
|
|
168
169
|
// Do we need to retry the request?
|
|
169
|
-
const retry = config.retry ?? (isServer ? 0 : 3)
|
|
170
|
+
const retry = config.retry ?? (environmentManager.isServer() ? 0 : 3)
|
|
170
171
|
const retryDelay = config.retryDelay ?? defaultRetryDelay
|
|
171
172
|
const delay =
|
|
172
173
|
typeof retryDelay === 'function'
|
package/src/streamedQuery.ts
CHANGED
|
@@ -66,12 +66,10 @@ export function streamedQuery<
|
|
|
66
66
|
const query = context.client
|
|
67
67
|
.getQueryCache()
|
|
68
68
|
.find({ queryKey: context.queryKey, exact: true })
|
|
69
|
-
const isRefetch = !!query && query.
|
|
69
|
+
const isRefetch = !!query && query.isFetched()
|
|
70
70
|
if (isRefetch && refetchMode === 'reset') {
|
|
71
71
|
query.setState({
|
|
72
|
-
|
|
73
|
-
data: undefined,
|
|
74
|
-
error: null,
|
|
72
|
+
...query.resetState,
|
|
75
73
|
fetchStatus: 'fetching',
|
|
76
74
|
})
|
|
77
75
|
}
|
package/src/thenable.ts
CHANGED
|
@@ -58,7 +58,7 @@ export function pendingThenable<T>(): PendingThenable<T> {
|
|
|
58
58
|
function finalize(data: Fulfilled<T> | Rejected) {
|
|
59
59
|
Object.assign(thenable, data)
|
|
60
60
|
|
|
61
|
-
// clear pending props
|
|
61
|
+
// clear pending props to avoid calling them twice
|
|
62
62
|
delete (thenable as Partial<PendingThenable<T>>).resolve
|
|
63
63
|
delete (thenable as Partial<PendingThenable<T>>).reject
|
|
64
64
|
}
|
package/src/utils.ts
CHANGED
|
@@ -86,6 +86,9 @@ export type QueryTypeFilter = 'all' | 'active' | 'inactive'
|
|
|
86
86
|
|
|
87
87
|
// UTILS
|
|
88
88
|
|
|
89
|
+
/** @deprecated
|
|
90
|
+
* use `environmentManager.isServer()` instead.
|
|
91
|
+
*/
|
|
89
92
|
export const isServer = typeof window === 'undefined' || 'Deno' in globalThis
|
|
90
93
|
|
|
91
94
|
export function noop(): void
|