Skip to content

Commit

Permalink
Add Cmd.Extra.andThen (#66)
Browse files Browse the repository at this point in the history
* Add Cmd.Extra.andThen

* elm-format
  • Loading branch information
Janiczek authored Oct 2, 2024
1 parent 57fa91a commit e9e54c7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs.json

Large diffs are not rendered by default.

23 changes: 21 additions & 2 deletions src/Cmd/Extra.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Cmd.Extra exposing
( perform, attempt, maybe, fromResult, fromMaybe
, pure, with, add, withTrigger, addTrigger, addIf, addTriggerMaybe, addMaybe
, pure, andThen, with, add, withTrigger, addTrigger, addIf, addTriggerMaybe, addMaybe
)

{-| Extra functions for working with Cmds.
Expand All @@ -13,7 +13,7 @@ module Cmd.Extra exposing
# Chaining in update
@docs pure, with, add, withTrigger, addTrigger, addIf, addTriggerMaybe, addMaybe
@docs pure, andThen, with, add, withTrigger, addTrigger, addIf, addTriggerMaybe, addMaybe
-}

Expand Down Expand Up @@ -138,6 +138,25 @@ pure model =
( model, Cmd.none )


{-| Allows chaining `update`-like functions.
sendNotification : Model -> (Model, Cmd Msg)
fireZeMissiles : Model -> (Model, Cmd Msg)
model
|> sendNotification -- we have (Model, Cmd Msg) now, but fireZeMissiles needs a Model
|> andThen fireZeMissiles
-}
andThen : (model1 -> ( model2, Cmd msg )) -> ( model1, Cmd msg ) -> ( model2, Cmd msg )
andThen fn ( model, cmd ) =
let
( newModel, newCmd ) =
fn model
in
( newModel, Cmd.batch [ cmd, newCmd ] )


{-| Add Cmd to model to create a pair.
-}
with : Cmd msg -> model -> ( model, Cmd msg )
Expand Down

0 comments on commit e9e54c7

Please sign in to comment.