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( @@ -121,76 +120,67 @@ ); } -type BindKeyserverCallParams = { - +dispatch: Dispatch, - +currentUserInfo: ?CurrentUserInfo, - +keyserverCallInfos: { +[keyserverID: string]: KeyserverCallInfo }, -}; - -const bindCallKeyserverEndpointSelector: BindKeyserverCallParams => < - Args: mixed, - Return, ->( +type BindCallKeyserverSelector = ( 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, - ); +) => Args => Promise; +function useBindCallKeyserverEndpointSelector( + dispatch: Dispatch, + currentUserInfo: ?CurrentUserInfo, + keyserverCallInfos: { +[keyserverID: string]: KeyserverCallInfo }, +): BindCallKeyserverSelector { + return React.useMemo( + () => + _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 promises: { [string]: Promise } = {}; - for (const keyserverID in requests) { - promises[keyserverID] = bindCallKeyserverEndpoint(keyserverID); - } - return promiseAll(promises); - }; - const keyserverIDs = Object.keys(keyserverCallInfos); - return keyserverCall(callKeyserverEndpoint, keyserverIDs); - }, - ); - }, -); + const keyserverIDs = Object.keys(keyserverCallInfos); + return keyserverCall(callKeyserverEndpoint, keyserverIDs); + }, + ), + [dispatch, currentUserInfo, keyserverCallInfos], + ); +} export type KeyserverCallParamOverride = Partial<{ +dispatch: Dispatch, @@ -202,27 +192,33 @@ keyserverCall: ActionFunc, paramOverride?: ?KeyserverCallParamOverride, ): Args => Promise { - const dispatch = useDispatch(); - const currentUserInfo = useSelector(state => state.currentUserInfo); + const baseDispatch = useDispatch(); + const baseCurrentUserInfo = useSelector(state => state.currentUserInfo); const keyserverInfos = useSelector( state => state.keyserverStore.keyserverInfos, ); const baseCombinedInfo = { - dispatch, + dispatch: baseDispatch, + currentUserInfo: baseCurrentUserInfo, keyserverInfos, - currentUserInfo, ...paramOverride, }; - const { keyserverInfos: keyserverInfoPartials, ...restCombinedInfo } = - baseCombinedInfo; + const { + dispatch, + currentUserInfo, + keyserverInfos: keyserverInfoPartials, + } = baseCombinedInfo; const keyserverCallInfos = useKeyserverCallInfos(keyserverInfoPartials); - const bindCallKeyserverEndpointToAction = bindCallKeyserverEndpointSelector({ - ...restCombinedInfo, - keyserverCallInfos, - }); + const bindCallKeyserverEndpointToAction = + useBindCallKeyserverEndpointSelector( + dispatch, + currentUserInfo, + keyserverCallInfos, + ); + return React.useMemo( () => bindCallKeyserverEndpointToAction(keyserverCall), [bindCallKeyserverEndpointToAction, keyserverCall],