choonkeat / elm-webapp

A setup for writing http based, client-server app in elm, inspired wholly by lamdera.com
https://package.elm-lang.org/packages/choonkeat/elm-webapp/latest
MIT License
57 stars 7 forks source link

How to extend types in Protocol.elm ? #8

Closed astrale-sharp closed 2 years ago

astrale-sharp commented 2 years ago

I'm messing around with https://package.elm-lang.org/packages/choonkeat/elm-webapp/latest and I'm trying to expand the types in Protocol.elm, But im unsure how to edit the Protocol/Auto.elm or if I should since it has been generated automatically (but by what?)

hello-app
├── Makefile
├── index.js
└── src
    ├── Client.elm
    ├── Server.elm
    ├── Protocol.elm <---- Here
    └── Protocol
        └── Auto.elm <---- and Here

When i edit for instance this :

type MsgFromClient
    = ManyMsgFromClient (List MsgFromClient)
    | SetGreeting String

To this :

type MsgFromClient
    = ManyMsgFromClient (List MsgFromClient)
    | SetGreeting String
    | Another

running make fails (expectedly) but it runs in a loop and if i correct back the code it still wont compile ! (I started from start again but i think supressing elm-stuff might do the trick.what's the right way to re-autogenerate the code so you can safely mess with the types? Thank you !

choonkeat commented 2 years ago

Oops sorry, the issue was closed by commit without comment

There were several issues causing the problems

choonkeat commented 2 years ago

There was the problem of "infinite loop" after we make a change to the code. This was because a crucial file scripts/wait-for-changes.js missing from the packaged npm bundle

Then again scripts/wait-for-changes.js has the same recursive problem on Linux 😮‍💨 https://github.com/choonkeat/elm-webapp/blob/8703775296bbd1908dc16b5309d39d83adb0c2b3/templates/crud/scripts/wait-for-changes.js#L5

trying hard to avoid too much dependencies ...

astrale-sharp commented 2 years ago

Thanks so much, it works better now!

Although the problem of "infinite loop" is still present on linux! If your elm code wont compile (you can check with elm make src/Server.elm and elm make src/Client.elm then running make will indeed go into infinite loop again.

choonkeat commented 2 years ago

Yes it's happening on Linux because scripts/wait-for-changes.js is probably spouting errors due to https://github.com/choonkeat/elm-webapp/issues/8#issuecomment-979954826

But the fix won't be as easy as https://github.com/choonkeat/elm-auto-encoder-decoder/commit/a94f289123373c7c14be9a6f5d9502728ade23fa since we do need a recursive file watcher here ...

Switching from fs.watch to something like chokidar or gaze is an option, but means burdening apps with more js dependencies...

astrale-sharp commented 2 years ago

Another option might be to warn linux users and have them ensure their files compiles (check by running elm make src/*.elm) and then maybe tweak the make file so it stops when encountering an error? (if that is possible)

choonkeat commented 2 years ago

actually if this only affects Linux, we could try using inotifywait (from apt-get install inotify-tools, apk add inotify-tools, or ...)

you can try editing your Makefile to replace

node scripts/wait-for-changes.js .

with

inotifywait -r -e close_write -e move -e create -e delete .

(take note of the trailing .)

if it works, I'll try to work the instructions or do a cli swap into the installation process