New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only one input port can be created. #758

Closed
Pilatch opened this Issue Nov 21, 2016 · 3 comments

Comments

Projects
None yet
2 participants
@Pilatch

Pilatch commented Nov 21, 2016

Even if you create multiple input ports, and use Sub.batch to make multiple subscriptions, on the JavaScript side you still only have "input" on the worker object. To reproduce...

Greet.elm:

    port module Greet exposing (..)

    import Json.Decode

    port greet : String -> Cmd a

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

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

    type Msg = Name String | Hobby String

    type alias Model = { visitors : Int }

    update : Msg -> Model -> ( Model, Cmd a )
    update msg model =
      case msg of
          Name name ->
            let newModel =
                  model.visitors + 1 |> Model
                greeting =
                  "Hello " ++ name ++ ", you are visitor number " ++ toString newModel.visitors ++ "."
            in
              (newModel, greet greeting)
          Hobby hobby ->
            let greeting =
                "Would you like to participate in some " ++ hobby ++ " with our " ++ toString model.visitors ++ " visitors?"
            in
                (model, greet greeting)

    subscriptions : Model -> Sub Msg
    subscriptions model =
      Sub.batch [
        inputName Name,
        inputHobby Hobby
      ]

    -- Elm assumes that your program will only have one input port.
    -- Regardless of what you name that port in your Elm code, it will be named `input`
    -- though the outbound ports will retain their names.
    -- How should I specify that I'm sending to inputHobby instead of inputName?

    main =
      Platform.program {
        init = ({ visitors = 0 }, Cmd.none),
        update = update,
        subscriptions = subscriptions
      }

... compile that to elm.js and then run greeter.js in Node:

    const elm = require('./elm.js')
    const elmPorts = elm.Greet.worker().ports
    console.log("elmPorts", elmPorts)
    process.stdout.write(`Ready to greet people whose names you enter. Press Ctrl + C to stop.\n`)

    elmPorts.greet.subscribe(greeting => {
      process.stdout.write(`${greeting}\n\n`)
    })
    process.stdin.resume()
    process.stdin.on('data', chunk => {
      elmPorts.input.send( chunk.toString('utf8').trim() )
    })

...and look at what's logged to the console. You'll see this:

    elmPorts { greet:
       { subscribe: [Function: subscribe],
         unsubscribe: [Function: unsubscribe] },
      input: { send: [Function: send] } }

Which shows that there's only the input port, even though Greet.elm does not define any port named input, and defines a total of three ports.

@process-bot

This comment has been minimized.

Show comment
Hide comment
@process-bot

process-bot Nov 21, 2016

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

process-bot commented Nov 21, 2016

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

@Pilatch

This comment has been minimized.

Show comment
Hide comment
@Pilatch

Pilatch Nov 21, 2016

Versions:

elm -v
0.18.0

elm-make -v
elm-make 0.18 (Elm Platform 0.18.0)

node -v
v6.3.0

OSX 10.11.6

Pilatch commented Nov 21, 2016

Versions:

elm -v
0.18.0

elm-make -v
elm-make 0.18 (Elm Platform 0.18.0)

node -v
v6.3.0

OSX 10.11.6

@Pilatch

This comment has been minimized.

Show comment
Hide comment
@Pilatch

Pilatch Nov 21, 2016

I'm an idiot. NO BUG!

Pilatch commented Nov 21, 2016

I'm an idiot. NO BUG!

@Pilatch Pilatch closed this Nov 21, 2016

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