So in listening to the latest ElmTown, episode 22, with Evan
Right out of the docs:
Elm’s interop is built on the observation that by enforcing some architectural rules, you can make full use of the old language without making sacrifices in the new one.
This system of messages keeps JS at arms length, and the ‘crazy’ our of our Elm code.
embedding Elm in HTML
Ok that sounds pretty straight forward. We’re coming at this from a mostly JS background, with little FP experience. But what does this look like?
Some Set up on the Elm side creating a port Cmd, and port Sub.
Cmd for sending messages/data to JS, and the Sub to receive messages/data from JS.
Our Elm module needs to be declared as
port module NOTE you should not have a ton of port modules in your Elm app!!! (Very few modules should have ports in them!) Why? Well if you require lots of ports from Elm to JS, then maybe Elm is not the right tool for the job? And, the best part? That’s ok!
What happen’s if there is an type :: List String and JS send us an Number!
Elm throws a runtime exception immediately when you call send with invalid data. We can’t fix JS, but we can protect our Elm environment from being contaminated with invalid data!!!
NOTE ports is not allowed in a published Elm package. Why? Consider downloading an Elm package, only to realize that it requires some funky JS work on the outside to get it to work! Poor user experience for sure.