joshburgess/elm-route-craft/1.0.0

App.Effect

The Effect type wraps Cmd with shared state updates and navigation.

Pages return Effect msg instead of Cmd msg from their update functions when they need to update shared state or navigate. The App module interprets Effects into actual Cmds.

update : Msg -> Model -> ( Model, Effect Msg )
update msg model =
    case msg of
        LoggedIn user ->
            ( model
            , Effect.batch
                [ Effect.updateShared (\s -> { s | user = Just user })
                , Effect.pushUrl "/"
                ]
            )


        FetchData ->
            ( model
            , Effect.sendCmd (Http.get { ... })
            )

Types

type Effect shared msg
= None
| Batch (List (Effect shared msg))
| SendCmd (Cmd msg)
| UpdateShared (shared -> shared)
| PushUrl String
| ReplaceUrl String
| Back Int
| LoadExternalUrl String

An effect that the App module interprets.

  • None : no effect
  • Batch : combine multiple effects
  • SendCmd : a regular Elm Cmd
  • UpdateShared : modify the shared state
  • PushUrl : navigate to a new URL (adds to history)
  • ReplaceUrl : navigate without adding history
  • Back : go back N steps in history
  • LoadExternalUrl : navigate to an external URL (full page load)

Primitives

none : Effect shared msg

No effect.

sendCmd : Cmd msg -> Effect shared msg

Wrap a regular Cmd as an Effect.

batch : List (Effect shared msg) -> Effect shared msg

Combine multiple effects.

Shared State

updateShared : (shared -> shared) -> Effect shared msg

Update the shared state with a transformation function.

Effect.updateShared (\s -> { s | user = Just user })
pushUrl : String -> Effect shared msg

Navigate to a URL (adds to browser history).

Effect.pushUrl "/blog/42"
replaceUrl : String -> Effect shared msg

Navigate to a URL without adding to browser history.

back : Int -> Effect shared msg

Go back N steps in browser history.

loadExternalUrl : String -> Effect shared msg

Navigate to an external URL. This triggers a full page load, leaving the SPA.

Transforming Effects

map : (a -> b) -> Effect shared a -> Effect shared b

Map over the message type of an Effect.