Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

queryObserver.ts

833 lines23.2 KB

1import { focusManager } from './focusManager'
2import { notifyManager } from './notifyManager'
3import { fetchState } from './query'
4import { Subscribable } from './subscribable'
5import { pendingThenable } from './thenable'
6import {
7 isServer,
8 isValidTimeout,
9 noop,
10 replaceData,
11 resolveEnabled,
12 resolveStaleTime,
13 shallowEqualObjects,
14 timeUntilStale,
15} from './utils'
16import { timeoutManager } from './timeoutManager'
17import type { ManagedTimerId } from './timeoutManager'
18import type { FetchOptions, Query, QueryState } from './query'
19import type { QueryClient } from './queryClient'
20import type { PendingThenable, Thenable } from './thenable'
21import type {
22 DefaultError,
23 DefaultedQueryObserverOptions,
24 PlaceholderDataFunction,
25 QueryKey,
26 QueryObserverBaseResult,
27 QueryObserverOptions,
28 QueryObserverResult,
29 QueryOptions,
30 RefetchOptions,
31} from './types'
32​
33type QueryObserverListener<TData, TError> = (
34 result: QueryObserverResult<TData, TError>,
35) => void
36​
37interface ObserverFetchOptions extends FetchOptions {
38 throwOnError?: boolean
40​
41export class QueryObserver<
42 TQueryFnData = unknown,
43 TError = DefaultError,
44 TData = TQueryFnData,
45 TQueryData = TQueryFnData,
46 TQueryKey extends QueryKey = QueryKey,
47> extends Subscribable<QueryObserverListener<TData, TError>> {
48 #client: QueryClient
49 #currentQuery: Query<TQueryFnData, TError, TQueryData, TQueryKey> = undefined!
50 #currentQueryInitialState: QueryState<TQueryData, TError> = undefined!
51 #currentResult: QueryObserverResult<TData, TError> = undefined!
52 #currentResultState?: QueryState<TQueryData, TError>
53 #currentResultOptions?: QueryObserverOptions<
54 TQueryFnData,
55 TError,
56 TData,
57 TQueryData,
58 TQueryKey
59 >
60 #currentThenable: Thenable<TData>
61 #selectError: TError | null
62 #selectFn?: (data: TQueryData) => TData
63 #selectResult?: TData
64 // This property keeps track of the last query with defined data.
65 // It will be used to pass the previous data and query to the placeholder function between renders.
66 #lastQueryWithDefinedData?: Query<TQueryFnData, TError, TQueryData, TQueryKey>
67 #staleTimeoutId?: ManagedTimerId
68 #refetchIntervalId?: ManagedTimerId
69 #currentRefetchInterval?: number | false
70 #trackedProps = new Set<keyof QueryObserverResult>()
71​
72 constructor(
73 client: QueryClient,
74 public options: QueryObserverOptions<
75 TQueryFnData,
76 TError,
77 TData,
78 TQueryData,
79 TQueryKey
80 >,
81 ) {
82 super()
83​
84 this.#client = client
85 this.#selectError = null
86 this.#currentThenable = pendingThenable()
87​
88 this.bindMethods()
89 this.setOptions(options)
90 }
91​
92 protected bindMethods(): void {
93 this.refetch = this.refetch.bind(this)
94 }
95​
96 protected onSubscribe(): void {
97 if (this.listeners.size === 1) {
98 this.#currentQuery.addObserver(this)
99​
100 if (shouldFetchOnMount(this.#currentQuery, this.options)) {
101 this.#executeFetch()
102 } else {
103 this.updateResult()
104 }
105​
106 this.#updateTimers()
107 }
108 }
109​
110 protected onUnsubscribe(): void {
111 if (!this.hasListeners()) {
112 this.destroy()
113 }
114 }
115​
116 shouldFetchOnReconnect(): boolean {
117 return shouldFetchOn(
118 this.#currentQuery,
119 this.options,
120 this.options.refetchOnReconnect,
121 )
122 }
123​
124 shouldFetchOnWindowFocus(): boolean {
125 return shouldFetchOn(
126 this.#currentQuery,
127 this.options,
128 this.options.refetchOnWindowFocus,
129 )
130 }
131​
132 destroy(): void {
133 this.listeners = new Set()
134 this.#clearStaleTimeout()
135 this.#clearRefetchInterval()
136 this.#currentQuery.removeObserver(this)
137 }
138​
139 setOptions(
140 options: QueryObserverOptions<
141 TQueryFnData,
142 TError,
143 TData,
144 TQueryData,
145 TQueryKey
146 >,
147 ): void {
148 const prevOptions = this.options
149 const prevQuery = this.#currentQuery
150​
151 this.options = this.#client.defaultQueryOptions(options)
152​
153 if (
154 this.options.enabled !== undefined &&
155 typeof this.options.enabled !== 'boolean' &&
156 typeof this.options.enabled !== 'function' &&
157 typeof resolveEnabled(this.options.enabled, this.#currentQuery) !==
158 'boolean'
159 ) {
160 throw new Error(
161 'Expected enabled to be a boolean or a callback that returns a boolean',
162 )
163 }
164​
165 this.#updateQuery()
166 this.#currentQuery.setOptions(this.options)
167​
168 if (
169 prevOptions._defaulted &&
170 !shallowEqualObjects(this.options, prevOptions)
171 ) {
172 this.#client.getQueryCache().notify({
173 type: 'observerOptionsUpdated',
174 query: this.#currentQuery,
175 observer: this,
176 })
177 }
178​
179 const mounted = this.hasListeners()
180​
181 // Fetch if there are subscribers
182 if (
183 mounted &&
184 shouldFetchOptionally(
185 this.#currentQuery,
186 prevQuery,
187 this.options,
188 prevOptions,
189 )
190 ) {
191 this.#executeFetch()
192 }
193​
194 // Update result
195 this.updateResult()
196​
197 // Update stale interval if needed
198 if (
199 mounted &&
200 (this.#currentQuery !== prevQuery ||
201 resolveEnabled(this.options.enabled, this.#currentQuery) !==
202 resolveEnabled(prevOptions.enabled, this.#currentQuery) ||
203 resolveStaleTime(this.options.staleTime, this.#currentQuery) !==
204 resolveStaleTime(prevOptions.staleTime, this.#currentQuery))
205 ) {
206 this.#updateStaleTimeout()
207 }
208​
209 const nextRefetchInterval = this.#computeRefetchInterval()
210​
211 // Update refetch interval if needed
212 if (
213 mounted &&
214 (this.#currentQuery !== prevQuery ||
215 resolveEnabled(this.options.enabled, this.#currentQuery) !==
216 resolveEnabled(prevOptions.enabled, this.#currentQuery) ||
217 nextRefetchInterval !== this.#currentRefetchInterval)
218 ) {
219 this.#updateRefetchInterval(nextRefetchInterval)
220 }
221 }
222​
223 getOptimisticResult(
224 options: DefaultedQueryObserverOptions<
225 TQueryFnData,
226 TError,
227 TData,
228 TQueryData,
229 TQueryKey
230 >,
231 ): QueryObserverResult<TData, TError> {
232 const query = this.#client.getQueryCache().build(this.#client, options)
233​
234 const result = this.createResult(query, options)
235​
236 if (shouldAssignObserverCurrentProperties(this, result)) {
237 // this assigns the optimistic result to the current Observer
238 // because if the query function changes, useQuery will be performing
239 // an effect where it would fetch again.
240 // When the fetch finishes, we perform a deep data cloning in order
241 // to reuse objects references. This deep data clone is performed against
242 // the `observer.currentResult.data` property
243 // When QueryKey changes, we refresh the query and get new `optimistic`
244 // result, while we leave the `observer.currentResult`, so when new data
245 // arrives, it finds the old `observer.currentResult` which is related
246 // to the old QueryKey. Which means that currentResult and selectData are
247 // out of sync already.
248 // To solve this, we move the cursor of the currentResult every time
249 // an observer reads an optimistic value.
250​
251 // When keeping the previous data, the result doesn't change until new
252 // data arrives.
253 this.#currentResult = result
254 this.#currentResultOptions = this.options
255 this.#currentResultState = this.#currentQuery.state
256 }
257 return result
258 }
259​
260 getCurrentResult(): QueryObserverResult<TData, TError> {
261 return this.#currentResult
262 }
263​
264 trackResult(
265 result: QueryObserverResult<TData, TError>,
266 onPropTracked?: (key: keyof QueryObserverResult) => void,
267 ): QueryObserverResult<TData, TError> {
268 return new Proxy(result, {
269 get: (target, key) => {
270 this.trackProp(key as keyof QueryObserverResult)
271 onPropTracked?.(key as keyof QueryObserverResult)
272 if (key === 'promise') {
273 this.trackProp('data')
274 if (
275 !this.options.experimental_prefetchInRender &&
276 this.#currentThenable.status === 'pending'
277 ) {
278 this.#currentThenable.reject(
279 new Error(
280 'experimental_prefetchInRender feature flag is not enabled',
281 ),
282 )
283 }
284 }
285 return Reflect.get(target, key)
286 },
287 })
288 }
289​
290 trackProp(key: keyof QueryObserverResult) {
291 this.#trackedProps.add(key)
292 }
293​
294 getCurrentQuery(): Query<TQueryFnData, TError, TQueryData, TQueryKey> {
295 return this.#currentQuery
296 }
297​
298 refetch({ ...options }: RefetchOptions = {}): Promise<
299 QueryObserverResult<TData, TError>
300 > {
301 return this.fetch({
302 ...options,
303 })
304 }
305​
306 fetchOptimistic(
307 options: QueryObserverOptions<
308 TQueryFnData,
309 TError,
310 TData,
311 TQueryData,
312 TQueryKey
313 >,
314 ): Promise<QueryObserverResult<TData, TError>> {
315 const defaultedOptions = this.#client.defaultQueryOptions(options)
316​
317 const query = this.#client
318 .getQueryCache()
319 .build(this.#client, defaultedOptions)
320​
321 return query.fetch().then(() => this.createResult(query, defaultedOptions))
322 }
323​
324 protected fetch(
325 fetchOptions: ObserverFetchOptions,
326 ): Promise<QueryObserverResult<TData, TError>> {
327 return this.#executeFetch({
328 ...fetchOptions,
329 cancelRefetch: fetchOptions.cancelRefetch ?? true,
330 }).then(() => {
331 this.updateResult()
332 return this.#currentResult
333 })
334 }
335​
336 #executeFetch(
337 fetchOptions?: Omit<ObserverFetchOptions, 'initialPromise'>,
338 ): Promise<TQueryData | undefined> {
339 // Make sure we reference the latest query as the current one might have been removed
340 this.#updateQuery()
341​
342 // Fetch
343 let promise: Promise<TQueryData | undefined> = this.#currentQuery.fetch(
344 this.options as QueryOptions<TQueryFnData, TError, TQueryData, TQueryKey>,
345 fetchOptions,
346 )
347​
348 if (!fetchOptions?.throwOnError) {
349 promise = promise.catch(noop)
350 }
351​
352 return promise
353 }
354​
355 #updateStaleTimeout(): void {
356 this.#clearStaleTimeout()
357 const staleTime = resolveStaleTime(
358 this.options.staleTime,
359 this.#currentQuery,
360 )
361​
362 if (isServer || this.#currentResult.isStale || !isValidTimeout(staleTime)) {
363 return
364 }
365​
366 const time = timeUntilStale(this.#currentResult.dataUpdatedAt, staleTime)
367​
368 // The timeout is sometimes triggered 1 ms before the stale time expiration.
369 // To mitigate this issue we always add 1 ms to the timeout.
370 const timeout = time + 1
371​
372 this.#staleTimeoutId = timeoutManager.setTimeout(() => {
373 if (!this.#currentResult.isStale) {
374 this.updateResult()
375 }
376 }, timeout)
377 }
378​
379 #computeRefetchInterval() {
380 return (
381 (typeof this.options.refetchInterval === 'function'
382 ? this.options.refetchInterval(this.#currentQuery)
383 : this.options.refetchInterval) ?? false
384 )
385 }
386​
387 #updateRefetchInterval(nextInterval: number | false): void {
388 this.#clearRefetchInterval()
389​
390 this.#currentRefetchInterval = nextInterval
391​
392 if (
393 isServer ||
394 resolveEnabled(this.options.enabled, this.#currentQuery) === false ||
395 !isValidTimeout(this.#currentRefetchInterval) ||
396 this.#currentRefetchInterval === 0
397 ) {
398 return
399 }
400​
401 this.#refetchIntervalId = timeoutManager.setInterval(() => {
402 if (
403 this.options.refetchIntervalInBackground ||
404 focusManager.isFocused()
405 ) {
406 this.#executeFetch()
407 }
408 }, this.#currentRefetchInterval)
409 }
410​
411 #updateTimers(): void {
412 this.#updateStaleTimeout()
413 this.#updateRefetchInterval(this.#computeRefetchInterval())
414 }
415​
416 #clearStaleTimeout(): void {
417 if (this.#staleTimeoutId) {
418 timeoutManager.clearTimeout(this.#staleTimeoutId)
419 this.#staleTimeoutId = undefined
420 }
421 }
422​
423 #clearRefetchInterval(): void {
424 if (this.#refetchIntervalId) {
425 timeoutManager.clearInterval(this.#refetchIntervalId)
426 this.#refetchIntervalId = undefined
427 }
428 }
429​
430 protected createResult(
431 query: Query<TQueryFnData, TError, TQueryData, TQueryKey>,
432 options: QueryObserverOptions<
433 TQueryFnData,
434 TError,
435 TData,
436 TQueryData,
437 TQueryKey
438 >,
439 ): QueryObserverResult<TData, TError> {
440 const prevQuery = this.#currentQuery
441 const prevOptions = this.options
442 const prevResult = this.#currentResult as
443 | QueryObserverResult<TData, TError>
444 | undefined
445 const prevResultState = this.#currentResultState
446 const prevResultOptions = this.#currentResultOptions
447 const queryChange = query !== prevQuery
448 const queryInitialState = queryChange
449 ? query.state
450 : this.#currentQueryInitialState
451​
452 const { state } = query
453 let newState = { ...state }
454 let isPlaceholderData = false
455 let data: TData | undefined
456​
457 // Optimistically set result in fetching state if needed
458 if (options._optimisticResults) {
459 const mounted = this.hasListeners()
460​
461 const fetchOnMount = !mounted && shouldFetchOnMount(query, options)
462​
463 const fetchOptionally =
464 mounted && shouldFetchOptionally(query, prevQuery, options, prevOptions)
465​
466 if (fetchOnMount || fetchOptionally) {
467 newState = {
468 ...newState,
469 ...fetchState(state.data, query.options),
470 }
471 }
472 if (options._optimisticResults === 'isRestoring') {
473 newState.fetchStatus = 'idle'
474 }
475 }
476​
477 let { error, errorUpdatedAt, status } = newState
478​
479 // Per default, use query data
480 data = newState.data as unknown as TData
481 let skipSelect = false
482​
483 // use placeholderData if needed
484 if (
485 options.placeholderData !== undefined &&
486 data === undefined &&
487 status === 'pending'
488 ) {
489 let placeholderData
490​
491 // Memoize placeholder data
492 if (
493 prevResult?.isPlaceholderData &&
494 options.placeholderData === prevResultOptions?.placeholderData
495 ) {
496 placeholderData = prevResult.data
497 // we have to skip select when reading this memoization
498 // because prevResult.data is already "selected"
499 skipSelect = true
500 } else {
501 // compute placeholderData
502 placeholderData =
503 typeof options.placeholderData === 'function'
504 ? (
505 options.placeholderData as unknown as PlaceholderDataFunction<TQueryData>
506 )(
507 this.#lastQueryWithDefinedData?.state.data,
508 this.#lastQueryWithDefinedData as any,
509 )
510 : options.placeholderData
511 }
512​
513 if (placeholderData !== undefined) {
514 status = 'success'
515 data = replaceData(
516 prevResult?.data,
517 placeholderData as unknown,
518 options,
519 ) as TData
520 isPlaceholderData = true
521 }
522 }
523​
524 // Select data if needed
525 // this also runs placeholderData through the select function
526 if (options.select && data !== undefined && !skipSelect) {
527 // Memoize select result
528 if (
529 prevResult &&
530 data === prevResultState?.data &&
531 options.select === this.#selectFn
532 ) {
533 data = this.#selectResult
534 } else {
535 try {
536 this.#selectFn = options.select
537 data = options.select(data as any)
538 data = replaceData(prevResult?.data, data, options)
539 this.#selectResult = data
540 this.#selectError = null
541 } catch (selectError) {
542 this.#selectError = selectError as TError
543 }
544 }
545 }
546​
547 if (this.#selectError) {
548 error = this.#selectError as any
549 data = this.#selectResult
550 errorUpdatedAt = Date.now()
551 status = 'error'
552 }
553​
554 const isFetching = newState.fetchStatus === 'fetching'
555 const isPending = status === 'pending'
556 const isError = status === 'error'
557​
558 const isLoading = isPending && isFetching
559 const hasData = data !== undefined
560​
561 const result: QueryObserverBaseResult<TData, TError> = {
562 status,
563 fetchStatus: newState.fetchStatus,
564 isPending,
565 isSuccess: status === 'success',
566 isError,
567 isInitialLoading: isLoading,
568 isLoading,
569 data,
570 dataUpdatedAt: newState.dataUpdatedAt,
571 error,
572 errorUpdatedAt,
573 failureCount: newState.fetchFailureCount,
574 failureReason: newState.fetchFailureReason,
575 errorUpdateCount: newState.errorUpdateCount,
576 isFetched: newState.dataUpdateCount > 0 || newState.errorUpdateCount > 0,
577 isFetchedAfterMount:
578 newState.dataUpdateCount > queryInitialState.dataUpdateCount ||
579 newState.errorUpdateCount > queryInitialState.errorUpdateCount,
580 isFetching,
581 isRefetching: isFetching && !isPending,
582 isLoadingError: isError && !hasData,
583 isPaused: newState.fetchStatus === 'paused',
584 isPlaceholderData,
585 isRefetchError: isError && hasData,
586 isStale: isStale(query, options),
587 refetch: this.refetch,
588 promise: this.#currentThenable,
589 isEnabled: resolveEnabled(options.enabled, query) !== false,
590 }
591​
592 const nextResult = result as QueryObserverResult<TData, TError>
593​
594 if (this.options.experimental_prefetchInRender) {
595 const finalizeThenableIfPossible = (thenable: PendingThenable<TData>) => {
596 if (nextResult.status === 'error') {
597 thenable.reject(nextResult.error)
598 } else if (nextResult.data !== undefined) {
599 thenable.resolve(nextResult.data)
600 }
601 }
602​
603 /**
604 * Create a new thenable and result promise when the results have changed
605 */
606 const recreateThenable = () => {
607 const pending =
608 (this.#currentThenable =
609 nextResult.promise =
610 pendingThenable())
611​
612 finalizeThenableIfPossible(pending)
613 }
614​
615 const prevThenable = this.#currentThenable
616 switch (prevThenable.status) {
617 case 'pending':
618 // Finalize the previous thenable if it was pending
619 // and we are still observing the same query
620 if (query.queryHash === prevQuery.queryHash) {
621 finalizeThenableIfPossible(prevThenable)
622 }
623 break
624 case 'fulfilled':
625 if (
626 nextResult.status === 'error' ||
627 nextResult.data !== prevThenable.value
628 ) {
629 recreateThenable()
630 }
631 break
632 case 'rejected':
633 if (
634 nextResult.status !== 'error' ||
635 nextResult.error !== prevThenable.reason
636 ) {
637 recreateThenable()
638 }
639 break
640 }
641 }
642​
643 return nextResult
644 }
645​
646 updateResult(): void {
647 const prevResult = this.#currentResult as
648 | QueryObserverResult<TData, TError>
649 | undefined
650​
651 const nextResult = this.createResult(this.#currentQuery, this.options)
652​
653 this.#currentResultState = this.#currentQuery.state
654 this.#currentResultOptions = this.options
655​
656 if (this.#currentResultState.data !== undefined) {
657 this.#lastQueryWithDefinedData = this.#currentQuery
658 }
659​
660 // Only notify and update result if something has changed
661 if (shallowEqualObjects(nextResult, prevResult)) {
662 return
663 }
664​
665 this.#currentResult = nextResult
666​
667 const shouldNotifyListeners = (): boolean => {
668 if (!prevResult) {
669 return true
670 }
671​
672 const { notifyOnChangeProps } = this.options
673 const notifyOnChangePropsValue =
674 typeof notifyOnChangeProps === 'function'
675 ? notifyOnChangeProps()
676 : notifyOnChangeProps
677​
678 if (
679 notifyOnChangePropsValue === 'all' ||
680 (!notifyOnChangePropsValue && !this.#trackedProps.size)
681 ) {
682 return true
683 }
684​
685 const includedProps = new Set(
686 notifyOnChangePropsValue ?? this.#trackedProps,
687 )
688​
689 if (this.options.throwOnError) {
690 includedProps.add('error')
691 }
692​
693 return Object.keys(this.#currentResult).some((key) => {
694 const typedKey = key as keyof QueryObserverResult
695 const changed = this.#currentResult[typedKey] !== prevResult[typedKey]
696​
697 return changed && includedProps.has(typedKey)
698 })
699 }
700​
701 this.#notify({ listeners: shouldNotifyListeners() })
702 }
703​
704 #updateQuery(): void {
705 const query = this.#client.getQueryCache().build(this.#client, this.options)
706​
707 if (query === this.#currentQuery) {
708 return
709 }
710​
711 const prevQuery = this.#currentQuery as
712 | Query<TQueryFnData, TError, TQueryData, TQueryKey>
713 | undefined
714 this.#currentQuery = query
715 this.#currentQueryInitialState = query.state
716​
717 if (this.hasListeners()) {
718 prevQuery?.removeObserver(this)
719 query.addObserver(this)
720 }
721 }
722​
723 onQueryUpdate(): void {
724 this.updateResult()
725​
726 if (this.hasListeners()) {
727 this.#updateTimers()
728 }
729 }
730​
731 #notify(notifyOptions: { listeners: boolean }): void {
732 notifyManager.batch(() => {
733 // First, trigger the listeners
734 if (notifyOptions.listeners) {
735 this.listeners.forEach((listener) => {
736 listener(this.#currentResult)
737 })
738 }
739​
740 // Then the cache listeners
741 this.#client.getQueryCache().notify({
742 query: this.#currentQuery,
743 type: 'observerResultsUpdated',
744 })
745 })
746 }
748​
749function shouldLoadOnMount(
750 query: Query<any, any, any, any>,
751 options: QueryObserverOptions<any, any, any, any>,
752): boolean {
753 return (
754 resolveEnabled(options.enabled, query) !== false &&
755 query.state.data === undefined &&
756 !(query.state.status === 'error' && options.retryOnMount === false)
757 )
759​
760function shouldFetchOnMount(
761 query: Query<any, any, any, any>,
762 options: QueryObserverOptions<any, any, any, any, any>,
763): boolean {
764 return (
765 shouldLoadOnMount(query, options) ||
766 (query.state.data !== undefined &&
767 shouldFetchOn(query, options, options.refetchOnMount))
768 )
770​
771function shouldFetchOn(
772 query: Query<any, any, any, any>,
773 options: QueryObserverOptions<any, any, any, any, any>,
774 field: (typeof options)['refetchOnMount'] &
775 (typeof options)['refetchOnWindowFocus'] &
776 (typeof options)['refetchOnReconnect'],
777) {
778 if (
779 resolveEnabled(options.enabled, query) !== false &&
780 resolveStaleTime(options.staleTime, query) !== 'static'
781 ) {
782 const value = typeof field === 'function' ? field(query) : field
783​
784 return value === 'always' || (value !== false && isStale(query, options))
785 }
786 return false
788​
789function shouldFetchOptionally(
790 query: Query<any, any, any, any>,
791 prevQuery: Query<any, any, any, any>,
792 options: QueryObserverOptions<any, any, any, any, any>,
793 prevOptions: QueryObserverOptions<any, any, any, any, any>,
794): boolean {
795 return (
796 (query !== prevQuery ||
797 resolveEnabled(prevOptions.enabled, query) === false) &&
798 (!options.suspense || query.state.status !== 'error') &&
799 isStale(query, options)
800 )
802​
803function isStale(
804 query: Query<any, any, any, any>,
805 options: QueryObserverOptions<any, any, any, any, any>,
806): boolean {
807 return (
808 resolveEnabled(options.enabled, query) !== false &&
809 query.isStaleByTime(resolveStaleTime(options.staleTime, query))
810 )
812​
813// this function would decide if we will update the observer's 'current'
814// properties after an optimistic reading via getOptimisticResult
815function shouldAssignObserverCurrentProperties<
816 TQueryFnData = unknown,
817 TError = unknown,
818 TData = TQueryFnData,
819 TQueryData = TQueryFnData,
820 TQueryKey extends QueryKey = QueryKey,
821>(
822 observer: QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>,
823 optimisticResult: QueryObserverResult<TData, TError>,
824) {
825 // if the newly created result isn't what the observer is holding as current,
826 // then we'll need to update the properties as well
827 if (!shallowEqualObjects(observer.getCurrentResult(), optimisticResult)) {
828 return true
829 }
830​
831 // basically, just keep previous properties if nothing changed
832 return false