From 09c36fc49d2f5f3c73915e1ef35dec9111ddf751 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Mon, 17 Apr 2023 11:42:00 -0300 Subject: [PATCH 1/2] feat: tea.Wait wait for the underlying context to finish. extract from #352 --- tea.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tea.go b/tea.go index 76c9b3be55..49eb56660b 100644 --- a/tea.go +++ b/tea.go @@ -545,6 +545,12 @@ func (p *Program) Kill() { p.cancel() } +// Wait waits/blocks until the underlying Program context is done. +// This is mainly useful for testing. +func (p *Program) Wait() { + <-p.ctx.Done() +} + // shutdown performs operations to free up resources and restore the terminal // to its original state. func (p *Program) shutdown(kill bool) { From 01c4fea67bec502b41541cf3493231bb72ee1e92 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Fri, 21 Apr 2023 16:41:19 +0000 Subject: [PATCH 2/2] fix: wait til the end of shutdown Signed-off-by: Carlos Alexandro Becker --- tea.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tea.go b/tea.go index f7132cc06d..e61bb5a6ae 100644 --- a/tea.go +++ b/tea.go @@ -97,8 +97,9 @@ type Program struct { ctx context.Context cancel context.CancelFunc - msgs chan Msg - errs chan error + msgs chan Msg + errs chan error + finished chan struct{} // where to send output, this will usually be os.Stdout. output *termenv.Output @@ -366,6 +367,7 @@ func (p *Program) Run() (Model, error) { handlers := handlers{} cmds := make(chan Cmd) p.errs = make(chan error) + p.finished = make(chan struct{}, 1) defer p.cancel() @@ -555,10 +557,9 @@ func (p *Program) Kill() { p.cancel() } -// Wait waits/blocks until the underlying Program context is done. -// This is mainly useful for testing. +// Wait waits/blocks until the underlying Program finished shutting down. func (p *Program) Wait() { - <-p.ctx.Done() + <-p.finished } // shutdown performs operations to free up resources and restore the terminal @@ -576,6 +577,7 @@ func (p *Program) shutdown(kill bool) { if p.restoreOutput != nil { _ = p.restoreOutput() } + p.finished <- struct{}{} } // ReleaseTerminal restores the original terminal state and cancels the input