fix: clean up unused audio objects

This commit is contained in:
Jeremy Kahn 2022-11-01 09:54:22 -05:00
parent 9c18f40d61
commit 0a05d197cb

View File

@ -45,6 +45,10 @@ export function useRoomAudio({ peerRoom }: UseRoomAudioConfig) {
if (peer.peerId === peerId) { if (peer.peerId === peerId) {
newPeer.audioState = audioState newPeer.audioState = audioState
if (audioState === AudioState.STOPPED) {
deletePeerAudio(peerId)
}
} }
return newPeer return newPeer
@ -125,6 +129,12 @@ export function useRoomAudio({ peerRoom }: UseRoomAudioConfig) {
setAudioStream(newSelfStream) setAudioStream(newSelfStream)
} }
const deletePeerAudio = (peerId: string) => {
const newPeerAudios = { ...peerAudios }
delete newPeerAudios[peerId]
setPeerAudios(newPeerAudios)
}
const handleAudioForNewPeer = (peerId: string) => { const handleAudioForNewPeer = (peerId: string) => {
if (audioStream) { if (audioStream) {
peerRoom.addStream(audioStream, peerId) peerRoom.addStream(audioStream, peerId)
@ -134,6 +144,7 @@ export function useRoomAudio({ peerRoom }: UseRoomAudioConfig) {
const handleAudioForLeavingPeer = (peerId: string) => { const handleAudioForLeavingPeer = (peerId: string) => {
if (audioStream) { if (audioStream) {
peerRoom.removeStream(audioStream, peerId) peerRoom.removeStream(audioStream, peerId)
deletePeerAudio(peerId)
} }
} }