clean up RPC structure
This commit is contained in:
+8
-12
@@ -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
|
||||
+10
-12
@@ -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,11 +76,12 @@ const hookGenerator = (rpc:T.HookRPC): T.HookInfo['generator'] => {
|
||||
socket.call.apply(socket, [res.uid, ...cbargs])
|
||||
})
|
||||
if(res.result == 'Success'){
|
||||
if(rpc.onClose)
|
||||
if(rpc.onClose){
|
||||
socket.on('close', async () => {
|
||||
rpc.onClose(res)
|
||||
rpc.onClose(res, rpc)
|
||||
})
|
||||
}
|
||||
}
|
||||
return res
|
||||
}`)
|
||||
}
|
||||
|
||||
+14
-9
@@ -6,26 +6,31 @@ let subcallback
|
||||
|
||||
new RPCServer(20000, [{
|
||||
name: "HelloWorldRPCGroup",
|
||||
exportRPCs: () => [{
|
||||
type: 'Call',
|
||||
exportRPCs: () => [
|
||||
{
|
||||
name: 'echo',
|
||||
call: async (s:string) => s,
|
||||
},
|
||||
{
|
||||
type: 'Hook',
|
||||
},{
|
||||
name: 'simpleSubscribe',
|
||||
hook: async(callback) => {
|
||||
subcallback = callback
|
||||
return new SubscriptionResponse(""+Math.random())
|
||||
}
|
||||
},{
|
||||
name: 'subscribe',
|
||||
hook: async (callback):Promise<any> => {
|
||||
subcallback = callback
|
||||
return new SubscriptionResponse(""+Math.random())
|
||||
},
|
||||
onClose: (res:SubscriptionResponse) => {
|
||||
console.log("Specific close handler for", res)
|
||||
onClose: (res:SubscriptionResponse, rpc:HookRPC) => {
|
||||
console.log("Specific close handler for", rpc.name, res)
|
||||
subcallback = null
|
||||
}
|
||||
},
|
||||
onCallback: (...args) => { console.log.apply(console, args) }
|
||||
},
|
||||
function add(...args:number[]):number {return args.reduce((a,b)=>a+b, 0)},
|
||||
function triggerCallback(...messages:any[]):number {return subcallback.apply({}, messages)},
|
||||
]
|
||||
]
|
||||
}])
|
||||
|
||||
try{
|
||||
|
||||
+17
-4
@@ -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", )
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user