@@ -288,9 +288,10 @@ export class QueryObserver<
288
288
289
289
this . staleTimeoutId = setTimeout ( ( ) => {
290
290
if ( ! this . currentResult . isStale ) {
291
+ const prevResult = this . currentResult
291
292
this . updateResult ( )
292
293
this . notify ( {
293
- listeners : this . options . notifyOnStaleChange === true ,
294
+ listeners : this . shouldNotifyListeners ( prevResult , this . currentResult ) ,
294
295
cache : true ,
295
296
} )
296
297
}
@@ -423,6 +424,42 @@ export class QueryObserver<
423
424
return result as QueryObserverResult < TData , TError >
424
425
}
425
426
427
+ private shouldNotifyListeners (
428
+ prevResult : QueryObserverResult ,
429
+ result : QueryObserverResult
430
+ ) : boolean {
431
+ const { notifyOnChangeProps, notifyOnChangePropsExclusions } = this . options
432
+
433
+ if ( prevResult === result ) {
434
+ return false
435
+ }
436
+
437
+ if ( ! notifyOnChangeProps && ! notifyOnChangePropsExclusions ) {
438
+ return true
439
+ }
440
+
441
+ const keys = Object . keys ( result )
442
+
443
+ for ( let i = 0 ; i < keys . length ; i ++ ) {
444
+ const key = keys [ i ] as keyof QueryObserverResult
445
+ const changed = prevResult [ key ] !== result [ key ]
446
+ const isIncluded = notifyOnChangeProps ?. some ( x => x === key )
447
+ const isExcluded = notifyOnChangePropsExclusions ?. some ( x => x === key )
448
+
449
+ if ( changed ) {
450
+ if ( notifyOnChangePropsExclusions && isExcluded ) {
451
+ break
452
+ }
453
+
454
+ if ( ! notifyOnChangeProps || isIncluded ) {
455
+ return true
456
+ }
457
+ }
458
+ }
459
+
460
+ return false
461
+ }
462
+
426
463
private updateResult ( willFetch ?: boolean ) : void {
427
464
const result = this . getNewResult ( willFetch )
428
465
@@ -466,7 +503,9 @@ export class QueryObserver<
466
503
prevQuery ?. removeObserver ( this )
467
504
this . currentQuery . addObserver ( this )
468
505
469
- if ( this . options . notifyOnStatusChange !== false ) {
506
+ if (
507
+ this . shouldNotifyListeners ( this . previousQueryResult , this . currentResult )
508
+ ) {
470
509
this . notify ( { listeners : true } )
471
510
}
472
511
}
@@ -494,13 +533,7 @@ export class QueryObserver<
494
533
notifyOptions . onError = true
495
534
}
496
535
497
- if (
498
- // Always notify if notifyOnStatusChange is set
499
- this . options . notifyOnStatusChange !== false ||
500
- // Otherwise only notify on data or error change
501
- currentResult . data !== prevResult . data ||
502
- currentResult . error !== prevResult . error
503
- ) {
536
+ if ( this . shouldNotifyListeners ( prevResult , currentResult ) ) {
504
537
notifyOptions . listeners = true
505
538
}
506
539
0 commit comments