From 4351c9f9037e4f57b830526d2167a8e46388641a Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Wed, 22 Apr 2020 10:15:04 -0400 Subject: [PATCH] Cmds no longer takes a Model as an argument, CmdMap no longer needed --- examples/http/main.go | 11 ++++++----- tea.go | 20 +++++--------------- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/examples/http/main.go b/examples/http/main.go index 73ec44c..68eef2a 100644 --- a/examples/http/main.go +++ b/examples/http/main.go @@ -65,15 +65,16 @@ func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) { func view(model tea.Model) string { m, _ := model.(Model) + s := fmt.Sprintf("Checking %s...", url) if m.Error != nil { - return fmt.Sprintf("Something went wrong: %s", m.Error) - } else if m.Status == 0 { - return fmt.Sprintf("Checking %s...", url) + s += fmt.Sprintf("something went wrong: %s", m.Error) + } else if m.Status != 0 { + s += fmt.Sprintf("%d %s", m.Status, http.StatusText(m.Status)) } - return fmt.Sprintf("The server said: %d %s", m.Status, http.StatusText(m.Status)) + return s } -func checkServer(_ tea.Model) tea.Msg { +func checkServer() tea.Msg { c := &http.Client{ Timeout: 10 * time.Second, } diff --git a/tea.go b/tea.go index 8c3479b..68d14e6 100644 --- a/tea.go +++ b/tea.go @@ -16,18 +16,8 @@ type Msg interface{} // Model contains the program's state. type Model interface{} -// Cmd is an IO operation. If it's nil it's considered a no-op. -type Cmd func(Model) Msg - -// CmdMap applies a given model to a command -func CmdMap(cmd Cmd, model Model) Cmd { - if cmd == nil { - return nil - } - return func(_ Model) Msg { - return cmd(model) - } -} +// Cmd is an IO operation that runs once. If it's nil it's considered a no-op. +type Cmd func() Msg // Batch peforms a bunch of commands concurrently with no ordering guarantees // about the results. @@ -35,7 +25,7 @@ func Batch(cmds ...Cmd) Cmd { if len(cmds) == 0 { return nil } - return func(_ Model) Msg { + return func() Msg { return batchMsg(cmds) } } @@ -132,7 +122,7 @@ func NewErrMsgFromErr(e error) ErrMsg { var ModelAssertionErr = NewErrMsg("could not perform assertion on model") // Quit is a command that tells the program to exit -func Quit(_ Model) Msg { +func Quit() Msg { return quitMsg{} } @@ -202,7 +192,7 @@ func (p *Program) Start() error { case cmd := <-cmds: if cmd != nil { go func() { - msgs <- cmd(p.model) + msgs <- cmd() }() } }