Skip to content

Commit

Permalink
Merge pull request #3 from simonchatts/master
Browse files Browse the repository at this point in the history
Add a couple of convenience functions
  • Loading branch information
etaque committed Jul 2, 2018
2 parents 1944bd4 + 80117b9 commit 7829069
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ type alias Response model action = ( model, Cmd action )
This package offers alternate ways to construct and transform responses:

* `res` and `taskRes` for inline constructs
* `withCmd`, `withTask` and `withNone` for piped (`|>`) constructs
* `withCmd`, `withCmds`, `withTask` and `withNone` for piped (`|>`) constructs
* `mapModel`, `mapCmd` and `mapBoth` for transformations
* `andThen` for sequencing multiple updates

Typical usage examples in `update` function:

Expand Down
2 changes: 1 addition & 1 deletion elm-package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "3.0.0",
"version": "3.1.0",
"summary": "Response utilities for Elm Architecture",
"repository": "https://github.com/etaque/elm-response.git",
"license": "BSD3",
Expand Down
46 changes: 44 additions & 2 deletions src/Response.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ module Response exposing (..)
Response utilities for Elm Architecture. Build responses from tasks, pipe them, map over.
# Construct
@docs Response, res, taskRes, withCmd, withTask, withNone
@docs Response, res, taskRes, withCmd, withCmds, withTask, withNone, pure
# Transform
@docs mapModel, mapCmd, mapBoth
@docs mapModel, mapCmd, mapBoth, andThen
-}

-- import Platform exposing (Cmd, Never)
Expand Down Expand Up @@ -45,6 +45,16 @@ withCmd cmd model =
res model cmd


{-| Construct a result from model and multiple cmds, flipped for piping:
{ model | foo = bar }
|> withCmds [someCmd1, someCmd1]
-}
withCmds : List (Cmd a) -> m -> Response m a
withCmds cmds model =
res model (Cmd.batch cmds)


{-| Construct a result from model and task, flipped for piping:
{ model | foo = bar }
Expand Down Expand Up @@ -84,3 +94,35 @@ mapCmd onCmd =
mapBoth : (m -> n) -> (a -> b) -> Response m a -> Response n b
mapBoth onModel onCmd ( m, fx ) =
res (onModel m) (Cmd.map onCmd fx)


{-| Sequence one update after another.
update1 : Model -> Response Model (Cmd Msg)
update2 : Model -> Response Model (Cmd Msg)
update3 : Model -> Response Model (Cmd Msg)
update1 model
|> andThen update2
|> andThen update3
-}
andThen : (m -> Response m a) -> Response m a -> Response m a
andThen update ( model1, cmd1 ) =
-- For those into abstractions: this can be viewed as considering
-- Response as a Writer monad, with the effect of monoidally
-- aggregating commands along the way.
let
( model2, cmd2 ) =
update model1
in
res model2 (Cmd.batch [ cmd1, cmd2 ])


{-| Synonym for withNone (for those with a personal preference).
pure { model | foo = bar }
-}
pure : m -> Response m a
pure =
-- For those into abstractions: same as for andThen.
withNone

0 comments on commit 7829069

Please sign in to comment.