Skip to content

Commit

Permalink
Implement skip/previous
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepyfran committed Sep 11, 2024
1 parent effe669 commit 025a69f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
29 changes: 25 additions & 4 deletions packages/components/player/src/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export class EchoPlayer extends LitElement {
this,
() => PlayerService.togglePlayback,
);
private _previousTrack = new EffectFn(this, () => PlayerService.previous);
private _skipTrack = new EffectFn(this, () => PlayerService.skip);

render() {
return this._player.render({
Expand All @@ -30,7 +32,10 @@ export class EchoPlayer extends LitElement {
)}
</div>
<div>
<button ?disabled=${!player.previouslyPlayedTracks.length}>
<button
@click=${this._onPreviousTrack}
?disabled=${!player.previouslyPlayedTracks.length}
>
</button>
<button
Expand All @@ -45,13 +50,21 @@ export class EchoPlayer extends LitElement {
>
</button>
<button ?disabled=${!player.comingUpTracks.length}></button>
<button
@click=${this._onSkipTrack}
?disabled=${!player.comingUpTracks.length}
>
</button>
</div>
<p>
Previously played tracks:
${JSON.stringify(player.previouslyPlayedTracks)}
${player.previouslyPlayedTracks.map((t) => t.name).join(", ")}
</p>
<p>
Coming up tracks:
${player.comingUpTracks.map((t) => t.name).join(", ")}
</p>
<p>Coming up tracks: ${player.comingUpTracks}</p>
</div>
`,
});
Expand All @@ -67,6 +80,14 @@ export class EchoPlayer extends LitElement {
private _onTogglePlayback() {
this._togglePlayback.run({});
}

private _onPreviousTrack() {
this._previousTrack.run({});
}

private _onSkipTrack() {
this._skipTrack.run({});
}
}

declare global {
Expand Down
42 changes: 40 additions & 2 deletions packages/services/player/src/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
type Track,
} from "@echo/core-types";
import {
Array,
Data,
Effect,
Exit,
Expand Down Expand Up @@ -86,8 +87,45 @@ const makePlayer = Effect.gen(function* () {

yield* mediaPlayer.value.player.togglePlayback;
}),
previous: Effect.void,
skip: Effect.void,
previous: Effect.gen(function* () {
// TODO: Refactor this to avoid duplication with other play commands.
const { previouslyPlayedTracks, comingUpTracks, status } =
yield* Ref.get(state);
const lastPlayedTrack = Array.last(previouslyPlayedTracks);
if (Option.isNone(lastPlayedTrack)) {
yield* Effect.logWarning(
"Attempted to play previous track, but the previous queue is empty",
);
return;
}

const { provider, player } = yield* resolveDependenciesForTrack(
providerCache,
lastPlayedTrack.value,
);

const currentTrack = Match.value(status).pipe(
Match.tag("Playing", ({ track }) => [track]),
Match.tag("Paused", ({ track }) => [track]),
Match.tag("Stopped", () => []),
Match.exhaustive,
);
const previousWithoutLast = Array.dropRight(previouslyPlayedTracks, 1);
const comingUpWithCurrent = [...currentTrack, ...comingUpTracks];

yield* playTrack(provider, player, lastPlayedTrack.value);
yield* commandQueue.offer(
UpdateState({
updateFn: (state) => ({
...state,
status: Playing({ track: lastPlayedTrack.value }),
previouslyPlayedTracks: previousWithoutLast,
comingUpTracks: comingUpWithCurrent,
}),
}),
);
}),
skip: commandQueue.offer(NextTrack()),
observe: state,
});
});
Expand Down

0 comments on commit 025a69f

Please sign in to comment.