This commit is contained in:
2019-09-25 20:53:53 +02:00
parent c45f9a2558
commit 5690a572d3
4 changed files with 34 additions and 10 deletions
+29 -6
View File
@@ -38,7 +38,11 @@ client.connect().then(async () => {
# Using callbacks # Using callbacks
rpclibrary offers a special type of call that can be used with callbacks rpclibrary offers a special type of call that can be used with callbacks. The callback *has to be the last argument* and may be the only passed function.
In order to function, some metadata has to be included in the return value of hooks. On success, the function is expected to return a `type SubscriptionResponse = { result: 'Success', uuid: string }` or in case of errors a `type ErrorResposne = { result: 'Error' }`.
The uuid, as the name implies, is used to uniquely identify the callback for a given invocation and also dictates the name given to the client-side RPC which you should unhook once you're done with it.
```typescript ```typescript
import {Backend, Frontend, Utils} from 'rpclibrary' import {Backend, Frontend, Utils} from 'rpclibrary'
@@ -52,7 +56,7 @@ const server = new Backend.RPCServer(20000, [{
{ {
name: 'subscribe', name: 'subscribe',
hook: async (callback) => { hook: async (callback) => {
const resp = Utils.makeSubResponse() const resp = Utils.makeSubResponse() //Convenience method to generate SubscriptionResponse
callbacks.set(resp.uuid, callback); callbacks.set(resp.uuid, callback);
return resp return resp
} }
@@ -77,8 +81,30 @@ client.connect().then(async () => {
}) })
``` ```
If you need to include further response data into your `SubscriptionResponse` you can extend it using the server's first generic parameter `SubResType`
```typescript
new RPCServer<{extension: string}>({
exportRPCs: () => [
{
name: 'subscribe',
hook: async (callback) => {
return {
result: 'Success',
uuid: 'very_random_string',
extension: 'your_data_here'
}
}
}
]
})
```
#Experimental typing support #Experimental typing support
It is possible to declare pseudo-interfaces for servers and clients It is possible to declare pseudo-interfaces for servers and clients by using server's second generic parameter.
This feature is currently still in development and considered **unstable and untested**. Use with caution.
```typescript ```typescript
type MyInterface = RPCInterface<{ type MyInterface = RPCInterface<{
Group1: { Group1: {
@@ -120,8 +146,5 @@ new RPCServer<{a:string}, MyInterface>(20001,
) )
``` ```
# Documentation # Documentation
[https://gitea.frontblock.me/fw-docs/rpclibrary] [https://gitea.frontblock.me/fw-docs/rpclibrary]
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "rpclibrary", "name": "rpclibrary",
"version": "1.2.0", "version": "1.2.1",
"description": "rpclibrary is a websocket on steroids!", "description": "rpclibrary is a websocket on steroids!",
"main": "./js/Index.js", "main": "./js/Index.js",
"repository": { "repository": {
+3 -2
View File
@@ -116,9 +116,10 @@ const extractArgs = (f:Function):string[] => {
* Simple utility function to create basic {@link SubscriptionResponse} * Simple utility function to create basic {@link SubscriptionResponse}
* @param uuid optional uuid to use, otherwise defaults to uuid/v4 * @param uuid optional uuid to use, otherwise defaults to uuid/v4
*/ */
export function makeSubResponse(uuid?:string):SubscriptionResponse{ export function makeSubResponse<T extends {} = {}>(extension:T):SubscriptionResponse & T{
return { return {
result: "Success", result: "Success",
uuid: uuid?uuid:uuidv4(), uuid: uuidv4(),
...extension
} }
} }
+1 -1
View File
@@ -111,7 +111,7 @@ describe('RPCSocket', () => {
if(x === 6) if(x === 6)
done() done()
else else
done(new Error('echo RPC response did not match')) done(new Error('add RPC response did not match'))
}) })
}) })