Cmds no longer takes a Model as an argument, CmdMap no longer needed

This commit is contained in:
Christian Rocha 2020-04-22 10:15:04 -04:00
parent 5612c85b72
commit 4351c9f903
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
2 changed files with 11 additions and 20 deletions

View File

@ -65,15 +65,16 @@ func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
func view(model tea.Model) string { func view(model tea.Model) string {
m, _ := model.(Model) m, _ := model.(Model)
s := fmt.Sprintf("Checking %s...", url)
if m.Error != nil { if m.Error != nil {
return fmt.Sprintf("Something went wrong: %s", m.Error) s += fmt.Sprintf("something went wrong: %s", m.Error)
} else if m.Status == 0 { } else if m.Status != 0 {
return fmt.Sprintf("Checking %s...", url) 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{ c := &http.Client{
Timeout: 10 * time.Second, Timeout: 10 * time.Second,
} }

20
tea.go
View File

@ -16,18 +16,8 @@ type Msg interface{}
// Model contains the program's state. // Model contains the program's state.
type Model interface{} type Model interface{}
// Cmd is an IO operation. If it's nil it's considered a no-op. // Cmd is an IO operation that runs once. If it's nil it's considered a no-op.
type Cmd func(Model) Msg type Cmd func() 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)
}
}
// Batch peforms a bunch of commands concurrently with no ordering guarantees // Batch peforms a bunch of commands concurrently with no ordering guarantees
// about the results. // about the results.
@ -35,7 +25,7 @@ func Batch(cmds ...Cmd) Cmd {
if len(cmds) == 0 { if len(cmds) == 0 {
return nil return nil
} }
return func(_ Model) Msg { return func() Msg {
return batchMsg(cmds) return batchMsg(cmds)
} }
} }
@ -132,7 +122,7 @@ func NewErrMsgFromErr(e error) ErrMsg {
var ModelAssertionErr = NewErrMsg("could not perform assertion on model") var ModelAssertionErr = NewErrMsg("could not perform assertion on model")
// Quit is a command that tells the program to exit // Quit is a command that tells the program to exit
func Quit(_ Model) Msg { func Quit() Msg {
return quitMsg{} return quitMsg{}
} }
@ -202,7 +192,7 @@ func (p *Program) Start() error {
case cmd := <-cmds: case cmd := <-cmds:
if cmd != nil { if cmd != nil {
go func() { go func() {
msgs <- cmd(p.model) msgs <- cmd()
}() }()
} }
} }