diff --git a/lib/keyserver-conn/call-keyserver-endpoint-provider.react.js b/lib/keyserver-conn/call-keyserver-endpoint-provider.react.js --- a/lib/keyserver-conn/call-keyserver-endpoint-provider.react.js +++ b/lib/keyserver-conn/call-keyserver-endpoint-provider.react.js @@ -35,10 +35,9 @@ const CallKeyserverEndpointContext: React.Context = React.createContext(); -let currentlyWaitingForNewCookie = false; -let serverEndpointCallsWaitingForNewCookie: (( - callServerEndpoint: ?CallServerEndpoint, -) => void)[] = []; +type OngoingRecoveryAttempt = { + +waitingCalls: Array<(callServerEndpoint: ?CallServerEndpoint) => mixed>, +}; export type BindServerCallsParams = { +dispatch: Dispatch, @@ -55,6 +54,10 @@ +children: React.Node, }; function CallKeyserverEndpointProvider(props: Props): React.Node { + const ongoingRecoveryAttemptsRef = React.useRef< + Map, + >(new Map()); + const bindCookieAndUtilsIntoCallServerEndpoint: ( params: BindServerCallsParams, ) => CallServerEndpoint = React.useCallback(params => { @@ -94,13 +97,15 @@ // just let the caller callServerEndpoint instance continue return Promise.resolve(null); } - if (!currentlyWaitingForNewCookie) { + const ongoingRecoveryAttempt = + ongoingRecoveryAttemptsRef.current.get(keyserverID); + if (!ongoingRecoveryAttempt) { // Our cookie seems to be valid return Promise.resolve(null); } // Wait to run until we get our new cookie return new Promise(r => - serverEndpointCallsWaitingForNewCookie.push(r), + ongoingRecoveryAttempt.waitingCalls.push(r), ); }; // This function is a helper for the next function defined below @@ -116,9 +121,10 @@ keyserverID, ); - currentlyWaitingForNewCookie = false; - const currentWaitingCalls = serverEndpointCallsWaitingForNewCookie; - serverEndpointCallsWaitingForNewCookie = []; + const ongoingRecoveryAttempt = + ongoingRecoveryAttemptsRef.current.get(keyserverID); + ongoingRecoveryAttemptsRef.current.delete(keyserverID); + const currentWaitingCalls = ongoingRecoveryAttempt?.waitingCalls ?? []; const newCallServerEndpoint = newSessionChange ? bindCookieAndUtilsIntoCallServerEndpoint({ @@ -147,12 +153,14 @@ // user to log-in after a cookieInvalidation while logged out return Promise.resolve(null); } - if (currentlyWaitingForNewCookie) { + const ongoingRecoveryAttempt = + ongoingRecoveryAttemptsRef.current.get(keyserverID); + if (ongoingRecoveryAttempt) { return new Promise(r => - serverEndpointCallsWaitingForNewCookie.push(r), + ongoingRecoveryAttempt.waitingCalls.push(r), ); } - currentlyWaitingForNewCookie = true; + ongoingRecoveryAttemptsRef.current.set(keyserverID, { waitingCalls: [] }); return attemptToResolveInvalidation(sessionChange); };