Merge pull request #848 from wolfmagnate/fix-nested-cmd-order

This commit is contained in:
Ayman Bagabas 2025-09-11 14:49:14 -04:00 committed by GitHub
commit ffa0502190
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 110 additions and 64 deletions

View file

@ -5,6 +5,7 @@ package main
import (
"fmt"
"os"
"time"
tea "github.com/charmbracelet/bubbletea"
)
@ -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:

122
tea.go
View file

@ -471,60 +471,12 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {
p.exec(msg.cmd, msg.fn)
case BatchMsg:
for _, cmd := range msg {
if cmd == nil {
continue
}
select {
case <-p.ctx.Done():
return model, nil
case 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()
switch msg := msg.(type) {
case BatchMsg:
var wg sync.WaitGroup
for _, cmd := range msg {
if cmd == nil {
continue
}
wg.Add(1)
cmd := cmd
go func() {
defer wg.Done()
p.Send(cmd())
}()
}
wg.Wait()
case sequenceMsg:
for _, cmd := range msg {
if cmd == nil {
continue
}
p.Send(cmd())
}
default:
p.Send(msg)
}
}
}()
case setWindowTitleMsg:
p.SetWindowTitle(string(msg))
case windowSizeMsg:
go p.checkResize()
go p.execSequenceMsg(msg)
continue
}
// Process internal messages for the renderer.
@ -546,6 +498,74 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {
}
}
func (p *Program) execSequenceMsg(msg sequenceMsg) {
if !p.startupOptions.has(withoutCatchPanics) {
defer func() {
if r := recover(); r != nil {
p.recoverFromGoPanic(r)
}
}()
}
// 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) {
if !p.startupOptions.has(withoutCatchPanics) {
defer func() {
if r := recover(); r != nil {
p.recoverFromGoPanic(r)
}
}()
}
// Execute commands one at a time.
var wg sync.WaitGroup
for _, cmd := range msg {
if cmd == nil {
continue
}
wg.Add(1)
go func() {
defer wg.Done()
if !p.startupOptions.has(withoutCatchPanics) {
defer func() {
if r := recover(); r != nil {
p.recoverFromGoPanic(r)
}
}()
}
msg := cmd()
switch msg := msg.(type) {
case BatchMsg:
p.execBatchMsg(msg)
case sequenceMsg:
p.execSequenceMsg(msg)
default:
p.Send(msg)
}
}()
}
wg.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.

View file

@ -18,6 +18,10 @@ type incrementMsg struct{}
type panicMsg struct{}
func panicCmd() Msg {
panic("testing goroutine panic behavior")
}
type testModel struct {
executed atomic.Value
counter atomic.Value
@ -434,14 +438,14 @@ func TestTeaNestedSequenceMsg(t *testing.T) {
m := &testModel{}
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
go p.Send(sequenceMsg{inc, Sequence(inc, inc), Quit})
go p.Send(sequenceMsg{inc, Sequence(inc, inc, Batch(inc, inc)), Quit})
if _, err := p.Run(); err != nil {
t.Fatal(err)
}
if m.counter.Load() != 3 {
t.Fatalf("counter should be 3, got %d", m.counter.Load())
if m.counter.Load() != 5 {
t.Fatalf("counter should be 5, got %d", m.counter.Load())
}
}
@ -502,10 +506,6 @@ func TestTeaGoroutinePanic(t *testing.T) {
var buf bytes.Buffer
var in bytes.Buffer
panicCmd := func() Msg {
panic("testing goroutine panic behavior")
}
m := &testModel{}
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
go func() {
@ -513,8 +513,9 @@ func TestTeaGoroutinePanic(t *testing.T) {
time.Sleep(time.Millisecond)
if m.executed.Load() != nil {
batch := make(BatchMsg, 10)
for i := range batch {
batch[i] = panicCmd
for i := 0; i < len(batch); i += 2 {
batch[i] = Sequence(panicCmd)
batch[i+1] = Batch(panicCmd)
}
p.Send(batch)
return