GlobalWebIndex/cmd-extra/1.1.0

NOTE: this package is not compatible with Elm 0.19.1

Cmd.Extra

Extra functions for working with Cmds.

Constructors

perform : msg -> Cmd msg

Cmd costructor. Usefull 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 -> 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 -> Cmd msg

Similar to attempt but works with Maybe instead

maybeCmd : Cmd String
maybeCmd =
    maybe <| Just 1


maybe Nothing == Cmd.none
--> True
fromResult : (a -> msg) -> Result x a -> Cmd msg

Construct from Result.

resultCmd : Cmd String
resultCmd =
  fromResult toString (Ok 1)


fromResult toString (Err ())
--> Cmd.none
fromMaybe : (a -> msg) -> Maybe a -> Cmd msg

Construct from Maybe.

stringCmd : Cmd String
stringCmd =
  toString (Just 1)


fromMaybe toString Nothing
--> Cmd.none

Chaining in update

pure : model -> ( model, Cmd msg )

Creates pair model with Cmd.none

pair : ( String, Cmd msg )
pair = pure "foo"


pair
  |> Tuple.second
  |> ((==) Cmd.none)
--> True
with : Cmd msg -> model -> ( model, Cmd msg )

Add Cmd to model to create a pair.

add : Cmd msg -> ( model, Cmd msg ) -> ( model, Cmd msg )

Add new cmd to an existing pair.

withTrigger : msg -> model -> ( model, Cmd msg )

Trigger Cmd from Msg and create a pair

addTrigger : msg -> ( model, Cmd msg ) -> ( model, Cmd msg )

Add new trigger of Msg to an existing pair.