Companion repository to my blog post about calling Elm code from Node.js.
The function is defined in test-func.elm
. The code that calls it is in aeldardin.js
(named after the project I extracted this from).
I'm not quite sure what the Main function in the Elm code is doing, but I believe it's more or less the standard Elm project setup with the code that would normally render HTML removed.
The following is my guesses about how this works. I'm really not an authority, so take this with a grain of salt:
The key code for responding to JS is this block:
Test newText ->
( Model newText, done (model.text ++ "a") )
This responds to the Test message (due to subscriptions model = test Test
), by doing two things:
- Update the model:
Model newText
, which Elm will use as the model for the next iteration. Note that this doesn't affect the current model, somodel.text
here is still whatever I set it to last time. - Sends a response out the
done
port, with a value ofmodel.text
with "a" appended. I believedone (...)
actually creates a Command object, which tells Elm to send the response on its next iteration.
The JS is more-or-less copied from index.html in evancz's Spelling example, except that I call send()
outside the subscribe()
callback to start things off (whereas evancz has Elm starting the conversation).