Convenience functions for working with updates in Elm
Allows you to conditionally trigger updates based on a predicate. Can be used with the pipeline operator.
For example:
update msg model =
case msg of
SomeMessage i ->
model ! []
|> filter (i > 10)
( andThen update BiggerThanTen
>> andThen update AnotherMessage
>> andThen update EvenMoreMessages
)
|> andThen (update AlwaysTriggeredAfterPredicate)If you want use to the pipeline operator in the nested pipeline, consider a lambda:
|> filter (i > 10)
( \state -> state
|> andThen update BiggerThanTen
|> andThen update AnotherMessage
|> andThen update EvenMoreMessages
)
|> andThen (update AlwaysTriggeredAfterPredicate)Allows you to update the model in an update pipeline.
For example
update msg model = model ! []
|> updateModel \model -> { model | a = 1 }
|> updateModel \model -> { model | b = 2 }Allows you to attach a Cmd to an update pipeline.
For example:
update msg model = model ! []
|> andThen update AMessage
|> addCmd doSomethingWithASideEffectAllows you to attach multiple messages to an update at once.
For example:
update msg model = model ! []
|> sequence update
[ AMessage
, AnotherMessage
, AThirdMessage
]This implements the identity function with regards to update pipelines. This is designed to be used with the :> operator, allowing you to write elements in the pipeline that do nothing at all.
import Update.Extra as Update
import Update.Extra.Infix exposing ((:>))
update msg model =
model ! []
:> Update.identityThis can be useful when you want to implement paths through the update pipeline without having to create
a Noop Msg. Its especially when working with Maybes, where it can be awkward to use the filter
function in a type-safe way.
import Update.Extra as Update
import Update.Extra.Infix exposing ((:>))
import Maybe.Extra exposing ((?))
type Msg
= UpdateName (Maybe String)
| SetupUser User
update msg model =
case msg of
UpdateName maybeAName ->
let
user : Maybe User
user =
Maybe.map createUser maybeAName
in
{ model | user = user }
:> Maybe.map (update << SetupUser) user ? Update.identity
Allows update call composition. Can be used with the pipeline operator (|>) to chain updates.
For example:
The same can be achieved using
Update.Extra.Infix.(:>).For example: