-
Notifications
You must be signed in to change notification settings - Fork 21
Conversation
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 |
There was a problem hiding this comment.
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
| -} | ||
| type Interop | ||
| = GetRandom Int | ||
|
|
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 | ||
| ) |
There was a problem hiding this comment.
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)), | ||
| }, |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 | ||
| ) |
There was a problem hiding this comment.
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
Also Converted endpoint from message into a function.
|
Fixes #2 |
No description provided.