Files
otunes/packages/convex/client/playback-queue.ts
T

264 lines
6.6 KiB
TypeScript

import type { Id } from "../convex/_generated/dataModel"
/**
* Queue rules shared by the clients, which apply them locally, and the Convex
* playback mutations.
*/
function shuffleArray<T>(items: T[]): T[] {
const next = [...items]
for (let i = next.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
;[next[i], next[j]] = [next[j], next[i]]
}
return next
}
export type RepeatMode = "none" | "once" | "always"
export type PlaybackQueueState = {
queue: Id<"songs">[]
history: Id<"songs">[]
shuffleEnabled: boolean
repeatEnabled: RepeatMode
/** Saved position of `queue[0]`, in seconds. */
progressSeconds: number
}
export const EMPTY_PLAYBACK_QUEUE: PlaybackQueueState = {
queue: [],
history: [],
shuffleEnabled: false,
repeatEnabled: "none",
progressSeconds: 0,
}
export const MAX_QUEUE_HISTORY = 30
function uniqueSongIds(songIds: Id<"songs">[]) {
return [...new Set(songIds)]
}
function appendToHistory(history: Id<"songs">[], songIds: Id<"songs">[]) {
const next = [...history, ...songIds]
return next.length <= MAX_QUEUE_HISTORY
? next
: next.slice(next.length - MAX_QUEUE_HISTORY)
}
export function startQueue(
state: PlaybackQueueState,
songIds: Id<"songs">[],
shuffled: boolean
): PlaybackQueueState {
const unique = uniqueSongIds(songIds)
return {
...state,
queue: shuffled ? shuffleArray(unique) : unique,
history: [],
shuffleEnabled: shuffled,
progressSeconds: 0,
}
}
export function startQueueFromSong(
state: PlaybackQueueState,
songId: Id<"songs">,
songIds: Id<"songs">[]
): PlaybackQueueState {
const index = songIds.indexOf(songId)
const rest = state.shuffleEnabled
? uniqueSongIds(songIds).filter((id) => id !== songId)
: index === -1
? []
: songIds.slice(index + 1)
return {
...state,
queue: [songId, ...(state.shuffleEnabled ? shuffleArray(rest) : rest)],
history: [],
progressSeconds: 0,
}
}
/**
* Fills in the rest of a queue started with only `songId`, unless the queue
* changed in the meantime.
*/
export function extendQueueFromSong(
state: PlaybackQueueState,
songId: Id<"songs">,
songIds: Id<"songs">[]
): PlaybackQueueState {
if (state.queue.length !== 1 || state.queue[0] !== songId) {
return state
}
const { queue } = startQueueFromSong(state, songId, songIds)
return queue.length > 1 ? { ...state, queue } : state
}
export function addToQueue(
state: PlaybackQueueState,
songIds: Id<"songs">[],
position: "next" | "last"
): PlaybackQueueState {
const toAdd = state.shuffleEnabled ? shuffleArray(songIds) : songIds
if (state.queue.length === 0) {
return { ...state, queue: toAdd, progressSeconds: 0 }
}
const [current, ...rest] = state.queue
return {
...state,
queue:
position === "next"
? [current, ...toAdd, ...rest]
: [...state.queue, ...toAdd],
}
}
export function toggleShuffle(state: PlaybackQueueState): PlaybackQueueState {
const shuffleEnabled = !state.shuffleEnabled
if (shuffleEnabled && state.queue.length > 1) {
const [current, ...upNext] = state.queue
return {
...state,
shuffleEnabled,
queue: [current, ...shuffleArray(upNext)],
}
}
return { ...state, shuffleEnabled }
}
export function cycleRepeat(state: PlaybackQueueState): PlaybackQueueState {
const repeatEnabled: RepeatMode =
state.repeatEnabled === "none"
? "once"
: state.repeatEnabled === "once"
? "always"
: "none"
return { ...state, repeatEnabled }
}
export function setShuffleEnabled(
state: PlaybackQueueState,
enabled: boolean
): PlaybackQueueState {
if (state.shuffleEnabled === enabled) {
return state
}
return toggleShuffle(state)
}
export function setRepeatMode(
state: PlaybackQueueState,
repeatEnabled: RepeatMode
): PlaybackQueueState {
if (state.repeatEnabled === repeatEnabled) {
return state
}
return { ...state, repeatEnabled }
}
/** Drops everything after the current song. The song that is playing stays. */
export function clearUpNext(state: PlaybackQueueState): PlaybackQueueState {
if (state.queue.length <= 1) {
return state
}
return { ...state, queue: [state.queue[0]] }
}
export function clearQueue(state: PlaybackQueueState): PlaybackQueueState {
return { ...state, queue: [], history: [], progressSeconds: 0 }
}
export function clearHistory(state: PlaybackQueueState): PlaybackQueueState {
return state.history.length === 0 ? state : { ...state, history: [] }
}
export function reorderQueue(
state: PlaybackQueueState,
songIds: Id<"songs">[]
): PlaybackQueueState {
const currentSongChanged =
songIds.length > 0 &&
state.queue.length > 0 &&
songIds[0] !== state.queue[0]
return {
...state,
queue: songIds,
history: currentSongChanged
? appendToHistory(state.history, [state.queue[0]])
: state.history,
progressSeconds: currentSongChanged ? 0 : state.progressSeconds,
}
}
export function skipToQueueIndex(
state: PlaybackQueueState,
index: number
): PlaybackQueueState {
if (index <= 0 || index >= state.queue.length) {
return state
}
return {
...state,
queue: state.queue.slice(index),
history: appendToHistory(state.history, state.queue.slice(0, index)),
progressSeconds: 0,
}
}
/** Plays `history[index]`, putting it and every later song back in the queue. */
export function skipToHistoryIndex(
state: PlaybackQueueState,
index: number
): PlaybackQueueState {
if (index < 0 || index >= state.history.length) {
return state
}
return {
...state,
queue: [...state.history.slice(index), ...state.queue],
history: state.history.slice(0, index),
progressSeconds: 0,
}
}
export function skipNext(state: PlaybackQueueState): PlaybackQueueState {
if (state.queue.length === 0) {
return state
}
if (state.repeatEnabled === "once") {
return { ...state, progressSeconds: 0 }
}
const [current, ...rest] = state.queue
if (state.repeatEnabled === "always") {
return { ...state, queue: [...rest, current], progressSeconds: 0 }
}
if (rest.length === 0) {
return { ...state, progressSeconds: 0 }
}
return {
...state,
queue: rest,
history: appendToHistory(state.history, [current]),
progressSeconds: 0,
}
}
export function skipPrevious(state: PlaybackQueueState): PlaybackQueueState {
if (state.queue.length === 0) {
return state
}
if (state.history.length === 0) {
return { ...state, progressSeconds: 0 }
}
return {
...state,
queue: [state.history[state.history.length - 1], ...state.queue],
history: state.history.slice(0, -1),
progressSeconds: 0,
}
}