mirror of
https://github.com/xtr-dev/rondevu-client.git
synced 2025-12-10 10:53:24 +00:00
Extract duplicate ICE candidate handler code to base PeerState class
Refactored common ICE candidate handling logic to reduce code duplication: - Added setupIceCandidateHandler() method to base PeerState class - Moved iceCandidateHandler property to base class - Updated cleanup() in base class to remove ICE candidate handler - Removed duplicate handler code from CreatingOfferState and AnsweringState - Both states now call this.setupIceCandidateHandler(offerId) This eliminates ~15 lines of duplicated code per state and ensures consistent ICE candidate handling across all states that need it. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,8 @@ import type RondevuPeer from './index.js';
|
||||
* Implements the State pattern for managing WebRTC connection lifecycle
|
||||
*/
|
||||
export abstract class PeerState {
|
||||
protected iceCandidateHandler?: (event: RTCPeerConnectionIceEvent) => void;
|
||||
|
||||
constructor(protected peer: RondevuPeer) {}
|
||||
|
||||
abstract get name(): string;
|
||||
@@ -29,8 +31,31 @@ export abstract class PeerState {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup trickle ICE candidate handler
|
||||
* Sends local ICE candidates to server as they are discovered
|
||||
*/
|
||||
protected setupIceCandidateHandler(offerId: string): void {
|
||||
this.iceCandidateHandler = async (event: RTCPeerConnectionIceEvent) => {
|
||||
if (event.candidate && offerId) {
|
||||
const candidateData = event.candidate.toJSON();
|
||||
if (candidateData.candidate && candidateData.candidate !== '') {
|
||||
try {
|
||||
await this.peer.offersApi.addIceCandidates(offerId, [candidateData]);
|
||||
} catch (err) {
|
||||
console.error('Error sending ICE candidate:', err);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
this.peer.pc.addEventListener('icecandidate', this.iceCandidateHandler);
|
||||
}
|
||||
|
||||
cleanup(): void {
|
||||
// Override in states that need cleanup
|
||||
// Clean up ICE candidate handler if it exists
|
||||
if (this.iceCandidateHandler) {
|
||||
this.peer.pc.removeEventListener('icecandidate', this.iceCandidateHandler);
|
||||
}
|
||||
}
|
||||
|
||||
async close(): Promise<void> {
|
||||
|
||||
Reference in New Issue
Block a user