summaryrefslogtreecommitdiff
path: root/lib/textui/progress.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-01-30 23:07:13 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-01-30 23:07:13 -0700
commit9eef4dd91c36b60a2d5a68141f1d0c07e25be129 (patch)
tree4754b06e54d7e888e636472007fc1bc87aa72d72 /lib/textui/progress.go
parent0134f07a4b97a455557277b2c89e0ee5ad6b2e62 (diff)
parent50a8b3eac39caccedb3ec34c150ba37e40cc2da5 (diff)
Merge branch 'lukeshu/fast-json'
Diffstat (limited to 'lib/textui/progress.go')
-rw-r--r--lib/textui/progress.go12
1 files changed, 7 insertions, 5 deletions
diff --git a/lib/textui/progress.go b/lib/textui/progress.go
index 68d986f..48a3901 100644
--- a/lib/textui/progress.go
+++ b/lib/textui/progress.go
@@ -7,9 +7,9 @@ package textui
import (
"context"
"fmt"
- "sync/atomic"
"time"
+ "git.lukeshu.com/go/typedsync"
"github.com/datawire/dlib/dlog"
)
@@ -26,7 +26,7 @@ type Progress[T Stats] struct {
cancel context.CancelFunc
done chan struct{}
- cur atomic.Value // Value[T]
+ cur typedsync.Value[T]
oldStat T
oldLine string
}
@@ -45,7 +45,7 @@ func NewProgress[T Stats](ctx context.Context, lvl dlog.LogLevel, interval time.
}
func (p *Progress[T]) Set(val T) {
- if p.cur.Swap(val) == nil {
+ if _, hadOld := p.cur.Swap(val); !hadOld {
go p.run()
}
}
@@ -56,8 +56,10 @@ func (p *Progress[T]) Done() {
}
func (p *Progress[T]) flush(force bool) {
- //nolint:forcetypeassert // It wasn't worth it to me (yet?) to make a typed wrapper around atomic.Value.
- cur := p.cur.Load().(T)
+ cur, ok := p.cur.Load()
+ if !ok {
+ panic("should not happen")
+ }
if !force && cur == p.oldStat {
return
}