Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/plugins/music-together/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import delay from 'delay';
import type { Permission, Profile, VideoData } from './types';

export type ConnectionEventMap = {
CLEAR_QUEUE: {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <@typescript-eslint/no-empty-object-type> reported by reviewdog 🐶
The {} ("empty object") type allows any non-nullish value, including literals like 0 and "".

  • If that's what you want, disable this lint rule with an inline comment or configure the 'allowObjectTypes' rule option.
  • If you want a type meaning "any object", you probably want object instead.
  • If you want a type meaning "any value", you probably want unknown instead.

ADD_SONGS: { videoList: VideoData[]; index?: number };
REMOVE_SONG: { index: number };
MOVE_SONG: { fromIndex: number; toIndex: number };
SET_INDEX: { index: number };
IDENTIFY: { profile: Profile } | undefined;
SYNC_PROFILE: { profiles: Record<string, Profile> } | undefined;
SYNC_QUEUE: { videoList: VideoData[] } | undefined;
Expand Down Expand Up @@ -171,9 +173,10 @@ export class Connection {
public async broadcast<Event extends keyof ConnectionEventMap>(
type: Event,
payload: ConnectionEventMap[Event],
after?: ConnectionEventUnion[],
) {
await Promise.all(
this.getConnections().map((conn) => conn.send({ type, payload })),
this.getConnections().map((conn) => conn.send({ type, payload, after })),
);
}

Expand Down
50 changes: 48 additions & 2 deletions src/plugins/music-together/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,23 @@ export default createPlugin<
this.ignoreChange = true;

switch (event.type) {
case 'CLEAR_QUEUE': {
if (conn && this.permission === 'host-only') {
await this.connection?.broadcast('SYNC_QUEUE', {
videoList: this.queue?.videoList ?? [],
});
return;
}

this.queue?.clear();
await this.connection?.broadcast('CLEAR_QUEUE', {});
break;
}
case 'SET_INDEX': {
this.queue?.setIndex(event.payload.index);
await this.connection?.broadcast('SET_INDEX', { index: event.payload.index });
break;
}
case 'ADD_SONGS': {
if (conn && this.permission === 'host-only') {
await this.connection?.broadcast('SYNC_QUEUE', {
Expand All @@ -234,7 +251,13 @@ export default createPlugin<
await this.connection?.broadcast('ADD_SONGS', {
...event.payload,
videoList,
});
}, event.after);

const afterevent = event.after?.at(0);
if (afterevent?.type === 'SET_INDEX') {
this.queue?.setIndex(afterevent.payload.index);
}

break;
}
case 'REMOVE_SONG': {
Expand Down Expand Up @@ -385,14 +408,22 @@ export default createPlugin<
const queueListener = async (event: ConnectionEventUnion) => {
this.ignoreChange = true;
switch (event.type) {
case 'CLEAR_QUEUE': {
await this.connection?.broadcast('CLEAR_QUEUE', {});
break;
}
case 'SET_INDEX': {
await this.connection?.broadcast('SET_INDEX', { index: event.payload.index });
break;
}
case 'ADD_SONGS': {
await this.connection?.broadcast('ADD_SONGS', {
...event.payload,
videoList: event.payload.videoList.map((it) => ({
...it,
ownerId: it.ownerId ?? this.connection!.id,
})),
});
}, event.after);
break;
}
case 'REMOVE_SONG': {
Expand Down Expand Up @@ -420,6 +451,14 @@ export default createPlugin<
const listener = async (event: ConnectionEventUnion) => {
this.ignoreChange = true;
switch (event.type) {
case 'CLEAR_QUEUE': {
this.queue?.clear();
break;
}
case 'SET_INDEX': {
this.queue?.setIndex(event.payload.index);
break;
}
case 'ADD_SONGS': {
const videoList: VideoData[] = event.payload.videoList.map(
(it) => ({
Expand All @@ -429,6 +468,13 @@ export default createPlugin<
);

await this.queue?.addVideos(videoList, event.payload.index);

const afterevent = event.after?.at(0);
if (afterevent?.type === 'SET_INDEX') {
this.queue?.setIndex(afterevent.payload.index);
}

Comment on lines +483 to +484
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Delete

Suggested change
}
}


break;
}
case 'REMOVE_SONG': {
Expand Down
7 changes: 6 additions & 1 deletion src/plugins/music-together/queue/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ export class Queue {
if (!this.internalDispatch) {
if (event.type === 'CLEAR') {
this.ignoreFlag = true;
this.broadcast({
type: 'CLEAR_QUEUE',
payload: {},
})
return;
}
if (event.type === 'ADD_ITEMS') {
if (this.ignoreFlag) {
Expand Down Expand Up @@ -347,7 +352,7 @@ export class Queue {
},
after: [
{
type: 'SYNC_PROGRESS',
type: 'SET_INDEX',
payload: {
index,
},
Expand Down
Loading