/** * 不带路由参数的远程调用代理 */ export interface RemoterProxy { /** * 使用默认路由参数null调用rpc */ defaultRoute: F; /** * 路由到serverId服务器,并返回rpc函数 * notify: 只发送消息,不接收返回,节省一次通信。 * (notify只有tcp的rpc协议支持.其它协议不要使用 notify) */ to(serverId: string, notify?: boolean): F; /** * 广播到所有定义了这个remoter的服务器 */ broadcast: F; } /** * 带路由参数的远程调用代理 */ export interface RemoterProxyWithRoute extends RemoterProxy { /** * 路由到routeParam,并返回rpc调用函数 * notify: 只发送消息,不接收返回,节省一次通信。 * (notify只有tcp的rpc协议支持.其它rpc协议不要使用 notify) */ route(routeParam: ROUTE, notify?: boolean): F; // 兼容老的写法 (routeParam: ROUTE, ...args: any[]): Promise; toServer(serverId: string, ...args: any[]): Promise; toServer(serverId: '*', ...args: any[]): Promise; } export function bindRemoterMethod(method: F & (() => Promise), thisArg: T, routeParamType: new (...args: any[]) => ROUTE): RemoterProxyWithRoute & ((routeParam: ROUTE) => Promise) & { toServer(serverId: string): Promise }; export function bindRemoterMethod(method: F & ((arg1: T1) => Promise), thisArg: T, routeParamType: new (...args: any[]) => ROUTE): RemoterProxyWithRoute & ((routeParam: ROUTE, arg1: T1) => Promise) & { toServer(serverId: string, arg1: T1): Promise }; export function bindRemoterMethod(method: F & ((arg1: T1, arg2: T2) => Promise), thisArg: T, routeParamType: new (...args: any[]) => ROUTE): RemoterProxyWithRoute & ((routeParam: ROUTE, arg1: T1, arg2: T2) => Promise) & { toServer(serverId: string, arg1: T1, arg2: T2): Promise }; export function bindRemoterMethod(method: F & ((arg1: T1, arg2: T2, arg3: T3) => Promise), thisArg: T, routeParamType: new (...args: any[]) => ROUTE): RemoterProxyWithRoute & ((routeParam: ROUTE, arg1: T1, arg2: T2, arg3: T3) => Promise) & { toServer(serverId: string, arg1: T1, arg2: T2, arg3: T3): Promise }; export function bindRemoterMethod(method: F & ((arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise), thisArg: T, routeParamType: new (...args: any[]) => ROUTE): RemoterProxyWithRoute & ((routeParam: ROUTE, arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise) & { toServer(serverId: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4): Promise }; export function bindRemoterMethod(method: F & ((arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise), thisArg: T, routeParamType: new (...args: any[]) => ROUTE): RemoterProxyWithRoute & ((routeParam: ROUTE, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise) & { toServer(serverId: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5): Promise }; export function bindRemoterMethod(method: F & ((arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6) => Promise), thisArg: T, routeParamType: new (...args: any[]) => ROUTE): RemoterProxyWithRoute & ((routeParam: ROUTE, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6) => Promise) & { toServer(serverId: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6): Promise }; export function bindRemoterMethod(method: F & ((arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7) => Promise), thisArg: T, routeParamType: new (...args: any[]) => ROUTE): RemoterProxyWithRoute & ((routeParam: ROUTE, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7) => Promise) & { toServer(serverId: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T): Promise }; export function bindRemoterMethod(method: F & ((arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8) => Promise), thisArg: T, routeParamType: new (...args: any[]) => ROUTE): RemoterProxyWithRoute & ((routeParam: ROUTE, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8) => Promise) & { toServer(serverId: string, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8): Promise }; export function bindRemoterMethod(method: F, thisArg: T): RemoterProxy; export function bindRemoterMethod(method: F, thisArg: T, routeParamType?: new (...args: any[]) => ROUTE): RemoterProxyWithRoute { return method.bind(thisArg) as any; } export type RemoterClass = { [P in keyof T]?: RemoterProxyWithRoute; }; type TestRoutify = { [P in K]: RemoterProxyWithRoute }; type FunctionKeys = { [K in keyof T]: T[K] extends Function ? K : never }[keyof T]; export type DefineRoutifyMethods = RemoterClass;