Browse Source

clean up RPC structure

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

+ 13
- 15
src/Utils.ts View File

6
 export const rpcToRpcinfo = (rpc : T.RPC, owner: T.Owner):T.RpcInfo => {
6
 export const rpcToRpcinfo = (rpc : T.RPC, owner: T.Owner):T.RpcInfo => {
7
     switch (typeof rpc){
7
     switch (typeof rpc){
8
         case  "object":
8
         case  "object":
9
-        switch(rpc.type){
10
-            case "Call" :
9
+            if(rpc['call']){
11
                 return {
10
                 return {
12
                     owner: owner,
11
                     owner: owner,
13
-                    argNames: extractArgs(rpc.call),
14
-                    type: rpc.type,
12
+                    argNames: extractArgs(rpc['call']),
13
+                    type: "Call",
15
                     name: rpc.name,
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
                 return {
19
                 return {
21
                     owner: owner,
20
                     owner: owner,
22
                     argNames: extractArgs(generator(undefined)),
21
                     argNames: extractArgs(generator(undefined)),
23
-                    type: rpc.type,
22
+                    type: "Hook",
24
                     name: rpc.name,
23
                     name: rpc.name,
25
                     generator: generator,
24
                     generator: generator,
26
-                    
27
                 }
25
                 }
28
-        }
29
-        break;
26
+            }
30
         case "function":
27
         case "function":
31
             if(!rpc.name) throw new Error(`
28
             if(!rpc.name) throw new Error(`
32
             RPC did not provide a name. 
29
             RPC did not provide a name. 
79
             socket.call.apply(socket, [res.uid, ...cbargs])
76
             socket.call.apply(socket, [res.uid, ...cbargs])
80
         })
77
         })
81
         if(res.result == 'Success'){
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
         return res
85
         return res
88
     }`)
86
     }`)

+ 24
- 19
test/TestBackend.ts View File

6
 
6
 
7
 new RPCServer(20000, [{
7
 new RPCServer(20000, [{
8
     name: "HelloWorldRPCGroup",
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
 try{
36
 try{

+ 17
- 4
test/TestFrontend.ts View File

13
     const handler = (s) => {
13
     const handler = (s) => {
14
         counter++
14
         counter++
15
         if(counter === 3)
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
     await client["HelloWorldRPCGroup"].subscribe(handler)
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