@@ -61,7 +61,7 @@ export default abstract class HTTPPollingDatafileManager implements DatafileMana
6161
6262 private readonly updateInterval : number
6363
64- private cancelTimeout ? : ( ) => void
64+ private cancelTimeout : ( ( ) => void ) | null
6565
6666 private isStarted : boolean
6767
@@ -71,10 +71,16 @@ export default abstract class HTTPPollingDatafileManager implements DatafileMana
7171
7272 private timeoutFactory : TimeoutFactory
7373
74- private currentRequest ? : AbortableRequest
74+ private currentRequest : AbortableRequest | null
7575
7676 private backoffController : BackoffController
7777
78+ // When true, this means the update interval timeout fired before the current
79+ // sync completed. In that case, we should sync again immediately upon
80+ // completion of the current request, instead of waiting another update
81+ // interval.
82+ private syncOnCurrentRequestComplete : boolean
83+
7884 constructor ( config : DatafileManagerConfig ) {
7985 const configWithDefaultsApplied : DatafileManagerConfig = {
8086 ...this . getConfigDefaults ( ) ,
@@ -117,7 +123,10 @@ export default abstract class HTTPPollingDatafileManager implements DatafileMana
117123 logger . warn ( 'Invalid updateInterval %s, defaulting to %s' , updateInterval , DEFAULT_UPDATE_INTERVAL )
118124 this . updateInterval = DEFAULT_UPDATE_INTERVAL
119125 }
126+ this . cancelTimeout = null
127+ this . currentRequest = null
120128 this . backoffController = new BackoffController ( )
129+ this . syncOnCurrentRequestComplete = false
121130 }
122131
123132 get ( ) : object | null {
@@ -138,14 +147,14 @@ export default abstract class HTTPPollingDatafileManager implements DatafileMana
138147 this . isStarted = false
139148 if ( this . cancelTimeout ) {
140149 this . cancelTimeout ( )
141- this . cancelTimeout = undefined
150+ this . cancelTimeout = null
142151 }
143152
144153 this . emitter . removeAllListeners ( )
145154
146155 if ( this . currentRequest ) {
147156 this . currentRequest . abort ( )
148- this . currentRequest = undefined
157+ this . currentRequest = null
149158 }
150159
151160 return Promise . resolve ( )
@@ -209,15 +218,17 @@ export default abstract class HTTPPollingDatafileManager implements DatafileMana
209218 return
210219 }
211220
212- this . currentRequest = undefined
221+ this . currentRequest = null
213222
214- if ( this . autoUpdate ) {
215- this . scheduleNextUpdate ( )
216- }
217223 if ( ! this . isReadyPromiseSettled && ! this . autoUpdate ) {
218224 // We will never resolve ready, so reject it
219225 this . rejectReadyPromise ( new Error ( 'Failed to become ready' ) )
220226 }
227+
228+ if ( this . autoUpdate && this . syncOnCurrentRequestComplete ) {
229+ this . syncDatafile ( )
230+ }
231+ this . syncOnCurrentRequestComplete = false
221232 }
222233
223234 private syncDatafile ( ) : void {
@@ -241,6 +252,10 @@ export default abstract class HTTPPollingDatafileManager implements DatafileMana
241252 this . currentRequest . responsePromise
242253 . then ( onRequestResolved , onRequestRejected )
243254 . then ( onRequestComplete , onRequestComplete )
255+
256+ if ( this . autoUpdate ) {
257+ this . scheduleNextUpdate ( )
258+ }
244259 }
245260
246261 private resolveReadyPromise ( ) : void {
@@ -258,7 +273,11 @@ export default abstract class HTTPPollingDatafileManager implements DatafileMana
258273 const nextUpdateDelay = Math . max ( currentBackoffDelay , this . updateInterval )
259274 logger . debug ( 'Scheduling sync in %s ms' , nextUpdateDelay )
260275 this . cancelTimeout = this . timeoutFactory . setTimeout ( ( ) => {
261- this . syncDatafile ( )
276+ if ( this . currentRequest ) {
277+ this . syncOnCurrentRequestComplete = true
278+ } else {
279+ this . syncDatafile ( )
280+ }
262281 } , nextUpdateDelay )
263282 }
264283
0 commit comments