Skip to content

fix: memoize RTCDataChannel fields #347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/polyfill/RTCDataChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export default class RTCDataChannel extends EventTarget implements globalThis.RT
#maxRetransmits: number | null;
#negotiated: boolean;
#ordered: boolean;
#id: number
#label: string
#protocol: string

#closeRequested = false;

Expand All @@ -33,6 +36,9 @@ export default class RTCDataChannel extends EventTarget implements globalThis.RT
this.#maxRetransmits = opts.maxRetransmits || null;
this.#negotiated = opts.negotiated || false;
this.#ordered = opts.ordered || true;
this.#id = this.#dataChannel.getId();
this.#label = this.#dataChannel.getLabel();
this.#protocol = this.#dataChannel.getProtocol();

// forward dataChannel events
this.#dataChannel.onOpen(() => {
Expand Down Expand Up @@ -131,11 +137,11 @@ export default class RTCDataChannel extends EventTarget implements globalThis.RT
}

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

get label(): string {
return this.#dataChannel.getLabel();
return this.#label;
}

get maxPacketLifeTime(): number | null {
Expand All @@ -155,7 +161,7 @@ export default class RTCDataChannel extends EventTarget implements globalThis.RT
}

get protocol(): string {
return this.#dataChannel.getProtocol();
return this.#protocol
}

get readyState(): globalThis.RTCDataChannelState {
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/event-promise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface EventPromiseOptions {
errorEvent?: string
}

export async function eventPromise <T = unknown> (emitter: EventTarget, event: string, opts?: EventPromiseOptions): Promise<T> {
return new Promise<T>((resolve, reject) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
emitter.addEventListener(event, (evt: any) => {
resolve(evt);
});
emitter.addEventListener(opts?.errorEvent ?? 'error', (err) => {
reject(err);
});
});
}
46 changes: 46 additions & 0 deletions test/jest-tests/polyfill.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { expect, jest } from '@jest/globals';
import { RTCPeerConnection } from '../../src/polyfill/index';
import { PeerConnection } from '../../src/lib/index';
import { eventPromise } from '../fixtures/event-promise';

describe('polyfill', () => {
// Default is 5000 ms but we need more
Expand Down Expand Up @@ -226,6 +227,51 @@ describe('polyfill', () => {
});
});

test('it can access datachannel informational fields after closing', async () => {
const peer1 = new RTCPeerConnection();
const peer2 = new RTCPeerConnection();

const label = 'label'
const protocol = 'protocol'

const dc: RTCDataChannel = peer1.createDataChannel(label, {
protocol
});

// Actions
const peer1Offer = await peer1.createOffer();
await peer2.setRemoteDescription(peer1Offer);

const peer2Answer = await peer2.createAnswer();
await peer1.setRemoteDescription(peer2Answer);

peer1.addEventListener('icecandidate', (e: RTCPeerConnectionIceEvent) => {
peer2.addIceCandidate(e.candidate);
});

peer2.addEventListener('icecandidate', (e: RTCPeerConnectionIceEvent) => {
peer1.addIceCandidate(e.candidate);
});

await eventPromise(dc, 'open');

const id = dc.id;
expect(dc.label).toEqual(label);
expect(dc.protocol).toEqual(protocol);

peer1.close();
peer2.close();

if (dc.readyState !== 'closed') {
await eventPromise(dc, 'close');
}

expect(dc.readyState).toEqual('closed');
expect(dc.id).toEqual(id);
expect(dc.label).toEqual(label);
expect(dc.protocol).toEqual(protocol);
});

test('it should accept a preconfigured PeerConnection', () => {
const peerConnection = new PeerConnection('Peer', {
iceServers: [],
Expand Down