Skip to content

Commit

Permalink
[WIP] Stateful
Browse files Browse the repository at this point in the history
  • Loading branch information
miniBill committed Sep 24, 2024
1 parent 862e83d commit bc6f1b5
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 125 deletions.
31 changes: 25 additions & 6 deletions generator/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,25 +344,39 @@ async function requireElm(compiledElmPath) {
const warnOriginal = console.warn;
console.warn = function () {};

let Elm = (await import(url.pathToFileURL(path.resolve(compiledElmPath)).href)).default;
let Elm = (
await import(url.pathToFileURL(path.resolve(compiledElmPath)).href)
).default;
console.warn = warnOriginal;
return Elm;
}

/**
* @param {string} expectedFilePath
* @param {string} moduleName
*/
function generatorWrapperFile(moduleName) {
async function generatorWrapperFile(expectedFilePath, moduleName) {
const moduleContent = await fs.promises.readFile(expectedFilePath, {
encoding: "utf-8",
});

const hasRun = /^run\s*=/m.test(moduleContent);
const hasMain = /^main\s*=/m.test(moduleContent);

const { data, app } =
hasMain && !hasRun
? { data: "main", app: "statefulApp" }
: { data: "run", app: "app" };

return `port module ScriptMain exposing (main)
import Pages.Internal.Platform.GeneratorApplication
import ${moduleName}
main : Pages.Internal.Platform.GeneratorApplication.Program
main =
Pages.Internal.Platform.GeneratorApplication.app
{ data = ${moduleName}.run
Pages.Internal.Platform.GeneratorApplication.${app}
{ data = ${moduleName}.${data}
, toJsPort = toJsPort
, fromJsPort = fromJsPort identity
, gotBatchSub = gotBatchSub identity
Expand All @@ -379,6 +393,11 @@ port fromJsPort : (Pages.Internal.Platform.GeneratorApplication.JsonValue -> msg
port gotBatchSub : (Pages.Internal.Platform.GeneratorApplication.JsonValue -> msg) -> Sub msg
`;
}

/**
* @param {any} value
* @param {any[]} previous
*/
function collect(value, previous) {
return previous.concat([value]);
}
Expand Down Expand Up @@ -406,7 +425,7 @@ async function compileElmForScript(elmModulePath) {
path.join(
`${projectDirectory}/elm-stuff/elm-pages/.elm-pages/ScriptMain.elm`
),
generatorWrapperFile(moduleName)
await generatorWrapperFile(expectedFilePath, moduleName)
);
let executableName = await lamderaOrElmFallback();
try {
Expand Down
2 changes: 2 additions & 0 deletions generator/src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,8 @@ async function runInternalJob(
return [requestHash, runStartSpinner(requestToPerform)];
case "elm-pages-internal://stop-spinner":
return [requestHash, runStopSpinner(requestToPerform)];
case "elm-pages-internal://exit":
process.exit(requestToPerform.body.args[0]);
default:
throw `Unexpected internal BackendTask request format: ${kleur.yellow(
JSON.stringify(2, null, requestToPerform)
Expand Down
6 changes: 3 additions & 3 deletions src/Pages/GeneratorProgramConfig.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module Pages.GeneratorProgramConfig exposing (GeneratorProgramConfig)
import Json.Decode as Decode
import Json.Encode
import Pages.Internal.Platform.ToJsPayload
import Pages.Script exposing (Script)
import Pages.Script exposing (StatefulScript)


type alias GeneratorProgramConfig =
{ data : Script
type alias GeneratorProgramConfig model msg =
{ data : StatefulScript model msg
, toJsPort : Json.Encode.Value -> Cmd Never
, fromJsPort : Sub Decode.Value
, gotBatchSub : Sub Decode.Value
Expand Down
Loading

0 comments on commit bc6f1b5

Please sign in to comment.