Search code examples
elm

Unregister multiple subscribers from a given port


To unregister a listener from a port, you can do:

app.port.unsubscribe(myListener)

Is there a way to unregister all the listeners to a given port, without calling unsubscribe explicitely for each one? I would like to be sure I won't forget any listener. I tried by calling unsubscribe() without any parameter, but it does not seem to have any effect.


Solution

  • This is not currently possible. You can see the exposed API for ports here:

    https://github.com/elm/core/blob/de38986116bd93affc1c7a12e548d1f01be9a9f1/src/Elm/Kernel/Platform.js#L343-L365

    The subs array is not exposed, so by default, there's no way to get at it without having the original callback.

    If this functionality is important to you however you might consider writing a wrapper on the Javascript side that keeps track of your subscribed callbacks for you, which you could use to implement an unsubscribeAll yourself.