Merge pull request #848 from wolfmagnate/fix-nested-cmd-order
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Ayman Bagabas · committed by GitHub · parents 21eecd5, 9e0e8f0 · Browse files · 3 files·+110 added−64 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+110 added−64 removed
- other:+110 added−64 removed
Provenance
- Commit
ffa0502- Tree
1acbb0c- Author
Ayman Bagabas <ayman.bagabas@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
21eecd5- Parent 2
9e0e8f0
3 files changed+110 added−64 removed
modified:examples/sequence/main.go+29 added-4 removed
3 unmodified lines4 5 import (6 "fmt"7 "os"8+ "time"9 10 tea "github.com/charmbracelet/bubbletea"11 )12 1 unmodified line14 15 func (m model) Init() tea.Cmd {16 return tea.Sequence(17 tea.Batch(17- tea.Println("A"),18- tea.Println("B"),19- tea.Println("C"),18+ tea.Sequence(19+ SleepPrintln("1-1-1", 1000),20+ SleepPrintln("1-1-2", 1000),21+ ),22+ tea.Batch(23+ SleepPrintln("1-2-1", 1500),24+ SleepPrintln("1-2-2", 1250),25+ ),26 ),21- tea.Println("Z"),27+ tea.Println("2"),28+ tea.Sequence(29+ tea.Batch(30+ SleepPrintln("3-1-1", 500),31+ SleepPrintln("3-1-2", 1000),32+ ),33+ tea.Sequence(34+ SleepPrintln("3-2-1", 750),35+ SleepPrintln("3-2-2", 500),36+ ),37+ ),38 tea.Quit,39 )40 }41 42+// print string after stopping for a certain period of time43+func SleepPrintln(s string, milisecond int) tea.Cmd {44+ printCmd := tea.Println(s)45+ return func() tea.Msg {46+ time.Sleep(time.Duration(milisecond) * time.Millisecond)47+ return printCmd()48+ }49+}50+51 func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {52 switch msg.(type) {53 case tea.KeyMsg:54 return m, tea.Quit14 unmodified linesmodified:tea_test.go+10 added-9 removed
16 unmodified lines17 type incrementMsg struct{}18 19 type panicMsg struct{}20 21+func panicCmd() Msg {22+ panic("testing goroutine panic behavior")23+}24+25 type testModel struct {26 executed atomic.Value27 counter atomic.Value28 }408 unmodified lines437 }438 439 m := &testModel{}440 p := NewProgram(m, WithInput(&in), WithOutput(&buf))437- go p.Send(sequenceMsg{inc, Sequence(inc, inc), Quit})441+ go p.Send(sequenceMsg{inc, Sequence(inc, inc, Batch(inc, inc)), Quit})442 443 if _, err := p.Run(); err != nil {444 t.Fatal(err)445 }446 443- if m.counter.Load() != 3 {444- t.Fatalf("counter should be 3, got %d", m.counter.Load())447+ if m.counter.Load() != 5 {448+ t.Fatalf("counter should be 5, got %d", m.counter.Load())449 }450 }451 452 func TestTeaSend(t *testing.T) {52 unmodified lines505 func TestTeaGoroutinePanic(t *testing.T) {506 var buf bytes.Buffer507 var in bytes.Buffer508 505- panicCmd := func() Msg {506- panic("testing goroutine panic behavior")507- }508-509 m := &testModel{}510 p := NewProgram(m, WithInput(&in), WithOutput(&buf))511 go func() {512 for {513 time.Sleep(time.Millisecond)514 if m.executed.Load() != nil {515 batch := make(BatchMsg, 10)516- for i := range batch {517- batch[i] = panicCmd516+ for i := 0; i < len(batch); i += 2 {517+ batch[i] = Sequence(panicCmd)518+ batch[i+1] = Batch(panicCmd)519 }520 p.Send(batch)521 return522 }13 unmodified linesmodified:tea.go+71 added-51 removed
469 unmodified lines470 // NB: this blocks.471 p.exec(msg.cmd, msg.fn)472 473 case BatchMsg:474- for _, cmd := range msg {475- if cmd == nil {476- continue477- }478- select {479- case <-p.ctx.Done():480- return model, nil481- case cmds <- cmd:482- }483- }474+ go p.execBatchMsg(msg)475 continue476 477 case sequenceMsg:487- go func() {488- // Execute commands one at a time, in order.489- for _, cmd := range msg {490- if cmd == nil {491- continue492- }493-494- msg := cmd()495- switch msg := msg.(type) {496- case BatchMsg:497- var wg sync.WaitGroup498- for _, cmd := range msg {499- if cmd == nil {500- continue501- }502- wg.Add(1)503- cmd := cmd504- go func() {505- defer wg.Done()506- p.Send(cmd())507- }()508- }509- wg.Wait()510- case sequenceMsg:511- for _, cmd := range msg {512- if cmd == nil {513- continue514- }515- p.Send(cmd())516- }517- default:518- p.Send(msg)519- }520- }521- }()522-523- case setWindowTitleMsg:524- p.SetWindowTitle(string(msg))525-526- case windowSizeMsg:527- go p.checkResize()478+ go p.execSequenceMsg(msg)479+ continue480 }481 482 // Process internal messages for the renderer.483 if r, ok := p.renderer.(*standardRenderer); ok {13 unmodified lines497 }498 }499 }500 501+func (p *Program) execSequenceMsg(msg sequenceMsg) {502+ if !p.startupOptions.has(withoutCatchPanics) {503+ defer func() {504+ if r := recover(); r != nil {505+ p.recoverFromGoPanic(r)506+ }507+ }()508+ }509+510+ // Execute commands one at a time, in order.511+ for _, cmd := range msg {512+ if cmd == nil {513+ continue514+ }515+ msg := cmd()516+ switch msg := msg.(type) {517+ case BatchMsg:518+ p.execBatchMsg(msg)519+ case sequenceMsg:520+ p.execSequenceMsg(msg)521+ default:522+ p.Send(msg)523+ }524+ }525+}526+527+func (p *Program) execBatchMsg(msg BatchMsg) {528+ if !p.startupOptions.has(withoutCatchPanics) {529+ defer func() {530+ if r := recover(); r != nil {531+ p.recoverFromGoPanic(r)532+ }533+ }()534+ }535+536+ // Execute commands one at a time.537+ var wg sync.WaitGroup538+ for _, cmd := range msg {539+ if cmd == nil {540+ continue541+ }542+ wg.Add(1)543+ go func() {544+ defer wg.Done()545+546+ if !p.startupOptions.has(withoutCatchPanics) {547+ defer func() {548+ if r := recover(); r != nil {549+ p.recoverFromGoPanic(r)550+ }551+ }()552+ }553+554+ msg := cmd()555+ switch msg := msg.(type) {556+ case BatchMsg:557+ p.execBatchMsg(msg)558+ case sequenceMsg:559+ p.execSequenceMsg(msg)560+ default:561+ p.Send(msg)562+ }563+ }()564+ }565+566+ wg.Wait() // wait for all commands from batch msg to finish567+}568+569 // Run initializes the program and runs its event loops, blocking until it gets570 // terminated by either [Program.Quit], [Program.Kill], or its signal handler.571 // Returns the final model.572 func (p *Program) Run() (returnModel Model, returnErr error) {363 unmodified lines