From 3d3b3fbf7b47e90941a71c5b3a8373991a180118 Mon Sep 17 00:00:00 2001 From: Nathan Wagner Date: Mon, 5 Feb 2024 21:42:01 +0000 Subject: [PATCH] new playAnimationUntil that stops animation when notify channel receives message --- toolkit.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/toolkit.go b/toolkit.go index f5500d7..906314a 100644 --- a/toolkit.go +++ b/toolkit.go @@ -64,7 +64,6 @@ func (tk *ToolKit) PlayAnimation(a Animation) error { return err } } - if err == io.EOF { return nil } @@ -72,6 +71,35 @@ func (tk *ToolKit) PlayAnimation(a Animation) error { return err } +func (tk *ToolKit) PlayAnimationUntil(a Animation, notify <-chan bool) error { + + var err error + var i image.Image + var n <-chan time.Time + var outerError error + for outerError == nil { + select { + case <-notify: + return nil + default: + i, n, err = a.Next() + if err != nil { + outerError = err + break + } + if err := tk.PlayImageUntil(i, n); err != nil { + return err + } + } + } + if outerError == io.EOF { + return nil + } + + return err + +} + // PlayImageUntil draws the given image until is notified to stop func (tk *ToolKit) PlayImageUntil(i image.Image, notify <-chan time.Time) error { defer func() {