feat: sequence support BatchMsg

This commit is contained in:
Christoffer Åström
2023-02-06 20:44:53 +01:00
committed by Christian Rocha
parent a3dc561104
commit e95e1a0db5
7 changed files with 59 additions and 5 deletions

19
tea.go
View File

@@ -24,6 +24,7 @@ import (
isatty "github.com/mattn/go-isatty"
"github.com/muesli/cancelreader"
"github.com/muesli/termenv"
"golang.org/x/sync/errgroup"
)
// ErrProgramKilled is returned by [Program.Run] when the program got killed.
@@ -313,8 +314,22 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {
if cmd == nil {
continue
}
p.Send(cmd())
msg := cmd()
if batchMsg, ok := msg.(BatchMsg); ok {
g, _ := errgroup.WithContext(p.ctx)
for _, cmd := range batchMsg {
cmd := cmd
g.Go(func() error {
p.Send(cmd())
return nil
})
}
//nolint:errcheck
g.Wait() // wait for all commands from batch msg to finish
continue
} else {
p.Send(msg)
}
}
}()
}