Add nil checks to CmdMap and SubMap functors

This commit is contained in:
Christian Rocha 2020-04-10 15:43:18 -04:00
parent 4a41a61919
commit 27416e9976
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
1 changed files with 8 additions and 1 deletions

9
tea.go
View File

@ -9,7 +9,8 @@ import (
"github.com/muesli/termenv" "github.com/muesli/termenv"
) )
// Msg represents an action. It's used signal Update to update the UI. // Msg represents an action and is usually the result of an IO operation. It's
// used to signal Update this model, and henceforth, the UI.
type Msg interface{} type Msg interface{}
// Model contains the updatable data for an application // Model contains the updatable data for an application
@ -20,6 +21,9 @@ type Cmd func(Model) Msg
// CmdMap applies a given model to a command // CmdMap applies a given model to a command
func CmdMap(cmd Cmd, model Model) Cmd { func CmdMap(cmd Cmd, model Model) Cmd {
if cmd == nil {
return nil
}
return func(_ Model) Msg { return func(_ Model) Msg {
return cmd(model) return cmd(model)
} }
@ -42,6 +46,9 @@ type Sub func(Model) Msg
// SubMap applies a given model to a subscription // SubMap applies a given model to a subscription
func SubMap(sub Sub, model Model) Sub { func SubMap(sub Sub, model Model) Sub {
if sub == nil {
return nil
}
return func(_ Model) Msg { return func(_ Model) Msg {
return sub(model) return sub(model)
} }