Skip to content

Commit d2954a3

Browse files
Updated src/content/blog/terminal-applications-in-go.mdx
1 parent 8cc20fb commit d2954a3

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

src/content/blog/terminal-applications-in-go.mdx

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -500,22 +500,20 @@ We can do this by calling the selector's `Update` method _first_ regardless of t
500500
```go title="app/main.go"
501501
func (m MainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
502502
503+
var cmds = []tea.Cmd{}
504+
503505
/* Possibly update the selector. If the selectorCmd
504-
is non-null it means that we want to process that event first
505-
and re-run our event loop */
506-
updatedSelector, selectorCmd := m.selector.Update(msg)
507-
m.selector = updatedSelector
508-
if selectorCmd != nil {
509-
return m, selectorCmd
510-
}
506+
is non-null it means that we want to process that event lets
507+
add it to our list of commands */
508+
var cmd tea.Cmd
509+
m.selector, cmd = m.selector.Update(msg)
510+
cmds = append(cmds, cmd)
511511
512512
switch msg := msg.(type) {
513513
/* ...all other events */
514514
```
515515
516-
I like this pattern because it encapsulates all of the selector logic in one place. The downside is that you may be updating the child component unnecessarily (versus if you only called it for the correct keystrokes). This is in my opinion a good tradeoff.
517-
518-
If the keypress triggers a command (like a selection) we return that command as well so that the event loop can restart.
516+
I like this pattern because it encapsulates all of the selector logic in one place. The downside is that you may be calling the Update method unnecessarily (versus if you only called it for the correct keystrokes). This is in my opinion a good tradeoff.
519517
520518
#### Handling Messages from the Selector
521519
@@ -529,11 +527,11 @@ func (m MainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
529527
return m, tea.Quit
530528
}
531529
532-
updatedSelector, selectorCmd := m.selector.maybeUpdate(msg)
533-
m.selector = updatedSelector
534-
if selectorCmd != nil {
535-
return m, selectorCmd
536-
}
530+
/* Handle possible commands by selector */
531+
var cmd tea.Cmd
532+
m.selector, cmd = m.selector.Update(msg)
533+
cmds = append(cmds, cmd)
534+
537535
538536
/* All other events */
539537
switch msg := msg.(type) {
@@ -575,14 +573,12 @@ But the foundation for more complexity is now in place. Let's handle that next.
575573
576574
Let's add the following `case` our our `Update` method in our main model, which will handle the `selectMsg` message type when someone makes a selection.
577575
578-
```go {10-17} title="app/main.go"
576+
```go {8-15} title="app/main.go"
579577
func (m MainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
580578
581-
updatedSelector, selectorCmd := m.selector.maybeUpdate(msg)
582-
m.selector = updatedSelector
583-
if selectorCmd != nil {
584-
return m, selectorCmd
585-
}
579+
var cmd tea.Cmd
580+
m.selector, cmd = m.selector.Update(msg)
581+
cmds = append(cmds, cmd)
586582
587583
switch msg := msg.(type) {
588584
case selectMsg:

0 commit comments

Comments
 (0)