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