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

queryObserver.ts

835 lines23.4 KB

1import { focusManager } from './focusManager'
2import { environmentManager } from './environmentManager'
3import { notifyManager } from './notifyManager'
4import { fetchState } from './query'
5import { Subscribable } from './subscribable'
6import { pendingThenable } from './thenable'
7import {
8 isValidTimeout,
9 noop,
10 replaceData,
11 resolveQueryBoolean,
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 resolveQueryBoolean(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 resolveQueryBoolean(this.options.enabled, this.#currentQuery) !==
201 resolveQueryBoolean(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 resolveQueryBoolean(this.options.enabled, this.#currentQuery) !==
215 resolveQueryBoolean(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 (
362 environmentManager.isServer() ||
363 this.#currentResult.isStale ||
364 !isValidTimeout(staleTime)
365 ) {
366 return
367 }
368​
369 const time = timeUntilStale(this.#currentResult.dataUpdatedAt, staleTime)
370​
371 // The timeout is sometimes triggered 1 ms before the stale time expiration.
372 // To mitigate this issue we always add 1 ms to the timeout.
373 const timeout = time + 1
374​
375 this.#staleTimeoutId = timeoutManager.setTimeout(() => {
376 if (!this.#currentResult.isStale) {
377 this.updateResult()
378 }
379 }, timeout)
380 }
381​
382 #computeRefetchInterval() {
383 return (
384 (typeof this.options.refetchInterval === 'function'
385 ? this.options.refetchInterval(this.#currentQuery)
386 : this.options.refetchInterval) ?? false
387 )
388 }
389​
390 #updateRefetchInterval(nextInterval: number | false): void {
391 this.#clearRefetchInterval()
392​
393 this.#currentRefetchInterval = nextInterval
394​
395 if (
396 environmentManager.isServer() ||
397 resolveQueryBoolean(this.options.enabled, this.#currentQuery) === false ||
398 !isValidTimeout(this.#currentRefetchInterval) ||
399 this.#currentRefetchInterval === 0
400 ) {
401 return
402 }
403​
404 this.#refetchIntervalId = timeoutManager.setInterval(() => {
405 if (
406 this.options.refetchIntervalInBackground ||
407 focusManager.isFocused()
408 ) {
409 this.#executeFetch()
410 }
411 }, this.#currentRefetchInterval)
412 }
413​
414 #updateTimers(): void {
415 this.#updateStaleTimeout()
416 this.#updateRefetchInterval(this.#computeRefetchInterval())
417 }
418​
419 #clearStaleTimeout(): void {
420 if (this.#staleTimeoutId !== undefined) {
421 timeoutManager.clearTimeout(this.#staleTimeoutId)
422 this.#staleTimeoutId = undefined
423 }
424 }
425​
426 #clearRefetchInterval(): void {
427 if (this.#refetchIntervalId !== undefined) {
428 timeoutManager.clearInterval(this.#refetchIntervalId)
429 this.#refetchIntervalId = undefined
430 }
431 }
432​
433 protected createResult(
434 query: Query<TQueryFnData, TError, TQueryData, TQueryKey>,
435 options: QueryObserverOptions<
436 TQueryFnData,
437 TError,
438 TData,
439 TQueryData,
440 TQueryKey
441 >,
442 ): QueryObserverResult<TData, TError> {
443 const prevQuery = this.#currentQuery
444 const prevOptions = this.options
445 const prevResult = this.#currentResult as
446 | QueryObserverResult<TData, TError>
447 | undefined
448 const prevResultState = this.#currentResultState
449 const prevResultOptions = this.#currentResultOptions
450 const queryChange = query !== prevQuery
451 const queryInitialState = queryChange
452 ? query.state
453 : this.#currentQueryInitialState
454​
455 const { state } = query
456 let newState = { ...state }
457 let isPlaceholderData = false
458 let data: TData | undefined
459​
460 // Optimistically set result in fetching state if needed
461 if (options._optimisticResults) {
462 const mounted = this.hasListeners()
463​
464 const fetchOnMount = !mounted && shouldFetchOnMount(query, options)
465​
466 const fetchOptionally =
467 mounted && shouldFetchOptionally(query, prevQuery, options, prevOptions)
468​
469 if (fetchOnMount || fetchOptionally) {
470 newState = {
471 ...newState,
472 ...fetchState(state.data, query.options),
473 }
474 }
475 if (options._optimisticResults === 'isRestoring') {
476 newState.fetchStatus = 'idle'
477 }
478 }
479​
480 let { error, errorUpdatedAt, status } = newState
481​
482 // Per default, use query data
483 data = newState.data as unknown as TData
484 let skipSelect = false
485​
486 // use placeholderData if needed
487 if (
488 options.placeholderData !== undefined &&
489 data === undefined &&
490 status === 'pending'
491 ) {
492 let placeholderData
493​
494 // Memoize placeholder data
495 if (
496 prevResult?.isPlaceholderData &&
497 options.placeholderData === prevResultOptions?.placeholderData
498 ) {
499 placeholderData = prevResult.data
500 // we have to skip select when reading this memoization
501 // because prevResult.data is already "selected"
502 skipSelect = true
503 } else {
504 // compute placeholderData
505 placeholderData =
506 typeof options.placeholderData === 'function'
507 ? (
508 options.placeholderData as unknown as PlaceholderDataFunction<TQueryData>
509 )(
510 this.#lastQueryWithDefinedData?.state.data,
511 this.#lastQueryWithDefinedData as any,
512 )
513 : options.placeholderData
514 }
515​
516 if (placeholderData !== undefined) {
517 status = 'success'
518 data = replaceData(
519 prevResult?.data,
520 placeholderData as unknown,
521 options,
522 ) as TData
523 isPlaceholderData = true
524 }
525 }
526​
527 // Select data if needed
528 // this also runs placeholderData through the select function
529 if (options.select && data !== undefined && !skipSelect) {
530 // Memoize select result
531 if (
532 prevResult &&
533 data === prevResultState?.data &&
534 options.select === this.#selectFn
535 ) {
536 data = this.#selectResult
537 } else {
538 try {
539 this.#selectFn = options.select
540 data = options.select(data as any)
541 data = replaceData(prevResult?.data, data, options)
542 this.#selectResult = data
543 this.#selectError = null
544 } catch (selectError) {
545 this.#selectError = selectError as TError
546 }
547 }
548 }
549​
550 if (this.#selectError) {
551 error = this.#selectError
552 data = this.#selectResult
553 errorUpdatedAt = Date.now()
554 status = 'error'
555 }
556​
557 const isFetching = newState.fetchStatus === 'fetching'
558 const isPending = status === 'pending'
559 const isError = status === 'error'
560​
561 const isLoading = isPending && isFetching
562 const hasData = data !== undefined
563​
564 const result: QueryObserverBaseResult<TData, TError> = {
565 status,
566 fetchStatus: newState.fetchStatus,
567 isPending,
568 isSuccess: status === 'success',
569 isError,
570 isInitialLoading: isLoading,
571 isLoading,
572 data,
573 dataUpdatedAt: newState.dataUpdatedAt,
574 error,
575 errorUpdatedAt,
576 failureCount: newState.fetchFailureCount,
577 failureReason: newState.fetchFailureReason,
578 errorUpdateCount: newState.errorUpdateCount,
579 isFetched: query.isFetched(),
580 isFetchedAfterMount:
581 newState.dataUpdateCount > queryInitialState.dataUpdateCount ||
582 newState.errorUpdateCount > queryInitialState.errorUpdateCount,
583 isFetching,
584 isRefetching: isFetching && !isPending,
585 isLoadingError: isError && !hasData,
586 isPaused: newState.fetchStatus === 'paused',
587 isPlaceholderData,
588 isRefetchError: isError && hasData,
589 isStale: isStale(query, options),
590 refetch: this.refetch,
591 promise: this.#currentThenable,
592 isEnabled: resolveQueryBoolean(options.enabled, query) !== false,
593 }
594​
595 const nextResult = result as QueryObserverResult<TData, TError>
596​
597 if (this.options.experimental_prefetchInRender) {
598 const hasResultData = nextResult.data !== undefined
599 const isErrorWithoutData = nextResult.status === 'error' && !hasResultData
600 const finalizeThenableIfPossible = (thenable: PendingThenable<TData>) => {
601 if (isErrorWithoutData) {
602 thenable.reject(nextResult.error)
603 } else if (hasResultData) {
604 thenable.resolve(nextResult.data as TData)
605 }
606 }
607​
608 /**
609 * Create a new thenable and result promise when the results have changed
610 */
611 const recreateThenable = () => {
612 const pending =
613 (this.#currentThenable =
614 nextResult.promise =
615 pendingThenable())
616​
617 finalizeThenableIfPossible(pending)
618 }
619​
620 const prevThenable = this.#currentThenable
621 switch (prevThenable.status) {
622 case 'pending':
623 // Finalize the previous thenable if it was pending
624 // and we are still observing the same query
625 if (query.queryHash === prevQuery.queryHash) {
626 finalizeThenableIfPossible(prevThenable)
627 }
628 break
629 case 'fulfilled':
630 if (isErrorWithoutData || nextResult.data !== prevThenable.value) {
631 recreateThenable()
632 }
633 break
634 case 'rejected':
635 if (!isErrorWithoutData || nextResult.error !== prevThenable.reason) {
636 recreateThenable()
637 }
638 break
639 }
640 }
641​
642 return nextResult
643 }
644​
645 updateResult(): void {
646 const prevResult = this.#currentResult as
647 | QueryObserverResult<TData, TError>
648 | undefined
649​
650 const nextResult = this.createResult(this.#currentQuery, this.options)
651​
652 this.#currentResultState = this.#currentQuery.state
653 this.#currentResultOptions = this.options
654​
655 if (this.#currentResultState.data !== undefined) {
656 this.#lastQueryWithDefinedData = this.#currentQuery
657 }
658​
659 // Only notify and update result if something has changed
660 if (shallowEqualObjects(nextResult, prevResult)) {
661 return
662 }
663​
664 this.#currentResult = nextResult
665​
666 const shouldNotifyListeners = (): boolean => {
667 if (!prevResult) {
668 return true
669 }
670​
671 const { notifyOnChangeProps } = this.options
672 const notifyOnChangePropsValue =
673 typeof notifyOnChangeProps === 'function'
674 ? notifyOnChangeProps()
675 : notifyOnChangeProps
676​
677 if (
678 notifyOnChangePropsValue === 'all' ||
679 (!notifyOnChangePropsValue && !this.#trackedProps.size)
680 ) {
681 return true
682 }
683​
684 const includedProps = new Set(
685 notifyOnChangePropsValue ?? this.#trackedProps,
686 )
687​
688 if (this.options.throwOnError) {
689 includedProps.add('error')
690 }
691​
692 return Object.keys(this.#currentResult).some((key) => {
693 const typedKey = key as keyof QueryObserverResult
694 const changed = this.#currentResult[typedKey] !== prevResult[typedKey]
695​
696 return changed && includedProps.has(typedKey)
697 })
698 }
699​
700 this.#notify({ listeners: shouldNotifyListeners() })
701 }
702​
703 #updateQuery(): void {
704 const query = this.#client.getQueryCache().build(this.#client, this.options)
705​
706 if (query === this.#currentQuery) {
707 return
708 }
709​
710 const prevQuery = this.#currentQuery as
711 | Query<TQueryFnData, TError, TQueryData, TQueryKey>
712 | undefined
713 this.#currentQuery = query
714 this.#currentQueryInitialState = query.state
715​
716 if (this.hasListeners()) {
717 prevQuery?.removeObserver(this)
718 query.addObserver(this)
719 }
720 }
721​
722 onQueryUpdate(): void {
723 this.updateResult()
724​
725 if (this.hasListeners()) {
726 this.#updateTimers()
727 }
728 }
729​
730 #notify(notifyOptions: { listeners: boolean }): void {
731 notifyManager.batch(() => {
732 // First, trigger the listeners
733 if (notifyOptions.listeners) {
734 this.listeners.forEach((listener) => {
735 listener(this.#currentResult)
736 })
737 }
738​
739 // Then the cache listeners
740 this.#client.getQueryCache().notify({
741 query: this.#currentQuery,
742 type: 'observerResultsUpdated',
743 })
744 })
745 }
747​
748function shouldLoadOnMount(
749 query: Query<any, any, any, any>,
750 options: QueryObserverOptions<any, any, any, any>,
751): boolean {
752 return (
753 resolveQueryBoolean(options.enabled, query) !== false &&
754 query.state.data === undefined &&
755 !(
756 query.state.status === 'error' &&
757 resolveQueryBoolean(options.retryOnMount, query) === false
758 )
759 )
761​
762function shouldFetchOnMount(
763 query: Query<any, any, any, any>,
764 options: QueryObserverOptions<any, any, any, any, any>,
765): boolean {
766 return (
767 shouldLoadOnMount(query, options) ||
768 (query.state.data !== undefined &&
769 shouldFetchOn(query, options, options.refetchOnMount))
770 )
772​
773function shouldFetchOn(
774 query: Query<any, any, any, any>,
775 options: QueryObserverOptions<any, any, any, any, any>,
776 field: (typeof options)['refetchOnMount'] &
777 (typeof options)['refetchOnWindowFocus'] &
778 (typeof options)['refetchOnReconnect'],
779) {
780 if (
781 resolveQueryBoolean(options.enabled, query) !== false &&
782 resolveStaleTime(options.staleTime, query) !== 'static'
783 ) {
784 const value = typeof field === 'function' ? field(query) : field
785​
786 return value === 'always' || (value !== false && isStale(query, options))
787 }
788 return false
790​
791function shouldFetchOptionally(
792 query: Query<any, any, any, any>,
793 prevQuery: Query<any, any, any, any>,
794 options: QueryObserverOptions<any, any, any, any, any>,
795 prevOptions: QueryObserverOptions<any, any, any, any, any>,
796): boolean {
797 return (
798 (query !== prevQuery ||
799 resolveQueryBoolean(prevOptions.enabled, query) === false) &&
800 (!options.suspense || query.state.status !== 'error') &&
801 isStale(query, options)
802 )
804​
805function isStale(
806 query: Query<any, any, any, any>,
807 options: QueryObserverOptions<any, any, any, any, any>,
808): boolean {
809 return (
810 resolveQueryBoolean(options.enabled, query) !== false &&
811 query.isStaleByTime(resolveStaleTime(options.staleTime, query))
812 )
814​
815// this function would decide if we will update the observer's 'current'
816// properties after an optimistic reading via getOptimisticResult
817function shouldAssignObserverCurrentProperties<
818 TQueryFnData = unknown,
819 TError = unknown,
820 TData = TQueryFnData,
821 TQueryData = TQueryFnData,
822 TQueryKey extends QueryKey = QueryKey,
823>(
824 observer: QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>,
825 optimisticResult: QueryObserverResult<TData, TError>,
826) {
827 // if the newly created result isn't what the observer is holding as current,
828 // then we'll need to update the properties as well
829 if (!shallowEqualObjects(observer.getCurrentResult(), optimisticResult)) {
830 return true
831 }
832​
833 // basically, just keep previous properties if nothing changed
834 return false