import { Controller , JsonRpcResponse , jsonRpcSerializeAsResponse , Router , Rpc } from "rpc-server" ; import { RequestHandler } from "./request-handler" ; type AsyncCallback = ( ... args : any ) => Promise < any > ; export default class RpcRouter extends Router { private readonly requestHandler : RequestHandler ; constructor ( { controllers , requestHandler } ) { super ( { controllers } ) ; this . requestHandler = requestHandler ; } async dispatch ( rpc ) { const controller = Object . values ( Controller . rpcMethods ) . find ( mapping => mapping . method === rpc . methodName ) ; if ( ! controller ) { throw Error ( `Cannot execute ${ rpc . methodName }: no controller` ) ; } const result = jsonRpcSerializeAsResponse ( { result : await new controller . type ( ) [ controller . callback ] ( this . requestHandler , rpc . parameters ) , type : rpc . methodName } , rpc . id as number ) ; this . requestHandler . outgoing . emit ( rpc . methodName , result ) ; return result ; } async subscribe ( event , callback ) { this . requestHandler . outgoing . on ( event , callback ) ; } async subscribeOnce ( event , callback ) { this . requestHandler . outgoing . once ( event , callback ) ; } async unsubscribe ( event , callback ? ) { this . requestHandler . outgoing . off ( event , callback ) ; } async emit ( event , data , emitter = "incoming" ) { let eventData = data ; if ( ! eventData [ "jsonrpc" ] ) { eventData = jsonRpcSerializeAsResponse ( eventData , Date . now ( ) ) ; } this . requestHandler [ emitter ] . emit ( event , eventData . result ) ; } eventListenerCount ( event ) { return typeof this . requestHandler . outgoing . listenerCount === "function" ? this . requestHandler . outgoing . listenerCount ( event ) : 0 ; } }