Skip to content

Commit

Permalink
Fix tracks not being playable on large libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
martpie committed Apr 2, 2024
1 parent 47cb382 commit 96286d6
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions src/stores/usePlayerStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const usePlayerStore = createPlayerStore<PlayerState>((set, get) => ({
* Start playing audio (queue instantiation, shuffle and everything...)
* TODO: this function ~could probably~ needs to be refactored ~a bit~
*/
start: async (tracks, _id): Promise<void> => {
start: async (queue, _id): Promise<void> => {
// TODO: implement start with no queue
// // If no queue is provided, we create it based on the screen the user is on
// if (!queue) {
Expand All @@ -83,14 +83,10 @@ const usePlayerStore = createPlayerStore<PlayerState>((set, get) => ({
// }
// }

if (tracks.length === 0) return;
if (queue.length === 0) return;

const state = get();

// on macOS, localStorage is quite limited, so we limit the max number of items
// in the queue
let queue = tracks.slice(0, 2000);

// Check if there's already a queue planned
if (queue === null && state.queue !== null) {
queue = state.queue;
Expand Down Expand Up @@ -496,6 +492,35 @@ function createPlayerStore<T extends PlayerState>(store: StateCreator<T>) {
return createStore(
persist(store, {
name: 'museeks-player',
partialize: (state) => {
// on macOS, localStorage is quite limited, so we limit the max number of items
// in the queue by slicing the queue around the currently playing track
// Should oldQueue be tackled in some ways?
const queue = state.queue;
const queueCursor = state.queueCursor ?? 0;
const queueStorageLimit = 1000;

if (queue.length < queueStorageLimit) {
return state;
}

const trackID = queue[queueCursor]._id;

const persistedQueue = queue.slice(
Math.max(0, queueCursor - queueStorageLimit / 2),
Math.min(queue.length, queueCursor + queueStorageLimit / 2),
);

const persistedCursor = persistedQueue.findIndex(
(track) => track._id === trackID,
);

return {
...state,
queue: persistedQueue,
queueCursor: persistedCursor,
};
},
onRehydrateStorage: () => {
return (state, error) => {
if (error || state == null) {
Expand Down

0 comments on commit 96286d6

Please sign in to comment.