From fc4bf2d084bd417cc8072bf4fff21f4d3fd40d8b Mon Sep 17 00:00:00 2001 From: Angelika Serwa Date: Fri, 7 Jun 2024 09:10:24 +0200 Subject: [PATCH] [EX-563] Remove useWebRTC hook (#65) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The hook is no longer needed as it’s a remainder from membrane-webrtc-sdk --- example/ios/Podfile.lock | 2 +- src/__mocks__/index.tsx | 7 -- src/hooks/useWebRTC.ts | 157 --------------------------------------- src/index.tsx | 1 - 4 files changed, 1 insertion(+), 166 deletions(-) delete mode 100644 src/hooks/useWebRTC.ts diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 3919ebdf..c91952e7 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -1410,4 +1410,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 8daa796ed34dbeeeb2b648d198968b27b4c2ae8d -COCOAPODS: 1.13.0 +COCOAPODS: 1.14.3 diff --git a/src/__mocks__/index.tsx b/src/__mocks__/index.tsx index 4499884f..fed7a227 100644 --- a/src/__mocks__/index.tsx +++ b/src/__mocks__/index.tsx @@ -38,13 +38,6 @@ const emptyPromise = async (): Promise => { }); }; -export const useWebRTC = () => { - return { - connect: emptyPromise, - disconnect: emptyPromise, - error: null, - }; -}; export const useEndpoints = () => []; export const setTargetTrackEncoding = emptyPromise; export const useCamera = () => { diff --git a/src/hooks/useWebRTC.ts b/src/hooks/useWebRTC.ts deleted file mode 100644 index 32ab73cf..00000000 --- a/src/hooks/useWebRTC.ts +++ /dev/null @@ -1,157 +0,0 @@ -import { Channel, Socket, MessageRef } from 'phoenix'; -import { useCallback, useEffect, useState, useRef } from 'react'; - -import { ConnectionOptions, Metadata } from '../MembraneWebRTC.types'; -import MembraneWebRTCModule from '../MembraneWebRTCModule'; -import { ReceivableEvents, eventEmitter } from '../common/eventEmitter'; - -/** - * The hook used to manage a connection with membrane server. - * @returns An object with functions to manage membrane server connection and `error` if connection failed. - */ -export function useWebRTC() { - const [error, setError] = useState(null); - // prevent user from calling connect methods multiple times - const lock = useRef(false); - const socket = useRef(null); - const webrtcChannel = useRef(null); - const onSocketError = useRef(null); - const onSocketClose = useRef(null); - - useEffect(() => { - const eventListener = eventEmitter.addListener( - ReceivableEvents.SendMediaEvent, - sendMediaEvent, - ); - return () => eventListener.remove(); - }, []); - - const sendMediaEvent = ({ event }: { event: string }) => { - if (webrtcChannel.current) { - webrtcChannel.current.push('mediaEvent', { data: event }); - } - }; - - const withLock = - (f: any) => - async (...args: any) => { - if (lock.current) return Promise.resolve(); - lock.current = true; - try { - await f(...args); - } catch (e) { - throw e; - } finally { - lock.current = false; - } - }; - - /** - * Connects to a server. - * @returns A promise that resolves on success or rejects in case of an error. - */ - const connect: ( - /** - * server url - */ - url: string, - roomName: string, - connectionOptions?: Partial< - ConnectionOptions - >, - ) => Promise = useCallback( - withLock( - async ( - url: string, - roomName: string, - connectionOptions: Partial< - ConnectionOptions - > = {}, - ) => { - setError(null); - const _socket = new Socket(url, { - params: connectionOptions.connectionParams, - }); - _socket.connect(); - - onSocketClose.current = _socket.onClose(cleanUp); - onSocketError.current = _socket.onError(() => { - setError(`Socket error occured.`); - cleanUp(); - }); - - const _webrtcChannel = _socket.channel( - `room:${roomName}`, - connectionOptions.socketChannelParams, - ); - - _webrtcChannel.on('mediaEvent', (event) => { - MembraneWebRTCModule.receiveMediaEvent(event.data); - }); - - _webrtcChannel.on('error', (error) => { - console.error(error); - setError( - `Received error report from the server: ${error.message ?? ''}`, - ); - cleanUp(); - }); - - _webrtcChannel.onError((reason) => { - console.error(reason); - setError(`Webrtc channel error occurred: ${reason}.`); - cleanUp(); - }); - - socket.current = _socket; - webrtcChannel.current = _webrtcChannel; - - await MembraneWebRTCModule.create(); - - await new Promise((resolve, reject) => { - _webrtcChannel - .join() - .receive('ok', () => { - resolve(); - }) - .receive('error', (_response) => { - console.error(_response); - reject(_response); - }); - }); - - await MembraneWebRTCModule.connect( - connectionOptions.endpointMetadata || {}, - ); - }, - ), - [], - ); - - /** - * Call this to gracefully disconnect from the server. After that you can connect again. - * @returns A promise that resolves on success or rejects in case of an error. - */ - const disconnect: () => Promise = useCallback( - withLock((): Promise => { - setError(null); - return cleanUp(); - }), - [], - ); - - const cleanUp = (): Promise => { - webrtcChannel.current?.leave(); - const refs: MessageRef[] = []; - if (onSocketClose.current) refs.push(onSocketClose.current); - if (onSocketError.current) refs.push(onSocketError.current); - socket.current?.off(refs); - return MembraneWebRTCModule.disconnect(); - }; - - return { - connect, - disconnect, - error, - }; -} diff --git a/src/index.tsx b/src/index.tsx index 6560dc9d..55f13e13 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -9,7 +9,6 @@ export { useEndpoints } from './hooks/useEndpoints'; export { useMicrophone } from './hooks/useMicrophone'; export { useRTCStatistics } from './hooks/useRTCStatistics'; export { useScreencast } from './hooks/useScreencast'; -export { useWebRTC } from './hooks/useWebRTC'; export { updateAudioTrackMetadata,