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 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<R.SubscriptionResponse | R.ErrorResponse>
export type AsyncFunction = (...args:any[]) => Promise<any>
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 => {
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(<T.HookRPC>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
}`)