From dfa427f2ed39473339ea093591cb3fceea5aa2ef Mon Sep 17 00:00:00 2001 From: JellyBrick Date: Mon, 12 May 2025 01:46:14 +0900 Subject: [PATCH] fix(music-together): Removing a connection on error --- src/plugins/music-together/connection.ts | 27 +++++++++++++++++------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/plugins/music-together/connection.ts b/src/plugins/music-together/connection.ts index cb9967f3..936b6f1e 100644 --- a/src/plugins/music-together/connection.ts +++ b/src/plugins/music-together/connection.ts @@ -1,4 +1,4 @@ -import { DataConnection, Peer, PeerErrorType } from 'peerjs'; +import { DataConnection, Peer, PeerError, PeerErrorType } from 'peerjs'; import delay from 'delay'; import type { Permission, Profile, VideoData } from './types'; @@ -89,8 +89,11 @@ export class Connection { listener({ type: 'CONNECTION_CLOSED', payload: null }, null); } this.listeners = []; + + this.connectionListeners.forEach((listener) => listener()); this.connectionListeners = []; this.connections = {}; + this.peer.disconnect(); this.peer.destroy(); }); @@ -207,18 +210,26 @@ export class Connection { }); }); - const onClose = (err?: Error) => { - if (err) reject(err); - - delete this.connections[conn.connectionId]; - - this.connectionListeners.forEach((listener) => listener(conn)); - + const onClose = ( + err?: PeerError< + | 'not-open-yet' + | 'message-too-big' + | 'negotiation-failed' + | 'connection-closed' + >, + ) => { if (conn.open) { conn.close({ flush: true, }); } + + delete this.connections[conn.connectionId]; + this.connectionListeners.forEach((listener) => listener(conn)); + + if (err) { + reject(err); + } }; conn.on('error', onClose); conn.on('close', onClose);