ccapndave/elm-update-extra/2.3.1

NOTE: this package is not compatible with Elm 0.19.1

Update.Extra

Convenience functions for working with updates in Elm

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

Allows update call composition. Can be used with the pipeline operator (|>) to chain updates.

For example:

update msg model =
  model ! []
    |> andThen update SomeMessage
    |> andThen update SomeOtherMessage
    |> andThen update (MessageWithArguments "Hello")
    ...

The same can be achieved using Update.Extra.Infix.(:>).

For example:

import Update.Extra.Infix exposing ((:>))


update msg model =
  model ! []
    :> update SomeMessage
    :> update SomeOtherMessage
    :> update (MessageWithArguments "Hello")
Bool
-> (( model, Cmd msg ) -> ( model, Cmd msg ))
-> ( model, Cmd msg )
-> ( model, Cmd msg )

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)
updateModel : (model -> model) -> ( model, Cmd msg ) -> ( model, Cmd msg )

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 }
addCmd : Cmd msg -> ( model, Cmd msg ) -> ( model, Cmd msg )

Allows you to attach a Cmd to an update pipeline.

For example:

update msg model = model ! []
  |> andThen update AMessage
  |> addCmd doSomethingWithASideEffect
mapCmd : (msg -> msg_) -> ( model, Cmd msg ) -> ( model, Cmd msg_ )

Map over the Cmd in an update pipeline

(msg -> model -> ( model, Cmd msg ))
-> List msg
-> ( model, Cmd msg )
-> ( model, Cmd msg )

Allows you to attach multiple messages to an update at once.

For example:

update msg model = model ! []
  |> sequence update
    [ AMessage
    , AnotherMessage
    , AThirdMessage
    ]
identity : model -> ( model, Cmd msg )

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.identity

This 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