diff --git a/lib/selectors/keyserver-selectors.js b/lib/selectors/keyserver-selectors.js --- a/lib/selectors/keyserver-selectors.js +++ b/lib/selectors/keyserver-selectors.js @@ -1,5 +1,6 @@ // @flow +import _memoize from 'lodash/memoize.js'; import { createSelector } from 'reselect'; import type { PlatformDetails } from '../types/device-types'; @@ -43,11 +44,17 @@ state: AppState, ) => state.keyserverStore.keyserverInfos[ashoatKeyserverID]?.connection; +const baseLastCommunicatedPlatformDetailsSelector: ( + id: string, +) => (state: AppState) => ?PlatformDetails = + (id: string) => (state: AppState) => + state.keyserverStore.keyserverInfos[id]?.lastCommunicatedPlatformDetails; + const lastCommunicatedPlatformDetailsSelector: ( - state: AppState, -) => ?PlatformDetails = (state: AppState) => - state.keyserverStore.keyserverInfos[ashoatKeyserverID] - ?.lastCommunicatedPlatformDetails; + id: string, +) => (state: AppState) => ?PlatformDetails = _memoize( + baseLastCommunicatedPlatformDetailsSelector, +); export { cookieSelector, diff --git a/lib/selectors/server-calls.js b/lib/selectors/server-calls.js --- a/lib/selectors/server-calls.js +++ b/lib/selectors/server-calls.js @@ -1,5 +1,6 @@ // @flow +import _memoize from 'lodash/memoize.js'; import { createSelector } from 'reselect'; import { @@ -26,14 +27,16 @@ +lastCommunicatedPlatformDetails: ?PlatformDetails, }; -const serverCallStateSelector: (state: AppState) => ServerCallState = +const baseServerCallStateSelector: ( + id: string, +) => (state: AppState) => ServerCallState = (id: string) => createSelector( cookieSelector, urlPrefixSelector, sessionIDSelector, (state: AppState) => state.currentUserInfo, connectionSelector, - lastCommunicatedPlatformDetailsSelector, + lastCommunicatedPlatformDetailsSelector(id), ( cookie: ?string, urlPrefix: ?string, @@ -51,4 +54,10 @@ }), ); +const serverCallStateSelector: ( + id: string, +) => (state: AppState) => ServerCallState = _memoize( + baseServerCallStateSelector, +); + export { serverCallStateSelector }; diff --git a/lib/utils/action-utils.js b/lib/utils/action-utils.js --- a/lib/utils/action-utils.js +++ b/lib/utils/action-utils.js @@ -12,6 +12,7 @@ CallServerEndpointOptions, } from './call-server-endpoint.js'; import { getConfig } from './config.js'; +import { ashoatKeyserverID } from './validation-utils.js'; import { serverCallStateSelector } from '../selectors/server-calls.js'; import { logInActionSources, @@ -435,7 +436,9 @@ paramOverride?: ?$Shape, ): F { const dispatch = useDispatch(); - const serverCallState = useSelector(serverCallStateSelector); + const serverCallState = useSelector( + serverCallStateSelector(ashoatKeyserverID), + ); return React.useMemo(() => { const { urlPrefix, connectionStatus } = serverCallState; invariant( diff --git a/native/socket.react.js b/native/socket.react.js --- a/native/socket.react.js +++ b/native/socket.react.js @@ -21,6 +21,7 @@ useDispatchActionPromise, fetchNewCookieFromNativeCredentials, } from 'lib/utils/action-utils.js'; +import { ashoatKeyserverID } from 'lib/utils/validation-utils.js'; import { InputStateContext } from './input/input-state.js'; import { @@ -102,7 +103,7 @@ }, [active, navContext]); const lastCommunicatedPlatformDetails = useSelector( - lastCommunicatedPlatformDetailsSelector, + lastCommunicatedPlatformDetailsSelector(ashoatKeyserverID), ); const dispatch = useDispatch(); diff --git a/web/socket.react.js b/web/socket.react.js --- a/web/socket.react.js +++ b/web/socket.react.js @@ -17,6 +17,7 @@ useServerCall, useDispatchActionPromise, } from 'lib/utils/action-utils.js'; +import { ashoatKeyserverID } from 'lib/utils/validation-utils.js'; import { useSelector } from './redux/redux-utils.js'; import { @@ -67,7 +68,7 @@ const callLogOut = useServerCall(logOut); const lastCommunicatedPlatformDetails = useSelector( - lastCommunicatedPlatformDetailsSelector, + lastCommunicatedPlatformDetailsSelector(ashoatKeyserverID), ); return (