Skip to content

Commit

Permalink
Keep rest of album in queue
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepyfran committed Sep 3, 2024
1 parent eddc1f2 commit 12ef585
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions packages/services/player/src/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,27 @@ const makePlayer = Effect.gen(function* () {
return Player.of({
playAlbum: (album) =>
Effect.gen(function* () {
// TODO: Make work with more than just the first track.
const firstTrack = album.tracks[0];
if (!firstTrack) {
const [track, ...restOfTracks] = album.tracks;
if (!track) {
yield* Effect.logError(
`Attempted to play album ${album.name}, but it has no tracks.`,
);
return;
}

const streamingProvider = firstTrack.resource.provider;
const streamingProvider = track.resource.provider;
const provider = yield* providerCache.get(streamingProvider);
if (Option.isNone(provider)) {
yield* Effect.logError(
`Attempted to play track ${firstTrack.id}, which is registered with the provider ${streamingProvider}, but the provider is not active.`,
`Attempted to play track ${track.id}, which is registered with the provider ${streamingProvider}, but the provider is not active.`,
);
return yield* Effect.fail(new ProviderNotReady(streamingProvider));
}

switch (firstTrack.resource.type) {
switch (track.resource.type) {
case "file": {
const file = yield* provider.value.provider
.fileUrlById(firstTrack.resource.fileId)
.fileUrlById(track.resource.fileId)
.pipe(Effect.mapError(() => new PlayNotFoundError()));
yield* provider.value.player.playFile(file);
break;
Expand All @@ -48,13 +50,14 @@ const makePlayer = Effect.gen(function* () {
yield* Ref.update(state, (current) => ({
...current,
status: "playing" as const,
currentTrack: Option.some(firstTrack),
currentTrack: Option.some(track),
previouslyPlayedTracks: [
...current.previouslyPlayedTracks,
...(Option.isSome(current.currentTrack)
? [current.currentTrack.value]
: []),
],
comingUpTracks: restOfTracks,
}));
}),
observe: state,
Expand Down

0 comments on commit 12ef585

Please sign in to comment.