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

Port Subscription Not working on initial load on 0.17 #582

Closed
knowthen opened this Issue Apr 30, 2016 · 4 comments

Comments

Projects
None yet
5 participants
@knowthen

knowthen commented Apr 30, 2016

Background: I was trying to pull in a JWT from localstorage using ports and I'm unable to do it on initial page load.

Summary: Elm Msg via port subscription, not generated on initial load, however it does work when triggered via button press

To provide a simplified example, I took the source from the interop section of the gitbook and slightly modified it as follows:

init : (Model, Cmd Msg)
init =
  -- (Model "" [], Cmd.none)
  (Model "" [], check "" )

I put a console.log into the javascript side and I can see that the handler on the js side is getting called

app.ports.check.subscribe(function(word) {
      console.log('js subscribe fired..');
      // put some dummy data in
      // var suggestions = spellCheck(word);
      var suggestions = ["Junk", "Jump"];
      app.ports.suggestions.send(suggestions);
    });

however the response app.ports.suggestions.send(suggestions); doesn't seem to create message on the elm side.

Oddly if I click the "check" button, a message (from the subscription) is created.

Here's a repo with the code demonstrating the apparent bug: https://github.com/knowthen/portbug

Visual Demo

@schnittchen

This comment has been minimized.

Show comment
Hide comment
@schnittchen

schnittchen May 15, 2016

I have the same problem here, and it sometimes works and sometimes doesn't.

I debugged around a bit and found this: Stepping into the port send has subs an empty (Elm) list here https://github.com/elm-lang/core/blob/master/src/Native/Platform.js#L547

Maybe @knowthen can confirm this observation.

schnittchen commented May 15, 2016

I have the same problem here, and it sometimes works and sometimes doesn't.

I debugged around a bit and found this: Stepping into the port send has subs an empty (Elm) list here https://github.com/elm-lang/core/blob/master/src/Native/Platform.js#L547

Maybe @knowthen can confirm this observation.

@CumpsD

This comment has been minimized.

Show comment
Hide comment
@CumpsD

CumpsD May 16, 2016

Same here, subs is empty, working around it with setTimeout(function(){ app.ports.jsMsgs.send(1); }, 0) for now, then they aren't empty

CumpsD commented May 16, 2016

Same here, subs is empty, working around it with setTimeout(function(){ app.ports.jsMsgs.send(1); }, 0) for now, then they aren't empty

@PhilippMeissner

This comment has been minimized.

Show comment
Hide comment
@PhilippMeissner

PhilippMeissner May 18, 2016

For the moment you might be happy using programWithFlags like the TodoMVC did. That's what I did as a momentarily workaround.
Here's the documentation: http://package.elm-lang.org/packages/elm-lang/html/1.0.0/Html-App#programWithFlags

Here's it in action :
https://github.com/evancz/elm-todomvc/blob/master/Todo.elm#L86
https://github.com/evancz/elm-todomvc/blob/master/index.html#L17

Hope that helps you out for the moment. Obviously this issue still needs to be looked into.

PhilippMeissner commented May 18, 2016

For the moment you might be happy using programWithFlags like the TodoMVC did. That's what I did as a momentarily workaround.
Here's the documentation: http://package.elm-lang.org/packages/elm-lang/html/1.0.0/Html-App#programWithFlags

Here's it in action :
https://github.com/evancz/elm-todomvc/blob/master/Todo.elm#L86
https://github.com/evancz/elm-todomvc/blob/master/index.html#L17

Hope that helps you out for the moment. Obviously this issue still needs to be looked into.

@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

evancz May 19, 2016

Member

Going with #595 as a simpler example.

Member

evancz commented May 19, 2016

Going with #595 as a simpler example.

@evancz evancz closed this May 19, 2016

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