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 @@ -42,8 +42,13 @@ const currentAsOfSelector: (state: AppState) => number = (state: AppState) => state.messageStore.currentAsOf[ashoatKeyserverID] ?? 0; -const urlPrefixSelector: (state: AppState) => ?string = (state: AppState) => - state.keyserverStore.keyserverInfos[ashoatKeyserverID]?.urlPrefix; +const baseUrlPrefixSelector: ( + keyserverID: string, +) => (state: AppState) => ?string = keyserverID => (state: AppState) => + state.keyserverStore.keyserverInfos[keyserverID]?.urlPrefix; + +const urlPrefixSelector: (keyserverID: string) => (state: AppState) => ?string = + _memoize(baseUrlPrefixSelector); const baseConnectionSelector: ( keyserverID: string, 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 @@ -32,7 +32,7 @@ ) => (state: AppState) => ServerCallState = keyserverID => createSelector( cookieSelector, - urlPrefixSelector, + urlPrefixSelector(keyserverID), sessionIDSelector, (state: AppState) => state.currentUserInfo, connectionSelector(keyserverID), diff --git a/native/account/logged-out-modal.react.js b/native/account/logged-out-modal.react.js --- a/native/account/logged-out-modal.react.js +++ b/native/account/logged-out-modal.react.js @@ -810,7 +810,7 @@ ); const persistedStateLoaded = usePersistedStateLoaded(); const cookie = useSelector(cookieSelector); - const urlPrefix = useSelector(urlPrefixSelector); + const urlPrefix = useSelector(urlPrefixSelector(ashoatKeyserverID)); invariant(urlPrefix, "missing urlPrefix for ashoat's keyserver"); const loggedIn = useSelector(isLoggedIn); const dimensions = useSelector(derivedDimensionsInfoSelector); diff --git a/native/data/sqlite-data-handler.js b/native/data/sqlite-data-handler.js --- a/native/data/sqlite-data-handler.js +++ b/native/data/sqlite-data-handler.js @@ -48,7 +48,7 @@ state => !!(state._persist && state._persist.rehydrated), ); const cookie = useSelector(cookieSelector); - const urlPrefix = useSelector(urlPrefixSelector); + const urlPrefix = useSelector(urlPrefixSelector(ashoatKeyserverID)); invariant(urlPrefix, "missing urlPrefix for ashoat's keyserver"); const staffCanSee = useStaffCanSee(); const { staffUserHasBeenLoggedIn } = React.useContext(StaffContext); diff --git a/native/profile/custom-server-modal.react.js b/native/profile/custom-server-modal.react.js --- a/native/profile/custom-server-modal.react.js +++ b/native/profile/custom-server-modal.react.js @@ -8,6 +8,7 @@ import { urlPrefixSelector } from 'lib/selectors/keyserver-selectors.js'; import type { Dispatch } from 'lib/types/redux-types.js'; import { setURLPrefix } from 'lib/utils/url-utils.js'; +import { ashoatKeyserverID } from 'lib/utils/validation-utils.js'; import Button from '../components/button.react.js'; import Modal from '../components/modal.react.js'; @@ -119,7 +120,7 @@ const ConnectedCustomServerModal: React.ComponentType = React.memo(function ConnectedCustomServerModal(props: BaseProps) { - const urlPrefix = useSelector(urlPrefixSelector); + const urlPrefix = useSelector(urlPrefixSelector(ashoatKeyserverID)); invariant(urlPrefix, "missing urlPrefix for ashoat's keyserver"); const customServer = useSelector(state => state.customServer); const styles = useStyles(unboundStyles); diff --git a/native/profile/dev-tools.react.js b/native/profile/dev-tools.react.js --- a/native/profile/dev-tools.react.js +++ b/native/profile/dev-tools.react.js @@ -9,6 +9,7 @@ import { urlPrefixSelector } from 'lib/selectors/keyserver-selectors.js'; import type { Dispatch } from 'lib/types/redux-types.js'; import { setURLPrefix } from 'lib/utils/url-utils.js'; +import { ashoatKeyserverID } from 'lib/utils/validation-utils.js'; import type { ProfileNavigationProp } from './profile.react.js'; import Button from '../components/button.react.js'; @@ -236,7 +237,7 @@ const ConnectedDevTools: React.ComponentType = React.memo( function ConnectedDevTools(props: BaseProps) { - const urlPrefix = useSelector(urlPrefixSelector); + const urlPrefix = useSelector(urlPrefixSelector(ashoatKeyserverID)); invariant(urlPrefix, "missing urlPrefix for ashoat's keyserver"); const customServer = useSelector(state => state.customServer); const colors = useColors(); diff --git a/native/selectors/socket-selectors.js b/native/selectors/socket-selectors.js --- a/native/selectors/socket-selectors.js +++ b/native/selectors/socket-selectors.js @@ -1,5 +1,6 @@ // @flow +import _memoize from 'lodash/memoize.js'; import { createSelector } from 'reselect'; import { @@ -27,9 +28,11 @@ import type { AppState } from '../redux/state-types.js'; import type { NavPlusRedux } from '../types/selector-types.js'; -const openSocketSelector: (state: AppState) => ?() => WebSocket = +const baseOpenSocketSelector: ( + keyserverID: string, +) => (state: AppState) => ?() => WebSocket = keyserverID => createSelector( - urlPrefixSelector, + urlPrefixSelector(keyserverID), // We don't actually use the cookie in the socket open function, // but we do use it in the initial message, and when the cookie changes // the socket needs to be reopened. By including the cookie here, @@ -44,6 +47,10 @@ }, ); +const openSocketSelector: ( + keyserverID: string, +) => (state: AppState) => ?() => WebSocket = _memoize(baseOpenSocketSelector); + const sessionIdentificationSelector: ( state: AppState, ) => SessionIdentification = createSelector( diff --git a/native/socket.react.js b/native/socket.react.js --- a/native/socket.react.js +++ b/native/socket.react.js @@ -46,7 +46,7 @@ const navContext = React.useContext(NavContext); const cookie = useSelector(cookieSelector); - const urlPrefix = useSelector(urlPrefixSelector); + const urlPrefix = useSelector(urlPrefixSelector(ashoatKeyserverID)); invariant(urlPrefix, 'missing urlPrefix for given keyserver id'); const connection = useSelector(connectionSelector(ashoatKeyserverID)); invariant(connection, 'keyserver missing from keyserverStore'); @@ -59,7 +59,7 @@ ); const currentUserInfo = useSelector(state => state.currentUserInfo); - const openSocket = useSelector(openSocketSelector); + const openSocket = useSelector(openSocketSelector(ashoatKeyserverID)); invariant(openSocket, 'openSocket failed to be created'); const sessionIdentification = useSelector(sessionIdentificationSelector); const preRequestUserState = useSelector(preRequestUserStateSelector); diff --git a/web/selectors/socket-selectors.js b/web/selectors/socket-selectors.js --- a/web/selectors/socket-selectors.js +++ b/web/selectors/socket-selectors.js @@ -1,6 +1,7 @@ // @flow import olm from '@commapp/olm'; +import _memoize from 'lodash/memoize.js'; import { createSelector } from 'reselect'; import { @@ -32,14 +33,20 @@ import { initOlm } from '../olm/olm-utils.js'; import type { AppState } from '../redux/redux-setup.js'; -const openSocketSelector: (state: AppState) => ?() => WebSocket = - createSelector(urlPrefixSelector, (urlPrefix: ?string) => { +const baseOpenSocketSelector: ( + keyserverID: string, +) => (state: AppState) => ?() => WebSocket = keyserverID => + createSelector(urlPrefixSelector(keyserverID), (urlPrefix: ?string) => { if (!urlPrefix) { return null; } return createOpenSocketFunction(urlPrefix); }); +const openSocketSelector: ( + keyserverID: string, +) => (state: AppState) => ?() => WebSocket = _memoize(baseOpenSocketSelector); + const sessionIdentificationSelector: ( state: AppState, ) => SessionIdentification = createSelector( diff --git a/web/socket.react.js b/web/socket.react.js --- a/web/socket.react.js +++ b/web/socket.react.js @@ -32,7 +32,7 @@ const WebSocket: React.ComponentType = React.memo(function WebSocket(props) { const cookie = useSelector(cookieSelector); - const urlPrefix = useSelector(urlPrefixSelector); + const urlPrefix = useSelector(urlPrefixSelector(ashoatKeyserverID)); invariant(urlPrefix, 'missing urlPrefix for given keyserver id'); const connection = useSelector(connectionSelector(ashoatKeyserverID)); invariant(connection, 'keyserver missing from keyserverStore'); @@ -43,7 +43,7 @@ state.lifecycleState !== 'background', ); - const openSocket = useSelector(openSocketSelector); + const openSocket = useSelector(openSocketSelector(ashoatKeyserverID)); invariant(openSocket, 'openSocket failed to be created'); const sessionIdentification = useSelector(sessionIdentificationSelector); const preRequestUserState = useSelector(preRequestUserStateSelector);