Skip to content

Implement ports and flags#6

Open
rgrempel wants to merge 13 commits intomasterfrom
from-to-port
Open

Implement ports and flags#6
rgrempel wants to merge 13 commits intomasterfrom
from-to-port

Conversation

@rgrempel
Copy link
Collaborator

@rgrempel rgrempel commented May 30, 2018

The needed steps are roughly:

  • Make a class for types that can be sent to & from ports with the representations that Elm uses
  • Write a runProgram variant that you can call from Javascript with flags, and provide a way to export to Javascript something you can call via .worker(flags) etc.
  • Implement outgoing ports
  • Implement incoming ports

@rgrempel
Copy link
Collaborator Author

rgrempel commented Jun 1, 2018

I'm thinking through how to actually implement ports now.

Elm allows ports to be defined in any module (as long as you say it is a port module). The compiler must make some kind of collection of these ports to attach to the ports object at program startup. We avoid that for effects managers, because we don't really need to know about effects managers until they are actually used (via a Cmd or Sub). We almost have the same luxury for ports, because a port (to be interesting) must eventually be used to generate a Cmd or Sub. However, by its very nature, the Javascript must subscribe to a port before we see the Cmd. (And, it could potentially send a message before we see the Sub).

So, I think we have to do one of several things.

  1. We could force the programmer to actually list all the ports when calling program. This might not be a bad idea, really, but it would diverge from what Elm does.

  2. We could have some kind of code-generation stage (mimicking what I expect the Elm compiler must be doing when it sees port module). But that seems awkward.

  3. When we set up the ports object for Javascript to use, we can make it a Javascript Proxy, so we can write a function to construct the Javascript end of ports on the fly (and keep a reference). Then, when we see the port on the Purescript side, we can use the reference to see whether there already have been subscriptions etc. This probably could be made to work, but would have at least one disadvantage -- we wouldn't be able to reject any access to ports where the port doesn't exist, because we wouldn't know whether the port might exist in the future or not.

So, none of these are ideal. I think I like the Proxy idea best, but it's possible that requiring ports to be explicitly provided to program is wiser.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments