diff --git a/lib/shared/dm-ops/process-dm-ops.js b/lib/shared/dm-ops/process-dm-ops.js --- a/lib/shared/dm-ops/process-dm-ops.js +++ b/lib/shared/dm-ops/process-dm-ops.js @@ -18,7 +18,6 @@ import { useDispatchWithMessageSource } from '../../hooks/ops-hooks.js'; import { mergeUpdatesWithMessageInfos } from '../../reducers/message-reducer.js'; import { getAllPeerUserIDAndDeviceIDs } from '../../selectors/user-selectors.js'; -import { usePeerToPeerCommunication } from '../../tunnelbroker/peer-to-peer-context.js'; import { processDMOpsActionType, queueDMOpsActionType, @@ -189,14 +188,12 @@ dmOperationSpecification: OutboundDMOperationSpecification, ) => Promise { const processDMOps = useProcessDMOperation(); - const { sendDMOperation } = usePeerToPeerCommunication(); return React.useCallback( async (dmOperationSpecification: OutboundDMOperationSpecification) => { await processDMOps(dmOperationSpecification); - await sendDMOperation(dmOperationSpecification); }, - [processDMOps, sendDMOperation], + [processDMOps], ); } diff --git a/lib/tunnelbroker/peer-to-peer-context.js b/lib/tunnelbroker/peer-to-peer-context.js --- a/lib/tunnelbroker/peer-to-peer-context.js +++ b/lib/tunnelbroker/peer-to-peer-context.js @@ -9,17 +9,11 @@ useTunnelbroker, } from './tunnelbroker-context.js'; import { usePeerOlmSessionsCreatorContext } from '../components/peer-olm-session-creator-provider.react.js'; -import { getAllPeerUserIDAndDeviceIDs } from '../selectors/user-selectors.js'; -import { - createMessagesToPeersFromDMOp, - type OutboundDMOperationSpecification, -} from '../shared/dm-ops/dm-op-utils.js'; import { type AuthMetadata, IdentityClientContext, type IdentityClientContextType, } from '../shared/identity-client-context.js'; -import { scheduleP2PMessagesActionType } from '../types/dm-ops.js'; import { type OutboundP2PMessage, outboundP2PMessageStatuses, @@ -31,16 +25,16 @@ import { getConfig } from '../utils/config.js'; import { getMessageForException } from '../utils/errors.js'; import { olmSessionErrors } from '../utils/olm-utils.js'; -import { useDispatch, useSelector } from '../utils/redux-utils.js'; type PeerToPeerContextType = { +processOutboundMessages: ( outboundMessageIDs: ?$ReadOnlyArray, dmOpID: ?string, ) => void, - +sendDMOperation: ( - op: OutboundDMOperationSpecification, - ) => Promise<$ReadOnlyArray>, + +getDMOpsSendingPromise: () => { + +promise: Promise<$ReadOnlyArray>, + +dmOpID: string, + }, +broadcastEphemeralMessage: ( contentPayload: string, recipients: $ReadOnlyArray<{ +userID: string, +deviceID: string }>, @@ -190,43 +184,24 @@ const identityContext = React.useContext(IdentityClientContext); invariant(identityContext, 'Identity context should be set'); - const dispatch = useDispatch(); const dmOpsSendingPromiseResolvers = React.useRef< Map< string, { - +resolve: ($ReadOnlyArray) => mixed, + +resolve: (messageIDs: $ReadOnlyArray) => mixed, +reject: Error => mixed, }, >, >(new Map()); - const allPeerUserIDAndDeviceIDs = useSelector(getAllPeerUserIDAndDeviceIDs); - const currentUserInfo = useSelector(state => state.currentUserInfo); - - const sendDMOperation = React.useCallback( - async (op: OutboundDMOperationSpecification) => { - const dmOpID = uuid.v4(); - const promise = new Promise<$ReadOnlyArray>((resolve, reject) => { - dmOpsSendingPromiseResolvers.current.set(dmOpID, { resolve, reject }); - }); - const messages = await createMessagesToPeersFromDMOp( - op, - allPeerUserIDAndDeviceIDs, - currentUserInfo, - ); - dispatch({ - type: scheduleP2PMessagesActionType, - payload: { - dmOpID, - messages, - }, - }); + const getDMOpsSendingPromise = React.useCallback(() => { + const dmOpID = uuid.v4(); + const promise = new Promise<$ReadOnlyArray>((resolve, reject) => { + dmOpsSendingPromiseResolvers.current.set(dmOpID, { resolve, reject }); + }); - return promise; - }, - [allPeerUserIDAndDeviceIDs, currentUserInfo, dispatch], - ); + return { promise, dmOpID }; + }, []); const processingQueue = React.useRef< Array<{ @@ -364,10 +339,14 @@ const value: PeerToPeerContextType = React.useMemo( () => ({ processOutboundMessages, - sendDMOperation, + getDMOpsSendingPromise, broadcastEphemeralMessage, }), - [broadcastEphemeralMessage, processOutboundMessages, sendDMOperation], + [ + broadcastEphemeralMessage, + processOutboundMessages, + getDMOpsSendingPromise, + ], ); return (