Changeset View
Changeset View
Standalone View
Standalone View
lib/utils/action-utils.js
Show All 30 Lines | import type { | ||||
Dispatch, | Dispatch, | ||||
PromisedAction, | PromisedAction, | ||||
BaseAction, | BaseAction, | ||||
} from '../types/redux-types.js'; | } from '../types/redux-types.js'; | ||||
import type { | import type { | ||||
ClientSessionChange, | ClientSessionChange, | ||||
PreRequestUserState, | PreRequestUserState, | ||||
} from '../types/session-types.js'; | } from '../types/session-types.js'; | ||||
import type { ConnectionStatus } from '../types/socket-types.js'; | |||||
import type { CurrentUserInfo } from '../types/user-types.js'; | import type { CurrentUserInfo } from '../types/user-types.js'; | ||||
function extractKeyserverIDFromID(id: string): string { | function extractKeyserverIDFromID(id: string): string { | ||||
return id.split('|')[0]; | return id.split('|')[0]; | ||||
} | } | ||||
let nextPromiseIndex = 0; | let nextPromiseIndex = 0; | ||||
▲ Show 20 Lines • Show All 179 Lines • ▼ Show 20 Lines | ) => { | ||||
try { | try { | ||||
const result = await callServerEndpoint( | const result = await callServerEndpoint( | ||||
cookie, | cookie, | ||||
innerBoundSetNewSession, | innerBoundSetNewSession, | ||||
() => new Promise(r => r(null)), | () => new Promise(r => r(null)), | ||||
() => new Promise(r => r(null)), | () => new Promise(r => r(null)), | ||||
urlPrefix, | urlPrefix, | ||||
null, | null, | ||||
'disconnected', | false, | ||||
null, | null, | ||||
null, | null, | ||||
endpoint, | endpoint, | ||||
data, | data, | ||||
dispatch, | dispatch, | ||||
options, | options, | ||||
false, | false, | ||||
keyserverID, | keyserverID, | ||||
▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | function bindCookieAndUtilsIntoCallServerEndpoint( | ||||
params: BindServerCallsParams, | params: BindServerCallsParams, | ||||
): CallServerEndpoint { | ): CallServerEndpoint { | ||||
const { | const { | ||||
dispatch, | dispatch, | ||||
cookie, | cookie, | ||||
urlPrefix, | urlPrefix, | ||||
sessionID, | sessionID, | ||||
currentUserInfo, | currentUserInfo, | ||||
connectionStatus, | isSocketConnected, | ||||
lastCommunicatedPlatformDetails, | lastCommunicatedPlatformDetails, | ||||
keyserverID, | keyserverID, | ||||
} = params; | } = params; | ||||
const loggedIn = !!(currentUserInfo && !currentUserInfo.anonymous && true); | const loggedIn = !!(currentUserInfo && !currentUserInfo.anonymous && true); | ||||
const boundSetNewSession = ( | const boundSetNewSession = ( | ||||
sessionChange: ClientSessionChange, | sessionChange: ClientSessionChange, | ||||
error: ?string, | error: ?string, | ||||
) => | ) => | ||||
▲ Show 20 Lines • Show All 86 Lines • ▼ Show 20 Lines | ): CallServerEndpoint { | ||||
) => | ) => | ||||
callServerEndpoint( | callServerEndpoint( | ||||
cookie, | cookie, | ||||
boundSetNewSession, | boundSetNewSession, | ||||
waitIfCookieInvalidated, | waitIfCookieInvalidated, | ||||
cookieInvalidationRecovery, | cookieInvalidationRecovery, | ||||
urlPrefix, | urlPrefix, | ||||
sessionID, | sessionID, | ||||
connectionStatus, | isSocketConnected, | ||||
lastCommunicatedPlatformDetails, | lastCommunicatedPlatformDetails, | ||||
socketAPIHandler, | socketAPIHandler, | ||||
endpoint, | endpoint, | ||||
data, | data, | ||||
dispatch, | dispatch, | ||||
options, | options, | ||||
loggedIn, | loggedIn, | ||||
keyserverID, | keyserverID, | ||||
); | ); | ||||
} | } | ||||
export type ActionFunc<F> = (callServerEndpoint: CallServerEndpoint) => F; | export type ActionFunc<F> = (callServerEndpoint: CallServerEndpoint) => F; | ||||
export type BindServerCall = <F>(serverCall: ActionFunc<F>) => F; | export type BindServerCall = <F>(serverCall: ActionFunc<F>) => F; | ||||
export type BindServerCallsParams = { | export type BindServerCallsParams = { | ||||
+dispatch: Dispatch, | +dispatch: Dispatch, | ||||
+cookie: ?string, | +cookie: ?string, | ||||
+urlPrefix: string, | +urlPrefix: string, | ||||
+sessionID: ?string, | +sessionID: ?string, | ||||
+currentUserInfo: ?CurrentUserInfo, | +currentUserInfo: ?CurrentUserInfo, | ||||
+connectionStatus: ConnectionStatus, | +isSocketConnected: boolean, | ||||
+lastCommunicatedPlatformDetails: ?PlatformDetails, | +lastCommunicatedPlatformDetails: ?PlatformDetails, | ||||
+keyserverID: string, | +keyserverID: string, | ||||
}; | }; | ||||
// All server calls needs to include some information from the Redux state | // All server calls needs to include some information from the Redux state | ||||
// (namely, the cookie). This information is used deep in the server call, | // (namely, the cookie). This information is used deep in the server call, | ||||
// at the point where callServerEndpoint is called. We don't want to bother | // at the point where callServerEndpoint is called. We don't want to bother | ||||
// propagating the cookie (and any future config info that callServerEndpoint | // propagating the cookie (and any future config info that callServerEndpoint | ||||
// needs) through to the server calls so they can pass it to callServerEndpoint. | // needs) through to the server calls so they can pass it to callServerEndpoint. | ||||
// Instead, we "curry" the cookie onto callServerEndpoint within react-redux's | // Instead, we "curry" the cookie onto callServerEndpoint within react-redux's | ||||
// connect's mapStateToProps function, and then pass that "bound" | // connect's mapStateToProps function, and then pass that "bound" | ||||
// callServerEndpoint that no longer needs the cookie as a parameter on to | // callServerEndpoint that no longer needs the cookie as a parameter on to | ||||
// the server call. | // the server call. | ||||
const baseCreateBoundServerCallsSelector = <F>( | const baseCreateBoundServerCallsSelector = <F>( | ||||
actionFunc: ActionFunc<F>, | actionFunc: ActionFunc<F>, | ||||
): (BindServerCallsParams => F) => | ): (BindServerCallsParams => F) => | ||||
createSelector( | createSelector( | ||||
(state: BindServerCallsParams) => state.dispatch, | (state: BindServerCallsParams) => state.dispatch, | ||||
(state: BindServerCallsParams) => state.cookie, | (state: BindServerCallsParams) => state.cookie, | ||||
(state: BindServerCallsParams) => state.urlPrefix, | (state: BindServerCallsParams) => state.urlPrefix, | ||||
(state: BindServerCallsParams) => state.sessionID, | (state: BindServerCallsParams) => state.sessionID, | ||||
(state: BindServerCallsParams) => state.currentUserInfo, | (state: BindServerCallsParams) => state.currentUserInfo, | ||||
(state: BindServerCallsParams) => state.connectionStatus, | (state: BindServerCallsParams) => state.isSocketConnected, | ||||
(state: BindServerCallsParams) => state.lastCommunicatedPlatformDetails, | (state: BindServerCallsParams) => state.lastCommunicatedPlatformDetails, | ||||
(state: BindServerCallsParams) => state.keyserverID, | (state: BindServerCallsParams) => state.keyserverID, | ||||
( | ( | ||||
dispatch: Dispatch, | dispatch: Dispatch, | ||||
cookie: ?string, | cookie: ?string, | ||||
urlPrefix: string, | urlPrefix: string, | ||||
sessionID: ?string, | sessionID: ?string, | ||||
currentUserInfo: ?CurrentUserInfo, | currentUserInfo: ?CurrentUserInfo, | ||||
connectionStatus: ConnectionStatus, | isSocketConnected: boolean, | ||||
lastCommunicatedPlatformDetails: ?PlatformDetails, | lastCommunicatedPlatformDetails: ?PlatformDetails, | ||||
keyserverID: string, | keyserverID: string, | ||||
) => { | ) => { | ||||
const boundCallServerEndpoint = bindCookieAndUtilsIntoCallServerEndpoint({ | const boundCallServerEndpoint = bindCookieAndUtilsIntoCallServerEndpoint({ | ||||
dispatch, | dispatch, | ||||
cookie, | cookie, | ||||
urlPrefix, | urlPrefix, | ||||
sessionID, | sessionID, | ||||
currentUserInfo, | currentUserInfo, | ||||
connectionStatus, | isSocketConnected, | ||||
lastCommunicatedPlatformDetails, | lastCommunicatedPlatformDetails, | ||||
keyserverID, | keyserverID, | ||||
}); | }); | ||||
return actionFunc(boundCallServerEndpoint); | return actionFunc(boundCallServerEndpoint); | ||||
}, | }, | ||||
); | ); | ||||
type CreateBoundServerCallsSelectorType = <F>( | type CreateBoundServerCallsSelectorType = <F>( | ||||
ActionFunc<F>, | ActionFunc<F>, | ||||
) => BindServerCallsParams => F; | ) => BindServerCallsParams => F; | ||||
const createBoundServerCallsSelector: CreateBoundServerCallsSelectorType = | const createBoundServerCallsSelector: CreateBoundServerCallsSelectorType = | ||||
(_memoize(baseCreateBoundServerCallsSelector): any); | (_memoize(baseCreateBoundServerCallsSelector): any); | ||||
function useServerCall<F>( | function useServerCall<F>( | ||||
serverCall: ActionFunc<F>, | serverCall: ActionFunc<F>, | ||||
paramOverride?: ?Partial<BindServerCallsParams>, | paramOverride?: ?Partial<BindServerCallsParams>, | ||||
): F { | ): F { | ||||
const dispatch = useDispatch(); | const dispatch = useDispatch(); | ||||
const serverCallState = useSelector( | const serverCallState = useSelector( | ||||
serverCallStateSelector(ashoatKeyserverID), | serverCallStateSelector(ashoatKeyserverID), | ||||
); | ); | ||||
return React.useMemo(() => { | return React.useMemo(() => { | ||||
const { urlPrefix, connectionStatus } = serverCallState; | const { urlPrefix, isSocketConnected } = serverCallState; | ||||
invariant( | invariant( | ||||
!!urlPrefix && !!connectionStatus, | !!urlPrefix && | ||||
isSocketConnected !== undefined && | |||||
isSocketConnected !== null, | |||||
'keyserver missing from keyserverStore', | 'keyserver missing from keyserverStore', | ||||
); | ); | ||||
return createBoundServerCallsSelector(serverCall)({ | return createBoundServerCallsSelector(serverCall)({ | ||||
...serverCallState, | ...serverCallState, | ||||
urlPrefix, | urlPrefix, | ||||
connectionStatus, | isSocketConnected, | ||||
dispatch, | dispatch, | ||||
...paramOverride, | ...paramOverride, | ||||
keyserverID: ashoatKeyserverID, | keyserverID: ashoatKeyserverID, | ||||
}); | }); | ||||
}, [serverCall, serverCallState, dispatch, paramOverride]); | }, [serverCall, serverCallState, dispatch, paramOverride]); | ||||
} | } | ||||
let socketAPIHandler: ?SocketAPIHandler = null; | let socketAPIHandler: ?SocketAPIHandler = null; | ||||
▲ Show 20 Lines • Show All 93 Lines • Show Last 20 Lines |