Skip to content

Commit

Permalink
Add doThen and sleep to Script module.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillonkearns committed Feb 14, 2024
1 parent 6af9181 commit f50db29
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
12 changes: 12 additions & 0 deletions generator/src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ async function runInternalJob(
];
} else if (requestToPerform.url === "elm-pages-internal://write-file") {
return [requestHash, await runWriteFileJob(requestToPerform)];
} else if (requestToPerform.url === "elm-pages-internal://sleep") {
return [requestHash, await runSleep(requestToPerform)];
} else if (requestToPerform.url === "elm-pages-internal://start-spinner") {
return [requestHash, runStartSpinner(requestToPerform)];
} else if (requestToPerform.url === "elm-pages-internal://stop-spinner") {
Expand Down Expand Up @@ -534,6 +536,16 @@ async function readFileJobNew(req, patternsToWatch) {
});
}
}

function runSleep(req) {
const { milliseconds } = req.body.args[0];
return new Promise((resolve) => {
setTimeout(() => {
resolve(jsonResponse(req, null));
}, milliseconds);
});
}

async function runWriteFileJob(req) {
const data = req.body.args[0];
try {
Expand Down
23 changes: 23 additions & 0 deletions src/Pages/Script.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Pages.Script exposing
, writeFile
, log
, Error(..)
, doThen, sleep
)

{-| An elm-pages Script is a way to execute an `elm-pages` `BackendTask`.
Expand Down Expand Up @@ -35,6 +36,7 @@ Read more about using the `elm-pages` CLI to run (or bundle) scripts, plus a bri
-}

import BackendTask exposing (BackendTask)
import BackendTask.Custom
import BackendTask.Http
import BackendTask.Internal.Request
import Cli.OptionsParser as OptionsParser
Expand Down Expand Up @@ -164,3 +166,24 @@ withCliOptions config execute =
config
|> Program.mapConfig execute
)
sleep : Int -> BackendTask error ()
sleep int =
BackendTask.Internal.Request.request
{ name = "sleep"
, body =
BackendTask.Http.jsonBody
(Encode.object
[ ( "milliseconds", Encode.int int )
]
)
, expect =
BackendTask.Http.expectJson (Decode.null ())
}




doThen : BackendTask error value -> BackendTask error () -> BackendTask error value
doThen task1 task2 =
task2
|> BackendTask.andThen (\() -> task1)

0 comments on commit f50db29

Please sign in to comment.