fix: handle nested SequenceMsg in event loop and use sync.WaitGroup f… (#1463)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix: handle nested SequenceMsg in event loop and use sync.WaitGroup for BatchMsg This simplifies the handling of nested BatchMsg within sequenceMsg and ensures that all commands in a BatchMsg are executed before proceeding. It also replaces errgroup with sync.WaitGroup for better clarity and performance when waiting for multiple goroutines to finish. Fixes: https://github.com/charmbracelet/bubbletea/issues/680 Supersedes: https://github.com/charmbracelet/bubbletea/pull/843 * chore: examples: bump dependencies
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+51 added−26 removed
- other:+51 added−26 removed
Provenance
- Commit
f5da8d0- Tree
7401312- Author
Ayman Bagabas <ayman.bagabas@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
c76509a
4 files changed+51 added−26 removed
modified:examples/go.mod+2 added-3 removed
21 unmodified lines22 github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect23 github.com/aymanbagabas/go-udiff v0.2.0 // indirect24 github.com/aymerick/douceur v0.2.0 // indirect25 github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc // indirect26- github.com/charmbracelet/x/ansi v0.9.2 // indirect26+ github.com/charmbracelet/x/ansi v0.9.3 // indirect27 github.com/charmbracelet/x/cellbuf v0.0.13 // indirect28 github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91 // indirect29 github.com/charmbracelet/x/exp/slice v0.0.0-20250327172914-2fdc97757edf // indirect30 github.com/charmbracelet/x/term v0.2.1 // indirect13 unmodified lines44 github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect45 github.com/yuin/goldmark v1.7.8 // indirect46 github.com/yuin/goldmark-emoji v1.0.5 // indirect47 golang.org/x/net v0.38.0 // indirect48- golang.org/x/sync v0.14.0 // indirect49- golang.org/x/sys v0.33.0 // indirect48+ golang.org/x/sys v0.34.0 // indirect49 golang.org/x/term v0.31.0 // indirect50 golang.org/x/text v0.24.0 // indirect51 )52 1 unmodified linemodified:examples/go.sum+4 added-6 removed
22 unmodified lines23 github.com/charmbracelet/harmonica v0.2.0 h1:8NxJWRWg/bzKqqEaaeFNipOu77YR5t8aSwG4pgaUBiQ=24 github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=25 github.com/charmbracelet/lipgloss v1.1.1-0.20250404203927-76690c660834 h1:ZR7e0ro+SZZiIZD7msJyA+NjkCNNavuiPBLgerbOziE=26 github.com/charmbracelet/lipgloss v1.1.1-0.20250404203927-76690c660834/go.mod h1:aKC/t2arECF6rNOnaKaVU6y4t4ZeHQzqfxedE/VkVhA=27-github.com/charmbracelet/x/ansi v0.9.2 h1:92AGsQmNTRMzuzHEYfCdjQeUzTrgE1vfO5/7fEVoXdY=28-github.com/charmbracelet/x/ansi v0.9.2/go.mod h1:3RQDQ6lDnROptfpWuUVIUG64bD2g2BgntdxH0Ya5TeE=27+github.com/charmbracelet/x/ansi v0.9.3 h1:BXt5DHS/MKF+LjuK4huWrC6NCvHtexww7dMayh6GXd0=28+github.com/charmbracelet/x/ansi v0.9.3/go.mod h1:3RQDQ6lDnROptfpWuUVIUG64bD2g2BgntdxH0Ya5TeE=29 github.com/charmbracelet/x/cellbuf v0.0.13 h1:/KBBKHuVRbq1lYx5BzEHBAFBP8VcQzJejZ/IA3iR28k=30 github.com/charmbracelet/x/cellbuf v0.0.13/go.mod h1:xe0nKWGd3eJgtqZRaN9RjMtK7xUYchjzPr7q6kcvCCs=31 github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91 h1:payRxjMjKgx2PaCWLZ4p3ro9y97+TVLZNaRZgJwSVDQ=32 github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=52 unmodified lines85 golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=86 golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=87 golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=88 golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=89-golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=90-golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=89 golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=90 golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=93-golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=94-golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=91+golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=92+golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=93 golang.org/x/term v0.31.0 h1:erwDkOK1Msy6offm1mOgvspSkslFnIGsFnxOKoufg3o=94 golang.org/x/term v0.31.0/go.mod h1:R4BeIy7D95HzImkxGkTW1UQTtP54tio2RyHz7PwK0aw=95 golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=96 golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=modified:tea_test.go+21 added
422 unmodified lines423 t.Fatalf("counter should be 3, got %d", m.counter.Load())424 }425 }426 427+func TestTeaNestedSequenceMsg(t *testing.T) {428+ var buf bytes.Buffer429+ var in bytes.Buffer430+431+ inc := func() Msg {432+ return incrementMsg{}433+ }434+435+ m := &testModel{}436+ p := NewProgram(m, WithInput(&in), WithOutput(&buf))437+ go p.Send(sequenceMsg{inc, Sequence(inc, inc), Quit})438+439+ if _, err := p.Run(); err != nil {440+ t.Fatal(err)441+ }442+443+ if m.counter.Load() != 3 {444+ t.Fatalf("counter should be 3, got %d", m.counter.Load())445+ }446+}447+448 func TestTeaSend(t *testing.T) {449 var buf bytes.Buffer450 var in bytes.Buffer451 83 unmodified linesmodified:tea.go+24 added-17 removed
22 unmodified lines23 "syscall"24 25 "github.com/charmbracelet/x/term"26 "github.com/muesli/cancelreader"27- "golang.org/x/sync/errgroup"27 )28 29 // ErrProgramPanic is returned by [Program.Run] when the program recovers from a panic.30 var ErrProgramPanic = errors.New("program experienced a panic")440 unmodified lines471 p.exec(msg.cmd, msg.fn)472 473 case BatchMsg:474 for _, cmd := range msg {476- select {477- case <-p.ctx.Done():478- return model, nil479- case cmds <- cmd:475+ if cmd == nil {476+ continue477 }478+ go p.Send(cmd())479 }480 continue481 482 case sequenceMsg:4 unmodified lines487 continue488 }489 490 msg := cmd()493- if batchMsg, ok := msg.(BatchMsg); ok {494- g, _ := errgroup.WithContext(p.ctx)495- for _, cmd := range batchMsg {491+ switch msg := msg.(type) {492+ case BatchMsg:493+ var wg sync.WaitGroup494+ for _, cmd := range msg {495+ if cmd == nil {496+ continue497+ }498+ wg.Add(1)499 cmd := cmd497- g.Go(func() error {500+ go func() {501+ defer wg.Done()502 p.Send(cmd())499- return nil500- })503+ }()504 }502-503- //nolint:errcheck,gosec504- g.Wait() // wait for all commands from batch msg to finish505- continue505+ wg.Wait()506+ case sequenceMsg:507+ for _, cmd := range msg {508+ if cmd == nil {509+ continue510+ }511+ p.Send(cmd())512+ }513+ default:514+ p.Send(msg)515 }507-508- p.Send(msg)516 }517 }()518 519 case setWindowTitleMsg:392 unmodified lines