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 5d6ccc2
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 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,26 +560,30 @@ 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)
const timeout = setTimeout(() => {
peerConnection.removeEventListener(
'connectionstatechange',
connectionStateChangeHandler
)
rej()
}, 5000)
const connectionStateChangeHandler = () => {
if (peerConnection.connectionState === 'connected') {
peerConnection.removeEventListener(
'connectionstatechange',
connectionStateChangeHandler
)
clearTimeout(timeout)
res(undefined)
}
}
peerConnection.addEventListener(
'connectionstatechange',
connectionStateChangeHandler,
{
once: true,
}
connectionStateChangeHandler
)
})

Expand Down

0 comments on commit 5d6ccc2

Please sign in to comment.