Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: properly call nested sequenceMsg and batchMsg #848

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 29 additions & 4 deletions examples/sequence/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main
import (
"fmt"
"os"
"time"

tea "github.com/charmbracelet/bubbletea"
)
Expand All @@ -14,15 +15,39 @@ type model struct{}
func (m model) Init() tea.Cmd {
return tea.Sequence(
tea.Batch(
tea.Println("A"),
tea.Println("B"),
tea.Println("C"),
tea.Sequence(
SleepPrintln("1-1-1", 1000),
SleepPrintln("1-1-2", 1000),
),
tea.Batch(
SleepPrintln("1-2-1", 1500),
SleepPrintln("1-2-2", 1250),
),
),
tea.Println("2"),
tea.Sequence(
tea.Batch(
SleepPrintln("3-1-1", 500),
SleepPrintln("3-1-2", 1000),
),
tea.Sequence(
SleepPrintln("3-2-1", 750),
SleepPrintln("3-2-2", 500),
),
),
tea.Println("Z"),
tea.Quit,
)
}

// print string after stopping for a certain period of time
func SleepPrintln(s string, milisecond int) tea.Cmd {
printCmd := tea.Println(s)
return func() tea.Msg {
time.Sleep(time.Duration(milisecond) * time.Millisecond)
return printCmd()
}
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.(type) {
case tea.KeyMsg:
Expand Down
76 changes: 47 additions & 29 deletions tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,38 +355,12 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {
p.exec(msg.cmd, msg.fn)

case BatchMsg:
for _, cmd := range msg {
cmds <- cmd
}
go p.execBatchMsg(msg)
continue

case sequenceMsg:
go func() {
// Execute commands one at a time, in order.
for _, cmd := range msg {
if cmd == nil {
continue
}

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
}

p.Send(msg)
}
}()
go p.execSequenceMsg(msg)
continue
}

// Process internal messages for the renderer.
Expand All @@ -402,6 +376,50 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {
}
}

func (p *Program) execSequenceMsg(msg sequenceMsg) {
// Execute commands one at a time, in order.
for _, cmd := range msg {
if cmd == nil {
continue
}
msg := cmd()
switch msg := msg.(type) {
case BatchMsg:
p.execBatchMsg(msg)
case sequenceMsg:
p.execSequenceMsg(msg)
default:
p.Send(msg)
}
}
}

func (p *Program) execBatchMsg(msg BatchMsg) {
// Execute commands one at a time.
g, _ := errgroup.WithContext(p.ctx)
for _, cmd := range msg {
cmd := cmd
if cmd == nil {
continue
}
g.Go(func() error {
msg := cmd()
switch msg := msg.(type) {
case BatchMsg:
p.execBatchMsg(msg)
case sequenceMsg:
p.execSequenceMsg(msg)
default:
p.Send(msg)
}
return nil
})
}

//nolint:errcheck
g.Wait() // wait for all commands from batch msg to finish
}

// Run initializes the program and runs its event loops, blocking until it gets
// terminated by either [Program.Quit], [Program.Kill], or its signal handler.
// Returns the final model.
Expand Down