Skip to content
This repository was archived by the owner on Sep 13, 2019. It is now read-only.

Conversation

@ktonon
Copy link
Owner

@ktonon ktonon commented Aug 8, 2017

No description provided.

This allows me to extend the opts:

  requestPort: 'requestPort',
  responsePort: 'responsePort',

  bindPorts: bindPorts,
});

and provide an intercept:

const bindPorts = function(app) {
, parseRoute = UrlParser.parseString Route.route
, update = update
, subscriptions = subscriptions
, interop = Serverless.Interop interopEncode interopDecoder
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replacing general purpose subscriptions with interop parameter. This is because we need to associate calls to JavaScript with specific connections. Serverless.Interop provides a way to do that

@coveralls
Copy link

Coverage Status

Coverage decreased (-4.5%) to 95.455% when pulling de4c4c3 on ports into 2aff2e4 on master.

-}
type Interop
= GetRandom Int

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an enumeration of the kinds of interop functions we can call, with signatures

interopEncode interop =
case interop of
GetRandom upper ->
Encode.int upper
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that signatures are not restricted to the usual values that can pass through a port, because we JSON encode the arguments before passing them through the port. This is to avoid a type explosion. The user provides a function to JSON encode arguments to Interop functions

demo/src/API.elm Outdated
( GET, Number ) ->
( Conn.interop (GetRandom 10) conn
, Cmd.none
)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the interop function is being called. Not super happy about the side-effect being put into the conn, but it was the cleanest syntax. It also opens up interop to middleware (i.e. Conn -> Conn functions), which could either be seen as a good thing, or a bad thing.


interop: {
getRandom: upper => Promise.resolve(Math.floor(Math.random() * upper)),
},
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the JavaScript side, the user writes an interop function handler. The interop call was GetRandom 10, so the function is named getRandom

interopDecoder interopName =
case interopName of
"getRandom" ->
Just <| Decode.map RandomNumber Decode.int
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value that gets returned from JavaScript needs to be decoded, and wrapped in an app msg

demo/src/API.elm Outdated
RandomNumber val ->
( Conn.respond ( 200, text <| (++) "Random value: " <| toString val ) conn
, Cmd.none
)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now the interop value can be handled in the usual update function

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling ba4a541 on ports into 2aff2e4 on master.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 13cd7cd on ports into 2aff2e4 on master.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 71ba75e on ports into 2aff2e4 on master.

@ktonon ktonon changed the title JavaScript Interop (wip) JavaScript Interop Aug 10, 2017
Also Converted endpoint from message into a function.
@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 9ffd3a6 on ports into 2aff2e4 on master.

@ktonon ktonon merged commit eb9f8b2 into master Aug 10, 2017
@ktonon
Copy link
Owner Author

ktonon commented Aug 10, 2017

Fixes #2

@ktonon ktonon deleted the ports branch August 11, 2017 00:17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants