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

Method updateResult

packages/query-core/src/queryObserver.ts:735–806  ·  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

733 * update was missed in the gap between creating the observer and subscribing to it.
734 */
735 updateResult(): void {
736 const prevResult = this.#currentResult as
737 | QueryObserverResult<TData, TError>
738 | undefined
739
740 const nextResult = this.createResult(this.#currentQuery, this.options)
741
742 this.#currentResultState = this.#currentQuery.state
743 this.#currentResultOptions = this.options
744
745 if (this.#currentResultState.data !== undefined) {
746 this.#lastQueryWithDefinedData = this.#currentQuery
747 }
748
749 // Only notify and update result if something has changed
750 if (shallowEqualObjects(nextResult, prevResult)) {
751 return
752 }
753
754 this.#currentResult = nextResult
755
756 const shouldNotifyListeners = (): boolean => {
757 if (!prevResult) {
758 return true
759 }
760
761 const { notifyOnChangeProps } = this.options
762 const notifyOnChangePropsValue =
763 typeof notifyOnChangeProps === 'function'
764 ? notifyOnChangeProps()
765 : notifyOnChangeProps
766
767 if (
768 notifyOnChangePropsValue === 'all' ||
769 (!notifyOnChangePropsValue && !this.#trackedProps.size)
770 ) {
771 return true
772 }
773
774 const includedProps = new Set(
775 notifyOnChangePropsValue ?? this.#trackedProps,
776 )
777
778 if (this.options.throwOnError) {
779 includedProps.add('error')
780 }
781
782 return Object.keys(this.#currentResult).some((key) => {
783 const typedKey = key as keyof QueryObserverResult
784 const changed = this.#currentResult[typedKey] !== prevResult[typedKey]
785
786 return changed && includedProps.has(typedKey)
787 })
788 }
789
790 const notifyListeners = shouldNotifyListeners()
791
792 notifyManager.batch(() => {

Callers 13

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