mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-15 12:21:47 +00:00
fix(music-together): fix duplicate queue listener
This commit is contained in:
@ -14,6 +14,7 @@ export type ConnectionEventMap = {
|
||||
| { progress?: number; state?: number; index?: number }
|
||||
| undefined;
|
||||
PERMISSION: Permission | undefined;
|
||||
CONNECTION_CLOSED: null;
|
||||
};
|
||||
export type ConnectionEventUnion = {
|
||||
[Event in keyof ConnectionEventMap]: {
|
||||
@ -31,7 +32,7 @@ type PromiseUtil<T> = {
|
||||
|
||||
export type ConnectionListener = (
|
||||
event: ConnectionEventUnion,
|
||||
conn: DataConnection,
|
||||
conn: DataConnection | null,
|
||||
) => void;
|
||||
export type ConnectionMode = 'host' | 'guest' | 'disconnected';
|
||||
export class Connection {
|
||||
@ -59,6 +60,16 @@ export class Connection {
|
||||
this._mode = 'host';
|
||||
await this.registerConnection(conn);
|
||||
});
|
||||
this.peer.on('close', () => {
|
||||
for (const listener of this.listeners) {
|
||||
listener({ type: 'CONNECTION_CLOSED', payload: null }, null);
|
||||
}
|
||||
this.listeners = [];
|
||||
this.connectionListeners = [];
|
||||
this.connections = {};
|
||||
this.peer.disconnect();
|
||||
this.peer.destroy();
|
||||
});
|
||||
this.peer.on('error', async (err) => {
|
||||
if (err.type === PeerErrorType.Network) {
|
||||
// retrying after 10 seconds
|
||||
@ -99,6 +110,10 @@ export class Connection {
|
||||
this._mode = 'disconnected';
|
||||
this.connections = {};
|
||||
this.connectionListeners = [];
|
||||
for (const listener of this.listeners) {
|
||||
listener({ type: 'CONNECTION_CLOSED', payload: null }, null);
|
||||
}
|
||||
this.listeners = [];
|
||||
this.peer.disconnect();
|
||||
this.peer.destroy();
|
||||
}
|
||||
@ -126,7 +141,9 @@ export class Connection {
|
||||
}
|
||||
|
||||
public on(listener: ConnectionListener) {
|
||||
this.listeners.push(listener);
|
||||
if (!this.listeners.includes(listener)) {
|
||||
this.listeners.push(listener);
|
||||
}
|
||||
}
|
||||
|
||||
public onConnections(listener: (connections?: DataConnection) => void) {
|
||||
@ -172,7 +189,6 @@ export class Connection {
|
||||
delete this.connections[conn.connectionId];
|
||||
|
||||
this.connectionListeners.forEach((listener) => listener(conn));
|
||||
this.connectionListeners = [];
|
||||
|
||||
if (conn.open) {
|
||||
conn.close({
|
||||
|
||||
Reference in New Issue
Block a user