From d6a987644042711488939fdc49cddfc097930114 Mon Sep 17 00:00:00 2001 From: Bas van den Aakster Date: Fri, 12 Dec 2025 23:06:22 +0100 Subject: [PATCH] Extract duplicate ICE candidate handling code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/rondevu.ts | 51 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/src/rondevu.ts b/src/rondevu.ts index a16bd57..606b7c4 100644 --- a/src/rondevu.ts +++ b/src/rondevu.ts @@ -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