@@ -149,7 +149,7 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
149
149
}
150
150
151
151
private dispatch ( action : Action < TData , TError > ) : void {
152
- this . state = queryReducer ( this . state , action )
152
+ this . state = reducer ( this . state , action )
153
153
154
154
notifyManager . batch ( ( ) => {
155
155
this . observers . forEach ( observer => {
@@ -298,32 +298,32 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
298
298
}
299
299
300
300
subscribeObserver ( observer : QueryObserver < any , any , any , any > ) : void {
301
- if ( this . observers . indexOf ( observer ) !== - 1 ) {
302
- return
303
- }
301
+ if ( this . observers . indexOf ( observer ) === - 1 ) {
302
+ this . observers . push ( observer )
304
303
305
- this . observers . push ( observer )
306
-
307
- // Stop the query from being garbage collected
308
- this . clearGcTimeout ( )
304
+ // Stop the query from being garbage collected
305
+ this . clearGcTimeout ( )
309
306
310
- this . cache . notify ( this )
307
+ this . cache . notify ( this )
308
+ }
311
309
}
312
310
313
311
unsubscribeObserver ( observer : QueryObserver < any , any , any , any > ) : void {
314
- this . observers = this . observers . filter ( x => x !== observer )
312
+ if ( this . observers . indexOf ( observer ) !== - 1 ) {
313
+ this . observers = this . observers . filter ( x => x !== observer )
314
+
315
+ if ( ! this . observers . length ) {
316
+ // If the transport layer does not support cancellation
317
+ // we'll let the query continue so the result can be cached
318
+ if ( this . isTransportCancelable ) {
319
+ this . cancel ( )
320
+ }
315
321
316
- if ( ! this . observers . length ) {
317
- // If the transport layer does not support cancellation
318
- // we'll let the query continue so the result can be cached
319
- if ( this . isTransportCancelable ) {
320
- this . cancel ( )
322
+ this . scheduleGc ( )
321
323
}
322
324
323
- this . scheduleGc ( )
325
+ this . cache . notify ( this )
324
326
}
325
-
326
- this . cache . notify ( this )
327
327
}
328
328
329
329
invalidate ( ) : void {
@@ -516,18 +516,19 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
516
516
) : Promise < TData > {
517
517
return new Promise < TData > ( ( outerResolve , outerReject ) => {
518
518
let resolved = false
519
- let continueLoop : ( ) => void
520
- let cancelTransport : ( ) => void
521
519
522
520
const done = ( ) => {
523
- resolved = true
521
+ if ( ! resolved ) {
522
+ resolved = true
524
523
525
- delete this . cancelFetch
526
- delete this . continueFetch
527
- delete this . isTransportCancelable
524
+ // End loop if currently paused
525
+ this . continueFetch ?.( )
528
526
529
- // End loop if currently paused
530
- continueLoop ?.( )
527
+ // Cleanup
528
+ delete this . cancelFetch
529
+ delete this . continueFetch
530
+ delete this . isTransportCancelable
531
+ }
531
532
}
532
533
533
534
const resolve = ( value : any ) => {
@@ -540,17 +541,6 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
540
541
outerReject ( value )
541
542
}
542
543
543
- // Create callback to cancel this fetch
544
- this . cancelFetch = cancelOptions => {
545
- reject ( new CancelledError ( cancelOptions ) )
546
- cancelTransport ?.( )
547
- }
548
-
549
- // Create callback to continue this fetch
550
- this . continueFetch = ( ) => {
551
- continueLoop ?.( )
552
- }
553
-
554
544
// Create loop function
555
545
const run = ( ) => {
556
546
// Do nothing if already resolved
@@ -568,20 +558,22 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
568
558
}
569
559
570
560
// Check if the transport layer support cancellation
571
- if ( isCancelable ( promiseOrValue ) ) {
572
- cancelTransport = ( ) => {
561
+ this . isTransportCancelable = isCancelable ( promiseOrValue )
562
+
563
+ // Create callback to cancel this fetch
564
+ this . cancelFetch = cancelOptions => {
565
+ reject ( new CancelledError ( cancelOptions ) )
566
+
567
+ // Cancel transport if supported
568
+ if ( isCancelable ( promiseOrValue ) ) {
573
569
try {
574
570
promiseOrValue . cancel ( )
575
571
} catch { }
576
572
}
577
- this . isTransportCancelable = true
578
573
}
579
574
580
575
Promise . resolve ( promiseOrValue )
581
- . then ( data => {
582
- // Resolve with data
583
- resolve ( data )
584
- } )
576
+ . then ( resolve )
585
577
. catch ( error => {
586
578
// Stop if the fetch is already resolved
587
579
if ( resolved ) {
@@ -614,7 +606,7 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
614
606
// Pause retry if the document is not visible or when the device is offline
615
607
if ( ! isDocumentVisible ( ) || ! isOnline ( ) ) {
616
608
return new Promise ( continueResolve => {
617
- continueLoop = continueResolve
609
+ this . continueFetch = continueResolve
618
610
} )
619
611
}
620
612
} )
@@ -701,7 +693,7 @@ function getDefaultState<TData, TError, TQueryFnData>(
701
693
}
702
694
}
703
695
704
- function queryReducer < TData , TError > (
696
+ function reducer < TData , TError > (
705
697
state : QueryState < TData , TError > ,
706
698
action : Action < TData , TError >
707
699
) : QueryState < TData , TError > {
0 commit comments