@@ -28,6 +28,9 @@ export interface RequestLoopOptions<TQuery extends DataQuery = DataQuery> {
2828 onCancel : ( ) => void ;
2929}
3030
31+ const DELAY_INTERVAL_MS = 10 ;
32+ const MAX_NEXT_REQUEST_DELAY = 10000 / DELAY_INTERVAL_MS ; // 10 seconds maximum delay between requests
33+
3134/**
3235 * Continue executing requests as long as `getNextQuery` returns a query
3336 */
@@ -39,7 +42,7 @@ export function getRequestLooper<T extends DataQuery = DataQuery>(
3942 let nextQuery : T | undefined = undefined ;
4043 let subscription : Subscription | undefined = undefined ;
4144 let loadingState : LoadingState | undefined = LoadingState . Loading ;
42- let nextRequestDelay = 1 ; // Seconds until the next request
45+ let nextRequestDelay = 1 ; // number of DELAY_INTERVAL_MS to wait before the next request
4346 let count = 1 ;
4447 let shouldCancel = false ;
4548
@@ -71,8 +74,10 @@ export function getRequestLooper<T extends DataQuery = DataQuery>(
7174 } else {
7275 loadingState = LoadingState . Loading ;
7376 }
74- // Calculate the time for the next request, cap at 10s
75- nextRequestDelay = nextRequestDelay * 2 > 10 ? 10 : nextRequestDelay * 2 ;
77+ // Calculate the number of DELAY_INTERVAL_MS to wait before the next request.
78+ // Caps it so the delay is not more than 10s
79+ nextRequestDelay =
80+ nextRequestDelay * 2 > MAX_NEXT_REQUEST_DELAY ? MAX_NEXT_REQUEST_DELAY : nextRequestDelay * 2 ;
7681 } else {
7782 loadingState = LoadingState . Done ;
7883 nextRequestDelay = 0 ;
@@ -99,7 +104,7 @@ export function getRequestLooper<T extends DataQuery = DataQuery>(
99104 . query ( { ...req , requestId : `${ req . requestId } .${ ++ count } ` , targets : [ next ] } )
100105 . subscribe ( observer ) ;
101106 nextQuery = undefined ;
102- } , nextRequestDelay * 1000 ) ;
107+ } , nextRequestDelay * DELAY_INTERVAL_MS ) ;
103108 } else {
104109 subscriber . complete ( ) ;
105110 }
0 commit comments