v 1.3.0 improve pseudointerfaces
This commit is contained in:
+3
-10
@@ -4,6 +4,7 @@ import { RPCServer } from '../src/Backend'
|
||||
import * as uuidv4 from "uuid/v4"
|
||||
import { RPCSocket } from "../src/Frontend";
|
||||
import { SubscriptionResponse } from "../src/Types";
|
||||
import { makeSubResponse } from "../src/Utils";
|
||||
|
||||
const add = (...args:number[]) => {return args.reduce((a,b)=>a+b, 0)}
|
||||
function makeServer(){
|
||||
@@ -18,21 +19,13 @@ function makeServer(){
|
||||
name: 'simpleSubscribe',
|
||||
hook: async(callback) => {
|
||||
subcallback = callback
|
||||
return {
|
||||
result: "Success",
|
||||
uuid: uuidv4(),
|
||||
topic: "test"
|
||||
}
|
||||
return makeSubResponse<{topic: string}>({topic: "test"})
|
||||
}
|
||||
},{
|
||||
name: 'subscribe',
|
||||
hook: async (callback) => {
|
||||
subcallback = callback
|
||||
return {
|
||||
result: "Success",
|
||||
uuid: uuidv4(),
|
||||
topic: "test"
|
||||
}
|
||||
return makeSubResponse<{topic: string}>({topic: "test"})
|
||||
},
|
||||
onClose: (res, rpc) => {
|
||||
console.log("onClose", rpc.name === 'subscribe' && res?"OK":"")
|
||||
|
||||
+20
-41
@@ -1,55 +1,40 @@
|
||||
import { RPCServer } from "../src/Backend";
|
||||
import { SubscriptionResponse, CallRPC, HookRPC, RPCInterface } from "../src/Types";
|
||||
import { SubscriptionResponse, RPCInterface } from "../src/Types";
|
||||
import { RPCSocket } from "../src/Frontend";
|
||||
import { makeSubResponse } from "../src/Utils";
|
||||
|
||||
type MyInterface = RPCInterface<{
|
||||
type SubresExtension = {a:string}
|
||||
|
||||
type MyInterface = {
|
||||
Group1: {
|
||||
triggerCallbacks: (...args:any[]) => Promise<void>,
|
||||
subscribe: (param:string, callback:Function) => Promise<SubscriptionResponse<{a: string}>>,
|
||||
subscribe: (param:string, callback:Function) => Promise<SubscriptionResponse<SubresExtension>>,
|
||||
unsubscribe: (uuid:string) => Promise<void>
|
||||
},
|
||||
Group2: {
|
||||
echo: (x:string) => Promise<string>
|
||||
}
|
||||
}>
|
||||
}
|
||||
|
||||
const callbacks:Map<string, Function> = new Map()
|
||||
|
||||
new RPCServer<{a:string}, MyInterface>(20000,
|
||||
new RPCServer<SubresExtension, MyInterface>(20000,
|
||||
[{
|
||||
name: "Group1",
|
||||
exportRPCs: () => [
|
||||
<HookRPC<MyInterface['Group1'], 'subscribe', {a:string}>>{
|
||||
name: 'subscribe',
|
||||
hook: async (param, callback) => { const _uuid = ""+Math.random(); console.log(param); callbacks.set(_uuid, callback); return { result: 'Success', a: '3', uuid: _uuid} }
|
||||
},
|
||||
<CallRPC<MyInterface['Group1'], 'unsubscribe'>>{
|
||||
name: 'unsubscribe',
|
||||
call: async (uuid) => { callbacks.delete(uuid) }
|
||||
},
|
||||
<CallRPC<MyInterface['Group1'], 'triggerCallbacks'>>{
|
||||
name: 'Group1',
|
||||
exportRPCs: () => [{
|
||||
name: 'triggerCallbacks',
|
||||
call: async (...args) => { callbacks.forEach(cb => cb.apply({}, args)) }
|
||||
}]
|
||||
},{
|
||||
name: 'Group2',
|
||||
exportRPCs: () => [
|
||||
<CallRPC<MyInterface['Group2'], 'echo'>>{
|
||||
name: 'echo',
|
||||
call: async (x) => x
|
||||
}]
|
||||
}]
|
||||
)
|
||||
|
||||
new RPCServer<{a:string}, MyInterface>(20001,
|
||||
[{
|
||||
name: "Group1",
|
||||
exportRPCs: () => []
|
||||
call: async () => { /*...*/ }
|
||||
},{
|
||||
name: 'subscribe',
|
||||
hook: async (param, callback) => { return makeSubResponse<SubresExtension>({a: "test"}) }
|
||||
},{
|
||||
name: 'unsubscribe',
|
||||
call: async(uuid) => { }
|
||||
}
|
||||
]
|
||||
},{
|
||||
name: 'Group2',
|
||||
exportRPCs: () => [{
|
||||
name: 'echo',
|
||||
call: async (x) => x+" lol"
|
||||
call: async (x) => "..."
|
||||
}]
|
||||
}]
|
||||
)
|
||||
@@ -65,10 +50,4 @@ RPCSocket.makeSocket<MyInterface>(20000, 'localhost').then((async (client) => {
|
||||
})
|
||||
|
||||
await client.Group1.triggerCallbacks("Hello", "World", "Callbacks")
|
||||
}))
|
||||
|
||||
RPCSocket.makeSocket<MyInterface>(20001, 'localhost').then((async (client) => {
|
||||
console.log(client)
|
||||
const r = await client.Group2.echo("hee")
|
||||
console.log(r)
|
||||
}))
|
||||
Reference in New Issue
Block a user