@@ -210,20 +210,29 @@ class Agent extends EmbeddedDocument {
210210 * @return {Object } request The configured axios instance to use for a request.
211211 */
212212 request ( data , parameters = { } ) {
213- instance . interceptors . request . use (
214- ( { httpAgent, httpsAgent, ...request } ) =>
215- new Promise ( ( resolve , reject ) =>
213+ const id = uuidv4 ( ) ;
214+ const interceptor = instance . interceptors . request . use (
215+ ( { httpAgent, httpsAgent, ...request } ) => {
216+ instance . interceptors . request . eject ( interceptor ) ;
217+ return new Promise ( ( resolve , reject ) =>
216218 this . push ( {
217- request : this . handleRequest ( request ) ,
219+ request : this . handleRequest ( request , id ) ,
218220 resolve,
219221 reject
220222 } )
221- )
223+ ) ;
224+ }
222225 ) ;
223226
224- instance . interceptors . response . use (
225- response => this . handleResponse ( response ) ,
226- error => this . handleError ( error )
227+ const response = instance . interceptors . response . use (
228+ response => {
229+ instance . interceptors . response . eject ( response ) ;
230+ return this . handleResponse ( response , id ) ;
231+ } ,
232+ error => {
233+ instance . interceptors . response . eject ( response ) ;
234+ return this . handleError ( error , id ) ;
235+ }
227236 ) ;
228237
229238 return instance (
@@ -244,17 +253,22 @@ class Agent extends EmbeddedDocument {
244253 * @description handles request data before it is sent to the resource. This function
245254 * will eventually be used to cancel the request and return the configuration body.
246255 * This function will test the url for an http proticol and reject if none exist.
247- * @param {Object } response The axios response
256+ * @param {Object } response The axios response.
257+ * @param {String } id the request id.
248258 * @return {Promise } the request configuration object
249259 */
250- handleResponse ( response ) {
260+ handleResponse ( response , id ) {
261+ const token = _ . get ( response , 'config.headers.Authorization' ) ;
262+ if ( token ) {
263+ this . connection . deactivate ( token , id ) ;
264+ }
251265 if ( typeof response . data !== 'object' ) {
252266 return Promise . reject ( {
253267 message : 'The Data API is currently unavailable' ,
254268 code : '1630'
255269 } ) ;
256270 } else {
257- this . connection . extend ( response . config . headers . Authorization ) ;
271+ this . connection . extend ( token ) ;
258272 return response ;
259273 }
260274 }
@@ -266,10 +280,12 @@ class Agent extends EmbeddedDocument {
266280 * @description handles request data before it is sent to the resource. This function
267281 * will eventually be used to cancel the request and return the configuration body.
268282 * This function will test the url for an http proticol and reject if none exist.
269- * @param {Object } config The axios request configuration
283+ * @param {Object } config The axios request configuration.
284+ * @param {String } id the request id.
270285 * @return {Promise } the request configuration object
271286 */
272- handleRequest ( config ) {
287+ handleRequest ( config , id ) {
288+ config . id = id ;
273289 return config . url . startsWith ( 'http' )
274290 ? omit ( config , [ 'params.request' , 'data.request' ] )
275291 : Promise . reject ( {
@@ -360,9 +376,17 @@ class Agent extends EmbeddedDocument {
360376 * function will add an expired property to the error response if it recieves a invalid
361377 * token response.
362378 * @param {Object } error The error recieved from the requested resource.
379+ * @param {String } id the request id.
363380 * @return {Promise } A promise rejection containing a code and a message
364381 */
365- handleError ( error ) {
382+ handleError ( error , id ) {
383+ const token = _ . get ( error , 'config.headers.Authorization' ) ;
384+ if ( token ) {
385+ this . connection . deactivate ( token , id ) ;
386+ }
387+
388+ this . connection . confirm ( ) ;
389+
366390 if ( error . code ) {
367391 return Promise . reject ( { code : error . code , message : error . message } ) ;
368392 } else if (
@@ -375,9 +399,7 @@ class Agent extends EmbeddedDocument {
375399 } ) ;
376400 } else {
377401 if ( error . response . data . messages [ 0 ] . code === '952' )
378- this . connection . clear (
379- _ . get ( error , 'response.config.headers.Authorization' )
380- ) ;
402+ this . connection . clear ( token ) ;
381403 return Promise . reject ( error . response . data . messages [ 0 ] ) ;
382404 }
383405 }
@@ -401,15 +423,24 @@ class Agent extends EmbeddedDocument {
401423 const WATCHER = setTimeout (
402424 function watch ( ) {
403425 this . connection . clear ( ) ;
404- if ( this . queue . length > 0 ) {
426+ if (
427+ this . queue . length > 0 &&
428+ ! this . connection . starting &&
429+ this . connection . available ( )
430+ ) {
405431 this . shift ( ) ;
406432 }
407433
408- if ( this . pending . length > 0 && this . connection . ready ( ) ) {
434+ if (
435+ this . pending . length > 0 &&
436+ ! this . connection . starting &&
437+ this . connection . available ( )
438+ ) {
409439 this . resolve ( ) ;
410440 }
411441
412442 if (
443+ this . pending . length > 0 &&
413444 ! this . connection . available ( ) &&
414445 ! this . connection . starting &&
415446 this . connection . sessions . length < this . concurrency
@@ -453,7 +484,8 @@ class Agent extends EmbeddedDocument {
453484 Object . assign (
454485 this . mutate ( pending . request , ( value , key ) =>
455486 key . replace ( / { { dot} } / g, '.' )
456- )
487+ ) ,
488+ { id : pending . id }
457489 ) ,
458490 _ . isEmpty ( this . agent ) ? { } : this . localize ( )
459491 )
0 commit comments