switch to atomic.Bool
This commit is contained in:
@@ -1,13 +1,15 @@
|
|||||||
package widgets
|
package widgets
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/dweymouth/supersonic/backend/mediaprovider"
|
"github.com/dweymouth/supersonic/backend/mediaprovider"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Component that manages lazily loading more tracks into a Tracklist
|
// Component that manages lazily loading more tracks into a Tracklist
|
||||||
// as the user scrolls near the bottom.
|
// as the user scrolls near the bottom.
|
||||||
type TracklistLoader struct {
|
type TracklistLoader struct {
|
||||||
disposed bool
|
disposed atomic.Bool
|
||||||
|
|
||||||
tracklist *Tracklist
|
tracklist *Tracklist
|
||||||
iter mediaprovider.TrackIterator
|
iter mediaprovider.TrackIterator
|
||||||
@@ -32,7 +34,7 @@ func NewTracklistLoader(tracklist *Tracklist, iter mediaprovider.TrackIterator)
|
|||||||
|
|
||||||
// Cancels all asynchronous loads so that they will no longer modify the tracklist.
|
// Cancels all asynchronous loads so that they will no longer modify the tracklist.
|
||||||
func (t *TracklistLoader) Dispose() {
|
func (t *TracklistLoader) Dispose() {
|
||||||
t.disposed = true
|
t.disposed.Store(true)
|
||||||
t.tracklist.OnTrackShown = nil
|
t.tracklist.OnTrackShown = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +42,7 @@ func (t *TracklistLoader) onTrackShown(tracknum int) {
|
|||||||
if tracknum > t.highestShown {
|
if tracknum > t.highestShown {
|
||||||
t.highestShown = tracknum
|
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
|
t.fetching = true
|
||||||
go t.loadMoreTracks(25)
|
go t.loadMoreTracks(25)
|
||||||
}
|
}
|
||||||
@@ -60,11 +62,11 @@ func (t *TracklistLoader) loadMoreTracks(num int) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
t.trackBuffer = append(t.trackBuffer, tr)
|
t.trackBuffer = append(t.trackBuffer, tr)
|
||||||
if t.disposed {
|
if t.disposed.Load() {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if t.disposed {
|
if t.disposed.Load() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
t.tracklist.AppendTracks(t.trackBuffer)
|
t.tracklist.AppendTracks(t.trackBuffer)
|
||||||
|
|||||||
Reference in New Issue
Block a user