elm-typescript-interop icon indicating copy to clipboard operation
elm-typescript-interop copied to clipboard

If port is not subscribed it is still in typings

Open wclr opened this issue 5 years ago • 3 comments
trafficstars

If the port is not subscribed it (and thus is absent on the app object) is still appears in typings. Just as a possible enhancement.

wclr avatar Nov 02 '20 13:11 wclr

It seems like a bug to me that ports are not generated unless they are defined. In fact, if I recall correctly, I think they are defined if you compile without the --optimize flag, but they get stripped away by the optimize step. I think it would make more sense if the compiled Elm code generated all available ports, whether they're used or not.

For example, if you comment out an invocation of an Elm-to-JS port, which would be a reasonable thing to do, you wouldn't expect to then have a null pointer error in your Elm setup JS code.

dillonkearns avatar Nov 02 '20 16:11 dillonkearns

I think they are defined if you compile without the --optimize flag

Well, I'm compiling in dev mode, output code is not minimized and optimized.

I thought it was an elm's feature.

would be a reasonable thing to do, you wouldn't expect to then have a null pointer error in your Elm setup JS code.

Need to think if it is correct, You think there should be opened an issue in elm/compiler on this?

wclr avatar Nov 03 '20 14:11 wclr

There's an issue that touches on it here: https://github.com/elm/compiler/issues/1988.

Here's my thinking on this. Imagine you add a port in development to track something for testing or debugging purposes. Then you comment out the invocation of that port Cmd before shipping the code to production. But you are still defining the port (port debugInfo : ...), so you don't think to remove the subscription in your JS code. You ship your code, and now you have a runtime exception in production. The same situation applies if it's not a debugging helper but instead a port that you don't need to call anymore. The key point is that I don't think it makes sense to have to ask yourself every time you remove an invocation of a port whether that was the last invocation of that port so you now need to remove some wiring code in JS.

Another way to approach the problem could be to use a proxy in JS around the Elm app instance.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy

In dev mode, you could choose to have errors or warnings when you're wiring up an unused port. Or not, that's a matter of preference I think. But then in production you could just do a no-op for any ports that aren't invoked.

dillonkearns avatar Nov 03 '20 16:11 dillonkearns