Pages.Script.Spinner

Running Steps

The easiest way to use spinners is to define a series of Steps and then run them with runSteps.

Steps are a sequential series of BackendTasks that are run one after the other. If a step fails (has a FatalError), its spinner will show a failure, and the remaining steps will not be run and will be displayed as cancelled (the step name in gray).

module StepsDemo exposing (run)

import BackendTask exposing (BackendTask)
import Pages.Script as Script exposing (Script)
import Pages.Script.Spinner as Spinner

run : Script
run =
    Script.withoutCliOptions
        (Spinner.steps
            |> Spinner.withStep "Compile Main.elm" (\() -> Script.exec "elm" [ "make", "src/Main.elm", "--output=/dev/null" ])
            |> Spinner.withStep "Verify formatting" (\() -> Script.exec "elm-format" [ "--validate", "src/" ])
            |> Spinner.withStep "elm-review" (\() -> Script.exec "elm-review" [])
            |> Spinner.runSteps
        )
type Steps error value
= Steps (BackendTask error value)

The definition of a series of BackendTasks to run, with a spinner for each step.

steps : Steps FatalError ()

Initialize an empty series of Steps.

String
-> (oldValue -> BackendTask FatalError newValue)
-> Steps FatalError oldValue
-> Steps FatalError newValue

Add a Step. See withStepWithOptions to configure the step's spinner.

Options FatalError newValue
-> (oldValue -> BackendTask FatalError newValue)
-> Steps FatalError oldValue
-> Steps FatalError newValue

Add a step with custom Options.

runSteps : Steps FatalError value -> BackendTask FatalError value

Perform the Steps in sequence.

Configuring Steps

type Options error value

Configuration that can be used with runTaskWithOptions and withStepWithOptions.

options : String -> Options error value

The default options for a spinner. The spinner text is a required argument and will be displayed as the step name.

import Pages.Script.Spinner as Spinner

example =
    Spinner.options "Compile Main.elm"
= Succeed
| Fail
| Warn
| Info

An icon used to indicate the completion status of a step. Set by using withOnCompletion.

(Result error value -> ( CompletionIcon, Maybe String ))
-> Options error value
-> Options error value

Set the completion icon and text based on the result of the task.

import Pages.Script.Spinner as Spinner

example =
    Spinner.options "Fetching data"
        |> Spinner.withOnCompletion
            (\result ->
                case result of
                    Ok _ ->
                        ( Spinner.Succeed, "Fetched data!" )

                    Err _ ->
                        ( Spinner.Fail
                        , Just "Could not fetch data."
                        )
            )

Running with BackendTask

runTask : String -> BackendTask error value -> BackendTask error value

Run a BackendTask with a spinner. The spinner will show a success icon if the task succeeds, and a failure icon if the task fails.

It's often easier to use steps when possible.

    module SequentialSteps exposing (run)

    import Pages.Script as Script exposing (Script, doThen, sleep)
    import Pages.Script.Spinner as Spinner


    run : Script
    run =
        Script.withoutCliOptions
            (sleep 3000
                |> Spinner.runTask "Step 1..."
                |> doThen
                    (sleep 3000
                        |> Spinner.runTask "Step 2..."
                        |> doThen
                            (sleep 3000
                                |> Spinner.runTask "Step 3..."
                            )
                    )
            )
Options error value
-> BackendTask error value
-> BackendTask error value

Low-Level

showStep : Options error value -> BackendTask error (Spinner error value)

showStep gives you a Spinner reference which you can use to start the spinner later with runSpinnerWithTask.

Most use cases can be achieved more easily using more high-level helpers, like runTask or steps. showStep can be useful if you have more dynamic steps that you want to reveal over time.

module ShowStepDemo exposing (run)

import BackendTask exposing (BackendTask)
import Pages.Script as Script exposing (Script, doThen, sleep)
import Pages.Script.Spinner as Spinner

run : Script
run =
    Script.withoutCliOptions
        (BackendTask.succeed
            (\spinner1 spinner2 spinner3 ->
                sleep 3000
                    |> Spinner.runSpinnerWithTask spinner1
                    |> doThen
                        (sleep 3000
                            |> Spinner.runSpinnerWithTask spinner2
                            |> doThen
                                (sleep 3000
                                    |> Spinner.runSpinnerWithTask spinner3
                                )
                        )
            )
            |> BackendTask.andMap
                (Spinner.options "Step 1" |> Spinner.showStep)
            |> BackendTask.andMap
                (Spinner.options "Step 2" |> Spinner.showStep)
            |> BackendTask.andMap
                (Spinner.options "Step 3" |> Spinner.showStep)
            |> BackendTask.andThen identity
        )
Spinner error value
-> BackendTask error value
-> BackendTask error value

After calling showStep to get a reference to a Spinner, use runSpinnerWithTask to run a BackendTask and show a failure or success completion status once it is done.

type Spinner error value