dillonkearns/elm-pages/12.3.0

Tui.Effect

The core Effects for Tui.Programs. Most notably, your TUI can resolve a BackendTask within its init and update and get back the result as a Msg.

update : Msg -> Model -> ( Model, Effect Msg )
update msg model =
    case msg of
        PressedStage file ->
            ( { model | staging = Just file }
            , Script.exec "git" [ "add", file ]
                |> Effect.perform (\() -> StagingComplete file)
            )


        StagingComplete file ->
            ( { model | staging = Nothing }
            , Effect.none
            )
type alias Effect msg =
Effect msg

An Effect can be passed to update or init. Like the Cmd part of a standard Elm TEA application.

Performing BackendTasks

perform : (a -> msg) -> BackendTask FatalError a -> Effect msg

Run a BackendTask and get the resolved data back in a Msg. If the BackendTask fails, the FatalError propogates and the TUI exits and prints the FatalError's message.

Script.command "git" [ "status", "--porcelain" ]
    |> BackendTask.map parseFiles
    |> Effect.perform GotFiles
attempt : (Result error a -> msg) -> BackendTask error a -> Effect msg

Like perform, but surfaces errors as Result values for you to handle BackendTask errors gracefully.

Script.command "git" [ "diff", file ]
    |> BackendTask.mapError (\_ -> "git diff failed")
    |> Effect.attempt
        (\result ->
            case result of
                Ok text ->
                    GotDiff text


                Err reason ->
                    DiffFailed reason
        )

Useful in combination with BackendTask.mapError.

Combining and Transforming

none : Effect msg

No effect.

batch : List (Effect msg) -> Effect msg

Combine multiple effects sequentially.

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

Transform the message type of an effect.

Exiting Program

exit : Effect msg

Exit the TUI (exit code 0). Terminal state is restored.

exitWithCode : Int -> Effect msg

Exit the TUI with a specific exit code. Terminal state is restored.