Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Stateful #471

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading