mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-02-05 05:44:07 +00:00
refactor(music-together): Removed delay dependency (#4201)
This commit is contained in:
@ -1,15 +1,9 @@
|
||||
import {
|
||||
type DataConnection,
|
||||
Peer,
|
||||
type PeerError,
|
||||
PeerErrorType,
|
||||
} from 'peerjs';
|
||||
import delay from 'delay';
|
||||
import { type DataConnection, Peer, type PeerError } from 'peerjs';
|
||||
|
||||
import type { Permission, Profile, VideoData } from './types';
|
||||
|
||||
export type ConnectionEventMap = {
|
||||
CLEAR_QUEUE: {};
|
||||
CLEAR_QUEUE: null;
|
||||
ADD_SONGS: { videoList: VideoData[]; index?: number };
|
||||
REMOVE_SONG: { index: number };
|
||||
MOVE_SONG: { fromIndex: number; toIndex: number };
|
||||
@ -104,16 +98,14 @@ export class Connection {
|
||||
this.peer.disconnect();
|
||||
this.peer.destroy();
|
||||
});
|
||||
this.peer.on('error', async (err) => {
|
||||
if (err.type === PeerErrorType.Network) {
|
||||
// retrying after 10 seconds
|
||||
await delay(10000);
|
||||
try {
|
||||
this.peer.reconnect();
|
||||
return;
|
||||
} catch {
|
||||
//ignored
|
||||
}
|
||||
this.peer.on('error', (err) => {
|
||||
if (err.type === 'network') {
|
||||
setTimeout(() => {
|
||||
try {
|
||||
this.peer.reconnect();
|
||||
} catch {}
|
||||
}, 10000);
|
||||
return;
|
||||
}
|
||||
|
||||
this.waitOpen.reject(err);
|
||||
@ -176,7 +168,9 @@ export class Connection {
|
||||
after?: ConnectionEventUnion[],
|
||||
) {
|
||||
await Promise.all(
|
||||
this.getConnections().map((conn) => conn.send({ type, payload, after })),
|
||||
this.getConnections().map(
|
||||
(conn) => conn.send({ type, payload, after }) ?? Promise.resolve(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -224,7 +224,7 @@ export default createPlugin<
|
||||
}
|
||||
|
||||
this.queue?.clear();
|
||||
await this.connection?.broadcast('CLEAR_QUEUE', {});
|
||||
await this.connection?.broadcast('CLEAR_QUEUE', null);
|
||||
break;
|
||||
}
|
||||
case 'SET_INDEX': {
|
||||
@ -413,7 +413,7 @@ export default createPlugin<
|
||||
this.ignoreChange = true;
|
||||
switch (event.type) {
|
||||
case 'CLEAR_QUEUE': {
|
||||
await this.connection?.broadcast('CLEAR_QUEUE', {});
|
||||
await this.connection?.broadcast('CLEAR_QUEUE', null);
|
||||
break;
|
||||
}
|
||||
case 'SET_INDEX': {
|
||||
|
||||
@ -316,7 +316,7 @@ export class Queue {
|
||||
this.ignoreFlag = true;
|
||||
this.broadcast({
|
||||
type: 'CLEAR_QUEUE',
|
||||
payload: {},
|
||||
payload: null,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user