forked from Mirrors/bubbletea
Add Tick to run timers independent of the system clock
This commit is contained in:
parent
7b887b0a05
commit
2a82e2a75e
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue