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

queryObserver.ts

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