Replace .on* event handlers with addEventListener/removeEventListener

Updated all event handler assignments to use addEventListener instead of .on* properties:
- peer/index.ts: Replaced onconnectionstatechange, ondatachannel, ontrack, onicecandidateerror
- creating-offer-state.ts: Replaced onicecandidate
- answering-state.ts: Replaced onicecandidate

Benefits:
- Proper cleanup with removeEventListener
- Prevents memory leaks by removing listeners when states/peer close
- Allows multiple listeners for the same event
- More modern and explicit event handling approach

All event handlers are now stored as class properties and properly cleaned up in cleanup()/close() methods.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-16 17:33:32 +01:00
parent 6c344ec8e1
commit 00f4da7250
3 changed files with 48 additions and 6 deletions

View File

@@ -6,6 +6,8 @@ import type RondevuPeer from './index.js';
* Answering an offer and sending to server
*/
export class AnsweringState extends PeerState {
private iceCandidateHandler?: (event: RTCPeerConnectionIceEvent) => void;
constructor(peer: RondevuPeer) {
super(peer);
}
@@ -31,7 +33,7 @@ export class AnsweringState extends PeerState {
await this.peer.offersApi.answer(offerId, answer.sdp!);
// Enable trickle ICE - send candidates as they arrive
this.peer.pc.onicecandidate = async (event: RTCPeerConnectionIceEvent) => {
this.iceCandidateHandler = async (event: RTCPeerConnectionIceEvent) => {
if (event.candidate && offerId) {
const candidateData = event.candidate.toJSON();
if (candidateData.candidate && candidateData.candidate !== '') {
@@ -43,6 +45,7 @@ export class AnsweringState extends PeerState {
}
}
};
this.peer.pc.addEventListener('icecandidate', this.iceCandidateHandler);
// Transition to exchanging ICE
const { ExchangingIceState } = await import('./exchanging-ice-state.js');
@@ -53,4 +56,10 @@ export class AnsweringState extends PeerState {
throw error;
}
}
cleanup(): void {
if (this.iceCandidateHandler) {
this.peer.pc.removeEventListener('icecandidate', this.iceCandidateHandler);
}
}
}