forked from wildeyedskies/stmp
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
spezifisch
committed
Oct 25, 2023
1 parent
4c68cee
commit bae1358
Showing
7 changed files
with
222 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type eventLoop struct { | ||
scrobbleTimer *time.Timer | ||
} | ||
|
||
func (ui *Ui) runEventLoops() { | ||
el := &eventLoop{} | ||
ui.eventLoop = el | ||
|
||
// create reused timer to scrobble after delay | ||
el.scrobbleTimer = time.NewTimer(0) | ||
if !el.scrobbleTimer.Stop() { | ||
<-el.scrobbleTimer.C | ||
} | ||
|
||
go ui.guiEventLoop() | ||
go ui.backgroundEventLoop() | ||
} | ||
|
||
func (ui *Ui) guiEventLoop() { | ||
ui.addStarredToList() | ||
|
||
//lint:ignore S1000 // additional cases may be added later | ||
//nolint:gosimple | ||
for { | ||
select { | ||
case msg := <-ui.logger.Prints: | ||
ui.app.QueueUpdate(func() { | ||
ui.logList.AddItem(msg, "", 0, nil) | ||
// Make sure the log list doesn't grow infinitely | ||
for ui.logList.GetItemCount() > 200 { | ||
ui.logList.RemoveItem(0) | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
|
||
// loop for blocking background tasks that would otherwise block the ui | ||
func (ui *Ui) backgroundEventLoop() { | ||
//lint:ignore S1000 // additional cases may be added later | ||
//nolint:gosimple | ||
for { | ||
select { | ||
case <-ui.eventLoop.scrobbleTimer.C: | ||
// scrobble submission delay elapsed | ||
paused, err := ui.player.IsPaused() | ||
ui.logger.Printf("scrobbler event: paused %v, err %v, qlen %d", paused, err, len(ui.player.Queue)) | ||
|
||
isPlaying := err == nil && !paused | ||
// TODO we need some mutexed thing to access ui data | ||
if len(ui.player.Queue) > 0 && isPlaying { | ||
// it's still playing, submit it | ||
currentSong := ui.player.Queue[0] | ||
ui.connection.ScrobbleSubmission(currentSong.Id, true) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.