@@ -149,7 +149,7 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
149149 }
150150
151151 private dispatch ( action : Action < TData , TError > ) : void {
152- this . state = queryReducer ( this . state , action )
152+ this . state = reducer ( this . state , action )
153153
154154 notifyManager . batch ( ( ) => {
155155 this . observers . forEach ( observer => {
@@ -298,32 +298,32 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
298298 }
299299
300300 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 )
304303
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 ( )
309306
310- this . cache . notify ( this )
307+ this . cache . notify ( this )
308+ }
311309 }
312310
313311 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+ }
315321
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 ( )
321323 }
322324
323- this . scheduleGc ( )
325+ this . cache . notify ( this )
324326 }
325-
326- this . cache . notify ( this )
327327 }
328328
329329 invalidate ( ) : void {
@@ -516,18 +516,19 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
516516 ) : Promise < TData > {
517517 return new Promise < TData > ( ( outerResolve , outerReject ) => {
518518 let resolved = false
519- let continueLoop : ( ) => void
520- let cancelTransport : ( ) => void
521519
522520 const done = ( ) => {
523- resolved = true
521+ if ( ! resolved ) {
522+ resolved = true
524523
525- delete this . cancelFetch
526- delete this . continueFetch
527- delete this . isTransportCancelable
524+ // End loop if currently paused
525+ this . continueFetch ?.( )
528526
529- // End loop if currently paused
530- continueLoop ?.( )
527+ // Cleanup
528+ delete this . cancelFetch
529+ delete this . continueFetch
530+ delete this . isTransportCancelable
531+ }
531532 }
532533
533534 const resolve = ( value : any ) => {
@@ -540,17 +541,6 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
540541 outerReject ( value )
541542 }
542543
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-
554544 // Create loop function
555545 const run = ( ) => {
556546 // Do nothing if already resolved
@@ -568,20 +558,22 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
568558 }
569559
570560 // 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 ) ) {
573569 try {
574570 promiseOrValue . cancel ( )
575571 } catch { }
576572 }
577- this . isTransportCancelable = true
578573 }
579574
580575 Promise . resolve ( promiseOrValue )
581- . then ( data => {
582- // Resolve with data
583- resolve ( data )
584- } )
576+ . then ( resolve )
585577 . catch ( error => {
586578 // Stop if the fetch is already resolved
587579 if ( resolved ) {
@@ -614,7 +606,7 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
614606 // Pause retry if the document is not visible or when the device is offline
615607 if ( ! isDocumentVisible ( ) || ! isOnline ( ) ) {
616608 return new Promise ( continueResolve => {
617- continueLoop = continueResolve
609+ this . continueFetch = continueResolve
618610 } )
619611 }
620612 } )
@@ -701,7 +693,7 @@ function getDefaultState<TData, TError, TQueryFnData>(
701693 }
702694}
703695
704- function queryReducer < TData , TError > (
696+ function reducer < TData , TError > (
705697 state : QueryState < TData , TError > ,
706698 action : Action < TData , TError >
707699) : QueryState < TData , TError > {
0 commit comments