bubbletea/subscriptions.go

22 lines
359 B
Go
Raw Normal View History

package tea
import (
"time"
)
// Every is a subscription that ticks with the system clock at the given
// duration
//
// TODO: make it cancelable
func Every(duration time.Duration, msg Msg) Sub {
return func() Msg {
n := time.Now()
d := n.Truncate(duration).Add(duration).Sub(n)
t := time.NewTimer(d)
select {
case <-t.C:
return msg
}
}
}