Changeset View
Changeset View
Standalone View
Standalone View
lib/utils/call-server-endpoint.js
Show All 20 Lines | import { | ||||
endpointIsSocketOnly, | endpointIsSocketOnly, | ||||
} from '../types/endpoints.js'; | } from '../types/endpoints.js'; | ||||
import { forcePolicyAcknowledgmentActionType } from '../types/policy-types.js'; | import { forcePolicyAcknowledgmentActionType } from '../types/policy-types.js'; | ||||
import type { Dispatch } from '../types/redux-types.js'; | import type { Dispatch } from '../types/redux-types.js'; | ||||
import type { | import type { | ||||
ServerSessionChange, | ServerSessionChange, | ||||
ClientSessionChange, | ClientSessionChange, | ||||
} from '../types/session-types.js'; | } from '../types/session-types.js'; | ||||
import type { ConnectionStatus } from '../types/socket-types'; | |||||
import type { CurrentUserInfo } from '../types/user-types.js'; | import type { CurrentUserInfo } from '../types/user-types.js'; | ||||
export type CallServerEndpointOptions = Partial<{ | export type CallServerEndpointOptions = Partial<{ | ||||
// null timeout means no timeout, which is the default for uploadBlob | // null timeout means no timeout, which is the default for uploadBlob | ||||
+timeout: ?number, // in milliseconds | +timeout: ?number, // in milliseconds | ||||
// getResultInfo will be called right before callServerEndpoint successfully | // getResultInfo will be called right before callServerEndpoint successfully | ||||
// resolves and includes additional information about the request | // resolves and includes additional information about the request | ||||
+getResultInfo: (resultInfo: CallServerEndpointResultInfo) => mixed, | +getResultInfo: (resultInfo: CallServerEndpointResultInfo) => mixed, | ||||
Show All 40 Lines | async function callServerEndpoint( | ||||
cookie: ?string, | cookie: ?string, | ||||
setNewSession: (sessionChange: ClientSessionChange, error: ?string) => void, | setNewSession: (sessionChange: ClientSessionChange, error: ?string) => void, | ||||
waitIfCookieInvalidated: () => Promise<?CallServerEndpoint>, | waitIfCookieInvalidated: () => Promise<?CallServerEndpoint>, | ||||
cookieInvalidationRecovery: ( | cookieInvalidationRecovery: ( | ||||
sessionChange: ClientSessionChange, | sessionChange: ClientSessionChange, | ||||
) => Promise<?CallServerEndpoint>, | ) => Promise<?CallServerEndpoint>, | ||||
urlPrefix: string, | urlPrefix: string, | ||||
sessionID: ?string, | sessionID: ?string, | ||||
connectionStatus: ConnectionStatus, | isSocketConnected: boolean, | ||||
lastCommunicatedPlatformDetails: ?PlatformDetails, | lastCommunicatedPlatformDetails: ?PlatformDetails, | ||||
socketAPIHandler: ?SocketAPIHandler, | socketAPIHandler: ?SocketAPIHandler, | ||||
endpoint: Endpoint, | endpoint: Endpoint, | ||||
input: { +[key: string]: mixed }, | input: { +[key: string]: mixed }, | ||||
dispatch: Dispatch, | dispatch: Dispatch, | ||||
options?: ?CallServerEndpointOptions, | options?: ?CallServerEndpointOptions, | ||||
loggedIn: boolean, | loggedIn: boolean, | ||||
keyserverID: string, | keyserverID: string, | ||||
): Promise<Object> { | ): Promise<Object> { | ||||
const possibleReplacement = await waitIfCookieInvalidated(); | const possibleReplacement = await waitIfCookieInvalidated(); | ||||
if (possibleReplacement) { | if (possibleReplacement) { | ||||
return await possibleReplacement(endpoint, input, options); | return await possibleReplacement(endpoint, input, options); | ||||
} | } | ||||
const shouldSendPlatformDetails = | const shouldSendPlatformDetails = | ||||
lastCommunicatedPlatformDetails && | lastCommunicatedPlatformDetails && | ||||
!_isEqual(lastCommunicatedPlatformDetails)(getConfig().platformDetails); | !_isEqual(lastCommunicatedPlatformDetails)(getConfig().platformDetails); | ||||
if ( | if ( | ||||
endpointIsSocketPreferred(endpoint) && | endpointIsSocketPreferred(endpoint) && | ||||
connectionStatus === 'connected' && | isSocketConnected && | ||||
socketAPIHandler && | socketAPIHandler && | ||||
!options?.urlPrefixOverride | !options?.urlPrefixOverride | ||||
) { | ) { | ||||
try { | try { | ||||
const result = await socketAPIHandler({ endpoint, input }); | const result = await socketAPIHandler({ endpoint, input }); | ||||
options?.getResultInfo?.({ interface: 'socket' }); | options?.getResultInfo?.({ interface: 'socket' }); | ||||
return result; | return result; | ||||
} catch (e) { | } catch (e) { | ||||
▲ Show 20 Lines • Show All 116 Lines • Show Last 20 Lines |