Skip to content

Commit

Permalink
Remove duplicate calls with isAuthenticating local promise
Browse files Browse the repository at this point in the history
  • Loading branch information
marcaaron committed Sep 21, 2022
1 parent a0be719 commit d1ad946
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/libs/Middleware/Reauthentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@ import * as Request from '../Request';
import Log from '../Log';
import NetworkConnection from '../NetworkConnection';

// We store a reference to the active authentication request so that we are only ever making one request to authenticate at a time.
let isAuthenticating = null;

/**
* @param {String} commandName
* @returns {Promise}
*/
function reauthenticate(commandName) {
if (isAuthenticating) {
return isAuthenticating;
}

isAuthenticating = Authentication.reauthenticate(commandName)
.then((response) => {
isAuthenticating = null;
return response;
})
.catch((error) => {
isAuthenticating = null;
throw error;
});

return isAuthenticating;
}

/**
* Reauthentication middleware
*
Expand Down Expand Up @@ -54,7 +79,7 @@ function Reauthentication(response, request, isFromSequentialQueue) {
return data;
}

return Authentication.reauthenticate(request.commandName)
return reauthenticate(request.commandName)
.then((authenticateResponse) => {
if (isFromSequentialQueue || apiRequestType === CONST.API_REQUEST_TYPE.MAKE_REQUEST_WITH_SIDE_EFFECTS) {
return Request.processWithMiddleware(request, isFromSequentialQueue);
Expand Down

0 comments on commit d1ad946

Please sign in to comment.