primait / prima-elm-extra / PrimaUpdate

Update function helpers


type alias PrimaUpdate model msg =
( model, Platform.Cmd.Cmd msg )

The alias for a (Model, Cmd Msg) pair.

init : Update Model Msg
init =
    { users = Nothing }
        |> PrimaUpdate.withCmd fetchUsers

Update helpers

withCmd : Platform.Cmd.Cmd msg -> model -> PrimaUpdate model msg

Wraps a command and a model in a PrimaUpdate pair

model
|> withCmd cmd
-- => (model, cmd)

andThen : (model -> PrimaUpdate otherModel msg) -> PrimaUpdate model msg -> PrimaUpdate otherModel msg

Concatenates Updates batching their updates

(someModel, someCommand)
|> andThen (\ someModel -> (anotherModel, anotherCommand))

-- =>  (anotherModel, Cmd.batch [someCommand, anotherCommand] )

withCmds : List (Platform.Cmd.Cmd msg) -> model -> PrimaUpdate model msg

Like withCmd but batches them

model
|> withCmds [cmd1, cmd2]
-- => (model, Cmd.batch [cmd1, cmd2])

withCmdsMap : List (model -> Platform.Cmd.Cmd msg) -> model -> PrimaUpdate model msg

Used to apply an updated model to the cmds in fluid style updating

    serializeUsers : Model -> Cmd Msg
    logEvent : String -> Model -> Cmd Msg

    update : Msg -> Model -> PrimaUpdate Model Msg
    update msg model =
        case msg of
            FetchedUsers newUsers ->
                model
                    |> setUsers newUsers
                    |> withCmdsMap [ serializeUsers, logEvent "got users" ]

withoutCmds : model -> PrimaUpdate model msg

Lifts a model to a (model, Cmd msg) pair

mapModel : (model -> otherModel) -> PrimaUpdate model msg -> PrimaUpdate otherModel msg

Maps the model preserving the current cmds

 { count = 0 }
    |> PrimaCmd.withoutCmds
    |> PrimaCmd.mapModel (updateCount ((+) 1))
    |> Expect.equal ({ count = 1 }, Cmd.none)

mapCmd : (msg -> otherMsg) -> PrimaUpdate model msg -> PrimaUpdate model otherMsg

Cmd.maps the pair cmd, preserving the model

intCmd : Cmd Int
intCmd =
    Task.succeed 42 |> Task.perform identity

pair : Update () String
pair =
    ()
        |> withCmd intCmd
        |> mapCmd String.fromInt