Skip to content

Commit

Permalink
reafactor(#141): reuse room ref
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyckahn committed Dec 2, 2024
1 parent e43b465 commit 7f72663
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
16 changes: 11 additions & 5 deletions src/components/Room/useRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ export function useRoom(
) {
const isPrivate = password !== undefined

const [peerRoom] = useState(
() => new PeerRoom({ password: password ?? roomId, ...roomConfig }, roomId)
)

const {
peerList,
setPeerList,
Expand All @@ -79,8 +75,17 @@ export function useRoom(
setPassword,
customUsername,
updatePeer,
peerRoomRef,
} = useContext(ShellContext)

const [peerRoom] = useState(
() =>
peerRoomRef.current ??
new PeerRoom({ password: password ?? roomId, ...roomConfig }, roomId)
)

peerRoomRef.current = peerRoom

const settingsContext = useContext(SettingsContext)
const { showActiveTypingStatus } = settingsContext.getUserSettings()
const [isMessageSending, setIsMessageSending] = useState(false)
Expand Down Expand Up @@ -188,9 +193,10 @@ export function useRoom(
return () => {
sendTypingStatusChange({ isTyping: false })
peerRoom.leaveRoom()
peerRoomRef.current = null
setPeerList([])
}
}, [peerRoom, setPeerList, sendTypingStatusChange])
}, [peerRoom, setPeerList, sendTypingStatusChange, peerRoomRef])

useEffect(() => {
setPassword(password)
Expand Down
6 changes: 5 additions & 1 deletion src/components/Shell/Shell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useContext,
useEffect,
useMemo,
useRef,
useState,
} from 'react'
import CssBaseline from '@mui/material/CssBaseline'
Expand All @@ -29,7 +30,7 @@ import {
AudioChannelName,
} from 'models/chat'
import { ErrorBoundary } from 'components/ErrorBoundary'
import { PeerConnectionType } from 'lib/PeerRoom'
import { PeerConnectionType, PeerRoom } from 'lib/PeerRoom'

import { Drawer } from './Drawer'
import { UpgradeDialog } from './UpgradeDialog'
Expand Down Expand Up @@ -63,6 +64,7 @@ export const Shell = ({ appNeedsUpdate, children, userPeerId }: ShellProps) => {
const [windowWidth] = useWindowSize()
const defaultSidebarsOpen = windowWidth >= theme.breakpoints.values.lg

const peerRoomRef = useRef<PeerRoom>(null)
const [isAlertShowing, setIsAlertShowing] = useState(false)
const [isDrawerOpen, setIsDrawerOpen] = useState(defaultSidebarsOpen)
const [isQRCodeDialogOpen, setIsQRCodeDialogOpen] = useState(false)
Expand Down Expand Up @@ -159,6 +161,7 @@ export const Shell = ({ appNeedsUpdate, children, userPeerId }: ShellProps) => {
setCustomUsername,
connectionTestResults,
updatePeer,
peerRoomRef,
}),
[
isEmbedded,
Expand Down Expand Up @@ -189,6 +192,7 @@ export const Shell = ({ appNeedsUpdate, children, userPeerId }: ShellProps) => {
setCustomUsername,
connectionTestResults,
updatePeer,
peerRoomRef,
]
)

Expand Down
25 changes: 16 additions & 9 deletions src/contexts/ShellContext.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { createContext, Dispatch, SetStateAction } from 'react'
import {
createContext,
Dispatch,
MutableRefObject,
SetStateAction,
} from 'react'

import { AlertOptions } from 'models/shell'
import { ConnectionTestResults } from 'components/Shell/useConnectionTest'
import { TrackerConnection } from 'lib/ConnectionTest'
import { PeerConnectionType, PeerRoom } from 'lib/PeerRoom'
import {
AudioChannel,
AudioChannelName,
AudioState,
ScreenShareState,
VideoState,
Peer,
AudioChannel,
PeerAudioChannelState,
AudioChannelName,
ScreenShareState,
VideoState,
} from 'models/chat'
import { PeerConnectionType } from 'lib/PeerRoom'
import { ConnectionTestResults } from 'components/Shell/useConnectionTest'
import { TrackerConnection } from 'lib/ConnectionTest'
import { AlertOptions } from 'models/shell'

interface ShellContextProps {
isEmbedded: boolean
Expand Down Expand Up @@ -47,6 +52,7 @@ interface ShellContextProps {
setCustomUsername: Dispatch<SetStateAction<string>>
connectionTestResults: ConnectionTestResults
updatePeer: (peerId: string, updatedProperties: Partial<Peer>) => void
peerRoomRef: MutableRefObject<PeerRoom | null>
}

export const ShellContext = createContext<ShellContextProps>({
Expand Down Expand Up @@ -87,4 +93,5 @@ export const ShellContext = createContext<ShellContextProps>({
trackerConnection: TrackerConnection.SEARCHING,
},
updatePeer: () => {},
peerRoomRef: { current: null },
})

0 comments on commit 7f72663

Please sign in to comment.