feat: add Sequence for running commands in order. Closes #413.

This deprecates Sequentially.
This commit is contained in:
Christian Rocha
2022-08-24 11:29:14 -07:00
parent 9fcebd151f
commit 989d49f3e6
3 changed files with 64 additions and 0 deletions

22
tea.go
View File

@@ -532,6 +532,17 @@ func (p *Program) StartReturningModel() (Model, error) {
case execMsg:
// NB: this blocks.
p.exec(msg.cmd, msg.fn)
case sequenceMsg:
go func() {
// Execute commands one at a time, in order.
for _, cmd := range msg {
select {
case p.msgs <- cmd():
case <-p.ctx.Done():
}
}
}()
}
// Process internal messages for the renderer.
@@ -736,3 +747,14 @@ func (p *Program) Printf(template string, args ...interface{}) {
messageBody: fmt.Sprintf(template, args...),
}
}
// sequenceMsg is used interally to run the the given commands in order.
type sequenceMsg []Cmd
// Sequence runs the given commands one at a time, in order. Contrast this with
// Batch, which runs commands concurrently.
func Sequence(cmds ...Cmd) Cmd {
return func() Msg {
return sequenceMsg(cmds)
}
}