Skip to content

Commit

Permalink
Resolve join promise after API channel ready (#185)
Browse files Browse the repository at this point in the history
Previously, the (implicit) promise returned by `join` would resolve
after establishing the transports, but before the API data channel was
attached.  This meant there was a small window of time where the API
data channel was not ready, and trying to use the subscriber API during
that time would fail silently.

This change returns a promise that does not resolve until after the API
channel is established.
  • Loading branch information
kevinmcconnell authored Apr 15, 2021
1 parent dfc37fa commit 4d1d436
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,24 @@ export default class Client {
}
};

this.transports[Role.sub].pc.ondatachannel = (ev: RTCDataChannelEvent) => {
if (ev.channel.label === API_CHANNEL) {
this.transports![Role.sub].api = ev.channel;
ev.channel.onmessage = (e) => {
if (this.onspeaker) {
this.onspeaker(JSON.parse(e.data));
}
};
return;
}
const apiReady = new Promise<void>((resolve) => {
this.transports![Role.sub].pc.ondatachannel = (ev: RTCDataChannelEvent) => {
if (ev.channel.label === API_CHANNEL) {
this.transports![Role.sub].api = ev.channel;
ev.channel.onmessage = (e) => {
if (this.onspeaker) {
this.onspeaker(JSON.parse(e.data));
}
};
resolve();
return;
}

if (this.ondatachannel) {
this.ondatachannel(ev);
}
};
if (this.ondatachannel) {
this.ondatachannel(ev);
}
};
});

const offer = await this.transports[Role.pub].pc.createOffer();
await this.transports[Role.pub].pc.setLocalDescription(offer);
Expand All @@ -130,6 +133,8 @@ export default class Client {
await this.transports[Role.pub].pc.setRemoteDescription(answer);
this.transports[Role.pub].candidates.forEach((c) => this.transports![Role.pub].pc.addIceCandidate(c));
this.transports[Role.pub].pc.onnegotiationneeded = this.onNegotiationNeeded.bind(this);

return apiReady;
}

leave() {
Expand Down

1 comment on commit 4d1d436

@adwpc
Copy link
Contributor

@adwpc adwpc commented on 4d1d436 Apr 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im so sad
This change make a error in ion-app-web:
trickle error: no rtc transport exists for this Peer

It worked fine when i use v1.7.0

Please sign in to comment.