mirror of
https://github.com/jeffvli/feishin.git
synced 2026-06-24 04:47:57 +02:00
fix duplicate PLAYER_PLAY event causing race condition in mpv
This commit is contained in:
@@ -209,6 +209,68 @@ function calculateNextIndex(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function emitPlayerPlayEvent(
|
||||||
|
targetSongUniqueId: string | undefined,
|
||||||
|
set: (fn: (state: PlayerState) => void) => void,
|
||||||
|
get: () => PlayerState,
|
||||||
|
): void {
|
||||||
|
// If playSongId is provided, find the song and start playback on it
|
||||||
|
if (targetSongUniqueId) {
|
||||||
|
let playIndex: number | undefined;
|
||||||
|
set((state) => {
|
||||||
|
const queue = state.getQueue();
|
||||||
|
const queueIndex = queue.items.findIndex(
|
||||||
|
(item) => item._uniqueId === targetSongUniqueId,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (queueIndex !== -1) {
|
||||||
|
if (
|
||||||
|
state.player.shuffle === PlayerShuffle.TRACK &&
|
||||||
|
state.queue.shuffled.length > 0
|
||||||
|
) {
|
||||||
|
// Find the shuffled position for this queue index
|
||||||
|
const shuffledPosition = state.queue.shuffled.findIndex(
|
||||||
|
(idx) => idx === queueIndex,
|
||||||
|
);
|
||||||
|
if (shuffledPosition !== -1) {
|
||||||
|
state.player.index = shuffledPosition;
|
||||||
|
playIndex = shuffledPosition;
|
||||||
|
} else {
|
||||||
|
state.player.index = queueIndex;
|
||||||
|
playIndex = queueIndex;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
state.player.index = queueIndex;
|
||||||
|
playIndex = queueIndex;
|
||||||
|
}
|
||||||
|
state.player.status = PlayerStatus.PLAYING;
|
||||||
|
setTimestampStore(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Emit PLAYER_PLAY event if playback was started
|
||||||
|
if (playIndex !== undefined) {
|
||||||
|
eventEmitter.emit('PLAYER_PLAY', {
|
||||||
|
id: targetSongUniqueId,
|
||||||
|
index: playIndex,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Otherwise, emit PLAYER_PLAY event for current song if available
|
||||||
|
const currentState = get();
|
||||||
|
const queue = currentState.getQueue();
|
||||||
|
const currentIndex = currentState.player.index;
|
||||||
|
const currentSong = queue.items[currentIndex];
|
||||||
|
|
||||||
|
if (currentSong && currentIndex !== undefined && currentIndex >= 0) {
|
||||||
|
eventEmitter.emit('PLAYER_PLAY', {
|
||||||
|
id: currentSong._uniqueId,
|
||||||
|
index: currentIndex,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Helper function to find shuffled position for a given queue index
|
// Helper function to find shuffled position for a given queue index
|
||||||
function findShuffledPositionForQueueIndex(
|
function findShuffledPositionForQueueIndex(
|
||||||
queueIndex: number,
|
queueIndex: number,
|
||||||
@@ -502,21 +564,7 @@ export const usePlayerStoreBase = createWithEqualityFn<PlayerState>()(
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const currentState = get();
|
emitPlayerPlayEvent(targetSongUniqueId, set, get);
|
||||||
const queue = currentState.getQueue();
|
|
||||||
const currentIndex = currentState.player.index;
|
|
||||||
const currentSong = queue.items[currentIndex];
|
|
||||||
|
|
||||||
if (
|
|
||||||
currentSong &&
|
|
||||||
currentIndex !== undefined &&
|
|
||||||
currentIndex >= 0
|
|
||||||
) {
|
|
||||||
eventEmitter.emit('PLAYER_PLAY', {
|
|
||||||
id: currentSong._uniqueId,
|
|
||||||
index: currentIndex,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Play.SHUFFLE: {
|
case Play.SHUFFLE: {
|
||||||
@@ -543,22 +591,7 @@ export const usePlayerStoreBase = createWithEqualityFn<PlayerState>()(
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Emit PLAYER_PLAY event after state is updated
|
emitPlayerPlayEvent(targetSongUniqueId, set, get);
|
||||||
const currentState = get();
|
|
||||||
const queue = currentState.getQueue();
|
|
||||||
const currentIndex = currentState.player.index;
|
|
||||||
const currentSong = queue.items[currentIndex];
|
|
||||||
|
|
||||||
if (
|
|
||||||
currentSong &&
|
|
||||||
currentIndex !== undefined &&
|
|
||||||
currentIndex >= 0
|
|
||||||
) {
|
|
||||||
eventEmitter.emit('PLAYER_PLAY', {
|
|
||||||
id: currentSong._uniqueId,
|
|
||||||
index: currentIndex,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -802,21 +835,7 @@ export const usePlayerStoreBase = createWithEqualityFn<PlayerState>()(
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const currentState = get();
|
emitPlayerPlayEvent(targetSongUniqueId, set, get);
|
||||||
const queue = currentState.getQueue();
|
|
||||||
const currentIndex = currentState.player.index;
|
|
||||||
const currentSong = queue.items[currentIndex];
|
|
||||||
|
|
||||||
if (
|
|
||||||
currentSong &&
|
|
||||||
currentIndex !== undefined &&
|
|
||||||
currentIndex >= 0
|
|
||||||
) {
|
|
||||||
eventEmitter.emit('PLAYER_PLAY', {
|
|
||||||
id: currentSong._uniqueId,
|
|
||||||
index: currentIndex,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Play.SHUFFLE: {
|
case Play.SHUFFLE: {
|
||||||
@@ -846,66 +865,13 @@ export const usePlayerStoreBase = createWithEqualityFn<PlayerState>()(
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Emit PLAYER_PLAY event after state is updated
|
emitPlayerPlayEvent(targetSongUniqueId, set, get);
|
||||||
const currentState = get();
|
|
||||||
const queue = currentState.getQueue();
|
|
||||||
const currentIndex = currentState.player.index;
|
|
||||||
const currentSong = queue.items[currentIndex];
|
|
||||||
if (currentSong && currentIndex !== undefined) {
|
|
||||||
eventEmitter.emit('PLAYER_PLAY', {
|
|
||||||
id: currentSong._uniqueId,
|
|
||||||
index: currentIndex,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If playSongId is provided, find the song and start playback on it
|
|
||||||
if (targetSongUniqueId) {
|
|
||||||
let playIndex: number | undefined;
|
|
||||||
set((state) => {
|
|
||||||
const queue = state.getQueue();
|
|
||||||
const queueIndex = queue.items.findIndex(
|
|
||||||
(item) => item._uniqueId === targetSongUniqueId,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (queueIndex !== -1) {
|
|
||||||
if (
|
|
||||||
state.player.shuffle === PlayerShuffle.TRACK &&
|
|
||||||
state.queue.shuffled.length > 0
|
|
||||||
) {
|
|
||||||
// Find the shuffled position for this queue index
|
|
||||||
const shuffledPosition = state.queue.shuffled.findIndex(
|
|
||||||
(idx) => idx === queueIndex,
|
|
||||||
);
|
|
||||||
if (shuffledPosition !== -1) {
|
|
||||||
state.player.index = shuffledPosition;
|
|
||||||
playIndex = shuffledPosition;
|
|
||||||
} else {
|
|
||||||
state.player.index = queueIndex;
|
|
||||||
playIndex = queueIndex;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
state.player.index = queueIndex;
|
|
||||||
playIndex = queueIndex;
|
|
||||||
}
|
|
||||||
state.player.status = PlayerStatus.PLAYING;
|
|
||||||
setTimestampStore(0);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Emit PLAYER_PLAY event if playback was started
|
|
||||||
if (playIndex !== undefined) {
|
|
||||||
eventEmitter.emit('PLAYER_PLAY', {
|
|
||||||
id: targetSongUniqueId,
|
|
||||||
index: playIndex,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
addToQueueByUniqueId: (items, uniqueId, edge, playSongId) => {
|
addToQueueByUniqueId: (items, uniqueId, edge, playSongId) => {
|
||||||
const newItems = items.map(toQueueSong);
|
const newItems = items.map(toQueueSong);
|
||||||
@@ -1099,48 +1065,7 @@ export const usePlayerStoreBase = createWithEqualityFn<PlayerState>()(
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// If playSongId is provided, find the song and start playback on it
|
emitPlayerPlayEvent(targetSongUniqueId, set, get);
|
||||||
if (targetSongUniqueId) {
|
|
||||||
let playIndex: number | undefined;
|
|
||||||
set((state) => {
|
|
||||||
const queue = state.getQueue();
|
|
||||||
const queueIndex = queue.items.findIndex(
|
|
||||||
(item) => item._uniqueId === targetSongUniqueId,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (queueIndex !== -1) {
|
|
||||||
if (
|
|
||||||
state.player.shuffle === PlayerShuffle.TRACK &&
|
|
||||||
state.queue.shuffled.length > 0
|
|
||||||
) {
|
|
||||||
// Find the shuffled position for this queue index
|
|
||||||
const shuffledPosition = state.queue.shuffled.findIndex(
|
|
||||||
(idx) => idx === queueIndex,
|
|
||||||
);
|
|
||||||
if (shuffledPosition !== -1) {
|
|
||||||
state.player.index = shuffledPosition;
|
|
||||||
playIndex = shuffledPosition;
|
|
||||||
} else {
|
|
||||||
state.player.index = queueIndex;
|
|
||||||
playIndex = queueIndex;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
state.player.index = queueIndex;
|
|
||||||
playIndex = queueIndex;
|
|
||||||
}
|
|
||||||
state.player.status = PlayerStatus.PLAYING;
|
|
||||||
setTimestampStore(0);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Emit PLAYER_PLAY event if playback was started
|
|
||||||
if (playIndex !== undefined) {
|
|
||||||
eventEmitter.emit('PLAYER_PLAY', {
|
|
||||||
id: targetSongUniqueId,
|
|
||||||
index: playIndex,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
clearQueue: () => {
|
clearQueue: () => {
|
||||||
set((state) => {
|
set((state) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user