Skip to content

Commit

Permalink
fix: Ensure loading context works even when we cannot seek
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Nov 7, 2023
1 parent a582d33 commit 4d530d3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
4 changes: 3 additions & 1 deletion cmd/daemon/controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ func (p *AppPlayer) loadContext(ctx *connectpb.Context, skipTo func(*connectpb.C
p.state.player.PositionAsOfTimestamp = 0

// if we fail to seek, just fallback to the first track
ctxTracks.TrySeek(skipTo)
if err := ctxTracks.TrySeek(skipTo); err != nil {
return fmt.Errorf("failed seeking to track: %w", err)
}

p.state.tracks = ctxTracks
p.state.player.Track = ctxTracks.CurrentTrack()
Expand Down
18 changes: 10 additions & 8 deletions tracks/paged_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type pagedListInterator[T any] struct {

func (i *pagedListInterator[T]) prev() bool {
if i.pos < 0 {
panic("invalid paged list iterator position")
panic(fmt.Sprintf("invalid paged list iterator position: %d", i.pos))
}

if i.pos == 0 {
Expand Down Expand Up @@ -58,7 +58,7 @@ func (i *pagedListInterator[T]) error() error {

func (i *pagedListInterator[T]) get() pagedListItem[T] {
if i.pos < 0 {
panic("invalid paged list iterator position")
panic(fmt.Sprintf("invalid paged list iterator position: %d", i.pos))
}

return i.list.list[i.pos]
Expand All @@ -76,7 +76,7 @@ func newPagedList[T any](pages librespot.PageResolver[T]) *pagedList[T] {

func (l *pagedList[T]) iterHere() *pagedListInterator[T] {
if l.pos < 0 {
panic("invalid paged list position")
panic(fmt.Sprintf("invalid paged list position: %d", l.pos))
}

return &pagedListInterator[T]{list: l, pos: l.pos, err: nil}
Expand All @@ -87,10 +87,12 @@ func (l *pagedList[T]) iterStart() *pagedListInterator[T] {
}

func (l *pagedList[T]) moveStart() error {
if pageIdx, err := l.fetchNextPage(); errors.Is(err, io.EOF) {
return fmt.Errorf("failed moving to start: no more pages as of %d", pageIdx)
} else if err != nil {
return fmt.Errorf("failed moving to start: %w", err)
if len(l.list) == 0 {
if pageIdx, err := l.fetchNextPage(); errors.Is(err, io.EOF) {
return fmt.Errorf("failed moving to start: no more pages as of %d", pageIdx)
} else if err != nil {
return fmt.Errorf("failed moving to start: %w", err)
}
}

l.pos = 0
Expand All @@ -108,7 +110,7 @@ func (l *pagedList[T]) move(iter *pagedListInterator[T]) {

func (l *pagedList[T]) get() pagedListItem[T] {
if l.pos < 0 {
panic("invalid paged list position")
panic(fmt.Sprintf("invalid paged list position: %d", l.pos))
}

return l.list[l.pos]
Expand Down
9 changes: 7 additions & 2 deletions tracks/tracks.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ func (tl *List) Metadata() map[string]string {
return tl.ctx.Metadata()
}

func (tl *List) TrySeek(f func(track *connectpb.ContextTrack) bool) {
func (tl *List) TrySeek(f func(track *connectpb.ContextTrack) bool) error {
if err := tl.Seek(f); err != nil {
_ = tl.tracks.moveStart()
err = tl.tracks.moveStart()
if err != nil {
return err
}
}

return nil
}

func (tl *List) Seek(f func(*connectpb.ContextTrack) bool) error {
Expand Down

0 comments on commit 4d530d3

Please sign in to comment.