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

queryObserver.ts

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