Skip to content

Commit b2527c6

Browse files
committed
fix: Merged pear-devs#4071 to fix sync issues
1 parent 05ecaa5 commit b2527c6

File tree

3 files changed

+78
-13
lines changed

3 files changed

+78
-13
lines changed

src/plugins/music-together/connection.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import delay from 'delay';
99
import type { Permission, Profile, VideoData } from './types';
1010

1111
export type ConnectionEventMap = {
12+
CLEAR_QUEUE: null;
1213
ADD_SONGS: { videoList: VideoData[]; index?: number };
1314
REMOVE_SONG: { index: number };
1415
MOVE_SONG: { fromIndex: number; toIndex: number };
16+
SET_INDEX: { index: number };
1517
IDENTIFY: { profile: Profile } | undefined;
1618
SYNC_PROFILE: { profiles: Record<string, Profile> } | undefined;
1719
SYNC_QUEUE: { videoList: VideoData[] } | undefined;
@@ -171,9 +173,10 @@ export class Connection {
171173
public async broadcast<Event extends keyof ConnectionEventMap>(
172174
type: Event,
173175
payload: ConnectionEventMap[Event],
176+
after?: ConnectionEventUnion[],
174177
) {
175178
await Promise.all(
176-
this.getConnections().map((conn) => conn.send({ type, payload })),
179+
this.getConnections().map((conn) => conn.send({ type, payload, after })),
177180
);
178181
}
179182

src/plugins/music-together/index.ts

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,25 @@ export default createPlugin<
215215
this.ignoreChange = true;
216216

217217
switch (event.type) {
218+
case 'CLEAR_QUEUE': {
219+
if (conn && this.permission === 'host-only') {
220+
await this.connection?.broadcast('SYNC_QUEUE', {
221+
videoList: this.queue?.videoList ?? [],
222+
});
223+
return;
224+
}
225+
226+
this.queue?.clear();
227+
await this.connection?.broadcast('CLEAR_QUEUE', null);
228+
break;
229+
}
230+
case 'SET_INDEX': {
231+
this.queue?.setIndex(event.payload.index);
232+
await this.connection?.broadcast('SET_INDEX', {
233+
index: event.payload.index,
234+
});
235+
break;
236+
}
218237
case 'ADD_SONGS': {
219238
if (conn && this.permission === 'host-only') {
220239
await this.connection?.broadcast('SYNC_QUEUE', {
@@ -231,10 +250,20 @@ export default createPlugin<
231250
);
232251

233252
await this.queue?.addVideos(videoList, event.payload.index);
234-
await this.connection?.broadcast('ADD_SONGS', {
235-
...event.payload,
236-
videoList,
237-
});
253+
await this.connection?.broadcast(
254+
'ADD_SONGS',
255+
{
256+
...event.payload,
257+
videoList,
258+
},
259+
event.after,
260+
);
261+
262+
const afterevent = event.after?.at(0);
263+
if (afterevent?.type === 'SET_INDEX') {
264+
this.queue?.setIndex(afterevent.payload.index);
265+
}
266+
238267
break;
239268
}
240269
case 'REMOVE_SONG': {
@@ -385,16 +414,30 @@ export default createPlugin<
385414
const queueListener = async (event: ConnectionEventUnion) => {
386415
this.ignoreChange = true;
387416
switch (event.type) {
388-
case 'ADD_SONGS': {
389-
await this.connection?.broadcast('ADD_SONGS', {
390-
...event.payload,
391-
videoList: event.payload.videoList.map((it) => ({
392-
...it,
393-
ownerId: it.ownerId ?? this.connection!.id,
394-
})),
417+
case 'CLEAR_QUEUE': {
418+
await this.connection?.broadcast('CLEAR_QUEUE', null);
419+
break;
420+
}
421+
case 'SET_INDEX': {
422+
await this.connection?.broadcast('SET_INDEX', {
423+
index: event.payload.index,
395424
});
396425
break;
397426
}
427+
case 'ADD_SONGS': {
428+
await this.connection?.broadcast(
429+
'ADD_SONGS',
430+
{
431+
...event.payload,
432+
videoList: event.payload.videoList.map((it) => ({
433+
...it,
434+
ownerId: it.ownerId ?? this.connection!.id,
435+
})),
436+
},
437+
event.after,
438+
);
439+
break;
440+
}
398441
case 'REMOVE_SONG': {
399442
await this.connection?.broadcast('REMOVE_SONG', event.payload);
400443
break;
@@ -420,6 +463,14 @@ export default createPlugin<
420463
const listener = async (event: ConnectionEventUnion) => {
421464
this.ignoreChange = true;
422465
switch (event.type) {
466+
case 'CLEAR_QUEUE': {
467+
this.queue?.clear();
468+
break;
469+
}
470+
case 'SET_INDEX': {
471+
this.queue?.setIndex(event.payload.index);
472+
break;
473+
}
423474
case 'ADD_SONGS': {
424475
const videoList: VideoData[] = event.payload.videoList.map(
425476
(it) => ({
@@ -429,6 +480,12 @@ export default createPlugin<
429480
);
430481

431482
await this.queue?.addVideos(videoList, event.payload.index);
483+
484+
const afterevent = event.after?.at(0);
485+
if (afterevent?.type === 'SET_INDEX') {
486+
this.queue?.setIndex(afterevent.payload.index);
487+
}
488+
432489
break;
433490
}
434491
case 'REMOVE_SONG': {

src/plugins/music-together/queue/queue.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,11 @@ export class Queue {
314314
if (!this.internalDispatch) {
315315
if (event.type === 'CLEAR') {
316316
this.ignoreFlag = true;
317+
this.broadcast({
318+
type: 'CLEAR_QUEUE',
319+
payload: null,
320+
});
321+
return;
317322
}
318323
if (event.type === 'ADD_ITEMS') {
319324
if (this.ignoreFlag) {
@@ -347,7 +352,7 @@ export class Queue {
347352
},
348353
after: [
349354
{
350-
type: 'SYNC_PROGRESS',
355+
type: 'SET_INDEX',
351356
payload: {
352357
index,
353358
},

0 commit comments

Comments
 (0)