diff --git a/lib/utils/keyserver-call.js b/lib/utils/keyserver-call.js --- a/lib/utils/keyserver-call.js +++ b/lib/utils/keyserver-call.js @@ -36,7 +36,6 @@ // _memoize memoizes the function by caching the result. // The first argument of the memoized function is used as the map cache key. const baseCreateBoundServerCallsSelector = ( - // eslint-disable-next-line no-unused-vars keyserverID: string, ): (BindServerCallsParams => CallServerEndpoint) => createSelector( @@ -127,70 +126,74 @@ +keyserverCallInfos: { +[keyserverID: string]: KeyserverCallInfo }, }; -const bindCallKeyserverEndpointSelector: BindKeyserverCallParams => < +type BindCallKeyserverSelectorSelector = BindKeyserverCallParams => < Args: mixed, Return, >( keyserverCall: ActionFunc, -) => Args => Promise = createSelector( - (state: BindKeyserverCallParams) => state.dispatch, - (state: BindKeyserverCallParams) => state.currentUserInfo, - (state: BindKeyserverCallParams) => state.keyserverCallInfos, - ( - dispatch: Dispatch, - currentUserInfo: ?CurrentUserInfo, - keyserverCallInfos: { +[keyserverID: string]: KeyserverCallInfo }, - ) => { - return _memoize( - ( - keyserverCall: ActionFunc, - ): (Args => Promise) => { - const callKeyserverEndpoint = ( - endpoint: Endpoint, - requests: { +[keyserverID: string]: ?{ +[string]: mixed } }, - options?: ?CallServerEndpointOptions, - ) => { - const bindCallKeyserverEndpoint = (keyserverID: string) => { - const { - cookie, - urlPrefix, - sessionID, - isSocketConnected, - lastCommunicatedPlatformDetails, - } = keyserverCallInfos[keyserverID]; - - const boundCallServerEndpoint = createBoundServerCallsSelector( - keyserverID, - )({ - dispatch, - currentUserInfo, - cookie, - urlPrefix, - sessionID, - isSocketConnected, - lastCommunicatedPlatformDetails, - keyserverID, - }); - - return boundCallServerEndpoint( - endpoint, - requests[keyserverID], - options, - ); - }; - - const promises: { [string]: Promise } = {}; - for (const keyserverID in requests) { - promises[keyserverID] = bindCallKeyserverEndpoint(keyserverID); - } - return promiseAll(promises); - }; - const keyserverIDs = Object.keys(keyserverCallInfos); - return keyserverCall(callKeyserverEndpoint, keyserverIDs); +) => Args => Promise; + +const createBindCallKeyserverEndpointSelector: () => BindCallKeyserverSelectorSelector = + () => + createSelector( + (state: BindKeyserverCallParams) => state.dispatch, + (state: BindKeyserverCallParams) => state.currentUserInfo, + (state: BindKeyserverCallParams) => state.keyserverCallInfos, + ( + dispatch: Dispatch, + currentUserInfo: ?CurrentUserInfo, + keyserverCallInfos: { +[keyserverID: string]: KeyserverCallInfo }, + ) => { + return _memoize( + ( + keyserverCall: ActionFunc, + ): (Args => Promise) => { + const callKeyserverEndpoint = ( + endpoint: Endpoint, + requests: { +[keyserverID: string]: ?{ +[string]: mixed } }, + options?: ?CallServerEndpointOptions, + ) => { + const bindCallKeyserverEndpoint = (keyserverID: string) => { + const { + cookie, + urlPrefix, + sessionID, + isSocketConnected, + lastCommunicatedPlatformDetails, + } = keyserverCallInfos[keyserverID]; + + const boundCallServerEndpoint = createBoundServerCallsSelector( + keyserverID, + )({ + dispatch, + currentUserInfo, + cookie, + urlPrefix, + sessionID, + isSocketConnected, + lastCommunicatedPlatformDetails, + keyserverID, + }); + + return boundCallServerEndpoint( + endpoint, + requests[keyserverID], + options, + ); + }; + + const promises: { [string]: Promise } = {}; + for (const keyserverID in requests) { + promises[keyserverID] = bindCallKeyserverEndpoint(keyserverID); + } + return promiseAll(promises); + }; + const keyserverIDs = Object.keys(keyserverCallInfos); + return keyserverCall(callKeyserverEndpoint, keyserverIDs); + }, + ); }, ); - }, -); export type KeyserverCallParamOverride = Partial<{ +dispatch: Dispatch, @@ -219,7 +222,15 @@ baseCombinedInfo; const keyserverCallInfos = useKeyserverCallInfos(keyserverInfoPartials); - const bindCallKeyserverEndpointToAction = bindCallKeyserverEndpointSelector({ + const bindCallKeyserverEndpointSelectorRef = + React.useRef(); + if (!bindCallKeyserverEndpointSelectorRef.current) { + bindCallKeyserverEndpointSelectorRef.current = + createBindCallKeyserverEndpointSelector(); + } + const bindSelector = bindCallKeyserverEndpointSelectorRef.current; + + const bindCallKeyserverEndpointToAction = bindSelector({ ...restCombinedInfo, keyserverCallInfos, });