From 707822558124b860e87570fb8ba011c584495931 Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Wed, 14 Feb 2024 16:34:19 -0800 Subject: [PATCH] Add Step API. --- examples/end-to-end/script/src/Steps2.elm | 16 +++++++++++ src/Pages/Script/Spinner.elm | 34 ++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 examples/end-to-end/script/src/Steps2.elm diff --git a/examples/end-to-end/script/src/Steps2.elm b/examples/end-to-end/script/src/Steps2.elm new file mode 100644 index 000000000..57d2c5bf6 --- /dev/null +++ b/examples/end-to-end/script/src/Steps2.elm @@ -0,0 +1,16 @@ +module Steps2 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 + (Spinner.steps + |> Spinner.withStep "Step 1" (\() -> sleep 3000) + |> Spinner.withStep "Step 2" (\() -> sleep 3000) + |> Spinner.withStep "Step 3" (\() -> sleep 3000) + |> Spinner.runSteps + ) diff --git a/src/Pages/Script/Spinner.elm b/src/Pages/Script/Spinner.elm index c63581b39..7f1db4992 100644 --- a/src/Pages/Script/Spinner.elm +++ b/src/Pages/Script/Spinner.elm @@ -1,8 +1,9 @@ -module Pages.Script.Spinner exposing (CompletionIcon(..), Options, Spinner, encodeCompletionIcon, options, runTask, runTaskExisting, runTaskWithOptions, showStep, spinner, start, withImmediateStart, withNamedAnimation, withOnCompletion) +module Pages.Script.Spinner exposing (CompletionIcon(..), Options, Spinner, encodeCompletionIcon, options, runSteps, runTask, runTaskExisting, runTaskWithOptions, showStep, spinner, start, steps, withImmediateStart, withNamedAnimation, withOnCompletion, withStep) import BackendTask exposing (BackendTask) import BackendTask.Http import BackendTask.Internal.Request +import FatalError exposing (FatalError) import Json.Decode as Decode import Json.Encode as Encode @@ -279,3 +280,34 @@ encodeCompletionIcon completionIcon = Custom string -> "custom" + + +type Steps error value + = Steps (BackendTask error value) + + +steps : Steps FatalError () +steps = + Steps (BackendTask.succeed ()) + + +withStep : String -> (oldValue -> BackendTask FatalError newValue) -> Steps FatalError oldValue -> Steps FatalError newValue +withStep text backendTask steps_ = + case steps_ of + Steps previousSteps -> + Steps + (BackendTask.map2 + (\pipelineValue newSpinner -> + runTaskExisting + newSpinner + (backendTask pipelineValue) + ) + previousSteps + (options text |> showStep) + |> BackendTask.andThen identity + ) + + +runSteps : Steps FatalError value -> BackendTask FatalError value +runSteps (Steps steps_) = + steps_