@tanstack/query-core 5.24.5 → 5.24.6

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.
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/queryObserver.ts"],"sourcesContent":["import {\n isServer,\n isValidTimeout,\n noop,\n replaceData,\n shallowEqualObjects,\n timeUntilStale,\n} from './utils'\nimport { notifyManager } from './notifyManager'\nimport { focusManager } from './focusManager'\nimport { Subscribable } from './subscribable'\nimport { canFetch } from './retryer'\nimport type { QueryClient } from './queryClient'\nimport type { FetchOptions, Query, QueryState } from './query'\nimport type {\n DefaultError,\n DefaultedQueryObserverOptions,\n PlaceholderDataFunction,\n QueryKey,\n QueryObserverBaseResult,\n QueryObserverOptions,\n QueryObserverResult,\n QueryOptions,\n RefetchOptions,\n} from './types'\n\ntype QueryObserverListener<TData, TError> = (\n result: QueryObserverResult<TData, TError>,\n) => void\n\nexport interface NotifyOptions {\n listeners?: boolean\n}\n\nexport interface ObserverFetchOptions extends FetchOptions {\n throwOnError?: boolean\n}\n\nexport class QueryObserver<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n> extends Subscribable<QueryObserverListener<TData, TError>> {\n #client: QueryClient\n #currentQuery: Query<TQueryFnData, TError, TQueryData, TQueryKey> = undefined!\n #currentQueryInitialState: QueryState<TQueryData, TError> = undefined!\n #currentResult: QueryObserverResult<TData, TError> = undefined!\n #currentResultState?: QueryState<TQueryData, TError>\n #currentResultOptions?: QueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >\n #selectError: TError | null\n #selectFn?: (data: TQueryData) => TData\n #selectResult?: TData\n // This property keeps track of the last query with defined data.\n // It will be used to pass the previous data and query to the placeholder function between renders.\n #lastQueryWithDefinedData?: Query<TQueryFnData, TError, TQueryData, TQueryKey>\n #staleTimeoutId?: ReturnType<typeof setTimeout>\n #refetchIntervalId?: ReturnType<typeof setInterval>\n #currentRefetchInterval?: number | false\n #trackedProps = new Set<keyof QueryObserverResult>()\n\n constructor(\n client: QueryClient,\n public options: QueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n ) {\n super()\n\n this.#client = client\n this.#selectError = null\n this.bindMethods()\n this.setOptions(options)\n }\n\n protected bindMethods(): void {\n this.refetch = this.refetch.bind(this)\n }\n\n protected onSubscribe(): void {\n if (this.listeners.size === 1) {\n this.#currentQuery.addObserver(this)\n\n if (shouldFetchOnMount(this.#currentQuery, this.options)) {\n this.#executeFetch()\n } else {\n this.updateResult()\n }\n\n this.#updateTimers()\n }\n }\n\n protected onUnsubscribe(): void {\n if (!this.hasListeners()) {\n this.destroy()\n }\n }\n\n shouldFetchOnReconnect(): boolean {\n return shouldFetchOn(\n this.#currentQuery,\n this.options,\n this.options.refetchOnReconnect,\n )\n }\n\n shouldFetchOnWindowFocus(): boolean {\n return shouldFetchOn(\n this.#currentQuery,\n this.options,\n this.options.refetchOnWindowFocus,\n )\n }\n\n destroy(): void {\n this.listeners = new Set()\n this.#clearStaleTimeout()\n this.#clearRefetchInterval()\n this.#currentQuery.removeObserver(this)\n }\n\n setOptions(\n options: QueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n notifyOptions?: NotifyOptions,\n ): void {\n const prevOptions = this.options\n const prevQuery = this.#currentQuery\n\n this.options = this.#client.defaultQueryOptions(options)\n\n if (\n this.options.enabled !== undefined &&\n typeof this.options.enabled !== 'boolean'\n ) {\n throw new Error('Expected enabled to be a boolean')\n }\n\n this.#updateQuery()\n\n if (!shallowEqualObjects(this.options, prevOptions)) {\n this.#client.getQueryCache().notify({\n type: 'observerOptionsUpdated',\n query: this.#currentQuery,\n observer: this,\n })\n }\n\n const mounted = this.hasListeners()\n\n // Fetch if there are subscribers\n if (\n mounted &&\n shouldFetchOptionally(\n this.#currentQuery,\n prevQuery,\n this.options,\n prevOptions,\n )\n ) {\n this.#executeFetch()\n }\n\n // Update result\n this.updateResult(notifyOptions)\n\n // Update stale interval if needed\n if (\n mounted &&\n (this.#currentQuery !== prevQuery ||\n this.options.enabled !== prevOptions.enabled ||\n this.options.staleTime !== prevOptions.staleTime)\n ) {\n this.#updateStaleTimeout()\n }\n\n const nextRefetchInterval = this.#computeRefetchInterval()\n\n // Update refetch interval if needed\n if (\n mounted &&\n (this.#currentQuery !== prevQuery ||\n this.options.enabled !== prevOptions.enabled ||\n nextRefetchInterval !== this.#currentRefetchInterval)\n ) {\n this.#updateRefetchInterval(nextRefetchInterval)\n }\n }\n\n getOptimisticResult(\n options: DefaultedQueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n ): QueryObserverResult<TData, TError> {\n const query = this.#client.getQueryCache().build(this.#client, options)\n\n const result = this.createResult(query, options)\n\n if (shouldAssignObserverCurrentProperties(this, result)) {\n // this assigns the optimistic result to the current Observer\n // because if the query function changes, useQuery will be performing\n // an effect where it would fetch again.\n // When the fetch finishes, we perform a deep data cloning in order\n // to reuse objects references. This deep data clone is performed against\n // the `observer.currentResult.data` property\n // When QueryKey changes, we refresh the query and get new `optimistic`\n // result, while we leave the `observer.currentResult`, so when new data\n // arrives, it finds the old `observer.currentResult` which is related\n // to the old QueryKey. Which means that currentResult and selectData are\n // out of sync already.\n // To solve this, we move the cursor of the currentResult every time\n // an observer reads an optimistic value.\n\n // When keeping the previous data, the result doesn't change until new\n // data arrives.\n this.#currentResult = result\n this.#currentResultOptions = this.options\n this.#currentResultState = this.#currentQuery.state\n }\n return result\n }\n\n getCurrentResult(): QueryObserverResult<TData, TError> {\n return this.#currentResult\n }\n\n trackResult(\n result: QueryObserverResult<TData, TError>,\n ): QueryObserverResult<TData, TError> {\n const trackedResult = {} as QueryObserverResult<TData, TError>\n\n Object.keys(result).forEach((key) => {\n Object.defineProperty(trackedResult, key, {\n configurable: false,\n enumerable: true,\n get: () => {\n this.#trackedProps.add(key as keyof QueryObserverResult)\n return result[key as keyof QueryObserverResult]\n },\n })\n })\n\n return trackedResult\n }\n\n getCurrentQuery(): Query<TQueryFnData, TError, TQueryData, TQueryKey> {\n return this.#currentQuery\n }\n\n refetch({ ...options }: RefetchOptions = {}): Promise<\n QueryObserverResult<TData, TError>\n > {\n return this.fetch({\n ...options,\n })\n }\n\n fetchOptimistic(\n options: QueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n ): Promise<QueryObserverResult<TData, TError>> {\n const defaultedOptions = this.#client.defaultQueryOptions(options)\n\n const query = this.#client\n .getQueryCache()\n .build(this.#client, defaultedOptions)\n query.isFetchingOptimistic = true\n\n return query.fetch().then(() => this.createResult(query, defaultedOptions))\n }\n\n protected fetch(\n fetchOptions: ObserverFetchOptions,\n ): Promise<QueryObserverResult<TData, TError>> {\n return this.#executeFetch({\n ...fetchOptions,\n cancelRefetch: fetchOptions.cancelRefetch ?? true,\n }).then(() => {\n this.updateResult()\n return this.#currentResult\n })\n }\n\n #executeFetch(\n fetchOptions?: ObserverFetchOptions,\n ): Promise<TQueryData | undefined> {\n // Make sure we reference the latest query as the current one might have been removed\n this.#updateQuery()\n\n // Fetch\n let promise: Promise<TQueryData | undefined> = this.#currentQuery.fetch(\n this.options as QueryOptions<TQueryFnData, TError, TQueryData, TQueryKey>,\n fetchOptions,\n )\n\n if (!fetchOptions?.throwOnError) {\n promise = promise.catch(noop)\n }\n\n return promise\n }\n\n #updateStaleTimeout(): void {\n this.#clearStaleTimeout()\n\n if (\n isServer ||\n this.#currentResult.isStale ||\n !isValidTimeout(this.options.staleTime)\n ) {\n return\n }\n\n const time = timeUntilStale(\n this.#currentResult.dataUpdatedAt,\n this.options.staleTime,\n )\n\n // The timeout is sometimes triggered 1 ms before the stale time expiration.\n // To mitigate this issue we always add 1 ms to the timeout.\n const timeout = time + 1\n\n this.#staleTimeoutId = setTimeout(() => {\n if (!this.#currentResult.isStale) {\n this.updateResult()\n }\n }, timeout)\n }\n\n #computeRefetchInterval() {\n return (\n (typeof this.options.refetchInterval === 'function'\n ? this.options.refetchInterval(this.#currentQuery)\n : this.options.refetchInterval) ?? false\n )\n }\n\n #updateRefetchInterval(nextInterval: number | false): void {\n this.#clearRefetchInterval()\n\n this.#currentRefetchInterval = nextInterval\n\n if (\n isServer ||\n this.options.enabled === false ||\n !isValidTimeout(this.#currentRefetchInterval) ||\n this.#currentRefetchInterval === 0\n ) {\n return\n }\n\n this.#refetchIntervalId = setInterval(() => {\n if (\n this.options.refetchIntervalInBackground ||\n focusManager.isFocused()\n ) {\n this.#executeFetch()\n }\n }, this.#currentRefetchInterval)\n }\n\n #updateTimers(): void {\n this.#updateStaleTimeout()\n this.#updateRefetchInterval(this.#computeRefetchInterval())\n }\n\n #clearStaleTimeout(): void {\n if (this.#staleTimeoutId) {\n clearTimeout(this.#staleTimeoutId)\n this.#staleTimeoutId = undefined\n }\n }\n\n #clearRefetchInterval(): void {\n if (this.#refetchIntervalId) {\n clearInterval(this.#refetchIntervalId)\n this.#refetchIntervalId = undefined\n }\n }\n\n protected createResult(\n query: Query<TQueryFnData, TError, TQueryData, TQueryKey>,\n options: QueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n ): QueryObserverResult<TData, TError> {\n const prevQuery = this.#currentQuery\n const prevOptions = this.options\n const prevResult = this.#currentResult as\n | QueryObserverResult<TData, TError>\n | undefined\n const prevResultState = this.#currentResultState\n const prevResultOptions = this.#currentResultOptions\n const queryChange = query !== prevQuery\n const queryInitialState = queryChange\n ? query.state\n : this.#currentQueryInitialState\n\n const { state } = query\n let { error, errorUpdatedAt, fetchStatus, status } = state\n let isPlaceholderData = false\n let data: TData | undefined\n\n // Optimistically set result in fetching state if needed\n if (options._optimisticResults) {\n const mounted = this.hasListeners()\n\n const fetchOnMount = !mounted && shouldFetchOnMount(query, options)\n\n const fetchOptionally =\n mounted && shouldFetchOptionally(query, prevQuery, options, prevOptions)\n\n if (fetchOnMount || fetchOptionally) {\n fetchStatus = canFetch(query.options.networkMode)\n ? 'fetching'\n : 'paused'\n if (!state.dataUpdatedAt) {\n status = 'pending'\n }\n }\n if (options._optimisticResults === 'isRestoring') {\n fetchStatus = 'idle'\n }\n }\n\n // Select data if needed\n if (options.select && state.data !== undefined) {\n // Memoize select result\n if (\n prevResult &&\n state.data === prevResultState?.data &&\n options.select === this.#selectFn\n ) {\n data = this.#selectResult\n } else {\n try {\n this.#selectFn = options.select\n data = options.select(state.data)\n data = replaceData(prevResult?.data, data, options)\n this.#selectResult = data\n this.#selectError = null\n } catch (selectError) {\n this.#selectError = selectError as TError\n }\n }\n }\n // Use query data\n else {\n data = state.data as unknown as TData\n }\n\n // Show placeholder data if needed\n if (\n options.placeholderData !== undefined &&\n data === undefined &&\n status === 'pending'\n ) {\n let placeholderData\n\n // Memoize placeholder data\n if (\n prevResult?.isPlaceholderData &&\n options.placeholderData === prevResultOptions?.placeholderData\n ) {\n placeholderData = prevResult.data\n } else {\n placeholderData =\n typeof options.placeholderData === 'function'\n ? (\n options.placeholderData as unknown as PlaceholderDataFunction<TQueryData>\n )(\n this.#lastQueryWithDefinedData?.state.data,\n this.#lastQueryWithDefinedData as any,\n )\n : options.placeholderData\n if (options.select && placeholderData !== undefined) {\n try {\n placeholderData = options.select(placeholderData)\n this.#selectError = null\n } catch (selectError) {\n this.#selectError = selectError as TError\n }\n }\n }\n\n if (placeholderData !== undefined) {\n status = 'success'\n data = replaceData(\n prevResult?.data,\n placeholderData as unknown,\n options,\n ) as TData\n isPlaceholderData = true\n }\n }\n\n if (this.#selectError) {\n error = this.#selectError as any\n data = this.#selectResult\n errorUpdatedAt = Date.now()\n status = 'error'\n }\n\n const isFetching = fetchStatus === 'fetching'\n const isPending = status === 'pending'\n const isError = status === 'error'\n\n const isLoading = isPending && isFetching\n\n const result: QueryObserverBaseResult<TData, TError> = {\n status,\n fetchStatus,\n isPending,\n isSuccess: status === 'success',\n isError,\n isInitialLoading: isLoading,\n isLoading,\n data,\n dataUpdatedAt: state.dataUpdatedAt,\n error,\n errorUpdatedAt,\n failureCount: state.fetchFailureCount,\n failureReason: state.fetchFailureReason,\n errorUpdateCount: state.errorUpdateCount,\n isFetched: state.dataUpdateCount > 0 || state.errorUpdateCount > 0,\n isFetchedAfterMount:\n state.dataUpdateCount > queryInitialState.dataUpdateCount ||\n state.errorUpdateCount > queryInitialState.errorUpdateCount,\n isFetching,\n isRefetching: isFetching && !isPending,\n isLoadingError: isError && state.dataUpdatedAt === 0,\n isPaused: fetchStatus === 'paused',\n isPlaceholderData,\n isRefetchError: isError && state.dataUpdatedAt !== 0,\n isStale: isStale(query, options),\n refetch: this.refetch,\n }\n\n return result as QueryObserverResult<TData, TError>\n }\n\n updateResult(notifyOptions?: NotifyOptions): void {\n const prevResult = this.#currentResult as\n | QueryObserverResult<TData, TError>\n | undefined\n\n const nextResult = this.createResult(this.#currentQuery, this.options)\n this.#currentResultState = this.#currentQuery.state\n this.#currentResultOptions = this.options\n\n if (this.#currentResultState.data !== undefined) {\n this.#lastQueryWithDefinedData = this.#currentQuery\n }\n\n // Only notify and update result if something has changed\n if (shallowEqualObjects(nextResult, prevResult)) {\n return\n }\n\n this.#currentResult = nextResult\n\n // Determine which callbacks to trigger\n const defaultNotifyOptions: NotifyOptions = {}\n\n const shouldNotifyListeners = (): boolean => {\n if (!prevResult) {\n return true\n }\n\n const { notifyOnChangeProps } = this.options\n const notifyOnChangePropsValue =\n typeof notifyOnChangeProps === 'function'\n ? notifyOnChangeProps()\n : notifyOnChangeProps\n\n if (\n notifyOnChangePropsValue === 'all' ||\n (!notifyOnChangePropsValue && !this.#trackedProps.size)\n ) {\n return true\n }\n\n const includedProps = new Set(\n notifyOnChangePropsValue ?? this.#trackedProps,\n )\n\n if (this.options.throwOnError) {\n includedProps.add('error')\n }\n\n return Object.keys(this.#currentResult).some((key) => {\n const typedKey = key as keyof QueryObserverResult\n const changed = this.#currentResult[typedKey] !== prevResult[typedKey]\n return changed && includedProps.has(typedKey)\n })\n }\n\n if (notifyOptions?.listeners !== false && shouldNotifyListeners()) {\n defaultNotifyOptions.listeners = true\n }\n\n this.#notify({ ...defaultNotifyOptions, ...notifyOptions })\n }\n\n #updateQuery(): void {\n const query = this.#client.getQueryCache().build(this.#client, this.options)\n\n if (query === this.#currentQuery) {\n return\n }\n\n const prevQuery = this.#currentQuery as\n | Query<TQueryFnData, TError, TQueryData, TQueryKey>\n | undefined\n this.#currentQuery = query\n this.#currentQueryInitialState = query.state\n\n if (this.hasListeners()) {\n prevQuery?.removeObserver(this)\n query.addObserver(this)\n }\n }\n\n onQueryUpdate(): void {\n this.updateResult()\n\n if (this.hasListeners()) {\n this.#updateTimers()\n }\n }\n\n #notify(notifyOptions: NotifyOptions): void {\n notifyManager.batch(() => {\n // First, trigger the listeners\n if (notifyOptions.listeners) {\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n }\n\n // Then the cache listeners\n this.#client.getQueryCache().notify({\n query: this.#currentQuery,\n type: 'observerResultsUpdated',\n })\n })\n }\n}\n\nfunction shouldLoadOnMount(\n query: Query<any, any, any, any>,\n options: QueryObserverOptions<any, any, any, any>,\n): boolean {\n return (\n options.enabled !== false &&\n !query.state.dataUpdatedAt &&\n !(query.state.status === 'error' && options.retryOnMount === false)\n )\n}\n\nfunction shouldFetchOnMount(\n query: Query<any, any, any, any>,\n options: QueryObserverOptions<any, any, any, any, any>,\n): boolean {\n return (\n shouldLoadOnMount(query, options) ||\n (query.state.dataUpdatedAt > 0 &&\n shouldFetchOn(query, options, options.refetchOnMount))\n )\n}\n\nfunction shouldFetchOn(\n query: Query<any, any, any, any>,\n options: QueryObserverOptions<any, any, any, any, any>,\n field: (typeof options)['refetchOnMount'] &\n (typeof options)['refetchOnWindowFocus'] &\n (typeof options)['refetchOnReconnect'],\n) {\n if (options.enabled !== false) {\n const value = typeof field === 'function' ? field(query) : field\n\n return value === 'always' || (value !== false && isStale(query, options))\n }\n return false\n}\n\nfunction shouldFetchOptionally(\n query: Query<any, any, any, any>,\n prevQuery: Query<any, any, any, any>,\n options: QueryObserverOptions<any, any, any, any, any>,\n prevOptions: QueryObserverOptions<any, any, any, any, any>,\n): boolean {\n return (\n options.enabled !== false &&\n (query !== prevQuery || prevOptions.enabled === false) &&\n (!options.suspense || query.state.status !== 'error') &&\n isStale(query, options)\n )\n}\n\nfunction isStale(\n query: Query<any, any, any, any>,\n options: QueryObserverOptions<any, any, any, any, any>,\n): boolean {\n return query.isStaleByTime(options.staleTime)\n}\n\n// this function would decide if we will update the observer's 'current'\n// properties after an optimistic reading via getOptimisticResult\nfunction shouldAssignObserverCurrentProperties<\n TQueryFnData = unknown,\n TError = unknown,\n TData = TQueryFnData,\n TQueryData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n observer: QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>,\n optimisticResult: QueryObserverResult<TData, TError>,\n) {\n // if the newly created result isn't what the observer is holding as current,\n // then we'll need to update the properties as well\n if (!shallowEqualObjects(observer.getCurrentResult(), optimisticResult)) {\n return true\n }\n\n // basically, just keep previous properties if nothing changed\n return false\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAOO;AACP,2BAA8B;AAC9B,0BAA6B;AAC7B,0BAA6B;AAC7B,qBAAyB;AA2BlB,IAAM,gBAAN,cAMG,iCAAmD;AAAA,EAwB3D,YACE,QACO,SAOP;AACA,UAAM;AARC;AAUP,SAAK,UAAU;AACf,SAAK,eAAe;AACpB,SAAK,YAAY;AACjB,SAAK,WAAW,OAAO;AAAA,EACzB;AAAA,EAvCA;AAAA,EACA,gBAAoE;AAAA,EACpE,4BAA4D;AAAA,EAC5D,iBAAqD;AAAA,EACrD;AAAA,EACA;AAAA,EAOA;AAAA,EACA;AAAA,EACA;AAAA;AAAA;AAAA,EAGA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAC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,cAAc;AAAA,QAC9B,CAAC;AAAA,MACH;AAGA,WAAK,QAAQ,cAAc,EAAE,OAAO;AAAA,QAClC,OAAO,KAAK;AAAA,QACZ,MAAM;AAAA,MACR,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AACF;AAEA,SAAS,kBACP,OACA,SACS;AACT,SACE,QAAQ,YAAY,SACpB,CAAC,MAAM,MAAM,iBACb,EAAE,MAAM,MAAM,WAAW,WAAW,QAAQ,iBAAiB;AAEjE;AAEA,SAAS,mBACP,OACA,SACS;AACT,SACE,kBAAkB,OAAO,OAAO,KAC/B,MAAM,MAAM,gBAAgB,KAC3B,cAAc,OAAO,SAAS,QAAQ,cAAc;AAE1D;AAEA,SAAS,cACP,OACA,SACA,OAGA;AACA,MAAI,QAAQ,YAAY,OAAO;AAC7B,UAAM,QAAQ,OAAO,UAAU,aAAa,MAAM,KAAK,IAAI;AAE3D,WAAO,UAAU,YAAa,UAAU,SAAS,QAAQ,OAAO,OAAO;AAAA,EACzE;AACA,SAAO;AACT;AAEA,SAAS,sBACP,OACA,WACA,SACA,aACS;AACT,SACE,QAAQ,YAAY,UACnB,UAAU,aAAa,YAAY,YAAY,WAC/C,CAAC,QAAQ,YAAY,MAAM,MAAM,WAAW,YAC7C,QAAQ,OAAO,OAAO;AAE1B;AAEA,SAAS,QACP,OACA,SACS;AACT,SAAO,MAAM,cAAc,QAAQ,SAAS;AAC9C;AAIA,SAAS,sCAOP,UACA,kBACA;AAGA,MAAI,KAAC,kCAAoB,SAAS,iBAAiB,GAAG,gBAAgB,GAAG;AACvE,WAAO;AAAA,EACT;AAGA,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../../src/queryObserver.ts"],"sourcesContent":["import {\n isServer,\n isValidTimeout,\n noop,\n replaceData,\n shallowEqualObjects,\n timeUntilStale,\n} from './utils'\nimport { notifyManager } from './notifyManager'\nimport { focusManager } from './focusManager'\nimport { Subscribable } from './subscribable'\nimport { canFetch } from './retryer'\nimport type { QueryClient } from './queryClient'\nimport type { FetchOptions, Query, QueryState } from './query'\nimport type {\n DefaultError,\n DefaultedQueryObserverOptions,\n PlaceholderDataFunction,\n QueryKey,\n QueryObserverBaseResult,\n QueryObserverOptions,\n QueryObserverResult,\n QueryOptions,\n RefetchOptions,\n} from './types'\n\ntype QueryObserverListener<TData, TError> = (\n result: QueryObserverResult<TData, TError>,\n) => void\n\nexport interface NotifyOptions {\n listeners?: boolean\n}\n\nexport interface ObserverFetchOptions extends FetchOptions {\n throwOnError?: boolean\n}\n\nexport class QueryObserver<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n> extends Subscribable<QueryObserverListener<TData, TError>> {\n #client: QueryClient\n #currentQuery: Query<TQueryFnData, TError, TQueryData, TQueryKey> = undefined!\n #currentQueryInitialState: QueryState<TQueryData, TError> = undefined!\n #currentResult: QueryObserverResult<TData, TError> = undefined!\n #currentResultState?: QueryState<TQueryData, TError>\n #currentResultOptions?: QueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >\n #selectError: TError | null\n #selectFn?: (data: TQueryData) => TData\n #selectResult?: TData\n // This property keeps track of the last query with defined data.\n // It will be used to pass the previous data and query to the placeholder function between renders.\n #lastQueryWithDefinedData?: Query<TQueryFnData, TError, TQueryData, TQueryKey>\n #staleTimeoutId?: ReturnType<typeof setTimeout>\n #refetchIntervalId?: ReturnType<typeof setInterval>\n #currentRefetchInterval?: number | false\n #trackedProps = new Set<keyof QueryObserverResult>()\n\n constructor(\n client: QueryClient,\n public options: QueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n ) {\n super()\n\n this.#client = client\n this.#selectError = null\n this.bindMethods()\n this.setOptions(options)\n }\n\n protected bindMethods(): void {\n this.refetch = this.refetch.bind(this)\n }\n\n protected onSubscribe(): void {\n if (this.listeners.size === 1) {\n this.#currentQuery.addObserver(this)\n\n if (shouldFetchOnMount(this.#currentQuery, this.options)) {\n this.#executeFetch()\n } else {\n this.updateResult()\n }\n\n this.#updateTimers()\n }\n }\n\n protected onUnsubscribe(): void {\n if (!this.hasListeners()) {\n this.destroy()\n }\n }\n\n shouldFetchOnReconnect(): boolean {\n return shouldFetchOn(\n this.#currentQuery,\n this.options,\n this.options.refetchOnReconnect,\n )\n }\n\n shouldFetchOnWindowFocus(): boolean {\n return shouldFetchOn(\n this.#currentQuery,\n this.options,\n this.options.refetchOnWindowFocus,\n )\n }\n\n destroy(): void {\n this.listeners = new Set()\n this.#clearStaleTimeout()\n this.#clearRefetchInterval()\n this.#currentQuery.removeObserver(this)\n }\n\n setOptions(\n options: QueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n notifyOptions?: NotifyOptions,\n ): void {\n const prevOptions = this.options\n const prevQuery = this.#currentQuery\n\n this.options = this.#client.defaultQueryOptions(options)\n\n if (\n this.options.enabled !== undefined &&\n typeof this.options.enabled !== 'boolean'\n ) {\n throw new Error('Expected enabled to be a boolean')\n }\n\n this.#updateQuery()\n\n if (!shallowEqualObjects(this.options, prevOptions)) {\n this.#client.getQueryCache().notify({\n type: 'observerOptionsUpdated',\n query: this.#currentQuery,\n observer: this,\n })\n }\n\n const mounted = this.hasListeners()\n\n // Fetch if there are subscribers\n if (\n mounted &&\n shouldFetchOptionally(\n this.#currentQuery,\n prevQuery,\n this.options,\n prevOptions,\n )\n ) {\n this.#executeFetch()\n }\n\n // Update result\n this.updateResult(notifyOptions)\n\n // Update stale interval if needed\n if (\n mounted &&\n (this.#currentQuery !== prevQuery ||\n this.options.enabled !== prevOptions.enabled ||\n this.options.staleTime !== prevOptions.staleTime)\n ) {\n this.#updateStaleTimeout()\n }\n\n const nextRefetchInterval = this.#computeRefetchInterval()\n\n // Update refetch interval if needed\n if (\n mounted &&\n (this.#currentQuery !== prevQuery ||\n this.options.enabled !== prevOptions.enabled ||\n nextRefetchInterval !== this.#currentRefetchInterval)\n ) {\n this.#updateRefetchInterval(nextRefetchInterval)\n }\n }\n\n getOptimisticResult(\n options: DefaultedQueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n ): QueryObserverResult<TData, TError> {\n const query = this.#client.getQueryCache().build(this.#client, options)\n\n const result = this.createResult(query, options)\n\n if (shouldAssignObserverCurrentProperties(this, result)) {\n // this assigns the optimistic result to the current Observer\n // because if the query function changes, useQuery will be performing\n // an effect where it would fetch again.\n // When the fetch finishes, we perform a deep data cloning in order\n // to reuse objects references. This deep data clone is performed against\n // the `observer.currentResult.data` property\n // When QueryKey changes, we refresh the query and get new `optimistic`\n // result, while we leave the `observer.currentResult`, so when new data\n // arrives, it finds the old `observer.currentResult` which is related\n // to the old QueryKey. Which means that currentResult and selectData are\n // out of sync already.\n // To solve this, we move the cursor of the currentResult every time\n // an observer reads an optimistic value.\n\n // When keeping the previous data, the result doesn't change until new\n // data arrives.\n this.#currentResult = result\n this.#currentResultOptions = this.options\n this.#currentResultState = this.#currentQuery.state\n }\n return result\n }\n\n getCurrentResult(): QueryObserverResult<TData, TError> {\n return this.#currentResult\n }\n\n trackResult(\n result: QueryObserverResult<TData, TError>,\n ): QueryObserverResult<TData, TError> {\n const trackedResult = {} as QueryObserverResult<TData, TError>\n\n Object.keys(result).forEach((key) => {\n Object.defineProperty(trackedResult, key, {\n configurable: false,\n enumerable: true,\n get: () => {\n this.#trackedProps.add(key as keyof QueryObserverResult)\n return result[key as keyof QueryObserverResult]\n },\n })\n })\n\n return trackedResult\n }\n\n getCurrentQuery(): Query<TQueryFnData, TError, TQueryData, TQueryKey> {\n return this.#currentQuery\n }\n\n refetch({ ...options }: RefetchOptions = {}): Promise<\n QueryObserverResult<TData, TError>\n > {\n return this.fetch({\n ...options,\n })\n }\n\n fetchOptimistic(\n options: QueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n ): Promise<QueryObserverResult<TData, TError>> {\n const defaultedOptions = this.#client.defaultQueryOptions(options)\n\n const query = this.#client\n .getQueryCache()\n .build(this.#client, defaultedOptions)\n query.isFetchingOptimistic = true\n\n return query.fetch().then(() => this.createResult(query, defaultedOptions))\n }\n\n protected fetch(\n fetchOptions: ObserverFetchOptions,\n ): Promise<QueryObserverResult<TData, TError>> {\n return this.#executeFetch({\n ...fetchOptions,\n cancelRefetch: fetchOptions.cancelRefetch ?? true,\n }).then(() => {\n this.updateResult()\n return this.#currentResult\n })\n }\n\n #executeFetch(\n fetchOptions?: ObserverFetchOptions,\n ): Promise<TQueryData | undefined> {\n // Make sure we reference the latest query as the current one might have been removed\n this.#updateQuery()\n\n // Fetch\n let promise: Promise<TQueryData | undefined> = this.#currentQuery.fetch(\n this.options as QueryOptions<TQueryFnData, TError, TQueryData, TQueryKey>,\n fetchOptions,\n )\n\n if (!fetchOptions?.throwOnError) {\n promise = promise.catch(noop)\n }\n\n return promise\n }\n\n #updateStaleTimeout(): void {\n this.#clearStaleTimeout()\n\n if (\n isServer ||\n this.#currentResult.isStale ||\n !isValidTimeout(this.options.staleTime)\n ) {\n return\n }\n\n const time = timeUntilStale(\n this.#currentResult.dataUpdatedAt,\n this.options.staleTime,\n )\n\n // The timeout is sometimes triggered 1 ms before the stale time expiration.\n // To mitigate this issue we always add 1 ms to the timeout.\n const timeout = time + 1\n\n this.#staleTimeoutId = setTimeout(() => {\n if (!this.#currentResult.isStale) {\n this.updateResult()\n }\n }, timeout)\n }\n\n #computeRefetchInterval() {\n return (\n (typeof this.options.refetchInterval === 'function'\n ? this.options.refetchInterval(this.#currentQuery)\n : this.options.refetchInterval) ?? false\n )\n }\n\n #updateRefetchInterval(nextInterval: number | false): void {\n this.#clearRefetchInterval()\n\n this.#currentRefetchInterval = nextInterval\n\n if (\n isServer ||\n this.options.enabled === false ||\n !isValidTimeout(this.#currentRefetchInterval) ||\n this.#currentRefetchInterval === 0\n ) {\n return\n }\n\n this.#refetchIntervalId = setInterval(() => {\n if (\n this.options.refetchIntervalInBackground ||\n focusManager.isFocused()\n ) {\n this.#executeFetch()\n }\n }, this.#currentRefetchInterval)\n }\n\n #updateTimers(): void {\n this.#updateStaleTimeout()\n this.#updateRefetchInterval(this.#computeRefetchInterval())\n }\n\n #clearStaleTimeout(): void {\n if (this.#staleTimeoutId) {\n clearTimeout(this.#staleTimeoutId)\n this.#staleTimeoutId = undefined\n }\n }\n\n #clearRefetchInterval(): void {\n if (this.#refetchIntervalId) {\n clearInterval(this.#refetchIntervalId)\n this.#refetchIntervalId = undefined\n }\n }\n\n protected createResult(\n query: Query<TQueryFnData, TError, TQueryData, TQueryKey>,\n options: QueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n ): QueryObserverResult<TData, TError> {\n const prevQuery = this.#currentQuery\n const prevOptions = this.options\n const prevResult = this.#currentResult as\n | QueryObserverResult<TData, TError>\n | undefined\n const prevResultState = this.#currentResultState\n const prevResultOptions = this.#currentResultOptions\n const queryChange = query !== prevQuery\n const queryInitialState = queryChange\n ? query.state\n : this.#currentQueryInitialState\n\n const { state } = query\n let { error, errorUpdatedAt, fetchStatus, status } = state\n let isPlaceholderData = false\n let data: TData | undefined\n\n // Optimistically set result in fetching state if needed\n if (options._optimisticResults) {\n const mounted = this.hasListeners()\n\n const fetchOnMount = !mounted && shouldFetchOnMount(query, options)\n\n const fetchOptionally =\n mounted && shouldFetchOptionally(query, prevQuery, options, prevOptions)\n\n if (fetchOnMount || fetchOptionally) {\n fetchStatus = canFetch(query.options.networkMode)\n ? 'fetching'\n : 'paused'\n if (state.data === undefined) {\n status = 'pending'\n }\n }\n if (options._optimisticResults === 'isRestoring') {\n fetchStatus = 'idle'\n }\n }\n\n // Select data if needed\n if (options.select && state.data !== undefined) {\n // Memoize select result\n if (\n prevResult &&\n state.data === prevResultState?.data &&\n options.select === this.#selectFn\n ) {\n data = this.#selectResult\n } else {\n try {\n this.#selectFn = options.select\n data = options.select(state.data)\n data = replaceData(prevResult?.data, data, options)\n this.#selectResult = data\n this.#selectError = null\n } catch (selectError) {\n this.#selectError = selectError as TError\n }\n }\n }\n // Use query data\n else {\n data = state.data as unknown as TData\n }\n\n // Show placeholder data if needed\n if (\n options.placeholderData !== undefined &&\n data === undefined &&\n status === 'pending'\n ) {\n let placeholderData\n\n // Memoize placeholder data\n if (\n prevResult?.isPlaceholderData &&\n options.placeholderData === prevResultOptions?.placeholderData\n ) {\n placeholderData = prevResult.data\n } else {\n placeholderData =\n typeof options.placeholderData === 'function'\n ? (\n options.placeholderData as unknown as PlaceholderDataFunction<TQueryData>\n )(\n this.#lastQueryWithDefinedData?.state.data,\n this.#lastQueryWithDefinedData as any,\n )\n : options.placeholderData\n if (options.select && placeholderData !== undefined) {\n try {\n placeholderData = options.select(placeholderData)\n this.#selectError = null\n } catch (selectError) {\n this.#selectError = selectError as TError\n }\n }\n }\n\n if (placeholderData !== undefined) {\n status = 'success'\n data = replaceData(\n prevResult?.data,\n placeholderData as unknown,\n options,\n ) as TData\n isPlaceholderData = true\n }\n }\n\n if (this.#selectError) {\n error = this.#selectError as any\n data = this.#selectResult\n errorUpdatedAt = Date.now()\n status = 'error'\n }\n\n const isFetching = fetchStatus === 'fetching'\n const isPending = status === 'pending'\n const isError = status === 'error'\n\n const isLoading = isPending && isFetching\n const hasData = state.data !== undefined\n\n const result: QueryObserverBaseResult<TData, TError> = {\n status,\n fetchStatus,\n isPending,\n isSuccess: status === 'success',\n isError,\n isInitialLoading: isLoading,\n isLoading,\n data,\n dataUpdatedAt: state.dataUpdatedAt,\n error,\n errorUpdatedAt,\n failureCount: state.fetchFailureCount,\n failureReason: state.fetchFailureReason,\n errorUpdateCount: state.errorUpdateCount,\n isFetched: state.dataUpdateCount > 0 || state.errorUpdateCount > 0,\n isFetchedAfterMount:\n state.dataUpdateCount > queryInitialState.dataUpdateCount ||\n state.errorUpdateCount > queryInitialState.errorUpdateCount,\n isFetching,\n isRefetching: isFetching && !isPending,\n isLoadingError: isError && !hasData,\n isPaused: fetchStatus === 'paused',\n isPlaceholderData,\n isRefetchError: isError && hasData,\n isStale: isStale(query, options),\n refetch: this.refetch,\n }\n\n return result as QueryObserverResult<TData, TError>\n }\n\n updateResult(notifyOptions?: NotifyOptions): void {\n const prevResult = this.#currentResult as\n | QueryObserverResult<TData, TError>\n | undefined\n\n const nextResult = this.createResult(this.#currentQuery, this.options)\n this.#currentResultState = this.#currentQuery.state\n this.#currentResultOptions = this.options\n\n if (this.#currentResultState.data !== undefined) {\n this.#lastQueryWithDefinedData = this.#currentQuery\n }\n\n // Only notify and update result if something has changed\n if (shallowEqualObjects(nextResult, prevResult)) {\n return\n }\n\n this.#currentResult = nextResult\n\n // Determine which callbacks to trigger\n const defaultNotifyOptions: NotifyOptions = {}\n\n const shouldNotifyListeners = (): boolean => {\n if (!prevResult) {\n return true\n }\n\n const { notifyOnChangeProps } = this.options\n const notifyOnChangePropsValue =\n typeof notifyOnChangeProps === 'function'\n ? notifyOnChangeProps()\n : notifyOnChangeProps\n\n if (\n notifyOnChangePropsValue === 'all' ||\n (!notifyOnChangePropsValue && !this.#trackedProps.size)\n ) {\n return true\n }\n\n const includedProps = new Set(\n notifyOnChangePropsValue ?? this.#trackedProps,\n )\n\n if (this.options.throwOnError) {\n includedProps.add('error')\n }\n\n return Object.keys(this.#currentResult).some((key) => {\n const typedKey = key as keyof QueryObserverResult\n const changed = this.#currentResult[typedKey] !== prevResult[typedKey]\n return changed && includedProps.has(typedKey)\n })\n }\n\n if (notifyOptions?.listeners !== false && shouldNotifyListeners()) {\n defaultNotifyOptions.listeners = true\n }\n\n this.#notify({ ...defaultNotifyOptions, ...notifyOptions })\n }\n\n #updateQuery(): void {\n const query = this.#client.getQueryCache().build(this.#client, this.options)\n\n if (query === this.#currentQuery) {\n return\n }\n\n const prevQuery = this.#currentQuery as\n | Query<TQueryFnData, TError, TQueryData, TQueryKey>\n | undefined\n this.#currentQuery = query\n this.#currentQueryInitialState = query.state\n\n if (this.hasListeners()) {\n prevQuery?.removeObserver(this)\n query.addObserver(this)\n }\n }\n\n onQueryUpdate(): void {\n this.updateResult()\n\n if (this.hasListeners()) {\n this.#updateTimers()\n }\n }\n\n #notify(notifyOptions: NotifyOptions): void {\n notifyManager.batch(() => {\n // First, trigger the listeners\n if (notifyOptions.listeners) {\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n }\n\n // Then the cache listeners\n this.#client.getQueryCache().notify({\n query: this.#currentQuery,\n type: 'observerResultsUpdated',\n })\n })\n }\n}\n\nfunction shouldLoadOnMount(\n query: Query<any, any, any, any>,\n options: QueryObserverOptions<any, any, any, any>,\n): boolean {\n return (\n options.enabled !== false &&\n query.state.data === undefined &&\n !(query.state.status === 'error' && options.retryOnMount === false)\n )\n}\n\nfunction shouldFetchOnMount(\n query: Query<any, any, any, any>,\n options: QueryObserverOptions<any, any, any, any, any>,\n): boolean {\n return (\n shouldLoadOnMount(query, options) ||\n (query.state.data !== undefined &&\n shouldFetchOn(query, options, options.refetchOnMount))\n )\n}\n\nfunction shouldFetchOn(\n query: Query<any, any, any, any>,\n options: QueryObserverOptions<any, any, any, any, any>,\n field: (typeof options)['refetchOnMount'] &\n (typeof options)['refetchOnWindowFocus'] &\n (typeof options)['refetchOnReconnect'],\n) {\n if (options.enabled !== false) {\n const value = typeof field === 'function' ? field(query) : field\n\n return value === 'always' || (value !== false && isStale(query, options))\n }\n return false\n}\n\nfunction shouldFetchOptionally(\n query: Query<any, any, any, any>,\n prevQuery: Query<any, any, any, any>,\n options: QueryObserverOptions<any, any, any, any, any>,\n prevOptions: QueryObserverOptions<any, any, any, any, any>,\n): boolean {\n return (\n options.enabled !== false &&\n (query !== prevQuery || prevOptions.enabled === false) &&\n (!options.suspense || query.state.status !== 'error') &&\n isStale(query, options)\n )\n}\n\nfunction isStale(\n query: Query<any, any, any, any>,\n options: QueryObserverOptions<any, any, any, any, any>,\n): boolean {\n return query.isStaleByTime(options.staleTime)\n}\n\n// this function would decide if we will update the observer's 'current'\n// properties after an optimistic reading via getOptimisticResult\nfunction shouldAssignObserverCurrentProperties<\n TQueryFnData = unknown,\n TError = unknown,\n TData = TQueryFnData,\n TQueryData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n observer: QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>,\n optimisticResult: QueryObserverResult<TData, TError>,\n) {\n // if the newly created result isn't what the observer is holding as current,\n // then we'll need to update the properties as well\n if (!shallowEqualObjects(observer.getCurrentResult(), optimisticResult)) {\n return true\n }\n\n // basically, just keep previous properties if nothing changed\n return false\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAOO;AACP,2BAA8B;AAC9B,0BAA6B;AAC7B,0BAA6B;AAC7B,qBAAyB;AA2BlB,IAAM,gBAAN,cAMG,iCAAmD;AAAA,EAwB3D,YACE,QACO,SAOP;AACA,UAAM;AARC;AAUP,SAAK,UAAU;AACf,SAAK,eAAe;AACpB,SAAK,YAAY;AACjB,SAAK,WAAW,OAAO;AAAA,EACzB;AAAA,EAvCA;AAAA,EACA,gBAAoE;AAAA,EACpE,4BAA4D;AAAA,EAC5D,iBAAqD;AAAA,EACrD;AAAA,EACA;AAAA,EAOA;AAAA,EACA;AAAA,EACA;AAAA;AAAA;AAAA,EAGA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,gBAAgB,oBAAI,IAA+B;AAAA,EAoBzC,cAAoB;AAC5B,SAAK,UAAU,KAAK,QAAQ,KAAK,IAAI;AAAA,EACvC;AAAA,EAEU,cAAoB;AAC5B,QAAI,KAAK,UAAU,SAAS,GAAG;AAC7B,WAAK,cAAc,YAAY,IAAI;AAEnC,UAAI,mBAAmB,KAAK,eAAe,KAAK,OAAO,GAAG;AACxD,aAAK,cAAc;AAAA,MACrB,OAAO;AACL,aAAK,aAAa;AAAA,MACpB;AAEA,WAAK,cAAc;AAAA,IACrB;AAAA,EACF;AAAA,EAEU,gBAAsB;AAC9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B,MAAM,MAAM,SAAS,UACpB,cAAc,OAAO,SAAS,QAAQ,cAAc;AAE1D;AAEA,SAAS,cACP,OACA,SACA,OAGA;AACA,MAAI,QAAQ,YAAY,OAAO;AAC7B,UAAM,QAAQ,OAAO,UAAU,aAAa,MAAM,KAAK,IAAI;AAE3D,WAAO,UAAU,YAAa,UAAU,SAAS,QAAQ,OAAO,OAAO;AAAA,EACzE;AACA,SAAO;AACT;AAEA,SAAS,sBACP,OACA,WACA,SACA,aACS;AACT,SACE,QAAQ,YAAY,UACnB,UAAU,aAAa,YAAY,YAAY,WAC/C,CAAC,QAAQ,YAAY,MAAM,MAAM,WAAW,YAC7C,QAAQ,OAAO,OAAO;AAE1B;AAEA,SAAS,QACP,OACA,SACS;AACT,SAAO,MAAM,cAAc,QAAQ,SAAS;AAC9C;AAIA,SAAS,sCAOP,UACA,kBACA;AAGA,MAAI,KAAC,kCAAoB,SAAS,iBAAiB,GAAG,gBAAgB,GAAG;AACvE,WAAO;AAAA,EACT;AAGA,SAAO;AACT;","names":[]}
@@ -234,7 +234,7 @@ var QueryObserver = class extends Subscribable {
234
234
  const fetchOptionally = mounted && shouldFetchOptionally(query, prevQuery, options, prevOptions);
235
235
  if (fetchOnMount || fetchOptionally) {
236
236
  fetchStatus = canFetch(query.options.networkMode) ? "fetching" : "paused";
237
- if (!state.dataUpdatedAt) {
237
+ if (state.data === void 0) {
238
238
  status = "pending";
239
239
  }
240
240
  }
@@ -297,6 +297,7 @@ var QueryObserver = class extends Subscribable {
297
297
  const isPending = status === "pending";
298
298
  const isError = status === "error";
299
299
  const isLoading = isPending && isFetching;
300
+ const hasData = state.data !== void 0;
300
301
  const result = {
301
302
  status,
302
303
  fetchStatus,
@@ -316,10 +317,10 @@ var QueryObserver = class extends Subscribable {
316
317
  isFetchedAfterMount: state.dataUpdateCount > queryInitialState.dataUpdateCount || state.errorUpdateCount > queryInitialState.errorUpdateCount,
317
318
  isFetching,
318
319
  isRefetching: isFetching && !isPending,
319
- isLoadingError: isError && state.dataUpdatedAt === 0,
320
+ isLoadingError: isError && !hasData,
320
321
  isPaused: fetchStatus === "paused",
321
322
  isPlaceholderData,
322
- isRefetchError: isError && state.dataUpdatedAt !== 0,
323
+ isRefetchError: isError && hasData,
323
324
  isStale: isStale(query, options),
324
325
  refetch: this.refetch
325
326
  };
@@ -398,10 +399,10 @@ var QueryObserver = class extends Subscribable {
398
399
  }
399
400
  };
400
401
  function shouldLoadOnMount(query, options) {
401
- return options.enabled !== false && !query.state.dataUpdatedAt && !(query.state.status === "error" && options.retryOnMount === false);
402
+ return options.enabled !== false && query.state.data === void 0 && !(query.state.status === "error" && options.retryOnMount === false);
402
403
  }
403
404
  function shouldFetchOnMount(query, options) {
404
- return shouldLoadOnMount(query, options) || query.state.dataUpdatedAt > 0 && shouldFetchOn(query, options, options.refetchOnMount);
405
+ return shouldLoadOnMount(query, options) || query.state.data !== void 0 && shouldFetchOn(query, options, options.refetchOnMount);
405
406
  }
406
407
  function shouldFetchOn(query, options, field) {
407
408
  if (options.enabled !== false) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/queryObserver.ts"],"sourcesContent":["import {\n isServer,\n isValidTimeout,\n noop,\n replaceData,\n shallowEqualObjects,\n timeUntilStale,\n} from './utils'\nimport { notifyManager } from './notifyManager'\nimport { focusManager } from './focusManager'\nimport { Subscribable } from './subscribable'\nimport { canFetch } from './retryer'\nimport type { QueryClient } from './queryClient'\nimport type { FetchOptions, Query, QueryState } from './query'\nimport type {\n DefaultError,\n DefaultedQueryObserverOptions,\n PlaceholderDataFunction,\n QueryKey,\n QueryObserverBaseResult,\n QueryObserverOptions,\n QueryObserverResult,\n QueryOptions,\n RefetchOptions,\n} from './types'\n\ntype QueryObserverListener<TData, TError> = (\n result: QueryObserverResult<TData, TError>,\n) => void\n\nexport interface NotifyOptions {\n listeners?: boolean\n}\n\nexport interface ObserverFetchOptions extends FetchOptions {\n throwOnError?: boolean\n}\n\nexport class QueryObserver<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n> extends Subscribable<QueryObserverListener<TData, TError>> {\n #client: QueryClient\n #currentQuery: Query<TQueryFnData, TError, TQueryData, TQueryKey> = undefined!\n #currentQueryInitialState: QueryState<TQueryData, TError> = undefined!\n #currentResult: QueryObserverResult<TData, TError> = undefined!\n #currentResultState?: QueryState<TQueryData, TError>\n #currentResultOptions?: QueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >\n #selectError: TError | null\n #selectFn?: (data: TQueryData) => TData\n #selectResult?: TData\n // This property keeps track of the last query with defined data.\n // It will be used to pass the previous data and query to the placeholder function between renders.\n #lastQueryWithDefinedData?: Query<TQueryFnData, TError, TQueryData, TQueryKey>\n #staleTimeoutId?: ReturnType<typeof setTimeout>\n #refetchIntervalId?: ReturnType<typeof setInterval>\n #currentRefetchInterval?: number | false\n #trackedProps = new Set<keyof QueryObserverResult>()\n\n constructor(\n client: QueryClient,\n public options: QueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n ) {\n super()\n\n this.#client = client\n this.#selectError = null\n this.bindMethods()\n this.setOptions(options)\n }\n\n protected bindMethods(): void {\n this.refetch = this.refetch.bind(this)\n }\n\n protected onSubscribe(): void {\n if (this.listeners.size === 1) {\n this.#currentQuery.addObserver(this)\n\n if (shouldFetchOnMount(this.#currentQuery, this.options)) {\n this.#executeFetch()\n } else {\n this.updateResult()\n }\n\n this.#updateTimers()\n }\n }\n\n protected onUnsubscribe(): void {\n if (!this.hasListeners()) {\n this.destroy()\n }\n }\n\n shouldFetchOnReconnect(): boolean {\n return shouldFetchOn(\n this.#currentQuery,\n this.options,\n this.options.refetchOnReconnect,\n )\n }\n\n shouldFetchOnWindowFocus(): boolean {\n return shouldFetchOn(\n this.#currentQuery,\n this.options,\n this.options.refetchOnWindowFocus,\n )\n }\n\n destroy(): void {\n this.listeners = new Set()\n this.#clearStaleTimeout()\n this.#clearRefetchInterval()\n this.#currentQuery.removeObserver(this)\n }\n\n setOptions(\n options: QueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n notifyOptions?: NotifyOptions,\n ): void {\n const prevOptions = this.options\n const prevQuery = this.#currentQuery\n\n this.options = this.#client.defaultQueryOptions(options)\n\n if (\n this.options.enabled !== undefined &&\n typeof this.options.enabled !== 'boolean'\n ) {\n throw new Error('Expected enabled to be a boolean')\n }\n\n this.#updateQuery()\n\n if (!shallowEqualObjects(this.options, prevOptions)) {\n this.#client.getQueryCache().notify({\n type: 'observerOptionsUpdated',\n query: this.#currentQuery,\n observer: this,\n })\n }\n\n const mounted = this.hasListeners()\n\n // Fetch if there are subscribers\n if (\n mounted &&\n shouldFetchOptionally(\n this.#currentQuery,\n prevQuery,\n this.options,\n prevOptions,\n )\n ) {\n this.#executeFetch()\n }\n\n // Update result\n this.updateResult(notifyOptions)\n\n // Update stale interval if needed\n if (\n mounted &&\n (this.#currentQuery !== prevQuery ||\n this.options.enabled !== prevOptions.enabled ||\n this.options.staleTime !== prevOptions.staleTime)\n ) {\n this.#updateStaleTimeout()\n }\n\n const nextRefetchInterval = this.#computeRefetchInterval()\n\n // Update refetch interval if needed\n if (\n mounted &&\n (this.#currentQuery !== prevQuery ||\n this.options.enabled !== prevOptions.enabled ||\n nextRefetchInterval !== this.#currentRefetchInterval)\n ) {\n this.#updateRefetchInterval(nextRefetchInterval)\n }\n }\n\n getOptimisticResult(\n options: DefaultedQueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n ): QueryObserverResult<TData, TError> {\n const query = this.#client.getQueryCache().build(this.#client, options)\n\n const result = this.createResult(query, options)\n\n if (shouldAssignObserverCurrentProperties(this, result)) {\n // this assigns the optimistic result to the current Observer\n // because if the query function changes, useQuery will be performing\n // an effect where it would fetch again.\n // When the fetch finishes, we perform a deep data cloning in order\n // to reuse objects references. This deep data clone is performed against\n // the `observer.currentResult.data` property\n // When QueryKey changes, we refresh the query and get new `optimistic`\n // result, while we leave the `observer.currentResult`, so when new data\n // arrives, it finds the old `observer.currentResult` which is related\n // to the old QueryKey. Which means that currentResult and selectData are\n // out of sync already.\n // To solve this, we move the cursor of the currentResult every time\n // an observer reads an optimistic value.\n\n // When keeping the previous data, the result doesn't change until new\n // data arrives.\n this.#currentResult = result\n this.#currentResultOptions = this.options\n this.#currentResultState = this.#currentQuery.state\n }\n return result\n }\n\n getCurrentResult(): QueryObserverResult<TData, TError> {\n return this.#currentResult\n }\n\n trackResult(\n result: QueryObserverResult<TData, TError>,\n ): QueryObserverResult<TData, TError> {\n const trackedResult = {} as QueryObserverResult<TData, TError>\n\n Object.keys(result).forEach((key) => {\n Object.defineProperty(trackedResult, key, {\n configurable: false,\n enumerable: true,\n get: () => {\n this.#trackedProps.add(key as keyof QueryObserverResult)\n return result[key as keyof QueryObserverResult]\n },\n })\n })\n\n return trackedResult\n }\n\n getCurrentQuery(): Query<TQueryFnData, TError, TQueryData, TQueryKey> {\n return this.#currentQuery\n }\n\n refetch({ ...options }: RefetchOptions = {}): Promise<\n QueryObserverResult<TData, TError>\n > {\n return this.fetch({\n ...options,\n })\n }\n\n fetchOptimistic(\n options: QueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n ): Promise<QueryObserverResult<TData, TError>> {\n const defaultedOptions = this.#client.defaultQueryOptions(options)\n\n const query = this.#client\n .getQueryCache()\n .build(this.#client, defaultedOptions)\n query.isFetchingOptimistic = true\n\n return query.fetch().then(() => this.createResult(query, defaultedOptions))\n }\n\n protected fetch(\n fetchOptions: ObserverFetchOptions,\n ): Promise<QueryObserverResult<TData, TError>> {\n return this.#executeFetch({\n ...fetchOptions,\n cancelRefetch: fetchOptions.cancelRefetch ?? true,\n }).then(() => {\n this.updateResult()\n return this.#currentResult\n })\n }\n\n #executeFetch(\n fetchOptions?: ObserverFetchOptions,\n ): Promise<TQueryData | undefined> {\n // Make sure we reference the latest query as the current one might have been removed\n this.#updateQuery()\n\n // Fetch\n let promise: Promise<TQueryData | undefined> = this.#currentQuery.fetch(\n this.options as QueryOptions<TQueryFnData, TError, TQueryData, TQueryKey>,\n fetchOptions,\n )\n\n if (!fetchOptions?.throwOnError) {\n promise = promise.catch(noop)\n }\n\n return promise\n }\n\n #updateStaleTimeout(): void {\n this.#clearStaleTimeout()\n\n if (\n isServer ||\n this.#currentResult.isStale ||\n !isValidTimeout(this.options.staleTime)\n ) {\n return\n }\n\n const time = timeUntilStale(\n this.#currentResult.dataUpdatedAt,\n this.options.staleTime,\n )\n\n // The timeout is sometimes triggered 1 ms before the stale time expiration.\n // To mitigate this issue we always add 1 ms to the timeout.\n const timeout = time + 1\n\n this.#staleTimeoutId = setTimeout(() => {\n if (!this.#currentResult.isStale) {\n this.updateResult()\n }\n }, timeout)\n }\n\n #computeRefetchInterval() {\n return (\n (typeof this.options.refetchInterval === 'function'\n ? this.options.refetchInterval(this.#currentQuery)\n : this.options.refetchInterval) ?? false\n )\n }\n\n #updateRefetchInterval(nextInterval: number | false): void {\n this.#clearRefetchInterval()\n\n this.#currentRefetchInterval = nextInterval\n\n if (\n isServer ||\n this.options.enabled === false ||\n !isValidTimeout(this.#currentRefetchInterval) ||\n this.#currentRefetchInterval === 0\n ) {\n return\n }\n\n this.#refetchIntervalId = setInterval(() => {\n if (\n this.options.refetchIntervalInBackground ||\n focusManager.isFocused()\n ) {\n this.#executeFetch()\n }\n }, this.#currentRefetchInterval)\n }\n\n #updateTimers(): void {\n this.#updateStaleTimeout()\n this.#updateRefetchInterval(this.#computeRefetchInterval())\n }\n\n #clearStaleTimeout(): void {\n if (this.#staleTimeoutId) {\n clearTimeout(this.#staleTimeoutId)\n this.#staleTimeoutId = undefined\n }\n }\n\n #clearRefetchInterval(): void {\n if (this.#refetchIntervalId) {\n clearInterval(this.#refetchIntervalId)\n this.#refetchIntervalId = undefined\n }\n }\n\n protected createResult(\n query: Query<TQueryFnData, TError, TQueryData, TQueryKey>,\n options: QueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n ): QueryObserverResult<TData, TError> {\n const prevQuery = this.#currentQuery\n const prevOptions = this.options\n const prevResult = this.#currentResult as\n | QueryObserverResult<TData, TError>\n | undefined\n const prevResultState = this.#currentResultState\n const prevResultOptions = this.#currentResultOptions\n const queryChange = query !== prevQuery\n const queryInitialState = queryChange\n ? query.state\n : this.#currentQueryInitialState\n\n const { state } = query\n let { error, errorUpdatedAt, fetchStatus, status } = state\n let isPlaceholderData = false\n let data: TData | undefined\n\n // Optimistically set result in fetching state if needed\n if (options._optimisticResults) {\n const mounted = this.hasListeners()\n\n const fetchOnMount = !mounted && shouldFetchOnMount(query, options)\n\n const fetchOptionally =\n mounted && shouldFetchOptionally(query, prevQuery, options, prevOptions)\n\n if (fetchOnMount || fetchOptionally) {\n fetchStatus = canFetch(query.options.networkMode)\n ? 'fetching'\n : 'paused'\n if (!state.dataUpdatedAt) {\n status = 'pending'\n }\n }\n if (options._optimisticResults === 'isRestoring') {\n fetchStatus = 'idle'\n }\n }\n\n // Select data if needed\n if (options.select && state.data !== undefined) {\n // Memoize select result\n if (\n prevResult &&\n state.data === prevResultState?.data &&\n options.select === this.#selectFn\n ) {\n data = this.#selectResult\n } else {\n try {\n this.#selectFn = options.select\n data = options.select(state.data)\n data = replaceData(prevResult?.data, data, options)\n this.#selectResult = data\n this.#selectError = null\n } catch (selectError) {\n this.#selectError = selectError as TError\n }\n }\n }\n // Use query data\n else {\n data = state.data as unknown as TData\n }\n\n // Show placeholder data if needed\n if (\n options.placeholderData !== undefined &&\n data === undefined &&\n status === 'pending'\n ) {\n let placeholderData\n\n // Memoize placeholder data\n if (\n prevResult?.isPlaceholderData &&\n options.placeholderData === prevResultOptions?.placeholderData\n ) {\n placeholderData = prevResult.data\n } else {\n placeholderData =\n typeof options.placeholderData === 'function'\n ? (\n options.placeholderData as unknown as PlaceholderDataFunction<TQueryData>\n )(\n this.#lastQueryWithDefinedData?.state.data,\n this.#lastQueryWithDefinedData as any,\n )\n : options.placeholderData\n if (options.select && placeholderData !== undefined) {\n try {\n placeholderData = options.select(placeholderData)\n this.#selectError = null\n } catch (selectError) {\n this.#selectError = selectError as TError\n }\n }\n }\n\n if (placeholderData !== undefined) {\n status = 'success'\n data = replaceData(\n prevResult?.data,\n placeholderData as unknown,\n options,\n ) as TData\n isPlaceholderData = true\n }\n }\n\n if (this.#selectError) {\n error = this.#selectError as any\n data = this.#selectResult\n errorUpdatedAt = Date.now()\n status = 'error'\n }\n\n const isFetching = fetchStatus === 'fetching'\n const isPending = status === 'pending'\n const isError = status === 'error'\n\n const isLoading = isPending && isFetching\n\n const result: QueryObserverBaseResult<TData, TError> = {\n status,\n fetchStatus,\n isPending,\n isSuccess: status === 'success',\n isError,\n isInitialLoading: isLoading,\n isLoading,\n data,\n dataUpdatedAt: state.dataUpdatedAt,\n error,\n errorUpdatedAt,\n failureCount: state.fetchFailureCount,\n failureReason: state.fetchFailureReason,\n errorUpdateCount: state.errorUpdateCount,\n isFetched: state.dataUpdateCount > 0 || state.errorUpdateCount > 0,\n isFetchedAfterMount:\n state.dataUpdateCount > queryInitialState.dataUpdateCount ||\n state.errorUpdateCount > queryInitialState.errorUpdateCount,\n isFetching,\n isRefetching: isFetching && !isPending,\n isLoadingError: isError && state.dataUpdatedAt === 0,\n isPaused: fetchStatus === 'paused',\n isPlaceholderData,\n isRefetchError: isError && state.dataUpdatedAt !== 0,\n isStale: isStale(query, options),\n refetch: this.refetch,\n }\n\n return result as QueryObserverResult<TData, TError>\n }\n\n updateResult(notifyOptions?: NotifyOptions): void {\n const prevResult = this.#currentResult as\n | QueryObserverResult<TData, TError>\n | undefined\n\n const nextResult = this.createResult(this.#currentQuery, this.options)\n this.#currentResultState = this.#currentQuery.state\n this.#currentResultOptions = this.options\n\n if (this.#currentResultState.data !== undefined) {\n this.#lastQueryWithDefinedData = this.#currentQuery\n }\n\n // Only notify and update result if something has changed\n if (shallowEqualObjects(nextResult, prevResult)) {\n return\n }\n\n this.#currentResult = nextResult\n\n // Determine which callbacks to trigger\n const defaultNotifyOptions: NotifyOptions = {}\n\n const shouldNotifyListeners = (): boolean => {\n if (!prevResult) {\n return true\n }\n\n const { notifyOnChangeProps } = this.options\n const notifyOnChangePropsValue =\n typeof notifyOnChangeProps === 'function'\n ? notifyOnChangeProps()\n : notifyOnChangeProps\n\n if (\n notifyOnChangePropsValue === 'all' ||\n (!notifyOnChangePropsValue && !this.#trackedProps.size)\n ) {\n return true\n }\n\n const includedProps = new Set(\n notifyOnChangePropsValue ?? this.#trackedProps,\n )\n\n if (this.options.throwOnError) {\n includedProps.add('error')\n }\n\n return Object.keys(this.#currentResult).some((key) => {\n const typedKey = key as keyof QueryObserverResult\n const changed = this.#currentResult[typedKey] !== prevResult[typedKey]\n return changed && includedProps.has(typedKey)\n })\n }\n\n if (notifyOptions?.listeners !== false && shouldNotifyListeners()) {\n defaultNotifyOptions.listeners = true\n }\n\n this.#notify({ ...defaultNotifyOptions, ...notifyOptions })\n }\n\n #updateQuery(): void {\n const query = this.#client.getQueryCache().build(this.#client, this.options)\n\n if (query === this.#currentQuery) {\n return\n }\n\n const prevQuery = this.#currentQuery as\n | Query<TQueryFnData, TError, TQueryData, TQueryKey>\n | undefined\n this.#currentQuery = query\n this.#currentQueryInitialState = query.state\n\n if (this.hasListeners()) {\n prevQuery?.removeObserver(this)\n query.addObserver(this)\n }\n }\n\n onQueryUpdate(): void {\n this.updateResult()\n\n if (this.hasListeners()) {\n this.#updateTimers()\n }\n }\n\n #notify(notifyOptions: NotifyOptions): void {\n notifyManager.batch(() => {\n // First, trigger the listeners\n if (notifyOptions.listeners) {\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n }\n\n // Then the cache listeners\n this.#client.getQueryCache().notify({\n query: this.#currentQuery,\n type: 'observerResultsUpdated',\n })\n })\n }\n}\n\nfunction shouldLoadOnMount(\n query: Query<any, any, any, any>,\n options: QueryObserverOptions<any, any, any, any>,\n): boolean {\n return (\n options.enabled !== false &&\n !query.state.dataUpdatedAt &&\n !(query.state.status === 'error' && options.retryOnMount === false)\n )\n}\n\nfunction shouldFetchOnMount(\n query: Query<any, any, any, any>,\n options: QueryObserverOptions<any, any, any, any, any>,\n): boolean {\n return (\n shouldLoadOnMount(query, options) ||\n (query.state.dataUpdatedAt > 0 &&\n shouldFetchOn(query, options, options.refetchOnMount))\n )\n}\n\nfunction shouldFetchOn(\n query: Query<any, any, any, any>,\n options: QueryObserverOptions<any, any, any, any, any>,\n field: (typeof options)['refetchOnMount'] &\n (typeof options)['refetchOnWindowFocus'] &\n (typeof options)['refetchOnReconnect'],\n) {\n if (options.enabled !== false) {\n const value = typeof field === 'function' ? field(query) : field\n\n return value === 'always' || (value !== false && isStale(query, options))\n }\n return false\n}\n\nfunction shouldFetchOptionally(\n query: Query<any, any, any, any>,\n prevQuery: Query<any, any, any, any>,\n options: QueryObserverOptions<any, any, any, any, any>,\n prevOptions: QueryObserverOptions<any, any, any, any, any>,\n): boolean {\n return (\n options.enabled !== false &&\n (query !== prevQuery || prevOptions.enabled === false) &&\n (!options.suspense || query.state.status !== 'error') &&\n isStale(query, options)\n )\n}\n\nfunction isStale(\n query: Query<any, any, any, any>,\n options: QueryObserverOptions<any, any, any, any, any>,\n): boolean {\n return query.isStaleByTime(options.staleTime)\n}\n\n// this function would decide if we will update the observer's 'current'\n// properties after an optimistic reading via getOptimisticResult\nfunction shouldAssignObserverCurrentProperties<\n TQueryFnData = unknown,\n TError = unknown,\n TData = TQueryFnData,\n TQueryData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n observer: QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>,\n optimisticResult: QueryObserverResult<TData, TError>,\n) {\n // if the newly created result isn't what the observer is holding as current,\n // then we'll need to update the properties as well\n if (!shallowEqualObjects(observer.getCurrentResult(), optimisticResult)) {\n return true\n }\n\n // basically, just keep previous properties if nothing changed\n return false\n}\n"],"mappings":";AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,qBAAqB;AAC9B,SAAS,oBAAoB;AAC7B,SAAS,oBAAoB;AAC7B,SAAS,gBAAgB;AA2BlB,IAAM,gBAAN,cAMG,aAAmD;AAAA,EAwB3D,YACE,QACO,SAOP;AACA,UAAM;AARC;AAUP,SAAK,UAAU;AACf,SAAK,eAAe;AACpB,SAAK,YAAY;AACjB,SAAK,WAAW,OAAO;AAAA,EACzB;AAAA,EAvCA;AAAA,EACA,gBAAoE;AAAA,EACpE,4BAA4D;AAAA,EAC5D,iBAAqD;AAAA,EACrD;AAAA,EACA;AAAA,EAOA;AAAA,EACA;AAAA,EACA;AAAA;AAAA;AAAA,EAGA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,gBAAgB,oBAAI,IAA+B;AAAA,EAoBzC,cAAoB;AAC5B,SAAK,UAAU,KAAK,QAAQ,KAAK,IAAI;AAAA,EACvC;AAAA,EAEU,cAAoB;AAC5B,QAAI,KAAK,UAAU,SAAS,GAAG;AAC7B,WAAK,cAAc,YAAY,IAAI;AAEnC,UAAI,mBAAmB,KAAK,eAAe,KAAK,OAAO,GAAG;AACxD,aAAK,cAAc;AAAA,MACrB,OAAO;AACL,aAAK,aAAa;AAAA,MACpB;AAEA,WAAK,cAAc;AAAA,IACrB;AAAA,EACF;AAAA,EAEU,gBAAsB;AAC9B,QAAI,CAAC,KAAK,aAAa,GAAG;AACxB,WAAK,QAAQ;AAAA,IACf;AAAA,EACF;AAAA,EAEA,yBAAkC;AAChC,WAAO;AAAA,MACL,KAAK;AAAA,MACL,KAAK;AAAA,MACL,KAAK,QAAQ;AAAA,IACf;AAAA,EACF;AAAA,EAEA,2BAAoC;AAClC,WAAO;AAAA,MACL,KAAK;AAAA,MACL,KAAK;AAAA,MACL,KAAK,QAAQ;AAAA,IAC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S;AACT,SACE,QAAQ,YAAY,UACnB,UAAU,aAAa,YAAY,YAAY,WAC/C,CAAC,QAAQ,YAAY,MAAM,MAAM,WAAW,YAC7C,QAAQ,OAAO,OAAO;AAE1B;AAEA,SAAS,QACP,OACA,SACS;AACT,SAAO,MAAM,cAAc,QAAQ,SAAS;AAC9C;AAIA,SAAS,sCAOP,UACA,kBACA;AAGA,MAAI,CAAC,oBAAoB,SAAS,iBAAiB,GAAG,gBAAgB,GAAG;AACvE,WAAO;AAAA,EACT;AAGA,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../../src/queryObserver.ts"],"sourcesContent":["import {\n isServer,\n isValidTimeout,\n noop,\n replaceData,\n shallowEqualObjects,\n timeUntilStale,\n} from './utils'\nimport { notifyManager } from './notifyManager'\nimport { focusManager } from './focusManager'\nimport { Subscribable } from './subscribable'\nimport { canFetch } from './retryer'\nimport type { QueryClient } from './queryClient'\nimport type { FetchOptions, Query, QueryState } from './query'\nimport type {\n DefaultError,\n DefaultedQueryObserverOptions,\n PlaceholderDataFunction,\n QueryKey,\n QueryObserverBaseResult,\n QueryObserverOptions,\n QueryObserverResult,\n QueryOptions,\n RefetchOptions,\n} from './types'\n\ntype QueryObserverListener<TData, TError> = (\n result: QueryObserverResult<TData, TError>,\n) => void\n\nexport interface NotifyOptions {\n listeners?: boolean\n}\n\nexport interface ObserverFetchOptions extends FetchOptions {\n throwOnError?: boolean\n}\n\nexport class QueryObserver<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n> extends Subscribable<QueryObserverListener<TData, TError>> {\n #client: QueryClient\n #currentQuery: Query<TQueryFnData, TError, TQueryData, TQueryKey> = undefined!\n #currentQueryInitialState: QueryState<TQueryData, TError> = undefined!\n #currentResult: QueryObserverResult<TData, TError> = undefined!\n #currentResultState?: QueryState<TQueryData, TError>\n #currentResultOptions?: QueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >\n #selectError: TError | null\n #selectFn?: (data: TQueryData) => TData\n #selectResult?: TData\n // This property keeps track of the last query with defined data.\n // It will be used to pass the previous data and query to the placeholder function between renders.\n #lastQueryWithDefinedData?: Query<TQueryFnData, TError, TQueryData, TQueryKey>\n #staleTimeoutId?: ReturnType<typeof setTimeout>\n #refetchIntervalId?: ReturnType<typeof setInterval>\n #currentRefetchInterval?: number | false\n #trackedProps = new Set<keyof QueryObserverResult>()\n\n constructor(\n client: QueryClient,\n public options: QueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n ) {\n super()\n\n this.#client = client\n this.#selectError = null\n this.bindMethods()\n this.setOptions(options)\n }\n\n protected bindMethods(): void {\n this.refetch = this.refetch.bind(this)\n }\n\n protected onSubscribe(): void {\n if (this.listeners.size === 1) {\n this.#currentQuery.addObserver(this)\n\n if (shouldFetchOnMount(this.#currentQuery, this.options)) {\n this.#executeFetch()\n } else {\n this.updateResult()\n }\n\n this.#updateTimers()\n }\n }\n\n protected onUnsubscribe(): void {\n if (!this.hasListeners()) {\n this.destroy()\n }\n }\n\n shouldFetchOnReconnect(): boolean {\n return shouldFetchOn(\n this.#currentQuery,\n this.options,\n this.options.refetchOnReconnect,\n )\n }\n\n shouldFetchOnWindowFocus(): boolean {\n return shouldFetchOn(\n this.#currentQuery,\n this.options,\n this.options.refetchOnWindowFocus,\n )\n }\n\n destroy(): void {\n this.listeners = new Set()\n this.#clearStaleTimeout()\n this.#clearRefetchInterval()\n this.#currentQuery.removeObserver(this)\n }\n\n setOptions(\n options: QueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n notifyOptions?: NotifyOptions,\n ): void {\n const prevOptions = this.options\n const prevQuery = this.#currentQuery\n\n this.options = this.#client.defaultQueryOptions(options)\n\n if (\n this.options.enabled !== undefined &&\n typeof this.options.enabled !== 'boolean'\n ) {\n throw new Error('Expected enabled to be a boolean')\n }\n\n this.#updateQuery()\n\n if (!shallowEqualObjects(this.options, prevOptions)) {\n this.#client.getQueryCache().notify({\n type: 'observerOptionsUpdated',\n query: this.#currentQuery,\n observer: this,\n })\n }\n\n const mounted = this.hasListeners()\n\n // Fetch if there are subscribers\n if (\n mounted &&\n shouldFetchOptionally(\n this.#currentQuery,\n prevQuery,\n this.options,\n prevOptions,\n )\n ) {\n this.#executeFetch()\n }\n\n // Update result\n this.updateResult(notifyOptions)\n\n // Update stale interval if needed\n if (\n mounted &&\n (this.#currentQuery !== prevQuery ||\n this.options.enabled !== prevOptions.enabled ||\n this.options.staleTime !== prevOptions.staleTime)\n ) {\n this.#updateStaleTimeout()\n }\n\n const nextRefetchInterval = this.#computeRefetchInterval()\n\n // Update refetch interval if needed\n if (\n mounted &&\n (this.#currentQuery !== prevQuery ||\n this.options.enabled !== prevOptions.enabled ||\n nextRefetchInterval !== this.#currentRefetchInterval)\n ) {\n this.#updateRefetchInterval(nextRefetchInterval)\n }\n }\n\n getOptimisticResult(\n options: DefaultedQueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n ): QueryObserverResult<TData, TError> {\n const query = this.#client.getQueryCache().build(this.#client, options)\n\n const result = this.createResult(query, options)\n\n if (shouldAssignObserverCurrentProperties(this, result)) {\n // this assigns the optimistic result to the current Observer\n // because if the query function changes, useQuery will be performing\n // an effect where it would fetch again.\n // When the fetch finishes, we perform a deep data cloning in order\n // to reuse objects references. This deep data clone is performed against\n // the `observer.currentResult.data` property\n // When QueryKey changes, we refresh the query and get new `optimistic`\n // result, while we leave the `observer.currentResult`, so when new data\n // arrives, it finds the old `observer.currentResult` which is related\n // to the old QueryKey. Which means that currentResult and selectData are\n // out of sync already.\n // To solve this, we move the cursor of the currentResult every time\n // an observer reads an optimistic value.\n\n // When keeping the previous data, the result doesn't change until new\n // data arrives.\n this.#currentResult = result\n this.#currentResultOptions = this.options\n this.#currentResultState = this.#currentQuery.state\n }\n return result\n }\n\n getCurrentResult(): QueryObserverResult<TData, TError> {\n return this.#currentResult\n }\n\n trackResult(\n result: QueryObserverResult<TData, TError>,\n ): QueryObserverResult<TData, TError> {\n const trackedResult = {} as QueryObserverResult<TData, TError>\n\n Object.keys(result).forEach((key) => {\n Object.defineProperty(trackedResult, key, {\n configurable: false,\n enumerable: true,\n get: () => {\n this.#trackedProps.add(key as keyof QueryObserverResult)\n return result[key as keyof QueryObserverResult]\n },\n })\n })\n\n return trackedResult\n }\n\n getCurrentQuery(): Query<TQueryFnData, TError, TQueryData, TQueryKey> {\n return this.#currentQuery\n }\n\n refetch({ ...options }: RefetchOptions = {}): Promise<\n QueryObserverResult<TData, TError>\n > {\n return this.fetch({\n ...options,\n })\n }\n\n fetchOptimistic(\n options: QueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n ): Promise<QueryObserverResult<TData, TError>> {\n const defaultedOptions = this.#client.defaultQueryOptions(options)\n\n const query = this.#client\n .getQueryCache()\n .build(this.#client, defaultedOptions)\n query.isFetchingOptimistic = true\n\n return query.fetch().then(() => this.createResult(query, defaultedOptions))\n }\n\n protected fetch(\n fetchOptions: ObserverFetchOptions,\n ): Promise<QueryObserverResult<TData, TError>> {\n return this.#executeFetch({\n ...fetchOptions,\n cancelRefetch: fetchOptions.cancelRefetch ?? true,\n }).then(() => {\n this.updateResult()\n return this.#currentResult\n })\n }\n\n #executeFetch(\n fetchOptions?: ObserverFetchOptions,\n ): Promise<TQueryData | undefined> {\n // Make sure we reference the latest query as the current one might have been removed\n this.#updateQuery()\n\n // Fetch\n let promise: Promise<TQueryData | undefined> = this.#currentQuery.fetch(\n this.options as QueryOptions<TQueryFnData, TError, TQueryData, TQueryKey>,\n fetchOptions,\n )\n\n if (!fetchOptions?.throwOnError) {\n promise = promise.catch(noop)\n }\n\n return promise\n }\n\n #updateStaleTimeout(): void {\n this.#clearStaleTimeout()\n\n if (\n isServer ||\n this.#currentResult.isStale ||\n !isValidTimeout(this.options.staleTime)\n ) {\n return\n }\n\n const time = timeUntilStale(\n this.#currentResult.dataUpdatedAt,\n this.options.staleTime,\n )\n\n // The timeout is sometimes triggered 1 ms before the stale time expiration.\n // To mitigate this issue we always add 1 ms to the timeout.\n const timeout = time + 1\n\n this.#staleTimeoutId = setTimeout(() => {\n if (!this.#currentResult.isStale) {\n this.updateResult()\n }\n }, timeout)\n }\n\n #computeRefetchInterval() {\n return (\n (typeof this.options.refetchInterval === 'function'\n ? this.options.refetchInterval(this.#currentQuery)\n : this.options.refetchInterval) ?? false\n )\n }\n\n #updateRefetchInterval(nextInterval: number | false): void {\n this.#clearRefetchInterval()\n\n this.#currentRefetchInterval = nextInterval\n\n if (\n isServer ||\n this.options.enabled === false ||\n !isValidTimeout(this.#currentRefetchInterval) ||\n this.#currentRefetchInterval === 0\n ) {\n return\n }\n\n this.#refetchIntervalId = setInterval(() => {\n if (\n this.options.refetchIntervalInBackground ||\n focusManager.isFocused()\n ) {\n this.#executeFetch()\n }\n }, this.#currentRefetchInterval)\n }\n\n #updateTimers(): void {\n this.#updateStaleTimeout()\n this.#updateRefetchInterval(this.#computeRefetchInterval())\n }\n\n #clearStaleTimeout(): void {\n if (this.#staleTimeoutId) {\n clearTimeout(this.#staleTimeoutId)\n this.#staleTimeoutId = undefined\n }\n }\n\n #clearRefetchInterval(): void {\n if (this.#refetchIntervalId) {\n clearInterval(this.#refetchIntervalId)\n this.#refetchIntervalId = undefined\n }\n }\n\n protected createResult(\n query: Query<TQueryFnData, TError, TQueryData, TQueryKey>,\n options: QueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n ): QueryObserverResult<TData, TError> {\n const prevQuery = this.#currentQuery\n const prevOptions = this.options\n const prevResult = this.#currentResult as\n | QueryObserverResult<TData, TError>\n | undefined\n const prevResultState = this.#currentResultState\n const prevResultOptions = this.#currentResultOptions\n const queryChange = query !== prevQuery\n const queryInitialState = queryChange\n ? query.state\n : this.#currentQueryInitialState\n\n const { state } = query\n let { error, errorUpdatedAt, fetchStatus, status } = state\n let isPlaceholderData = false\n let data: TData | undefined\n\n // Optimistically set result in fetching state if needed\n if (options._optimisticResults) {\n const mounted = this.hasListeners()\n\n const fetchOnMount = !mounted && shouldFetchOnMount(query, options)\n\n const fetchOptionally =\n mounted && shouldFetchOptionally(query, prevQuery, options, prevOptions)\n\n if (fetchOnMount || fetchOptionally) {\n fetchStatus = canFetch(query.options.networkMode)\n ? 'fetching'\n : 'paused'\n if (state.data === undefined) {\n status = 'pending'\n }\n }\n if (options._optimisticResults === 'isRestoring') {\n fetchStatus = 'idle'\n }\n }\n\n // Select data if needed\n if (options.select && state.data !== undefined) {\n // Memoize select result\n if (\n prevResult &&\n state.data === prevResultState?.data &&\n options.select === this.#selectFn\n ) {\n data = this.#selectResult\n } else {\n try {\n this.#selectFn = options.select\n data = options.select(state.data)\n data = replaceData(prevResult?.data, data, options)\n this.#selectResult = data\n this.#selectError = null\n } catch (selectError) {\n this.#selectError = selectError as TError\n }\n }\n }\n // Use query data\n else {\n data = state.data as unknown as TData\n }\n\n // Show placeholder data if needed\n if (\n options.placeholderData !== undefined &&\n data === undefined &&\n status === 'pending'\n ) {\n let placeholderData\n\n // Memoize placeholder data\n if (\n prevResult?.isPlaceholderData &&\n options.placeholderData === prevResultOptions?.placeholderData\n ) {\n placeholderData = prevResult.data\n } else {\n placeholderData =\n typeof options.placeholderData === 'function'\n ? (\n options.placeholderData as unknown as PlaceholderDataFunction<TQueryData>\n )(\n this.#lastQueryWithDefinedData?.state.data,\n this.#lastQueryWithDefinedData as any,\n )\n : options.placeholderData\n if (options.select && placeholderData !== undefined) {\n try {\n placeholderData = options.select(placeholderData)\n this.#selectError = null\n } catch (selectError) {\n this.#selectError = selectError as TError\n }\n }\n }\n\n if (placeholderData !== undefined) {\n status = 'success'\n data = replaceData(\n prevResult?.data,\n placeholderData as unknown,\n options,\n ) as TData\n isPlaceholderData = true\n }\n }\n\n if (this.#selectError) {\n error = this.#selectError as any\n data = this.#selectResult\n errorUpdatedAt = Date.now()\n status = 'error'\n }\n\n const isFetching = fetchStatus === 'fetching'\n const isPending = status === 'pending'\n const isError = status === 'error'\n\n const isLoading = isPending && isFetching\n const hasData = state.data !== undefined\n\n const result: QueryObserverBaseResult<TData, TError> = {\n status,\n fetchStatus,\n isPending,\n isSuccess: status === 'success',\n isError,\n isInitialLoading: isLoading,\n isLoading,\n data,\n dataUpdatedAt: state.dataUpdatedAt,\n error,\n errorUpdatedAt,\n failureCount: state.fetchFailureCount,\n failureReason: state.fetchFailureReason,\n errorUpdateCount: state.errorUpdateCount,\n isFetched: state.dataUpdateCount > 0 || state.errorUpdateCount > 0,\n isFetchedAfterMount:\n state.dataUpdateCount > queryInitialState.dataUpdateCount ||\n state.errorUpdateCount > queryInitialState.errorUpdateCount,\n isFetching,\n isRefetching: isFetching && !isPending,\n isLoadingError: isError && !hasData,\n isPaused: fetchStatus === 'paused',\n isPlaceholderData,\n isRefetchError: isError && hasData,\n isStale: isStale(query, options),\n refetch: this.refetch,\n }\n\n return result as QueryObserverResult<TData, TError>\n }\n\n updateResult(notifyOptions?: NotifyOptions): void {\n const prevResult = this.#currentResult as\n | QueryObserverResult<TData, TError>\n | undefined\n\n const nextResult = this.createResult(this.#currentQuery, this.options)\n this.#currentResultState = this.#currentQuery.state\n this.#currentResultOptions = this.options\n\n if (this.#currentResultState.data !== undefined) {\n this.#lastQueryWithDefinedData = this.#currentQuery\n }\n\n // Only notify and update result if something has changed\n if (shallowEqualObjects(nextResult, prevResult)) {\n return\n }\n\n this.#currentResult = nextResult\n\n // Determine which callbacks to trigger\n const defaultNotifyOptions: NotifyOptions = {}\n\n const shouldNotifyListeners = (): boolean => {\n if (!prevResult) {\n return true\n }\n\n const { notifyOnChangeProps } = this.options\n const notifyOnChangePropsValue =\n typeof notifyOnChangeProps === 'function'\n ? notifyOnChangeProps()\n : notifyOnChangeProps\n\n if (\n notifyOnChangePropsValue === 'all' ||\n (!notifyOnChangePropsValue && !this.#trackedProps.size)\n ) {\n return true\n }\n\n const includedProps = new Set(\n notifyOnChangePropsValue ?? this.#trackedProps,\n )\n\n if (this.options.throwOnError) {\n includedProps.add('error')\n }\n\n return Object.keys(this.#currentResult).some((key) => {\n const typedKey = key as keyof QueryObserverResult\n const changed = this.#currentResult[typedKey] !== prevResult[typedKey]\n return changed && includedProps.has(typedKey)\n })\n }\n\n if (notifyOptions?.listeners !== false && shouldNotifyListeners()) {\n defaultNotifyOptions.listeners = true\n }\n\n this.#notify({ ...defaultNotifyOptions, ...notifyOptions })\n }\n\n #updateQuery(): void {\n const query = this.#client.getQueryCache().build(this.#client, this.options)\n\n if (query === this.#currentQuery) {\n return\n }\n\n const prevQuery = this.#currentQuery as\n | Query<TQueryFnData, TError, TQueryData, TQueryKey>\n | undefined\n this.#currentQuery = query\n this.#currentQueryInitialState = query.state\n\n if (this.hasListeners()) {\n prevQuery?.removeObserver(this)\n query.addObserver(this)\n }\n }\n\n onQueryUpdate(): void {\n this.updateResult()\n\n if (this.hasListeners()) {\n this.#updateTimers()\n }\n }\n\n #notify(notifyOptions: NotifyOptions): void {\n notifyManager.batch(() => {\n // First, trigger the listeners\n if (notifyOptions.listeners) {\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n }\n\n // Then the cache listeners\n this.#client.getQueryCache().notify({\n query: this.#currentQuery,\n type: 'observerResultsUpdated',\n })\n })\n }\n}\n\nfunction shouldLoadOnMount(\n query: Query<any, any, any, any>,\n options: QueryObserverOptions<any, any, any, any>,\n): boolean {\n return (\n options.enabled !== false &&\n query.state.data === undefined &&\n !(query.state.status === 'error' && options.retryOnMount === false)\n )\n}\n\nfunction shouldFetchOnMount(\n query: Query<any, any, any, any>,\n options: QueryObserverOptions<any, any, any, any, any>,\n): boolean {\n return (\n shouldLoadOnMount(query, options) ||\n (query.state.data !== undefined &&\n shouldFetchOn(query, options, options.refetchOnMount))\n )\n}\n\nfunction shouldFetchOn(\n query: Query<any, any, any, any>,\n options: QueryObserverOptions<any, any, any, any, any>,\n field: (typeof options)['refetchOnMount'] &\n (typeof options)['refetchOnWindowFocus'] &\n (typeof options)['refetchOnReconnect'],\n) {\n if (options.enabled !== false) {\n const value = typeof field === 'function' ? field(query) : field\n\n return value === 'always' || (value !== false && isStale(query, options))\n }\n return false\n}\n\nfunction shouldFetchOptionally(\n query: Query<any, any, any, any>,\n prevQuery: Query<any, any, any, any>,\n options: QueryObserverOptions<any, any, any, any, any>,\n prevOptions: QueryObserverOptions<any, any, any, any, any>,\n): boolean {\n return (\n options.enabled !== false &&\n (query !== prevQuery || prevOptions.enabled === false) &&\n (!options.suspense || query.state.status !== 'error') &&\n isStale(query, options)\n )\n}\n\nfunction isStale(\n query: Query<any, any, any, any>,\n options: QueryObserverOptions<any, any, any, any, any>,\n): boolean {\n return query.isStaleByTime(options.staleTime)\n}\n\n// this function would decide if we will update the observer's 'current'\n// properties after an optimistic reading via getOptimisticResult\nfunction shouldAssignObserverCurrentProperties<\n TQueryFnData = unknown,\n TError = unknown,\n TData = TQueryFnData,\n TQueryData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n observer: QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>,\n optimisticResult: QueryObserverResult<TData, TError>,\n) {\n // if the newly created result isn't what the observer is holding as current,\n // then we'll need to update the properties as well\n if (!shallowEqualObjects(observer.getCurrentResult(), optimisticResult)) {\n return true\n }\n\n // basically, just keep previous properties if nothing changed\n return false\n}\n"],"mappings":";AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,qBAAqB;AAC9B,SAAS,oBAAoB;AAC7B,SAAS,oBAAoB;AAC7B,SAAS,gBAAgB;AA2BlB,IAAM,gBAAN,cAMG,aAAmD;AAAA,EAwB3D,YACE,QACO,SAOP;AACA,UAAM;AARC;AAUP,SAAK,UAAU;AACf,SAAK,eAAe;AACpB,SAAK,YAAY;AACjB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[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/query-core",
3
- "version": "5.24.5",
3
+ "version": "5.24.6",
4
4
  "description": "The framework agnostic core that powers TanStack Query",
5
5
  "author": "tannerlinsley",
6
6
  "license": "MIT",
package/src/query.ts CHANGED
@@ -250,7 +250,7 @@ export class Query<
250
250
  isStale(): boolean {
251
251
  return (
252
252
  this.state.isInvalidated ||
253
- !this.state.dataUpdatedAt ||
253
+ this.state.data === undefined ||
254
254
  this.#observers.some((observer) => observer.getCurrentResult().isStale)
255
255
  )
256
256
  }
@@ -258,7 +258,7 @@ export class Query<
258
258
  isStaleByTime(staleTime = 0): boolean {
259
259
  return (
260
260
  this.state.isInvalidated ||
261
- !this.state.dataUpdatedAt ||
261
+ this.state.data === undefined ||
262
262
  !timeUntilStale(this.state.dataUpdatedAt, staleTime)
263
263
  )
264
264
  }
@@ -329,7 +329,7 @@ export class Query<
329
329
  fetchOptions?: FetchOptions,
330
330
  ): Promise<TData> {
331
331
  if (this.state.fetchStatus !== 'idle') {
332
- if (this.state.dataUpdatedAt && fetchOptions?.cancelRefetch) {
332
+ if (this.state.data !== undefined && fetchOptions?.cancelRefetch) {
333
333
  // Silently cancel current fetch if the user wants to cancel refetch
334
334
  this.cancel({ silent: true })
335
335
  } else if (this.#promise) {
@@ -546,7 +546,7 @@ export class Query<
546
546
  fetchStatus: canFetch(this.options.networkMode)
547
547
  ? 'fetching'
548
548
  : 'paused',
549
- ...(!state.dataUpdatedAt && {
549
+ ...(state.data === undefined && {
550
550
  error: null,
551
551
  status: 'pending',
552
552
  }),
@@ -444,7 +444,7 @@ export class QueryObserver<
444
444
  fetchStatus = canFetch(query.options.networkMode)
445
445
  ? 'fetching'
446
446
  : 'paused'
447
- if (!state.dataUpdatedAt) {
447
+ if (state.data === undefined) {
448
448
  status = 'pending'
449
449
  }
450
450
  }
@@ -536,6 +536,7 @@ export class QueryObserver<
536
536
  const isError = status === 'error'
537
537
 
538
538
  const isLoading = isPending && isFetching
539
+ const hasData = state.data !== undefined
539
540
 
540
541
  const result: QueryObserverBaseResult<TData, TError> = {
541
542
  status,
@@ -558,10 +559,10 @@ export class QueryObserver<
558
559
  state.errorUpdateCount > queryInitialState.errorUpdateCount,
559
560
  isFetching,
560
561
  isRefetching: isFetching && !isPending,
561
- isLoadingError: isError && state.dataUpdatedAt === 0,
562
+ isLoadingError: isError && !hasData,
562
563
  isPaused: fetchStatus === 'paused',
563
564
  isPlaceholderData,
564
- isRefetchError: isError && state.dataUpdatedAt !== 0,
565
+ isRefetchError: isError && hasData,
565
566
  isStale: isStale(query, options),
566
567
  refetch: this.refetch,
567
568
  }
@@ -683,7 +684,7 @@ function shouldLoadOnMount(
683
684
  ): boolean {
684
685
  return (
685
686
  options.enabled !== false &&
686
- !query.state.dataUpdatedAt &&
687
+ query.state.data === undefined &&
687
688
  !(query.state.status === 'error' && options.retryOnMount === false)
688
689
  )
689
690
  }
@@ -694,7 +695,7 @@ function shouldFetchOnMount(
694
695
  ): boolean {
695
696
  return (
696
697
  shouldLoadOnMount(query, options) ||
697
- (query.state.dataUpdatedAt > 0 &&
698
+ (query.state.data !== undefined &&
698
699
  shouldFetchOn(query, options, options.refetchOnMount))
699
700
  )
700
701
  }
@@ -841,6 +841,24 @@ describe('query', () => {
841
841
  expect(initialDataUpdatedAtSpy).toHaveBeenCalled()
842
842
  })
843
843
 
844
+ test('should work with initialDataUpdatedAt set to zero', async () => {
845
+ const key = queryKey()
846
+
847
+ await queryClient.prefetchQuery({
848
+ queryKey: key,
849
+ queryFn: () => 'data',
850
+ staleTime: Infinity,
851
+ initialData: 'initial',
852
+ initialDataUpdatedAt: 0,
853
+ })
854
+
855
+ expect(queryCache.find({ queryKey: key })?.state).toMatchObject({
856
+ data: 'initial',
857
+ status: 'success',
858
+ dataUpdatedAt: 0,
859
+ })
860
+ })
861
+
844
862
  test('queries should be garbage collected even if they never fetched', async () => {
845
863
  const key = queryKey()
846
864