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
)
Add a Step. See withStepWithOptions to configure the step's spinner.
Add a step with custom Options.
Configuration that can be used with runTaskWithOptions and withStepWithOptions.
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"
An icon used to indicate the completion status of a step. Set by using withOnCompletion.
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."
)
)
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..."
)
)
)
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
)
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.
The definition of a series of
BackendTasks to run, with a spinner for each step.