From 0c937f47b83acb05cec69cd6a2ff735809affdad Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Fri, 3 May 2024 09:23:29 -0300 Subject: [PATCH] fix: stop and drain timers (#993) Signed-off-by: Carlos Alexandro Becker --- commands.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/commands.go b/commands.go index 164dad21ad..2541bea0e4 100644 --- a/commands.go +++ b/commands.go @@ -94,11 +94,16 @@ type sequenceMsg []Cmd // // Every is analogous to Tick in the Elm Architecture. func Every(duration time.Duration, fn func(time.Time) Msg) Cmd { + n := time.Now() + d := n.Truncate(duration).Add(duration).Sub(n) + t := time.NewTimer(d) return func() Msg { - n := time.Now() - d := n.Truncate(duration).Add(duration).Sub(n) - t := time.NewTimer(d) - return fn(<-t.C) + ts := <-t.C + t.Stop() + for len(t.C) > 0 { + <-t.C + } + return fn(ts) } } @@ -141,9 +146,14 @@ func Every(duration time.Duration, fn func(time.Time) Msg) Cmd { // return m, nil // } func Tick(d time.Duration, fn func(time.Time) Msg) Cmd { + t := time.NewTimer(d) return func() Msg { - t := time.NewTimer(d) - return fn(<-t.C) + ts := <-t.C + t.Stop() + for len(t.C) > 0 { + <-t.C + } + return fn(ts) } }