Skip to content

Commit

Permalink
fix(jukebox): make sure we clean up "seekable" event listener
Browse files Browse the repository at this point in the history
related #411
  • Loading branch information
sentriz committed Nov 25, 2023
1 parent 60c1fb1 commit b199bc1
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions jukebox/jukebox.go
Expand Up @@ -36,9 +36,8 @@ func MPVArg(k string, v any) string {
}

type Jukebox struct {
cmd *exec.Cmd
conn *mpvipc.Connection
events <-chan *mpvipc.Event
cmd *exec.Cmd
conn *mpvipc.Connection

mu sync.RWMutex
}
Expand Down Expand Up @@ -87,7 +86,6 @@ func (j *Jukebox) Start(sockPath string, mpvExtraArgs []string) error {
if _, err := j.conn.Call("observe_property", 0, "seekable"); err != nil {
return fmt.Errorf("observe property: %w", err)
}
j.events, _ = j.conn.NewEventListener()

return nil
}
Expand Down Expand Up @@ -205,20 +203,24 @@ func (j *Jukebox) SkipToPlaylistIndex(i int, offsetSecs int) error {
}
}

ch, stop := j.conn.NewEventListener()
defer close(stop)

if _, err := j.conn.Call("playlist-play-index", i); err != nil {
return fmt.Errorf("playlist play index: %w", err)
}

if offsetSecs > 0 {
if err := waitFor(j.events, matchEventSeekable); err != nil {
if err := waitFor(ch, matchEventSeekable); err != nil {
return fmt.Errorf("waiting for file load: %w", err)
}
if _, err := j.conn.Call("seek", offsetSecs, "absolute"); err != nil {
return fmt.Errorf("seek: %w", err)
}
if err := j.conn.Set("pause", false); err != nil {
return fmt.Errorf("play: %w", err)
}
}

if err := j.conn.Set("pause", false); err != nil {
return fmt.Errorf("play: %w", err)
}
return nil
}
Expand Down Expand Up @@ -370,8 +372,6 @@ func waitFor[T any](ch <-chan T, match func(e T) bool) error {
quit := time.NewTicker(1 * time.Second)
defer quit.Stop()

defer time.Sleep(350 * time.Millisecond)

for {
select {
case <-quit.C:
Expand Down

0 comments on commit b199bc1

Please sign in to comment.