Skip to content

Ports leak between apps when compiled together #2357

Description

@lydell

Here are two minimal Elm programs, that both use ports with different names:

port module A exposing (main)


port a_outgoing : String -> Cmd msg


port a_incoming : (String -> msg) -> Sub msg


main : Program () String String
main =
    Platform.worker
        { init = \() -> ( "", Cmd.none )
        , update = \_ s -> ( s, a_outgoing s )
        , subscriptions = \_ -> a_incoming identity
        }
port module B exposing (main)


port b_outgoing : String -> Cmd msg


port b_incoming : (String -> msg) -> Sub msg


main : Program () String String
main =
    Platform.worker
        { init = \() -> ( "", Cmd.none )
        , update = \_ s -> ( s, b_outgoing s )
        , subscriptions = \_ -> b_incoming identity
        }

Let’s compile them together into the same JavaScript file:

elm make src/A.elm src/B.elm --output combined.js

Then let’s initialize both:

const appA = Elm.A.init();
const appB = Elm.A.init();

console.log("A ports", appA.ports);
console.log("B ports", appB.ports);

Both logs show 4 ports available: a_outgoing, a_incoming, b_outgoing, b_incoming

I would have expected:

  • appA: a_outgoing, a_incoming
  • appB: b_outgoing, b_incoming

If you do appA.b_outgoing.subscribe(console.log) nothing is ever logged, and if you do appA.b_incoming.send("") nothing ever gets to A and not to B either. So the extra ports are harmless, they’re just confusing.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions