Browse Source

v 1.2.1

master
peter 5 years ago
parent
commit
5690a572d3
4 changed files with 34 additions and 10 deletions
  1. 29
    6
      README.md
  2. 1
    1
      package.json
  3. 3
    2
      src/Utils.ts
  4. 1
    1
      test/Test.ts

+ 29
- 6
README.md View File

38
 
38
 
39
 # Using callbacks
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
 ```typescript
47
 ```typescript
44
 import {Backend, Frontend, Utils} from 'rpclibrary'
48
 import {Backend, Frontend, Utils} from 'rpclibrary'
52
         {
56
         {
53
             name: 'subscribe',
57
             name: 'subscribe',
54
             hook: async (callback) => {
58
             hook: async (callback) => {
55
-                const resp = Utils.makeSubResponse()
59
+                const resp = Utils.makeSubResponse() //Convenience method to generate SubscriptionResponse
56
                 callbacks.set(resp.uuid, callback); 
60
                 callbacks.set(resp.uuid, callback); 
57
                 return resp
61
                 return resp
58
             }
62
             }
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
 #Experimental typing support
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
 ```typescript
108
 ```typescript
83
 type MyInterface = RPCInterface<{ 
109
 type MyInterface = RPCInterface<{ 
84
     Group1: { 
110
     Group1: { 
120
 )
146
 )
121
 ```
147
 ```
122
 
148
 
123
-
124
-
125
-
126
 # Documentation
149
 # Documentation
127
 [https://gitea.frontblock.me/fw-docs/rpclibrary]
150
 [https://gitea.frontblock.me/fw-docs/rpclibrary]

+ 1
- 1
package.json View File

1
 {
1
 {
2
   "name": "rpclibrary",
2
   "name": "rpclibrary",
3
-  "version": "1.2.0",
3
+  "version": "1.2.1",
4
   "description": "rpclibrary is a websocket on steroids!",
4
   "description": "rpclibrary is a websocket on steroids!",
5
   "main": "./js/Index.js",
5
   "main": "./js/Index.js",
6
   "repository": {
6
   "repository": {

+ 3
- 2
src/Utils.ts View File

116
  * Simple utility function to create basic {@link SubscriptionResponse}
116
  * Simple utility function to create basic {@link SubscriptionResponse}
117
  * @param uuid optional uuid to use, otherwise defaults to uuid/v4
117
  * @param uuid optional uuid to use, otherwise defaults to uuid/v4
118
  */
118
  */
119
-export function makeSubResponse(uuid?:string):SubscriptionResponse{
119
+export function makeSubResponse<T extends {} = {}>(extension:T):SubscriptionResponse & T{
120
     return {
120
     return {
121
         result: "Success",
121
         result: "Success",
122
-        uuid: uuid?uuid:uuidv4(),
122
+        uuid: uuidv4(),
123
+        ...extension
123
     }
124
     }
124
 }
125
 }

+ 1
- 1
test/Test.ts View File

111
             if(x === 6)
111
             if(x === 6)
112
                 done()
112
                 done()
113
             else
113
             else
114
-                done(new Error('echo RPC response did not match'))   
114
+                done(new Error('add RPC response did not match'))   
115
         })
115
         })
116
     })
116
     })
117
 
117
 

Loading…
Cancel
Save