mirror of
https://github.com/xtr-dev/rondevu-client.git
synced 2025-12-10 02:43:25 +00:00
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>
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import { PeerState } from './state.js';
|
|
import type { PeerOptions } from './types.js';
|
|
import type RondevuPeer from './index.js';
|
|
|
|
/**
|
|
* Answering an offer and sending to server
|
|
*/
|
|
export class AnsweringState extends PeerState {
|
|
constructor(peer: RondevuPeer) {
|
|
super(peer);
|
|
}
|
|
|
|
get name() { return 'answering'; }
|
|
|
|
async answer(offerId: string, offerSdp: string, options: PeerOptions): Promise<void> {
|
|
try {
|
|
this.peer.role = 'answerer';
|
|
this.peer.offerId = offerId;
|
|
|
|
// Set remote description
|
|
await this.peer.pc.setRemoteDescription({
|
|
type: 'offer',
|
|
sdp: offerSdp
|
|
});
|
|
|
|
// Create answer
|
|
const answer = await this.peer.pc.createAnswer();
|
|
await this.peer.pc.setLocalDescription(answer);
|
|
|
|
// Send answer to server immediately (don't wait for ICE)
|
|
await this.peer.offersApi.answer(offerId, answer.sdp!);
|
|
|
|
// Enable trickle ICE - send candidates as they arrive
|
|
this.setupIceCandidateHandler(offerId);
|
|
|
|
// Transition to exchanging ICE
|
|
const { ExchangingIceState } = await import('./exchanging-ice-state.js');
|
|
this.peer.setState(new ExchangingIceState(this.peer, offerId, options));
|
|
} catch (error) {
|
|
const { FailedState } = await import('./failed-state.js');
|
|
this.peer.setState(new FailedState(this.peer, error as Error));
|
|
throw error;
|
|
}
|
|
}
|
|
}
|