17
17
import { getLogger } from '@optimizely/js-sdk-logging'
18
18
import { DatafileManager , DatafileManagerConfig , DatafileUpdate } from './datafileManager' ;
19
19
import EventEmitter from './eventEmitter'
20
- import { Response , Headers } from './http' ;
20
+ import { AbortableRequest , Response , Headers } from './http' ;
21
21
import { DEFAULT_UPDATE_INTERVAL , MIN_UPDATE_INTERVAL , DEFAULT_URL_TEMPLATE , SDK_KEY_TOKEN } from './config'
22
22
import { TimeoutFactory , DEFAULT_TIMEOUT_FACTORY } from './timeoutFactory'
23
23
@@ -31,8 +31,10 @@ function isValidUpdateInterval(updateInterval: number): boolean {
31
31
32
32
export default abstract class HTTPPollingDatafileManager implements DatafileManager {
33
33
// Make an HTTP get request to the given URL with the given headers
34
- // Return a promise for a Response. If we can't get a response, the promise is rejected
35
- protected abstract makeGetRequest ( reqUrl : string , headers : Headers ) : Promise < Response >
34
+ // Return an AbortableRequest, which has a promise for a Response.
35
+ // If we can't get a response, the promise is rejected.
36
+ // The request will be aborted if the manager is stopped while the request is in flight.
37
+ protected abstract makeGetRequest ( reqUrl : string , headers : Headers ) : AbortableRequest
36
38
37
39
public readonly onReady : Promise < void >
38
40
@@ -62,6 +64,8 @@ export default abstract class HTTPPollingDatafileManager implements DatafileMana
62
64
63
65
private timeoutFactory : TimeoutFactory
64
66
67
+ private currentRequest ?: AbortableRequest
68
+
65
69
constructor ( config : DatafileManagerConfig ) {
66
70
const {
67
71
datafile,
@@ -125,7 +129,14 @@ export default abstract class HTTPPollingDatafileManager implements DatafileMana
125
129
this . cancelTimeout ( )
126
130
this . cancelTimeout = undefined
127
131
}
132
+
128
133
this . emitter . removeAllListeners ( )
134
+
135
+ if ( this . currentRequest ) {
136
+ this . currentRequest . abort ( )
137
+ this . currentRequest = undefined
138
+ }
139
+
129
140
return Promise . resolve ( )
130
141
}
131
142
@@ -178,6 +189,8 @@ export default abstract class HTTPPollingDatafileManager implements DatafileMana
178
189
return
179
190
}
180
191
192
+ this . currentRequest = undefined
193
+
181
194
if ( this . liveUpdates ) {
182
195
this . scheduleNextUpdate ( )
183
196
}
@@ -196,7 +209,7 @@ export default abstract class HTTPPollingDatafileManager implements DatafileMana
196
209
const datafileUrl = this . getUrl ( this . sdkKey )
197
210
198
211
logger . debug ( 'Making datafile request to url %s with headers: %s' , datafileUrl , ( ) => JSON . stringify ( headers ) )
199
- const datafileFetch = this . makeGetRequest ( datafileUrl , headers )
212
+ this . currentRequest = this . makeGetRequest ( datafileUrl , headers )
200
213
201
214
const onFetchComplete = ( ) => {
202
215
this . onFetchComplete ( )
@@ -207,7 +220,7 @@ export default abstract class HTTPPollingDatafileManager implements DatafileMana
207
220
const logMakeGetRequestError = ( err : any ) => {
208
221
this . logMakeGetRequestError ( err )
209
222
}
210
- datafileFetch
223
+ this . currentRequest . responsePromise
211
224
. then ( tryUpdatingDatafile , logMakeGetRequestError )
212
225
. then ( onFetchComplete , onFetchComplete )
213
226
}
0 commit comments