Skip to content

Commit

Permalink
delete underlying object on close
Browse files Browse the repository at this point in the history
  • Loading branch information
murat-dogan committed Mar 14, 2024
1 parent d866015 commit ac251b9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
11 changes: 6 additions & 5 deletions polyfill/RTCDataChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default class _RTCDataChannel extends EventTarget {
}

get bufferedAmount() {
return this.#dataChannel.bufferedAmount();
return this.#dataChannel?.bufferedAmount() || 0;
}

get bufferedAmountLowThreshold() {
Expand All @@ -111,15 +111,15 @@ export default class _RTCDataChannel extends EventTarget {
set bufferedAmountLowThreshold(value) {
const number = Number(value) || 0;
this.#bufferedAmountLowThreshold = number;
this.#dataChannel.setBufferedAmountLowThreshold(number);
this.#dataChannel?.setBufferedAmountLowThreshold(number);
}

get id() {
return this.#dataChannel.getId();
return this.#dataChannel?.getId() || null;
}

get label() {
return this.#dataChannel.getLabel();
return this.#dataChannel?.getLabel() || null;
}

get maxPacketLifeTime() {
Expand All @@ -139,7 +139,7 @@ export default class _RTCDataChannel extends EventTarget {
}

get protocol() {
return this.#dataChannel.getProtocol();
return this.#dataChannel?.getProtocol() || '';
}

get readyState() {
Expand Down Expand Up @@ -171,5 +171,6 @@ export default class _RTCDataChannel extends EventTarget {
this.dispatchEvent(new Event('closing'));

this.#dataChannel.close();
this.#dataChannel = null;
}
}
48 changes: 31 additions & 17 deletions polyfill/RTCPeerConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ export default class _RTCPeerConnection extends EventTarget {
pc: this,
extraFunctions: {
maxDataChannelId: () => {
return this.#peerConnection.maxDataChannelId();
return this.#peerConnection?.maxDataChannelId() ?? 65535;
},
maxMessageSize: () => {
return this.#peerConnection.maxMessageSize();
return this.#peerConnection?.maxMessageSize() ?? 65535;
},
localCandidates: () => {
return this.#localCandidates;
Expand All @@ -138,7 +138,7 @@ export default class _RTCPeerConnection extends EventTarget {
return this.#remoteCandidates;
},
selectedCandidatePair: () => {
return this.#peerConnection.getSelectedCandidatePair();
return this.#peerConnection?.getSelectedCandidatePair() ?? {};
},
},
});
Expand All @@ -149,47 +149,47 @@ export default class _RTCPeerConnection extends EventTarget {
}

get connectionState() {
return this.#peerConnection.state();
return this.#peerConnection?.state() ?? 'closed';
}

get iceConnectionState() {
return this.#peerConnection.iceState();
return this.#peerConnection?.iceState() ?? 'closed';
}

get iceGatheringState() {
return this.#peerConnection.gatheringState();
return this.#peerConnection?.gatheringState() ?? 'new';
}

get currentLocalDescription() {
return new RTCSessionDescription(this.#peerConnection.localDescription());
return new RTCSessionDescription(this.#peerConnection?.localDescription() ?? {});
}

get currentRemoteDescription() {
return new RTCSessionDescription(this.#peerConnection.remoteDescription());
return new RTCSessionDescription(this.#peerConnection?.remoteDescription() ?? {});
}

get localDescription() {
return new RTCSessionDescription(this.#peerConnection.localDescription());
return new RTCSessionDescription(this.#peerConnection?.localDescription() ?? {});
}

get pendingLocalDescription() {
return new RTCSessionDescription(this.#peerConnection.localDescription());
return new RTCSessionDescription(this.#peerConnection?.localDescription() ?? {});
}

get pendingRemoteDescription() {
return new RTCSessionDescription(this.#peerConnection.remoteDescription());
return new RTCSessionDescription(this.#peerConnection?.remoteDescription() ?? {});
}

get remoteDescription() {
return new RTCSessionDescription(this.#peerConnection.remoteDescription());
return new RTCSessionDescription(this.#peerConnection?.remoteDescription() ?? {});
}

get sctp() {
return this.#sctp;
}

get signalingState() {
return this.#peerConnection.signalingState();
return this.#peerConnection?.signalingState() ?? 'closed';
}

static generateCertificate(keygenAlgorithm) {
Expand All @@ -201,6 +201,10 @@ export default class _RTCPeerConnection extends EventTarget {
throw new DOMException('Candidate invalid');
}

if (!this.#peerConnection) {
throw new DOMException('Peer connection is closed');
}

this.#remoteCandidates.push(
new RTCIceCandidate({ candidate: candidate.candidate, sdpMid: candidate.sdpMid || '0' }),
);
Expand All @@ -219,17 +223,23 @@ export default class _RTCPeerConnection extends EventTarget {
// close all channels before shutting down
this.#dataChannels.forEach((channel) => {
channel.close();
channel = null;
});

this.#peerConnection.close();
this.#peerConnection?.close();
this.#peerConnection = null;
}

createAnswer() {
return this.#localAnswer;
}

createDataChannel(label, opts = {}) {
const channel = this.#peerConnection.createDataChannel(label, opts);
if (!this.#peerConnection) {
throw new DOMException('Peer connection is closed');
}

const channel = this.#peerConnection?.createDataChannel(label, opts);
const dataChannel = new RTCDataChannel(channel, opts);

// ensure we can close all channels when shutting down
Expand Down Expand Up @@ -260,6 +270,10 @@ export default class _RTCPeerConnection extends EventTarget {
getStats() {
return new Promise((resolve) => {
let report = new Map();
if (!this.#peerConnection) {
return resolve(report);
}

let cp = this.#peerConnection.getSelectedCandidatePair();
let bytesSent = this.#peerConnection.bytesSent();
let bytesReceived = this.#peerConnection.bytesReceived();
Expand Down Expand Up @@ -344,15 +358,15 @@ export default class _RTCPeerConnection extends EventTarget {
// any other type causes libdatachannel to throw
return;
}
this.#peerConnection.setLocalDescription(description.type);
this.#peerConnection?.setLocalDescription(description.type);
}

async setRemoteDescription(description) {
if (description.sdp == null) {
throw new DOMException('Remote SDP must be set');
}

this.#peerConnection.setRemoteDescription(description.sdp, description.type);
this.#peerConnection?.setRemoteDescription(description.sdp, description.type);
}
}

Expand Down

0 comments on commit ac251b9

Please sign in to comment.