Orasund/elm-action - version: 2.1.3

for more information visit the package's GitHub page

Package contains the following modules:

elm-action

DEPRECATED

Use Elm-Spa instead


This module lets you organize your update function for different states.

For example:

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case ( msg, model ) of
        ( GuestSpecific guestMsg, Guest ) ->
            updateGuest guestMsg
                |> Action.config
                |> Action.withTransition initUser User never
                |> Action.withUpdate (always Guest) never
                |> Action.apply

        ( UserSpecific userMsg, User userModel ) ->
            updateUser userMsg
                |> Action.config
                |> Action.withUpdate User UserSpecific
                |> Action.withExit ( Guest, Cmd.none )
                |> Action.apply

Here we have two states: Guest and User. Each of them has there own update function:

updateGuest : GuestMsg -> GuestAction
updateGuest msg =
    case msg of
        LoggedIn {name,pass} ->
            if pass = "password" then
                Action.transitioning name
            else
                Action.updating ((),Cmd.none)

updateUser : UserMsg -> User -> UserAction
updateUser msg user =
    case msg of
        Commented string ->
            Debug.todo "send comment to server"
        LoggedOut ->
            Action.exiting

Under the hood, an Action is a state transition of a state machine. The corresponding state machine looks like this:

   LoggedOut?  +--User<-+   !pass == "password"
               |        |
               v        |
               +->Guest-+   ?LoggedIn => pass == "password"?
                   ^    |
                   +----+   !pass /= "password"

Alternative Solutions

I've seen multiple different approaches for the same problem and it might be that my solution isn't suitable for your needs.