Skip to content

Commit

Permalink
refactor: use tea.Batch in nested model example
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed Aug 5, 2022
1 parent 09cf14f commit fa23d0b
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions examples/nested-models/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func (m mainModel) Init() tea.Cmd {
}

func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
switch msg := msg.(type) {
// Handle IO -> keypress, WindowSizeMSg
case tea.KeyMsg:
Expand All @@ -70,44 +72,40 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} else {
m.state = timerView
}
return m, nil
case "n":
var cmd tea.Cmd
if m.state == timerView {
m.timer = timer.New(defaultTime)
cmd = m.timer.Init()
cmds = append(cmds, m.timer.Init())
} else {
m.Next()
m.resetSpinner()
cmd = spinner.Tick
cmds = append(cmds, spinner.Tick)
}
return m, cmd
}
switch m.state {
// update whichever model is focused
case spinnerView:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
cmds = append(cmds, cmd)
default:
// another way to do it
newModel, cmd := m.timer.Update(msg)
m.timer = newModel
// if this were your own model, you would need to wrap the type
// before assignment because its type would be tea.Model
// i.e. m.list = newModel.(list.Model)
return m, cmd
cmds = append(cmds, cmd)
}
case spinner.TickMsg:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
cmds = append(cmds, cmd)
case timer.TickMsg:
var cmd tea.Cmd
m.timer, cmd = m.timer.Update(msg)
return m, cmd
cmds = append(cmds, cmd)
}
return m, nil
return m, tea.Batch(cmds...)
}

func (m mainModel) View() string {
Expand Down

0 comments on commit fa23d0b

Please sign in to comment.