forked from Mirrors/bubbletea
Built-in subscriptions are clearer without the extra type definition
This commit is contained in:
parent
b6eeef2127
commit
767f4bec2d
|
@ -4,16 +4,10 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewEveryMsg is used by Every to create a new message. It contains the time
|
|
||||||
// at which the timer finished.
|
|
||||||
type NewEveryMsg func(time.Time) Msg
|
|
||||||
|
|
||||||
// Every is a subscription that ticks with the system clock at the given
|
// Every is a subscription that ticks with the system clock at the given
|
||||||
// duration, similar to cron. It's particularly useful if you have several
|
// duration, similar to cron. It's particularly useful if you have several
|
||||||
// subscriptions that need to run in sync.
|
// subscriptions that need to run in sync.
|
||||||
//
|
func Every(duration time.Duration, newMsg func(time.Time) Msg) Sub {
|
||||||
// TODO: make it cancelable.
|
|
||||||
func Every(duration time.Duration, newMsg NewEveryMsg) Sub {
|
|
||||||
return func() Msg {
|
return func() Msg {
|
||||||
n := time.Now()
|
n := time.Now()
|
||||||
d := n.Truncate(duration).Add(duration).Sub(n)
|
d := n.Truncate(duration).Add(duration).Sub(n)
|
||||||
|
@ -27,9 +21,7 @@ func Every(duration time.Duration, newMsg NewEveryMsg) Sub {
|
||||||
|
|
||||||
// Tick is a subscription that at an interval independent of the system clock
|
// Tick is a subscription that at an interval independent of the system clock
|
||||||
// at the given duration. That is, it begins precisely when invoked.
|
// at the given duration. That is, it begins precisely when invoked.
|
||||||
//
|
func Tick(d time.Duration, newMsg func(time.Time) Msg) Sub {
|
||||||
// TODO: make it cancelable.
|
|
||||||
func Tick(d time.Duration, newMsg NewEveryMsg) Sub {
|
|
||||||
return func() Msg {
|
return func() Msg {
|
||||||
t := time.NewTimer(d)
|
t := time.NewTimer(d)
|
||||||
select {
|
select {
|
||||||
|
|
Loading…
Reference in New Issue