Browse Source

clean up RPC structure

master
peter 4 years ago
parent
commit
160be94e3b
4 changed files with 62 additions and 50 deletions
  1. 8
    12
      src/Types.ts
  2. 13
    15
      src/Utils.ts
  3. 24
    19
      test/TestBackend.ts
  4. 17
    4
      test/TestFrontend.ts

+ 8
- 12
src/Types.ts View File

@@ -18,36 +18,32 @@ export type SocketConf = {
18 18
 
19 19
 export type RPCType = 'Hook' | 'Unhook' | 'Call'
20 20
 
21
-export type BaseRPC = { 
22
-    type: RPCType
21
+export type HookRPC = {
23 22
     name: Name
24
-}
25
-
26
-export type HookRPC = BaseRPC & {
27
-    type: 'Hook'
28 23
     hook: HookFunction
29 24
     onCallback?: CallbackFunction,
30 25
     onClose?: HookCloseFunction
31 26
 }
32 27
 
33
-export type CallRPC = (BaseRPC & {
34
-    type: 'Call'
28
+export type CallRPC = {
29
+    name: Name
35 30
     call: AsyncFunction
36
-} ) | Function
31
+} | Function
37 32
 
38 33
 export type RPC = CallRPC | HookRPC
39 34
 
40 35
 export type BaseInfo = {
36
+    name: Name,
41 37
     owner: Name,
42 38
     argNames: Name[],
43 39
 }
44 40
 
45
-export type HookInfo = BaseRPC & BaseInfo & { 
41
+export type HookInfo = BaseInfo & { 
46 42
     type: 'Hook', 
47 43
     generator: (socket) => HookFunction
48 44
 }
49 45
 
50
-export type CallInfo = BaseRPC & BaseInfo & {
46
+export type CallInfo = BaseInfo & {
51 47
     type: 'Call',
52 48
     call: AsyncFunction
53 49
 }
@@ -56,7 +52,7 @@ export type RpcInfo = HookInfo |  CallInfo
56 52
 export type ExtendedRpcInfo = RpcInfo & { uniqueName: string } 
57 53
 
58 54
 export type OnFunction = (type: 'error' | 'close', f: (e?:any)=>void) => I.Socket
59
-export type HookCloseFunction = (res:R.SubscriptionResponse) => any
55
+export type HookCloseFunction = (res:R.SubscriptionResponse, rpc:HookRPC) => any
60 56
 export type HookFunction = (...args:any[]) => Promise<R.SubscriptionResponse | R.ErrorResponse>
61 57
 export type AsyncFunction = (...args:any[]) => Promise<any>
62 58
 export type CallbackFunction = (arg: any) => void

+ 13
- 15
src/Utils.ts View File

@@ -6,27 +6,24 @@ import * as I from "./Interfaces";
6 6
 export const rpcToRpcinfo = (rpc : T.RPC, owner: T.Owner):T.RpcInfo => {
7 7
     switch (typeof rpc){
8 8
         case  "object":
9
-        switch(rpc.type){
10
-            case "Call" :
9
+            if(rpc['call']){
11 10
                 return {
12 11
                     owner: owner,
13
-                    argNames: extractArgs(rpc.call),
14
-                    type: rpc.type,
12
+                    argNames: extractArgs(rpc['call']),
13
+                    type: "Call",
15 14
                     name: rpc.name,
16
-                    call: rpc.call,
15
+                    call: rpc['call'],
17 16
                 }
18
-            case "Hook" :
19
-                const generator = hookGenerator(rpc)
17
+            }else{
18
+                const generator = hookGenerator(<T.HookRPC>rpc)
20 19
                 return {
21 20
                     owner: owner,
22 21
                     argNames: extractArgs(generator(undefined)),
23
-                    type: rpc.type,
22
+                    type: "Hook",
24 23
                     name: rpc.name,
25 24
                     generator: generator,
26
-                    
27 25
                 }
28
-        }
29
-        break;
26
+            }
30 27
         case "function":
31 28
             if(!rpc.name) throw new Error(`
32 29
             RPC did not provide a name. 
@@ -79,10 +76,11 @@ const hookGenerator = (rpc:T.HookRPC): T.HookInfo['generator'] => {
79 76
             socket.call.apply(socket, [res.uid, ...cbargs])
80 77
         })
81 78
         if(res.result == 'Success'){
82
-            if(rpc.onClose)
83
-            socket.on('close', async () => {
84
-                rpc.onClose(res)
85
-            })
79
+            if(rpc.onClose){
80
+                socket.on('close', async () => {
81
+                    rpc.onClose(res, rpc)
82
+                })
83
+            }
86 84
         }
87 85
         return res
88 86
     }`)

+ 24
- 19
test/TestBackend.ts View File

@@ -6,26 +6,31 @@ let subcallback
6 6
 
7 7
 new RPCServer(20000, [{
8 8
     name: "HelloWorldRPCGroup",
9
-    exportRPCs: () => [{
10
-        type: 'Call',
11
-        name: 'echo',
12
-        call: async (s:string) => s,
13
-    },
14
-    {
15
-        type: 'Hook',
16
-        name: 'subscribe',
17
-        hook: async (callback):Promise<any> => {
18
-            subcallback = callback
19
-            return new SubscriptionResponse(""+Math.random())
9
+    exportRPCs: () => [
10
+        {
11
+            name: 'echo',
12
+            call: async (s:string) => s,
13
+        },{
14
+            name: 'simpleSubscribe',
15
+            hook: async(callback) => {
16
+                subcallback =  callback
17
+                return new SubscriptionResponse(""+Math.random())
18
+            }
19
+        },{
20
+            name: 'subscribe',
21
+            hook: async (callback):Promise<any> => {
22
+                subcallback = callback
23
+                return new SubscriptionResponse(""+Math.random())
24
+            },
25
+            onClose: (res:SubscriptionResponse, rpc:HookRPC) => { 
26
+                console.log("Specific close handler for", rpc.name, res)
27
+                subcallback = null 
28
+            },
29
+            onCallback: (...args) => { console.log.apply(console, args) }
20 30
         },
21
-        onClose: (res:SubscriptionResponse) => { 
22
-            console.log("Specific close handler for", res)
23
-            subcallback = null 
24
-        }
25
-    },
26
-    function add(...args:number[]):number {return args.reduce((a,b)=>a+b, 0)},
27
-    function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)},
28
-]
31
+        function add(...args:number[]):number {return args.reduce((a,b)=>a+b, 0)},
32
+        function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)},
33
+    ]
29 34
 }])
30 35
 
31 36
 try{

+ 17
- 4
test/TestFrontend.ts View File

@@ -13,11 +13,24 @@ client.connect().then(async _ => {
13 13
     const handler = (s) => {
14 14
         counter++
15 15
         if(counter === 3)
16
-            console.log("callback was called 3 times", counter === 3)
16
+        console.log("subscribe call counter met", counter === 3)
17 17
     }
18 18
 
19 19
     await client["HelloWorldRPCGroup"].subscribe(handler)
20
-    client["HelloWorldRPCGroup"].triggerCallback("test1", "test1", "test1", )
21
-    client["HelloWorldRPCGroup"].triggerCallback("test2", "test2", "test2", )
22
-    client["HelloWorldRPCGroup"].triggerCallback("test3", "test3", "test3", )
20
+    await Promise.all([
21
+        client["HelloWorldRPCGroup"].triggerCallback("test1", "test1", "test1", ),
22
+        client["HelloWorldRPCGroup"].triggerCallback("test2", "test2", "test2", ),
23
+        client["HelloWorldRPCGroup"].triggerCallback("test3", "test3", "test3", ),
24
+    ])
25
+
26
+    counter = 0
27
+    const simplehandler = (s) => {
28
+        counter++
29
+        if(counter === 3)
30
+            console.log("simpleSubscribe call counter met", counter === 3)
31
+    }
32
+    await client["HelloWorldRPCGroup"].simpleSubscribe(simplehandler)
33
+    client["HelloWorldRPCGroup"].triggerCallback("simple1", "simple1", "simple1", )
34
+    client["HelloWorldRPCGroup"].triggerCallback("simple2", "simple2", "simple2", )
35
+    client["HelloWorldRPCGroup"].triggerCallback("simple3", "simple3", "simple3", )
23 36
 })

Loading…
Cancel
Save