@@ -157,39 +157,32 @@ export class QueryObserver<TResult, TError> {
157157
158158 private createResult ( ) : QueryResult < TResult , TError > {
159159 const { currentQuery, previousResult, config } = this
160-
161- const {
162- canFetchMore,
163- error,
164- failureCount,
165- isFetched,
166- isFetching,
167- isFetchingMore,
168- isLoading,
169- isStale,
170- } = currentQuery . state
171-
172- let { data, status, updatedAt } = currentQuery . state
160+ const { state } = currentQuery
161+ let { data, status, updatedAt } = state
173162
174163 // Keep previous data if needed
175- if ( config . keepPreviousData && isLoading && previousResult ?. isSuccess ) {
164+ if (
165+ config . keepPreviousData &&
166+ state . isLoading &&
167+ previousResult ?. isSuccess
168+ ) {
176169 data = previousResult . data
177170 updatedAt = previousResult . updatedAt
178171 status = previousResult . status
179172 }
180173
181174 return {
182175 ...getStatusProps ( status ) ,
183- canFetchMore,
176+ canFetchMore : state . canFetchMore ,
184177 clear : this . clear ,
185178 data,
186- error,
187- failureCount,
179+ error : state . error ,
180+ failureCount : state . failureCount ,
188181 fetchMore : this . fetchMore ,
189- isFetched,
190- isFetching,
191- isFetchingMore,
192- isStale,
182+ isFetched : state . isFetched ,
183+ isFetching : state . isFetching ,
184+ isFetchingMore : state . isFetchingMore ,
185+ isStale : state . isStale ,
193186 query : currentQuery ,
194187 refetch : this . refetch ,
195188 updatedAt,
@@ -229,20 +222,35 @@ export class QueryObserver<TResult, TError> {
229222 _state : QueryState < TResult , TError > ,
230223 action : Action < TResult , TError >
231224 ) : void {
232- this . currentResult = this . createResult ( )
225+ const { config } = this
233226
234- const { data, error, isSuccess, isError } = this . currentResult
227+ // Store current result and get new result
228+ const prevResult = this . currentResult
229+ this . currentResult = this . createResult ( )
230+ const result = this . currentResult
235231
236- if ( action . type === 'Success' && isSuccess ) {
237- this . config . onSuccess ?.( data ! )
238- this . config . onSettled ?.( data ! , null )
232+ // We need to check the action because the state could have
233+ // transitioned from success to success in case of `setQueryData`.
234+ if ( action . type === 'Success' && result . isSuccess ) {
235+ config . onSuccess ?.( result . data ! )
236+ config . onSettled ?.( result . data ! , null )
239237 this . updateRefetchInterval ( )
240- } else if ( action . type === 'Error' && isError ) {
241- this . config . onError ?.( error ! )
242- this . config . onSettled ?.( undefined , error ! )
238+ } else if ( action . type === 'Error' && result . isError ) {
239+ config . onError ?.( result . error ! )
240+ config . onSettled ?.( undefined , result . error ! )
243241 this . updateRefetchInterval ( )
244242 }
245243
246- this . updateListener ?.( this . currentResult )
244+ // Decide if we need to notify the listener
245+ const notify =
246+ // Always notify on data or error change
247+ result . data !== prevResult . data ||
248+ result . error !== prevResult . error ||
249+ // Maybe notify on other changes
250+ config . notifyOnStatusChange
251+
252+ if ( notify ) {
253+ this . updateListener ?.( result )
254+ }
247255 }
248256}
0 commit comments