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:
2025-12-12 23:06:22 +01:00
parent 9262043e97
commit d6a9876440

View File

@@ -354,6 +354,29 @@ export class Rondevu {
this.usernameClaimed = true 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 * Create a single offer and publish it to the server
*/ */
@@ -398,19 +421,7 @@ export class Rondevu {
this.debug(`Offer created: ${offerId}`) this.debug(`Offer created: ${offerId}`)
// Set up ICE candidate handler // Set up ICE candidate handler
pc.onicecandidate = async (event) => { this.setupIceCandidateHandler(pc, serviceFqn, offerId)
if (event.candidate) {
try {
await this.api.addOfferIceCandidates(
serviceFqn,
offerId,
[event.candidate.toJSON()]
)
} catch (err) {
console.error('[Rondevu] Failed to send ICE candidate:', err)
}
}
}
// Monitor connection state // Monitor connection state
pc.onconnectionstatechange = () => { pc.onconnectionstatechange = () => {
@@ -617,19 +628,7 @@ export class Rondevu {
}) })
// 4. Set up ICE candidate exchange // 4. Set up ICE candidate exchange
pc.onicecandidate = async (event) => { this.setupIceCandidateHandler(pc, serviceData.serviceFqn, serviceData.offerId)
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)
}
}
}
// 5. Poll for remote ICE candidates // 5. Poll for remote ICE candidates
let lastIceTimestamp = 0 let lastIceTimestamp = 0