ccapndave / elm-update-extra / Update.Extra

Convenience functions for working with updates in Elm

andThen : (msg -> model -> ( model, Platform.Cmd.Cmd a )) -> msg -> ( model, Platform.Cmd.Cmd a ) -> ( model, Platform.Cmd.Cmd a )

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

For example:

update msg model =
  ( model, Cmd.none )
    |> andThen update SomeMessage
    |> andThen update SomeOtherMessage
    |> andThen update (MessageWithArguments "Hello")
    ...

filter : Basics.Bool -> (( model, Platform.Cmd.Cmd msg ) -> ( model, Platform.Cmd.Cmd msg )) -> ( model, Platform.Cmd.Cmd msg ) -> ( model, Platform.Cmd.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, Cmd.none )
                |> 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, Platform.Cmd.Cmd msg ) -> ( model, Platform.Cmd.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 : Platform.Cmd.Cmd msg -> ( model, Platform.Cmd.Cmd msg ) -> ( model, Platform.Cmd.Cmd msg )

Allows you to attach a Cmd to an update pipeline.

For example:

update msg model =
    ( model, Cmd.none )
        |> andThen update AMessage
        |> addCmd doSomethingWithASideEffect

mapCmd : (msg -> msg_) -> ( model, Platform.Cmd.Cmd msg ) -> ( model, Platform.Cmd.Cmd msg_ )

Map over the Cmd in an update pipeline

sequence : (msg -> model -> ( model, Platform.Cmd.Cmd a )) -> List msg -> ( model, Platform.Cmd.Cmd a ) -> ( model, Platform.Cmd.Cmd a )

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

For example:

update msg model =
    ( model, Cmd.none )
        |> sequence update
            [ AMessage
            , AnotherMessage
            , AThirdMessage
            ]