primait/prima-elm-extra/1.0.0

PrimaUpdate

Update function helpers

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

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

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

Update helpers

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

Wraps a command and a model in a PrimaUpdate pair

model
|> withCmd cmd
-- => (model, cmd)
(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 (Cmd msg) -> model -> PrimaUpdate model msg

Like withCmd but batches them

model
|> withCmds [cmd1, cmd2]
-- => (model, Cmd.batch [cmd1, cmd2])
withCmdsMap : List (model -> 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

(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)
(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