|
@@ -38,7 +38,11 @@ client.connect().then(async () => {
|
38
|
38
|
|
39
|
39
|
# Using callbacks
|
40
|
40
|
|
41
|
|
-rpclibrary offers a special type of call that can be used with callbacks
|
|
41
|
+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.
|
|
42
|
+
|
|
43
|
+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' }`.
|
|
44
|
+
|
|
45
|
+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.
|
42
|
46
|
|
43
|
47
|
```typescript
|
44
|
48
|
import {Backend, Frontend, Utils} from 'rpclibrary'
|
|
@@ -52,7 +56,7 @@ const server = new Backend.RPCServer(20000, [{
|
52
|
56
|
{
|
53
|
57
|
name: 'subscribe',
|
54
|
58
|
hook: async (callback) => {
|
55
|
|
- const resp = Utils.makeSubResponse()
|
|
59
|
+ const resp = Utils.makeSubResponse() //Convenience method to generate SubscriptionResponse
|
56
|
60
|
callbacks.set(resp.uuid, callback);
|
57
|
61
|
return resp
|
58
|
62
|
}
|
|
@@ -77,8 +81,30 @@ client.connect().then(async () => {
|
77
|
81
|
})
|
78
|
82
|
```
|
79
|
83
|
|
|
84
|
+If you need to include further response data into your `SubscriptionResponse` you can extend it using the server's first generic parameter `SubResType`
|
|
85
|
+
|
|
86
|
+```typescript
|
|
87
|
+new RPCServer<{extension: string}>({
|
|
88
|
+ exportRPCs: () => [
|
|
89
|
+ {
|
|
90
|
+ name: 'subscribe',
|
|
91
|
+ hook: async (callback) => {
|
|
92
|
+ return {
|
|
93
|
+ result: 'Success',
|
|
94
|
+ uuid: 'very_random_string',
|
|
95
|
+ extension: 'your_data_here'
|
|
96
|
+ }
|
|
97
|
+ }
|
|
98
|
+ }
|
|
99
|
+ ]
|
|
100
|
+})
|
|
101
|
+
|
|
102
|
+```
|
|
103
|
+
|
80
|
104
|
#Experimental typing support
|
81
|
|
-It is possible to declare pseudo-interfaces for servers and clients
|
|
105
|
+It is possible to declare pseudo-interfaces for servers and clients by using server's second generic parameter.
|
|
106
|
+This feature is currently still in development and considered **unstable and untested**. Use with caution.
|
|
107
|
+
|
82
|
108
|
```typescript
|
83
|
109
|
type MyInterface = RPCInterface<{
|
84
|
110
|
Group1: {
|
|
@@ -120,8 +146,5 @@ new RPCServer<{a:string}, MyInterface>(20001,
|
120
|
146
|
)
|
121
|
147
|
```
|
122
|
148
|
|
123
|
|
-
|
124
|
|
-
|
125
|
|
-
|
126
|
149
|
# Documentation
|
127
|
150
|
[https://gitea.frontblock.me/fw-docs/rpclibrary]
|