mirror of
https://github.com/xtr-dev/rondevu-client.git
synced 2025-12-13 04:13:25 +00:00
Extract duplicate ICE candidate handling code
Refactor ICE candidate handler setup: - Create setupIceCandidateHandler() private method - Remove duplicate code from createOffer() and connectToService() - Both methods now call the shared handler setup Benefits: - DRY principle: 13 lines of duplicate code eliminated - Easier to maintain: changes to ICE handling logic only needed in one place - Consistent error handling across both code paths 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -354,6 +354,29 @@ export class Rondevu {
|
||||
this.usernameClaimed = true
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up ICE candidate handler to send candidates to the server
|
||||
*/
|
||||
private setupIceCandidateHandler(
|
||||
pc: RTCPeerConnection,
|
||||
serviceFqn: string,
|
||||
offerId: string
|
||||
): void {
|
||||
pc.onicecandidate = async (event) => {
|
||||
if (event.candidate) {
|
||||
try {
|
||||
await this.api.addOfferIceCandidates(
|
||||
serviceFqn,
|
||||
offerId,
|
||||
[event.candidate.toJSON()]
|
||||
)
|
||||
} catch (err) {
|
||||
console.error('[Rondevu] Failed to send ICE candidate:', err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a single offer and publish it to the server
|
||||
*/
|
||||
@@ -398,19 +421,7 @@ export class Rondevu {
|
||||
this.debug(`Offer created: ${offerId}`)
|
||||
|
||||
// Set up ICE candidate handler
|
||||
pc.onicecandidate = async (event) => {
|
||||
if (event.candidate) {
|
||||
try {
|
||||
await this.api.addOfferIceCandidates(
|
||||
serviceFqn,
|
||||
offerId,
|
||||
[event.candidate.toJSON()]
|
||||
)
|
||||
} catch (err) {
|
||||
console.error('[Rondevu] Failed to send ICE candidate:', err)
|
||||
}
|
||||
}
|
||||
}
|
||||
this.setupIceCandidateHandler(pc, serviceFqn, offerId)
|
||||
|
||||
// Monitor connection state
|
||||
pc.onconnectionstatechange = () => {
|
||||
@@ -617,19 +628,7 @@ export class Rondevu {
|
||||
})
|
||||
|
||||
// 4. Set up ICE candidate exchange
|
||||
pc.onicecandidate = async (event) => {
|
||||
if (event.candidate) {
|
||||
try {
|
||||
await this.api.addOfferIceCandidates(
|
||||
serviceData.serviceFqn,
|
||||
serviceData.offerId,
|
||||
[event.candidate.toJSON()]
|
||||
)
|
||||
} catch (err) {
|
||||
console.error('[Rondevu] Failed to send ICE candidate:', err)
|
||||
}
|
||||
}
|
||||
}
|
||||
this.setupIceCandidateHandler(pc, serviceData.serviceFqn, serviceData.offerId)
|
||||
|
||||
// 5. Poll for remote ICE candidates
|
||||
let lastIceTimestamp = 0
|
||||
|
||||
Reference in New Issue
Block a user