v 1.3.0 improve pseudointerfaces

This commit is contained in:
2019-09-30 23:48:05 +02:00
parent 74bfa2545b
commit cc78248e44
8 changed files with 58 additions and 82 deletions
+1 -1
View File
@@ -29,7 +29,7 @@ export class RPCServer<
*/
constructor(
private port:number,
private exporters: I.RPCExporter<T.RPCInterface<InterfaceT>, keyof T.RPCInterface<InterfaceT>, SubResType>[] = [],
private exporters: I.RPCExporter<T.RPCInterface<InterfaceT>, keyof InterfaceT, SubResType>[] = [],
conf: T.SocketConf = {}
){
+1 -1
View File
@@ -129,7 +129,7 @@ export class RPCSocket implements I.Socket{
* @param fnName The function name
* @param fnArgs A string-list of parameters
*/
private callGenerator(fnName: string, fnArgs:string[]): T.AsyncFunction{
private callGenerator(fnName: string, fnArgs:string[]): T.AnyFunction{
const headerArgs = fnArgs.join(",")
const argParams = fnArgs.map(stripAfterEquals).join(",")
return eval( '( () => async ('+headerArgs+') => { return await this.socket.call("'+fnName+'", '+argParams+')} )()' )
+5 -5
View File
@@ -2,15 +2,15 @@ import * as T from "./Types";
import * as I from "./Interfaces"
/**
* Interface for all classes that may export RPCs
* Interface for all classes that export RPCs
*/
export interface RPCExporter<
Ifc extends T.RPCInterface,
K extends keyof Ifc,
Ifc extends T.RPCInterface,
Name extends keyof Ifc,
SubresT = {}
>{
name: K
exportRPCs() : T.RPC<Ifc[K], keyof Ifc[K], SubresT>[]
name: Name
exportRPCs() : T.RPCInterfaceArray<Ifc, SubresT>[Name]
}
/**
+25 -21
View File
@@ -1,9 +1,7 @@
import * as I from "./Interfaces";
export type AnyFunction = (...any:any)=>any
export type AsyncFunction<Fn extends AnyFunction = AnyFunction> = ReturnType<Fn> extends Promise<any> ? Fn : (...any:Parameters<Fn>) => Promise<ReturnType<Fn>>
export type RPCGroup = { [name in string] : AsyncFunction }
export type RPCInterface<Impl extends RPCInterface = {}> = { [groupName in string]: RPCGroup } & Impl
export type AnyFunction = (...args:any) => any
export type HookFunction<F extends AnyFunction = AnyFunction, SubresT = {}> = (...args: Parameters<F>) => Promise<SubscriptionResponse<SubresT> | ErrorResponse>
export type Visibility = "127.0.0.1" | "0.0.0.0"
export type ConnectionHandler = (socket:I.Socket) => void
@@ -19,7 +17,6 @@ export type SocketConf = {
export type ResponseType = "Subscribe" | "Success" | "Error"
export type Outcome = "Success" | "Error"
export type Respose<T> = T & { result: Outcome }
export type SuccessResponse<T = {}> = Respose<T> & { result: "Success" }
export type ErrorResponse<T = {}> = Respose<T> & { result: "Error", message?:string }
@@ -27,22 +24,31 @@ export type SubscriptionResponse<T = {}> = Respose<T> & { result: "Success"; uui
export type RPCType = 'Hook' | 'Unhook' | 'Call'
export type HookT<G extends RPCGroup, K extends keyof G, SubresT> = AsyncFunction<HookFunction<G[K], SubresT>>
export type CallT<G extends RPCGroup, K extends keyof G> = AsyncFunction<G[K]>
export type CallRPC<N, F> = {
name: N
call: F
}
export type HookRPC<G extends RPCGroup, K extends keyof G, SubresT = {}> = {
name: K
hook: HookT<G, K, SubresT>
onCallback?: CallbackFunction,
export type HookRPC<N, F extends AnyFunction, SubresT = {}> = {
name: N
hook: HookFunction<F, SubresT>
onCallback?: AnyFunction
onClose?: HookCloseFunction<SubresT>
}
export type CallRPC<G extends RPCGroup, K extends keyof G> = {
name: K
call: CallT<G,K>
}
export type RPC<N, F extends AnyFunction, SubresT = {}> = HookRPC<N, F, SubresT> | CallRPC<N,F> | F
export type RPC<G extends RPCGroup, Kg extends keyof G, SubresT> = CallRPC<G, Kg> | HookRPC<G, Kg, SubresT>
export type RPCInterface<Impl extends RPCInterface = {}> = {
[grp in string] : {
[rpc in string] : AnyFunction
}
} & Impl
export type RPCInterfaceArray<Itfc extends RPCInterface, SubresT = {}> = {
[grp in keyof Itfc]: Array<
{ [rpc in keyof Itfc[grp]]: RPC<rpc, Itfc[grp][rpc], SubresT> }[keyof Itfc[grp]]
>
}
export type BaseInfo = {
name: string,
@@ -50,14 +56,14 @@ export type BaseInfo = {
argNames: string[],
}
export type HookInfo<T = {}> = BaseInfo & {
export type HookInfo<SubresT = {}> = BaseInfo & {
type: 'Hook',
generator: (socket?:I.Socket) => HookFunction<AnyFunction, T>
generator: (socket?:I.Socket) => (...args:any) => SubresT
}
export type CallInfo = BaseInfo & {
type: 'Call',
call: AsyncFunction
call: AnyFunction
}
export type RpcInfo = HookInfo | CallInfo
@@ -65,5 +71,3 @@ export type ExtendedRpcInfo = RpcInfo & { uniqueName: string }
export type OnFunction = (type: 'error' | 'close', f: (e?:any)=>void) => I.Socket
export type HookCloseFunction<T = {}> = (res:SubscriptionResponse<T>, rpc:HookRPC<any, any, T>) => any
export type HookFunction<F extends AnyFunction = AnyFunction, SubResT = {}> = AsyncFunction<(...args:Parameters<F>) => SubscriptionResponse<SubResT> | ErrorResponse>
export type CallbackFunction = (callback:AnyFunction, ...args:any) => any