mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-17 10:35:28 -07:00
Built-in subscriptions are clearer without the extra type definition
This commit is contained in:
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user