Skip to content

Commit

Permalink
refactor: emit "peer-unavailable" error only on relevant connections
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasgloning committed Aug 26, 2023
1 parent 5404187 commit 1738e69
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions lib/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,19 +374,15 @@ export class Peer extends EventEmitter<PeerEvents> {
`Could not connect to peer ${peerId}`,
);

// This is so that an error is emited on a connection that has just been created,
// but could not be established because the server responded with
// `ServerMessageType.Expire` (but yes, we emit the error on all connections).
// See https://github.com/peers/peerjs/issues/924
//
// TODO how about `this._cleanupPeer(peerId);` instead?
// Or how about make the server send back `connectionId`, and only emit the error
// on that connection?
const connections = this._connections.get(peerId);
if (connections) {
for (const c of connections) {
c.emit("error", new Error(`Server says ${peerId} is unavailable`));
}
// Emit an error on all connections with this peer.
const connections = (this._connections.get(peerId) ?? []).filter(
(c) => c.peer === peerId,
);
for (const conn of connections) {
conn.emitError(
"peer-unavailable",
`Server says ${peerId} is unavailable`,
);
}

break;
Expand Down

0 comments on commit 1738e69

Please sign in to comment.