MCPcopy Create free account
hub / github.com/TanStack/query / updateResult

Method updateResult

packages/query-core/src/queryObserver.ts:763–835  ·  view source on GitHub ↗

* Recomputes and stores the current result from the current query/options, notifying listeners * if it changed. Framework adapters call this right after subscribing to make sure no query * update was missed in the gap between creating the observer and subscribing to it.

()

Source from the content-addressed store, hash-verified

761 * update was missed in the gap between creating the observer and subscribing to it.
762 */
763 updateResult(): void {
764 const prevResult = this.#currentResult as
765 | QueryObserverResult<TData, TError>
766 | undefined
767
768 const nextResult = this.createResult(this.#currentQuery, this.options)
769
770 this.#currentResultOptions = this.options
771
772 if (this.#currentQuery.state.data !== undefined) {
773 this.#lastQueryWithDefinedData = this.#currentQuery
774 }
775
776 // Only notify and update result if something has changed
777 if (shallowEqualObjects(nextResult, prevResult)) {
778 return
779 }
780
781 this.#currentResult = nextResult
782
783 const shouldNotifyListeners = (): boolean => {
784 if (!prevResult) {
785 return true
786 }
787
788 const { notifyOnChangeProps } = this.options
789 const notifyOnChangePropsValue =
790 typeof notifyOnChangeProps === 'function'
791 ? notifyOnChangeProps()
792 : notifyOnChangeProps
793
794 if (
795 notifyOnChangePropsValue === 'all' ||
796 (!notifyOnChangePropsValue && !this.#trackedProps.size)
797 ) {
798 return true
799 }
800
801 if (
802 this.options.throwOnError &&
803 this.#currentResult.error !== prevResult.error
804 ) {
805 return true
806 }
807
808 for (const key of notifyOnChangePropsValue ?? this.#trackedProps) {
809 if (
810 this.#currentResult[key as keyof QueryObserverResult] !==
811 prevResult[key as keyof QueryObserverResult]
812 ) {
813 return true
814 }
815 }
816 return false
817 }
818
819 const notifyListeners = shouldNotifyListeners()
820

Callers 14

onSubscribeMethod · 0.95
setOptionsMethod · 0.95
fetchMethod · 0.95
#updateStaleTimeoutMethod · 0.95
onQueryUpdateMethod · 0.95
onConnectedMethod · 0.80
onQueryClientChangedMethod · 0.80
onConnectedMethod · 0.80
onQueryClientChangedMethod · 0.80
useBaseQueryFunction · 0.80
useBaseQueryFunction · 0.80
createBaseQueryFunction · 0.80

Calls 5

createResultMethod · 0.95
shallowEqualObjectsFunction · 0.90
getQueryCacheMethod · 0.80
listenerFunction · 0.50
notifyMethod · 0.45

Tested by

no test coverage detected