From 160be94e3bc5a4e156c9c81b0820919336222525 Mon Sep 17 00:00:00 2001 From: peter Date: Sat, 21 Sep 2019 13:11:57 +0200 Subject: [PATCH] clean up RPC structure --- src/Types.ts | 20 ++++++++------------ src/Utils.ts | 28 +++++++++++++--------------- test/TestBackend.ts | 43 ++++++++++++++++++++++++------------------- test/TestFrontend.ts | 21 +++++++++++++++++---- 4 files changed, 62 insertions(+), 50 deletions(-) diff --git a/src/Types.ts b/src/Types.ts index 7a9e12e..fe551fd 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -18,36 +18,32 @@ export type SocketConf = { export type RPCType = 'Hook' | 'Unhook' | 'Call' -export type BaseRPC = { - type: RPCType +export type HookRPC = { name: Name -} - -export type HookRPC = BaseRPC & { - type: 'Hook' hook: HookFunction onCallback?: CallbackFunction, onClose?: HookCloseFunction } -export type CallRPC = (BaseRPC & { - type: 'Call' +export type CallRPC = { + name: Name call: AsyncFunction -} ) | Function +} | Function export type RPC = CallRPC | HookRPC export type BaseInfo = { + name: Name, owner: Name, argNames: Name[], } -export type HookInfo = BaseRPC & BaseInfo & { +export type HookInfo = BaseInfo & { type: 'Hook', generator: (socket) => HookFunction } -export type CallInfo = BaseRPC & BaseInfo & { +export type CallInfo = BaseInfo & { type: 'Call', call: AsyncFunction } @@ -56,7 +52,7 @@ export type RpcInfo = HookInfo | CallInfo export type ExtendedRpcInfo = RpcInfo & { uniqueName: string } export type OnFunction = (type: 'error' | 'close', f: (e?:any)=>void) => I.Socket -export type HookCloseFunction = (res:R.SubscriptionResponse) => any +export type HookCloseFunction = (res:R.SubscriptionResponse, rpc:HookRPC) => any export type HookFunction = (...args:any[]) => Promise export type AsyncFunction = (...args:any[]) => Promise export type CallbackFunction = (arg: any) => void \ No newline at end of file diff --git a/src/Utils.ts b/src/Utils.ts index 785cd1e..643b349 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -6,27 +6,24 @@ import * as I from "./Interfaces"; export const rpcToRpcinfo = (rpc : T.RPC, owner: T.Owner):T.RpcInfo => { switch (typeof rpc){ case "object": - switch(rpc.type){ - case "Call" : + if(rpc['call']){ return { owner: owner, - argNames: extractArgs(rpc.call), - type: rpc.type, + argNames: extractArgs(rpc['call']), + type: "Call", name: rpc.name, - call: rpc.call, + call: rpc['call'], } - case "Hook" : - const generator = hookGenerator(rpc) + }else{ + const generator = hookGenerator(rpc) return { owner: owner, argNames: extractArgs(generator(undefined)), - type: rpc.type, + type: "Hook", name: rpc.name, generator: generator, - } - } - break; + } case "function": if(!rpc.name) throw new Error(` RPC did not provide a name. @@ -79,10 +76,11 @@ const hookGenerator = (rpc:T.HookRPC): T.HookInfo['generator'] => { socket.call.apply(socket, [res.uid, ...cbargs]) }) if(res.result == 'Success'){ - if(rpc.onClose) - socket.on('close', async () => { - rpc.onClose(res) - }) + if(rpc.onClose){ + socket.on('close', async () => { + rpc.onClose(res, rpc) + }) + } } return res }`) diff --git a/test/TestBackend.ts b/test/TestBackend.ts index a80ffc4..994282d 100644 --- a/test/TestBackend.ts +++ b/test/TestBackend.ts @@ -6,26 +6,31 @@ let subcallback new RPCServer(20000, [{ name: "HelloWorldRPCGroup", - exportRPCs: () => [{ - type: 'Call', - name: 'echo', - call: async (s:string) => s, - }, - { - type: 'Hook', - name: 'subscribe', - hook: async (callback):Promise => { - subcallback = callback - return new SubscriptionResponse(""+Math.random()) + exportRPCs: () => [ + { + name: 'echo', + call: async (s:string) => s, + },{ + name: 'simpleSubscribe', + hook: async(callback) => { + subcallback = callback + return new SubscriptionResponse(""+Math.random()) + } + },{ + name: 'subscribe', + hook: async (callback):Promise => { + subcallback = callback + return new SubscriptionResponse(""+Math.random()) + }, + onClose: (res:SubscriptionResponse, rpc:HookRPC) => { + console.log("Specific close handler for", rpc.name, res) + subcallback = null + }, + onCallback: (...args) => { console.log.apply(console, args) } }, - onClose: (res:SubscriptionResponse) => { - console.log("Specific close handler for", res) - subcallback = null - } - }, - function add(...args:number[]):number {return args.reduce((a,b)=>a+b, 0)}, - function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)}, -] + function add(...args:number[]):number {return args.reduce((a,b)=>a+b, 0)}, + function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)}, + ] }]) try{ diff --git a/test/TestFrontend.ts b/test/TestFrontend.ts index 6f8866f..5d541e9 100644 --- a/test/TestFrontend.ts +++ b/test/TestFrontend.ts @@ -13,11 +13,24 @@ client.connect().then(async _ => { const handler = (s) => { counter++ if(counter === 3) - console.log("callback was called 3 times", counter === 3) + console.log("subscribe call counter met", counter === 3) } await client["HelloWorldRPCGroup"].subscribe(handler) - client["HelloWorldRPCGroup"].triggerCallback("test1", "test1", "test1", ) - client["HelloWorldRPCGroup"].triggerCallback("test2", "test2", "test2", ) - client["HelloWorldRPCGroup"].triggerCallback("test3", "test3", "test3", ) + await Promise.all([ + client["HelloWorldRPCGroup"].triggerCallback("test1", "test1", "test1", ), + client["HelloWorldRPCGroup"].triggerCallback("test2", "test2", "test2", ), + client["HelloWorldRPCGroup"].triggerCallback("test3", "test3", "test3", ), + ]) + + counter = 0 + const simplehandler = (s) => { + counter++ + if(counter === 3) + console.log("simpleSubscribe call counter met", counter === 3) + } + await client["HelloWorldRPCGroup"].simpleSubscribe(simplehandler) + client["HelloWorldRPCGroup"].triggerCallback("simple1", "simple1", "simple1", ) + client["HelloWorldRPCGroup"].triggerCallback("simple2", "simple2", "simple2", ) + client["HelloWorldRPCGroup"].triggerCallback("simple3", "simple3", "simple3", ) })