Comparing version 0.14.0 to 0.15.0
@@ -28,3 +28,5 @@ // Generated by LiveScript 1.4.0 | ||
debug("handling message '" + message + "' in response to '" + responseTo + "'"); | ||
handler(message, payload); | ||
handler(payload, { | ||
outcome: message | ||
}); | ||
} | ||
@@ -31,0 +33,0 @@ return !!handler; |
{ | ||
"name": "exorelay", | ||
"version": "0.14.0", | ||
"version": "0.15.0", | ||
"author": "Kevin Goslar", | ||
@@ -5,0 +5,0 @@ "dependencies": { |
@@ -48,5 +48,5 @@ # Exosphere Communication Relay for JavaScript | ||
```coffeescript | ||
exoRelay.registerHandler 'users.create', (userData, {reply}) -> | ||
exoRelay.registerHandler 'user.create', (userData, {reply}) -> | ||
# on this line we would create a user database record with the attributes given in userData | ||
reply 'users.created', id: 456, name: userData.name | ||
reply 'user.created', id: 456, name: userData.name | ||
``` | ||
@@ -70,4 +70,8 @@ | ||
You can handle the incoming replies to your outgoing messages: | ||
## Handle incoming replies | ||
If a message you send expects a reply, | ||
you can provide the handler for it right when you send it: | ||
```coffeescript | ||
@@ -78,2 +82,79 @@ exoRelay.send 'users.create', name: 'Will Riker', (createdUser) -> | ||
Service calls are more expensive than in-process function calls. | ||
They are also higher-level, crossing functional boundaries within your application. | ||
Hence they (should) have more complex APIs than function calls. | ||
* replies to commands often return the state changes caused by the command, | ||
to avoid having to do another call to the service to query the new state | ||
* commands often have more than one outcome. | ||
For example, the command | ||
_"transfer $100 from the checking account to the savings account"_ | ||
sent to an accounting service can reply with: | ||
<table> | ||
<tr> | ||
<th>transferred</th> | ||
<td>the money was transferred</td> | ||
</tr> | ||
<tr> | ||
<th>pending</th> | ||
<td>the transfer was initiated, but is pending a third-party approval</td> | ||
</tr> | ||
<tr> | ||
<th>transaction limit exceeded</th> | ||
<td>the account doesn't allow that much money to be transferred at once</td> | ||
</tr> | ||
<tr> | ||
<th>daily limit exceeded</th> | ||
<td>the daily transaction limit was exceeded</td> | ||
</tr> | ||
<tr> | ||
<th>insufficient funds</th> | ||
<td>there isn't enough money in the checking account</td> | ||
</tr> | ||
<tr> | ||
<th>unknown account</th> | ||
<td>one of the given accounts was not found</td> | ||
</tr> | ||
<tr> | ||
<th>unauthorized</th> | ||
<td>the currently logged in user does not have privileges to make this transfer</td> | ||
</tr> | ||
<tr> | ||
<th>internal error</th> | ||
<td>an internal error occurred in the accounting service</td> | ||
</tr> | ||
</table> | ||
The outcome is provided as part of the optional second parameter to the reply handler. | ||
```livescript | ||
exoRelay.send 'transfer', amount: 100, from: 'checking', to: 'savings', (txn, {outcome}) -> | ||
switch outcome | ||
| 'transferred' => ... | ||
| 'pending' => ... | ||
| ... | ||
``` | ||
A different use case for checking outcomes is ongoing monitoring of commands | ||
that take a while to execute. | ||
A service can send multiple replies, causing the reply handler to be called | ||
multiple times. Each reply can be a different message type: | ||
```livescript | ||
exoRelay.send 'file.copy', from: 'large.csv', to: 'backup.csv', (payload, {outcome}) -> | ||
switch outcome | ||
| 'file.copying' => console.log "still copying, #{payload.percent}% done" | ||
| 'file.copied' => console.log 'file copy finished!' | ||
``` | ||
Another use case is streaming responses, where a larger result is sent in chunks: | ||
```livescript | ||
exoRelay.send 'file.read', path: 'large.csv', (payload, {outcome}) -> | ||
switch outcome | ||
| 'file.read-chunk' => result += payload | ||
| 'file.read-done' => console.log "finished reading #{payload.megabytes} MB!" | ||
``` | ||
More examples for handling incoming replies are [here](features/incoming-replies.feature). | ||
@@ -83,7 +164,5 @@ Message handlers also provide a shortcut to send messages: | ||
```coffeescript | ||
exoRelay.registerHandler 'users.create', (userData, {send, reply}) -> | ||
send 'passwords.encrypt' userData.password, (encryptedPassword) -> | ||
userData.encryptedPassword = encryptedPassword | ||
# on this line we would create a user database record with the attributes given in userData | ||
reply 'users.created', id: 456, name: userData.name | ||
exoRelay.registerHandler 'users.create', (createdUser, {send}) -> | ||
send 'passwords.encrypt' createdUser.password, (encryptedPassword) -> | ||
... | ||
``` | ||
@@ -90,0 +169,0 @@ |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
138294
3249
172