mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-02 02:59:09 -07:00
Add Tick to run timers independent of the system clock
This commit is contained in:
@@ -9,9 +9,10 @@ import (
|
||||
type NewEveryMsg func(time.Time) Msg
|
||||
|
||||
// Every is a subscription that ticks with the system clock at the given
|
||||
// duration.
|
||||
// duration, similar to cron. It's particularly useful if you have several
|
||||
// subscriptions that need to run in sync.
|
||||
//
|
||||
// TODO: make it cancelable
|
||||
// TODO: make it cancelable.
|
||||
func Every(duration time.Duration, newMsg NewEveryMsg) Sub {
|
||||
return func() Msg {
|
||||
n := time.Now()
|
||||
@@ -23,3 +24,17 @@ func Every(duration time.Duration, newMsg NewEveryMsg) Sub {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Tick is a subscription that at an interval independent of the system clock
|
||||
// at the given duration. That is, it begins precisely when invoked.
|
||||
//
|
||||
// TODO: make it cancelable.
|
||||
func Tick(d time.Duration, newMsg NewEveryMsg) Sub {
|
||||
return func() Msg {
|
||||
t := time.NewTimer(d)
|
||||
select {
|
||||
case now := <-t.C:
|
||||
return newMsg(now)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user