Extra functions for working with Cmds.
perform : msg -> Platform.Cmd.Cmd msg
Cmd costructor. Useful when you want to artificially emit Cmd from update function.
performed : Cmd String
performed =
perform "foo"
"real world" example:
type alias Model =
()
type Msg
= Fire
| FireRockets
update : Msg -> Model -> ( Model, Cmd Msg )
update msg () =
case msg of
Fire ->
( (), perform FireRockets )
FireRockets ->
Debug.crash "World ended:("
attempt : Result x msg -> Platform.Cmd.Cmd msg
Similar to perform but takes Result msg
and performs action only on Ok
.
attempted : Cmd String
attempted =
attempt <| Ok "I'm fine"
attempt (Err "Failed") == Cmd.none
--> True
maybe : Maybe msg -> Platform.Cmd.Cmd msg
Similar to attempt but works with Maybe
instead
maybeCmd : Cmd Int
maybeCmd =
maybe <| Just 1
maybe Nothing == Cmd.none
--> True
fromResult : (a -> msg) -> Result x a -> Platform.Cmd.Cmd msg
Construct from Result.
resultCmd : Cmd (Result Never Int)
resultCmd =
fromResult identity (Ok 1)
fromResult identity (Err ())
--> Cmd.none
fromMaybe : (a -> msg) -> Maybe a -> Platform.Cmd.Cmd msg
Construct from Maybe.
maybeCmd : Cmd (Maybe Int)
maybeCmd =
identity (Just 1)
fromMaybe identity Nothing
--> Cmd.none
pure : model -> ( model, Platform.Cmd.Cmd msg )
Creates pair model
with Cmd.none
pair : ( String, Cmd msg )
pair = pure "foo"
pair
|> Tuple.second
|> ((==) Cmd.none)
--> True
with : Platform.Cmd.Cmd msg -> model -> ( model, Platform.Cmd.Cmd msg )
Add Cmd to model to create a pair.
add : Platform.Cmd.Cmd msg -> ( model, Platform.Cmd.Cmd msg ) -> ( model, Platform.Cmd.Cmd msg )
Add new cmd to an existing pair.
withTrigger : msg -> model -> ( model, Platform.Cmd.Cmd msg )
Trigger Cmd from Msg and create a pair
addTrigger : msg -> ( model, Platform.Cmd.Cmd msg ) -> ( model, Platform.Cmd.Cmd msg )
Add new trigger of Msg to an existing pair.
addIf : Basics.Bool -> Platform.Cmd.Cmd msg -> ( model, Platform.Cmd.Cmd msg ) -> ( model, Platform.Cmd.Cmd msg )
Add new cmd to an existing pair under a certain condition.
prevCmd : Cmd String
prevCmd =
perform "foo"
newCmd : Cmd String
newCmd =
perform "bar"
( "model", prevCmd )
|> addIf False newCmd
|> Tuple.second
|> ((==) prevCmd)
--> True
addTriggerMaybe : Maybe msg -> ( model, Platform.Cmd.Cmd msg ) -> ( model, Platform.Cmd.Cmd msg )
addTrigger
if Just, do nothing if Nothing
addMaybe : (a -> Platform.Cmd.Cmd msg) -> Maybe a -> ( model, Platform.Cmd.Cmd msg ) -> ( model, Platform.Cmd.Cmd msg )
Add new cmd to an existing pair based on the Maybe value
prevCmd : Cmd String
prevCmd =
perform "prev"
( "model", prevCmd )
|> addMaybe identity Nothing
|> Tuple.second
|> ((==) prevCmd)
--> True