Skip to content

Commit

Permalink
Fix cleanup logic when waiting for peer connection to be connected
Browse files Browse the repository at this point in the history
  • Loading branch information
third774 committed Aug 14, 2024
1 parent b00b65c commit c01a9f5
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions app/utils/rxjs/RxjsPeer.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export class RxjsPeer {
await peerConnection.setRemoteDescription(
new RTCSessionDescription(response.sessionDescription)
)
await connected(peerConnection)
await peerConnectionIsConnected(peerConnection)
}

return {
Expand Down Expand Up @@ -425,7 +425,7 @@ export class RxjsPeer {
if (renegotiationResponse.errorCode) {
throw new Error(renegotiationResponse.errorDescription)
} else {
await connected(peerConnection)
await peerConnectionIsConnected(peerConnection)
}
}

Expand Down Expand Up @@ -560,11 +560,17 @@ async function resolveTrack(
})
}

async function connected(peerConnection: RTCPeerConnection) {
async function peerConnectionIsConnected(peerConnection: RTCPeerConnection) {
if (peerConnection.connectionState !== 'connected') {
const connected = new Promise((res, rej) => {
// timeout after 5s
setTimeout(rej, 5000)
setTimeout(() => {
peerConnection.removeEventListener(
'connectionstatechange',
connectionStateChangeHandler
)
rej()
}, 5000)
const connectionStateChangeHandler = () => {
if (peerConnection.connectionState === 'connected') {
peerConnection.removeEventListener(
Expand All @@ -576,10 +582,7 @@ async function connected(peerConnection: RTCPeerConnection) {
}
peerConnection.addEventListener(
'connectionstatechange',
connectionStateChangeHandler,
{
once: true,
}
connectionStateChangeHandler
)
})

Expand Down

0 comments on commit c01a9f5

Please sign in to comment.