clean up RPC structure

This commit is contained in:
2019-09-21 13:11:57 +02:00
parent 759bb9c40f
commit 160be94e3b
4 changed files with 62 additions and 50 deletions
+8 -12
View File
@@ -18,36 +18,32 @@ export type SocketConf = {
export type RPCType = 'Hook' | 'Unhook' | 'Call' export type RPCType = 'Hook' | 'Unhook' | 'Call'
export type BaseRPC = { export type HookRPC = {
type: RPCType
name: Name name: Name
}
export type HookRPC = BaseRPC & {
type: 'Hook'
hook: HookFunction hook: HookFunction
onCallback?: CallbackFunction, onCallback?: CallbackFunction,
onClose?: HookCloseFunction onClose?: HookCloseFunction
} }
export type CallRPC = (BaseRPC & { export type CallRPC = {
type: 'Call' name: Name
call: AsyncFunction call: AsyncFunction
} ) | Function } | Function
export type RPC = CallRPC | HookRPC export type RPC = CallRPC | HookRPC
export type BaseInfo = { export type BaseInfo = {
name: Name,
owner: Name, owner: Name,
argNames: Name[], argNames: Name[],
} }
export type HookInfo = BaseRPC & BaseInfo & { export type HookInfo = BaseInfo & {
type: 'Hook', type: 'Hook',
generator: (socket) => HookFunction generator: (socket) => HookFunction
} }
export type CallInfo = BaseRPC & BaseInfo & { export type CallInfo = BaseInfo & {
type: 'Call', type: 'Call',
call: AsyncFunction call: AsyncFunction
} }
@@ -56,7 +52,7 @@ export type RpcInfo = HookInfo | CallInfo
export type ExtendedRpcInfo = RpcInfo & { uniqueName: string } export type ExtendedRpcInfo = RpcInfo & { uniqueName: string }
export type OnFunction = (type: 'error' | 'close', f: (e?:any)=>void) => I.Socket 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<R.SubscriptionResponse | R.ErrorResponse> export type HookFunction = (...args:any[]) => Promise<R.SubscriptionResponse | R.ErrorResponse>
export type AsyncFunction = (...args:any[]) => Promise<any> export type AsyncFunction = (...args:any[]) => Promise<any>
export type CallbackFunction = (arg: any) => void export type CallbackFunction = (arg: any) => void
+13 -15
View File
@@ -6,27 +6,24 @@ import * as I from "./Interfaces";
export const rpcToRpcinfo = (rpc : T.RPC, owner: T.Owner):T.RpcInfo => { export const rpcToRpcinfo = (rpc : T.RPC, owner: T.Owner):T.RpcInfo => {
switch (typeof rpc){ switch (typeof rpc){
case "object": case "object":
switch(rpc.type){ if(rpc['call']){
case "Call" :
return { return {
owner: owner, owner: owner,
argNames: extractArgs(rpc.call), argNames: extractArgs(rpc['call']),
type: rpc.type, type: "Call",
name: rpc.name, name: rpc.name,
call: rpc.call, call: rpc['call'],
} }
case "Hook" : }else{
const generator = hookGenerator(rpc) const generator = hookGenerator(<T.HookRPC>rpc)
return { return {
owner: owner, owner: owner,
argNames: extractArgs(generator(undefined)), argNames: extractArgs(generator(undefined)),
type: rpc.type, type: "Hook",
name: rpc.name, name: rpc.name,
generator: generator, generator: generator,
} }
} }
break;
case "function": case "function":
if(!rpc.name) throw new Error(` if(!rpc.name) throw new Error(`
RPC did not provide a name. 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]) socket.call.apply(socket, [res.uid, ...cbargs])
}) })
if(res.result == 'Success'){ if(res.result == 'Success'){
if(rpc.onClose) if(rpc.onClose){
socket.on('close', async () => { socket.on('close', async () => {
rpc.onClose(res) rpc.onClose(res, rpc)
}) })
}
} }
return res return res
}`) }`)
+24 -19
View File
@@ -6,26 +6,31 @@ let subcallback
new RPCServer(20000, [{ new RPCServer(20000, [{
name: "HelloWorldRPCGroup", name: "HelloWorldRPCGroup",
exportRPCs: () => [{ exportRPCs: () => [
type: 'Call', {
name: 'echo', name: 'echo',
call: async (s:string) => s, call: async (s:string) => s,
}, },{
{ name: 'simpleSubscribe',
type: 'Hook', hook: async(callback) => {
name: 'subscribe', subcallback = callback
hook: async (callback):Promise<any> => { return new SubscriptionResponse(""+Math.random())
subcallback = callback }
return new SubscriptionResponse(""+Math.random()) },{
name: 'subscribe',
hook: async (callback):Promise<any> => {
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) => { function add(...args:number[]):number {return args.reduce((a,b)=>a+b, 0)},
console.log("Specific close handler for", res) function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)},
subcallback = null ]
}
},
function add(...args:number[]):number {return args.reduce((a,b)=>a+b, 0)},
function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)},
]
}]) }])
try{ try{
+17 -4
View File
@@ -13,11 +13,24 @@ client.connect().then(async _ => {
const handler = (s) => { const handler = (s) => {
counter++ counter++
if(counter === 3) 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) await client["HelloWorldRPCGroup"].subscribe(handler)
client["HelloWorldRPCGroup"].triggerCallback("test1", "test1", "test1", ) await Promise.all([
client["HelloWorldRPCGroup"].triggerCallback("test2", "test2", "test2", ) client["HelloWorldRPCGroup"].triggerCallback("test1", "test1", "test1", ),
client["HelloWorldRPCGroup"].triggerCallback("test3", "test3", "test3", ) 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", )
}) })