I have an idea for a workaround for a long-standing issue with unmounting Html-based elm apps, curious to hear feedback on where this could go wrong. The issue is described here: https://github.com/elm-lang/core/issues/886 and the fix is here: https://gist.github.com/joefiorini/4372e2362e658d3092419e708416f92c.
AFAIK, this bug is the main thing keeping us from being able to mount multiple Elm apps on the same page. I’m looking into a possible architecture that would rely on this ability. Since it’s not very easy to contribute bug fixes to Elm libraries atm, I thought I’d try to come up with a workaround.
The basic idea is to wrap Html programs with a new program that does 3 things:
- Tracks whether or not the app is mounted by wrapping the app’s model with an
isMounted
property - Renders a web component that dispatches a custom event when it’s detached from the DOM
- Makes
subscriptions
Sub.none
when theisMounted
property returnsFalse
, otherwise returns the app’s subscriptions
UPDATE: I’ve simplified and ellie-ified my example to something one could implement in any app. Appreciate any feedback on this anyone has. https://ellie-app.com/cLp75wZFBa1/0