v 1.2.1
This commit is contained in:
@@ -38,7 +38,11 @@ client.connect().then(async () => {
|
||||
|
||||
# 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
|
||||
import {Backend, Frontend, Utils} from 'rpclibrary'
|
||||
@@ -52,7 +56,7 @@ const server = new Backend.RPCServer(20000, [{
|
||||
{
|
||||
name: 'subscribe',
|
||||
hook: async (callback) => {
|
||||
const resp = Utils.makeSubResponse()
|
||||
const resp = Utils.makeSubResponse() //Convenience method to generate SubscriptionResponse
|
||||
callbacks.set(resp.uuid, callback);
|
||||
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
|
||||
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
|
||||
type MyInterface = RPCInterface<{
|
||||
Group1: {
|
||||
@@ -120,8 +146,5 @@ new RPCServer<{a:string}, MyInterface>(20001,
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
# Documentation
|
||||
[https://gitea.frontblock.me/fw-docs/rpclibrary]
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "rpclibrary",
|
||||
"version": "1.2.0",
|
||||
"version": "1.2.1",
|
||||
"description": "rpclibrary is a websocket on steroids!",
|
||||
"main": "./js/Index.js",
|
||||
"repository": {
|
||||
|
||||
+3
-2
@@ -116,9 +116,10 @@ const extractArgs = (f:Function):string[] => {
|
||||
* Simple utility function to create basic {@link SubscriptionResponse}
|
||||
* @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 {
|
||||
result: "Success",
|
||||
uuid: uuid?uuid:uuidv4(),
|
||||
uuid: uuidv4(),
|
||||
...extension
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -111,7 +111,7 @@ describe('RPCSocket', () => {
|
||||
if(x === 6)
|
||||
done()
|
||||
else
|
||||
done(new Error('echo RPC response did not match'))
|
||||
done(new Error('add RPC response did not match'))
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user