@tanstack/query-core 5.24.7 → 5.25.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/hydration.d.cts +1 -1
- package/build/legacy/hydration.d.ts +1 -1
- package/build/legacy/index.cjs +3 -1
- package/build/legacy/index.cjs.map +1 -1
- package/build/legacy/index.d.cts +1 -1
- package/build/legacy/index.d.ts +1 -1
- package/build/legacy/index.js +4 -2
- package/build/legacy/index.js.map +1 -1
- package/build/legacy/infiniteQueryBehavior.cjs +12 -3
- package/build/legacy/infiniteQueryBehavior.cjs.map +1 -1
- package/build/legacy/infiniteQueryBehavior.d.cts +1 -1
- package/build/legacy/infiniteQueryBehavior.d.ts +1 -1
- package/build/legacy/infiniteQueryBehavior.js +13 -4
- package/build/legacy/infiniteQueryBehavior.js.map +1 -1
- package/build/legacy/infiniteQueryObserver.d.cts +1 -1
- package/build/legacy/infiniteQueryObserver.d.ts +1 -1
- package/build/legacy/mutation.d.cts +1 -1
- package/build/legacy/mutation.d.ts +1 -1
- package/build/legacy/mutationCache.d.cts +1 -1
- package/build/legacy/mutationCache.d.ts +1 -1
- package/build/legacy/mutationObserver.d.cts +1 -1
- package/build/legacy/mutationObserver.d.ts +1 -1
- package/build/legacy/queriesObserver.d.cts +1 -1
- package/build/legacy/queriesObserver.d.ts +1 -1
- package/build/legacy/query.cjs +8 -1
- 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 +9 -2
- package/build/legacy/query.js.map +1 -1
- package/build/legacy/queryCache.d.cts +1 -1
- package/build/legacy/queryCache.d.ts +1 -1
- package/build/legacy/{queryClient-JUWz6dOI.d.cts → queryClient-QsbA-3YL.d.cts} +4 -2
- package/build/legacy/{queryClient-p98HhdxQ.d.ts → queryClient-pTTqDRDI.d.ts} +4 -2
- package/build/legacy/queryClient.cjs +9 -3
- 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 +11 -4
- package/build/legacy/queryClient.js.map +1 -1
- package/build/legacy/queryObserver.d.cts +1 -1
- package/build/legacy/queryObserver.d.ts +1 -1
- package/build/legacy/retryer.d.cts +1 -1
- package/build/legacy/retryer.d.ts +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/utils.cjs +3 -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 +2 -0
- package/build/legacy/utils.js.map +1 -1
- package/build/modern/hydration.d.cts +1 -1
- package/build/modern/hydration.d.ts +1 -1
- package/build/modern/index.cjs +3 -1
- package/build/modern/index.cjs.map +1 -1
- package/build/modern/index.d.cts +1 -1
- package/build/modern/index.d.ts +1 -1
- package/build/modern/index.js +4 -2
- package/build/modern/index.js.map +1 -1
- package/build/modern/infiniteQueryBehavior.cjs +12 -3
- package/build/modern/infiniteQueryBehavior.cjs.map +1 -1
- package/build/modern/infiniteQueryBehavior.d.cts +1 -1
- package/build/modern/infiniteQueryBehavior.d.ts +1 -1
- package/build/modern/infiniteQueryBehavior.js +13 -4
- package/build/modern/infiniteQueryBehavior.js.map +1 -1
- package/build/modern/infiniteQueryObserver.d.cts +1 -1
- package/build/modern/infiniteQueryObserver.d.ts +1 -1
- package/build/modern/mutation.d.cts +1 -1
- package/build/modern/mutation.d.ts +1 -1
- package/build/modern/mutationCache.d.cts +1 -1
- package/build/modern/mutationCache.d.ts +1 -1
- package/build/modern/mutationObserver.d.cts +1 -1
- package/build/modern/mutationObserver.d.ts +1 -1
- package/build/modern/queriesObserver.d.cts +1 -1
- package/build/modern/queriesObserver.d.ts +1 -1
- package/build/modern/query.cjs +8 -1
- 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 +9 -2
- package/build/modern/query.js.map +1 -1
- package/build/modern/queryCache.d.cts +1 -1
- package/build/modern/queryCache.d.ts +1 -1
- package/build/modern/{queryClient-JUWz6dOI.d.cts → queryClient-QsbA-3YL.d.cts} +4 -2
- package/build/modern/{queryClient-p98HhdxQ.d.ts → queryClient-pTTqDRDI.d.ts} +4 -2
- package/build/modern/queryClient.cjs +9 -3
- 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 +11 -4
- package/build/modern/queryClient.js.map +1 -1
- package/build/modern/queryObserver.d.cts +1 -1
- package/build/modern/queryObserver.d.ts +1 -1
- package/build/modern/retryer.d.cts +1 -1
- package/build/modern/retryer.d.ts +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/utils.cjs +3 -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 +2 -0
- package/build/modern/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +2 -1
- package/src/infiniteQueryBehavior.ts +15 -6
- package/src/query.ts +11 -2
- package/src/queryClient.ts +14 -3
- package/src/tests/queryClient.test.tsx +88 -11
- package/src/types.ts +2 -2
- package/src/utils.ts +3 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils.ts"],"sourcesContent":["import type { Mutation } from './mutation'\nimport type { Query } from './query'\nimport type {\n FetchStatus,\n MutationKey,\n MutationStatus,\n QueryKey,\n QueryOptions,\n} from './types'\n\n// TYPES\n\nexport interface QueryFilters {\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?: QueryKey\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 /**\n * Match mutation key exactly\n */\n exact?: boolean\n /**\n * Include mutations matching this predicate function\n */\n predicate?: (mutation: Mutation<any, any, any>) => boolean\n /**\n * Include mutations matching this mutation key\n */\n mutationKey?: 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 window\n\nexport function noop(): undefined {\n return undefined\n}\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 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).some((key) => !partialMatchKey(a[key], b[key]))\n }\n\n return false\n}\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): T\nexport function replaceEqualDeep(a: any, b: any): any {\n if (a === b) {\n return a\n }\n\n const array = isPlainArray(a) && isPlainArray(b)\n\n if (array || (isPlainObject(a) && isPlainObject(b))) {\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 ? [] : {}\n\n let equalItems = 0\n\n for (let i = 0; i < bSize; i++) {\n const key = array ? i : bItems[i]\n if (\n !array &&\n a[key] === undefined &&\n b[key] === undefined &&\n aItems.includes(key)\n ) {\n copy[key] = undefined\n equalItems++\n } else {\n copy[key] = replaceEqualDeep(a[key], b[key])\n if (copy[key] === a[key] && a[key] !== undefined) {\n equalItems++\n }\n }\n }\n\n return aSize === bSize && equalItems === aSize ? a : copy\n }\n\n return b\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) {\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 Object {\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 // 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(ms: number): Promise<void> {\n return new Promise((resolve) => {\n setTimeout(resolve, ms)\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 // 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"],"mappings":";AAgEO,IAAM,WAAW,OAAO,WAAW,eAAe,UAAU;AAE5D,SAAS,OAAkB;AAChC,SAAO;AACT;AAEO,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,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,QAAI,SAAS,cAAc,UAAU;AACnC,aAAO;AAAA,IACT;AAAA,EACF;AAEA,MAAI,OAAO,UAAU,aAAa,MAAM,QAAQ,MAAM,OAAO;AAC3D,WAAO;AAAA,EACT;AAEA,MAAI,eAAe,gBAAgB,MA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"],"sourcesContent":["import type { Mutation } from './mutation'\nimport type { Query } from './query'\nimport type {\n FetchStatus,\n MutationKey,\n MutationStatus,\n QueryKey,\n QueryOptions,\n} from './types'\n\n// TYPES\n\nexport interface QueryFilters {\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?: QueryKey\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 /**\n * Match mutation key exactly\n */\n exact?: boolean\n /**\n * Include mutations matching this predicate function\n */\n predicate?: (mutation: Mutation<any, any, any>) => boolean\n /**\n * Include mutations matching this mutation key\n */\n mutationKey?: 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 window\n\nexport function noop(): undefined {\n return undefined\n}\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 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).some((key) => !partialMatchKey(a[key], b[key]))\n }\n\n return false\n}\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): T\nexport function replaceEqualDeep(a: any, b: any): any {\n if (a === b) {\n return a\n }\n\n const array = isPlainArray(a) && isPlainArray(b)\n\n if (array || (isPlainObject(a) && isPlainObject(b))) {\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 ? [] : {}\n\n let equalItems = 0\n\n for (let i = 0; i < bSize; i++) {\n const key = array ? i : bItems[i]\n if (\n !array &&\n a[key] === undefined &&\n b[key] === undefined &&\n aItems.includes(key)\n ) {\n copy[key] = undefined\n equalItems++\n } else {\n copy[key] = replaceEqualDeep(a[key], b[key])\n if (copy[key] === a[key] && a[key] !== undefined) {\n equalItems++\n }\n }\n }\n\n return aSize === bSize && equalItems === aSize ? a : copy\n }\n\n return b\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) {\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 Object {\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 // 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(ms: number): Promise<void> {\n return new Promise((resolve) => {\n setTimeout(resolve, ms)\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 // 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"],"mappings":";AAgEO,IAAM,WAAW,OAAO,WAAW,eAAe,UAAU;AAE5D,SAAS,OAAkB;AAChC,SAAO;AACT;AAEO,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,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,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WAAW,CAAC,GAAG,OAAO,IAAI;AAChC,SAAO,OAAO,SAAS,SAAS,MAAM,SAAS,MAAM,CAAC,IAAI;AAC5D;AAEO,SAAS,WAAc,OAAiB,MAAS,MAAM,GAAa;AACzE,QAAM,WAAW,CAAC,MAAM,GAAG,KAAK;AAChC,SAAO,OAAO,SAAS,SAAS,MAAM,SAAS,MAAM,GAAG,EAAE,IAAI;AAChE;AAEO,IAAM,YAAY,OAAO;","names":[]}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -20,8 +20,9 @@ export {
|
|
|
20
20
|
matchQuery,
|
|
21
21
|
matchMutation,
|
|
22
22
|
keepPreviousData,
|
|
23
|
+
skipToken,
|
|
23
24
|
} from './utils'
|
|
24
|
-
export type { MutationFilters, QueryFilters, Updater } from './utils'
|
|
25
|
+
export type { MutationFilters, QueryFilters, Updater, SkipToken } from './utils'
|
|
25
26
|
export { isCancelledError } from './retryer'
|
|
26
27
|
export {
|
|
27
28
|
dehydrate,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { addToEnd, addToStart } from './utils'
|
|
1
|
+
import { addToEnd, addToStart, skipToken } from './utils'
|
|
2
2
|
import type { QueryBehavior } from './query'
|
|
3
3
|
import type {
|
|
4
4
|
InfiniteData,
|
|
@@ -38,11 +38,20 @@ export function infiniteQueryBehavior<TQueryFnData, TError, TData, TPageParam>(
|
|
|
38
38
|
|
|
39
39
|
// Get query function
|
|
40
40
|
const queryFn =
|
|
41
|
-
context.options.queryFn
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
41
|
+
context.options.queryFn && context.options.queryFn !== skipToken
|
|
42
|
+
? context.options.queryFn
|
|
43
|
+
: () => {
|
|
44
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
45
|
+
if (context.options.queryFn === skipToken) {
|
|
46
|
+
console.error(
|
|
47
|
+
`Attempted to invoke queryFn when set to skipToken. This is likely a configuration error. Query hash: '${context.options.queryHash}'`,
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return Promise.reject(
|
|
52
|
+
new Error(`Missing queryFn: '${context.options.queryHash}'`),
|
|
53
|
+
)
|
|
54
|
+
}
|
|
46
55
|
|
|
47
56
|
// Create function to fetch a page
|
|
48
57
|
const fetchPage = async (
|
package/src/query.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { noop, replaceData, timeUntilStale } from './utils'
|
|
1
|
+
import { noop, replaceData, skipToken, timeUntilStale } from './utils'
|
|
2
2
|
import { notifyManager } from './notifyManager'
|
|
3
3
|
import { canFetch, createRetryer, isCancelledError } from './retryer'
|
|
4
4
|
import { Removable } from './removable'
|
|
@@ -387,11 +387,20 @@ export class Query<
|
|
|
387
387
|
|
|
388
388
|
// Create fetch function
|
|
389
389
|
const fetchFn = () => {
|
|
390
|
-
if (
|
|
390
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
391
|
+
if (this.options.queryFn === skipToken) {
|
|
392
|
+
console.error(
|
|
393
|
+
`Attempted to invoke queryFn when set to skipToken. This is likely a configuration error. Query hash: '${this.options.queryHash}'`,
|
|
394
|
+
)
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
if (!this.options.queryFn || this.options.queryFn === skipToken) {
|
|
391
399
|
return Promise.reject(
|
|
392
400
|
new Error(`Missing queryFn: '${this.options.queryHash}'`),
|
|
393
401
|
)
|
|
394
402
|
}
|
|
403
|
+
|
|
395
404
|
this.#abortSignalConsumed = false
|
|
396
405
|
if (this.options.persister) {
|
|
397
406
|
return this.options.persister(
|
package/src/queryClient.ts
CHANGED
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
hashQueryKeyByOptions,
|
|
5
5
|
noop,
|
|
6
6
|
partialMatchKey,
|
|
7
|
+
skipToken,
|
|
7
8
|
} from './utils'
|
|
8
9
|
import { QueryCache } from './queryCache'
|
|
9
10
|
import { MutationCache } from './mutationCache'
|
|
@@ -80,8 +81,8 @@ export class QueryClient {
|
|
|
80
81
|
this.#queryCache.onFocus()
|
|
81
82
|
}
|
|
82
83
|
})
|
|
83
|
-
this.#unsubscribeOnline = onlineManager.subscribe(() => {
|
|
84
|
-
if (
|
|
84
|
+
this.#unsubscribeOnline = onlineManager.subscribe((online) => {
|
|
85
|
+
if (online) {
|
|
85
86
|
this.resumePausedMutations()
|
|
86
87
|
this.#queryCache.onOnline()
|
|
87
88
|
}
|
|
@@ -391,7 +392,10 @@ export class QueryClient {
|
|
|
391
392
|
}
|
|
392
393
|
|
|
393
394
|
resumePausedMutations(): Promise<unknown> {
|
|
394
|
-
|
|
395
|
+
if (onlineManager.isOnline()) {
|
|
396
|
+
return this.#mutationCache.resumePausedMutations()
|
|
397
|
+
}
|
|
398
|
+
return Promise.resolve()
|
|
395
399
|
}
|
|
396
400
|
|
|
397
401
|
getQueryCache(): QueryCache {
|
|
@@ -534,6 +538,13 @@ export class QueryClient {
|
|
|
534
538
|
defaultedOptions.networkMode = 'offlineFirst'
|
|
535
539
|
}
|
|
536
540
|
|
|
541
|
+
if (
|
|
542
|
+
defaultedOptions.enabled !== true &&
|
|
543
|
+
defaultedOptions.queryFn === skipToken
|
|
544
|
+
) {
|
|
545
|
+
defaultedOptions.enabled = false
|
|
546
|
+
}
|
|
547
|
+
|
|
537
548
|
return defaultedOptions as DefaultedQueryObserverOptions<
|
|
538
549
|
TQueryFnData,
|
|
539
550
|
TError,
|
|
@@ -3,23 +3,21 @@ import { waitFor } from '@testing-library/react'
|
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
5
|
MutationObserver,
|
|
6
|
+
QueryClient,
|
|
6
7
|
QueryObserver,
|
|
8
|
+
dehydrate,
|
|
7
9
|
focusManager,
|
|
10
|
+
hydrate,
|
|
8
11
|
onlineManager,
|
|
9
12
|
} from '..'
|
|
10
|
-
import {
|
|
13
|
+
import { skipToken } from '../utils'
|
|
11
14
|
import {
|
|
12
15
|
createQueryClient,
|
|
13
16
|
mockOnlineManagerIsOnline,
|
|
14
17
|
queryKey,
|
|
15
18
|
sleep,
|
|
16
19
|
} from './utils'
|
|
17
|
-
import type {
|
|
18
|
-
QueryCache,
|
|
19
|
-
QueryClient,
|
|
20
|
-
QueryFunction,
|
|
21
|
-
QueryObserverOptions,
|
|
22
|
-
} from '..'
|
|
20
|
+
import type { QueryCache, QueryFunction, QueryObserverOptions } from '..'
|
|
23
21
|
|
|
24
22
|
describe('queryClient', () => {
|
|
25
23
|
let queryClient: QueryClient
|
|
@@ -1357,6 +1355,7 @@ describe('queryClient', () => {
|
|
|
1357
1355
|
test('should refetch all active queries', async () => {
|
|
1358
1356
|
const key1 = queryKey()
|
|
1359
1357
|
const key2 = queryKey()
|
|
1358
|
+
const key3 = queryKey()
|
|
1360
1359
|
const queryFn1 = vi.fn<Array<unknown>, string>().mockReturnValue('data1')
|
|
1361
1360
|
const queryFn2 = vi.fn<Array<unknown>, string>().mockReturnValue('data2')
|
|
1362
1361
|
const observer1 = new QueryObserver(queryClient, {
|
|
@@ -1369,13 +1368,21 @@ describe('queryClient', () => {
|
|
|
1369
1368
|
queryFn: queryFn2,
|
|
1370
1369
|
enabled: false,
|
|
1371
1370
|
})
|
|
1371
|
+
const observer3 = new QueryObserver(queryClient, {
|
|
1372
|
+
queryKey: key3,
|
|
1373
|
+
queryFn: skipToken,
|
|
1374
|
+
})
|
|
1375
|
+
let didSkipTokenRun = false
|
|
1372
1376
|
observer1.subscribe(() => undefined)
|
|
1373
1377
|
observer2.subscribe(() => undefined)
|
|
1378
|
+
observer3.subscribe(() => (didSkipTokenRun = true))
|
|
1374
1379
|
await queryClient.resetQueries()
|
|
1380
|
+
observer3.destroy()
|
|
1375
1381
|
observer2.destroy()
|
|
1376
1382
|
observer1.destroy()
|
|
1377
1383
|
expect(queryFn1).toHaveBeenCalledTimes(2)
|
|
1378
1384
|
expect(queryFn2).toHaveBeenCalledTimes(0)
|
|
1385
|
+
expect(didSkipTokenRun).toBe(false)
|
|
1379
1386
|
})
|
|
1380
1387
|
})
|
|
1381
1388
|
|
|
@@ -1458,8 +1465,8 @@ describe('queryClient', () => {
|
|
|
1458
1465
|
const observer2 = new MutationObserver(queryClient, {
|
|
1459
1466
|
mutationFn: async () => 2,
|
|
1460
1467
|
})
|
|
1461
|
-
void observer1.mutate()
|
|
1462
|
-
void observer2.mutate()
|
|
1468
|
+
void observer1.mutate()
|
|
1469
|
+
void observer2.mutate()
|
|
1463
1470
|
|
|
1464
1471
|
await waitFor(() => {
|
|
1465
1472
|
expect(observer1.getCurrentResult().isPaused).toBeTruthy()
|
|
@@ -1500,8 +1507,8 @@ describe('queryClient', () => {
|
|
|
1500
1507
|
return 2
|
|
1501
1508
|
},
|
|
1502
1509
|
})
|
|
1503
|
-
void observer1.mutate()
|
|
1504
|
-
void observer2.mutate()
|
|
1510
|
+
void observer1.mutate()
|
|
1511
|
+
void observer2.mutate()
|
|
1505
1512
|
|
|
1506
1513
|
await waitFor(() => {
|
|
1507
1514
|
expect(observer1.getCurrentResult().isPaused).toBeTruthy()
|
|
@@ -1521,6 +1528,76 @@ describe('queryClient', () => {
|
|
|
1521
1528
|
expect(orders).toEqual(['1start', '1end', '2start', '2end'])
|
|
1522
1529
|
})
|
|
1523
1530
|
|
|
1531
|
+
test('should resumePausedMutations when coming online after having called resumePausedMutations while offline', async () => {
|
|
1532
|
+
const consoleMock = vi.spyOn(console, 'error')
|
|
1533
|
+
consoleMock.mockImplementation(() => undefined)
|
|
1534
|
+
onlineManager.setOnline(false)
|
|
1535
|
+
|
|
1536
|
+
const observer = new MutationObserver(queryClient, {
|
|
1537
|
+
mutationFn: async () => 1,
|
|
1538
|
+
})
|
|
1539
|
+
|
|
1540
|
+
void observer.mutate()
|
|
1541
|
+
|
|
1542
|
+
expect(observer.getCurrentResult().isPaused).toBeTruthy()
|
|
1543
|
+
|
|
1544
|
+
await queryClient.resumePausedMutations()
|
|
1545
|
+
|
|
1546
|
+
// still paused because we are still offline
|
|
1547
|
+
expect(observer.getCurrentResult().isPaused).toBeTruthy()
|
|
1548
|
+
|
|
1549
|
+
onlineManager.setOnline(true)
|
|
1550
|
+
|
|
1551
|
+
await waitFor(() => {
|
|
1552
|
+
expect(observer.getCurrentResult().status).toBe('success')
|
|
1553
|
+
})
|
|
1554
|
+
})
|
|
1555
|
+
|
|
1556
|
+
test('should resumePausedMutations when coming online after having restored cache (and resumed) while offline', async () => {
|
|
1557
|
+
const consoleMock = vi.spyOn(console, 'error')
|
|
1558
|
+
consoleMock.mockImplementation(() => undefined)
|
|
1559
|
+
onlineManager.setOnline(false)
|
|
1560
|
+
|
|
1561
|
+
const observer = new MutationObserver(queryClient, {
|
|
1562
|
+
mutationFn: async () => 1,
|
|
1563
|
+
})
|
|
1564
|
+
|
|
1565
|
+
void observer.mutate()
|
|
1566
|
+
|
|
1567
|
+
expect(observer.getCurrentResult().isPaused).toBeTruthy()
|
|
1568
|
+
|
|
1569
|
+
const state = dehydrate(queryClient)
|
|
1570
|
+
|
|
1571
|
+
const newQueryClient = new QueryClient({
|
|
1572
|
+
defaultOptions: {
|
|
1573
|
+
mutations: {
|
|
1574
|
+
mutationFn: async () => 1,
|
|
1575
|
+
},
|
|
1576
|
+
},
|
|
1577
|
+
})
|
|
1578
|
+
|
|
1579
|
+
newQueryClient.mount()
|
|
1580
|
+
|
|
1581
|
+
hydrate(newQueryClient, state)
|
|
1582
|
+
|
|
1583
|
+
// still paused because we are still offline
|
|
1584
|
+
expect(
|
|
1585
|
+
newQueryClient.getMutationCache().getAll()[0]?.state.isPaused,
|
|
1586
|
+
).toBeTruthy()
|
|
1587
|
+
|
|
1588
|
+
await newQueryClient.resumePausedMutations()
|
|
1589
|
+
|
|
1590
|
+
onlineManager.setOnline(true)
|
|
1591
|
+
|
|
1592
|
+
await waitFor(() => {
|
|
1593
|
+
expect(
|
|
1594
|
+
newQueryClient.getMutationCache().getAll()[0]?.state.status,
|
|
1595
|
+
).toBe('success')
|
|
1596
|
+
})
|
|
1597
|
+
|
|
1598
|
+
newQueryClient.unmount()
|
|
1599
|
+
})
|
|
1600
|
+
|
|
1524
1601
|
test('should notify queryCache and mutationCache after multiple mounts and single unmount', async () => {
|
|
1525
1602
|
const testClient = createQueryClient()
|
|
1526
1603
|
testClient.mount()
|
package/src/types.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import type { MutationState } from './mutation'
|
|
4
4
|
import type { FetchDirection, Query, QueryBehavior } from './query'
|
|
5
5
|
import type { RetryDelayValue, RetryValue } from './retryer'
|
|
6
|
-
import type { QueryFilters, QueryTypeFilter } from './utils'
|
|
6
|
+
import type { QueryFilters, QueryTypeFilter, SkipToken } from './utils'
|
|
7
7
|
import type { QueryCache } from './queryCache'
|
|
8
8
|
import type { MutationCache } from './mutationCache'
|
|
9
9
|
|
|
@@ -147,7 +147,7 @@ export interface QueryOptions<
|
|
|
147
147
|
* Setting it to `Infinity` will disable garbage collection.
|
|
148
148
|
*/
|
|
149
149
|
gcTime?: number
|
|
150
|
-
queryFn?: QueryFunction<TQueryFnData, TQueryKey, TPageParam>
|
|
150
|
+
queryFn?: QueryFunction<TQueryFnData, TQueryKey, TPageParam> | SkipToken
|
|
151
151
|
persister?: QueryPersister<
|
|
152
152
|
NoInfer<TQueryFnData>,
|
|
153
153
|
NoInfer<TQueryKey>,
|
package/src/utils.ts
CHANGED
|
@@ -342,3 +342,6 @@ export function addToStart<T>(items: Array<T>, item: T, max = 0): Array<T> {
|
|
|
342
342
|
const newItems = [item, ...items]
|
|
343
343
|
return max && newItems.length > max ? newItems.slice(0, -1) : newItems
|
|
344
344
|
}
|
|
345
|
+
|
|
346
|
+
export const skipToken = Symbol()
|
|
347
|
+
export type SkipToken = typeof skipToken
|