Skip to content

Commit

Permalink
switch to atomic.Bool
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Sep 7, 2024
1 parent 65efac0 commit 0d281bc
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions ui/widgets/tracklistloader.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package widgets

import (
"sync/atomic"

"github.com/dweymouth/supersonic/backend/mediaprovider"
)

// Component that manages lazily loading more tracks into a Tracklist
// as the user scrolls near the bottom.
type TracklistLoader struct {
disposed bool
disposed atomic.Bool

tracklist *Tracklist
iter mediaprovider.TrackIterator
Expand All @@ -32,15 +34,15 @@ func NewTracklistLoader(tracklist *Tracklist, iter mediaprovider.TrackIterator)

// Cancels all asynchronous loads so that they will no longer modify the tracklist.
func (t *TracklistLoader) Dispose() {
t.disposed = true
t.disposed.Store(true)
t.tracklist.OnTrackShown = nil
}

func (t *TracklistLoader) onTrackShown(tracknum int) {
if tracknum > t.highestShown {
t.highestShown = tracknum
}
if t.highestShown >= t.len-25 && !t.fetching && !t.done && !t.disposed {
if t.highestShown >= t.len-25 && !t.fetching && !t.done && !t.disposed.Load() {
t.fetching = true
go t.loadMoreTracks(25)
}
Expand All @@ -60,11 +62,11 @@ func (t *TracklistLoader) loadMoreTracks(num int) {
break
}
t.trackBuffer = append(t.trackBuffer, tr)
if t.disposed {
if t.disposed.Load() {
break
}
}
if t.disposed {
if t.disposed.Load() {
return
}
t.tracklist.AppendTracks(t.trackBuffer)
Expand Down

0 comments on commit 0d281bc

Please sign in to comment.