浏览代码

v 1.2.1

master
peter 5 年前
父节点
当前提交
5690a572d3
共有 4 个文件被更改,包括 34 次插入10 次删除
  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 查看文件

@@ -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]

+ 1
- 1
package.json 查看文件

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

+ 3
- 2
src/Utils.ts 查看文件

@@ -116,9 +116,10 @@ const extractArgs = (f:Function):string[] => {
116 116
  * Simple utility function to create basic {@link SubscriptionResponse}
117 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 120
     return {
121 121
         result: "Success",
122
-        uuid: uuid?uuid:uuidv4(),
122
+        uuid: uuidv4(),
123
+        ...extension
123 124
     }
124 125
 }

+ 1
- 1
test/Test.ts 查看文件

@@ -111,7 +111,7 @@ describe('RPCSocket', () => {
111 111
             if(x === 6)
112 112
                 done()
113 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
 

正在加载...
取消
保存