handleCommands runs commands in a goroutine and sends the result to the program's message channel.
(cmds chan Cmd)
| 748 | // handleCommands runs commands in a goroutine and sends the result to the |
| 749 | // program's message channel. |
| 750 | func (p *Program) handleCommands(cmds chan Cmd) chan struct{} { |
| 751 | ch := make(chan struct{}) |
| 752 | |
| 753 | go func() { |
| 754 | defer close(ch) |
| 755 | |
| 756 | for { |
| 757 | select { |
| 758 | case <-p.ctx.Done(): |
| 759 | return |
| 760 | |
| 761 | case cmd := <-cmds: |
| 762 | if cmd == nil { |
| 763 | continue |
| 764 | } |
| 765 | |
| 766 | // Don't wait on these goroutines, otherwise the shutdown |
| 767 | // latency would get too large as a Cmd can run for some time |
| 768 | // (e.g. tick commands that sleep for half a second). It's not |
| 769 | // possible to cancel them so we'll have to leak the goroutine |
| 770 | // until Cmd returns. |
| 771 | go func() { |
| 772 | // Recover from panics. |
| 773 | if !p.disableCatchPanics { |
| 774 | defer func() { |
| 775 | if r := recover(); r != nil { |
| 776 | p.recoverFromPanic(r) |
| 777 | } |
| 778 | }() |
| 779 | } |
| 780 | |
| 781 | msg := cmd() // this can be long. |
| 782 | p.Send(msg) |
| 783 | }() |
| 784 | } |
| 785 | } |
| 786 | }() |
| 787 | |
| 788 | return ch |
| 789 | } |
| 790 | |
| 791 | // eventLoop is the central message loop. It receives and handles the default |
| 792 | // Bubble Tea messages, update the model and triggers redraws. |
no test coverage detected