import JsonRpcServer , { ErrResCallback } from '../messaging/JsonRpcServer' ; import { AuthenticateRequest , BuyRequest , GetSharedStateRequest , InitAccountRequest , RegisterHubRequest , ListChannelsRequest , ToggleFrameRequest , LockWalletRequest } from './yns' ; import WorkerProxy from '../../frame/WorkerProxy' ; import { AllowedOrigins } from '../messaging/OriginValidator' ; import { ReadyBroadcastEvent } from './ReadyBroadcast' ; import { WorkerReadyBroadcastEvent } from './WorkerReadyBroadcast' ; import { ResetBroadcastEvent } from './ResetBroadcast' ; export default class FrameServer extends JsonRpcServer { private workerProxy : WorkerProxy ; constructor ( allowedOrigins , workerProxy ) { super ( 'FrameServer' , allowedOrigins , window , window . parent ) ; this . workerProxy = workerProxy ; this . passThroughHandler ( RegisterHubRequest . method ) ; this . passThroughHandler ( InitAccountRequest . method ) ; this . passThroughHandler ( BuyRequest . method ) ; this . passThroughHandler ( ListChannelsRequest . method ) ; this . passThroughHandler ( GetSharedStateRequest . method ) ; this . passThroughHandler ( AuthenticateRequest . method ) ; this . passThroughHandler ( ToggleFrameRequest . method ) ; this . passThroughHandler ( LockWalletRequest . method ) ; this . passThroughEvent ( ReadyBroadcastEvent ) ; this . passThroughEvent ( WorkerReadyBroadcastEvent ) ; this . passThroughEvent ( ResetBroadcastEvent ) ; } private passThroughHandler ( method ) { this . addHandler ( method , ( cb , ... args ) => { this . workerProxy . call ( method , ... args ) . then ( ( res ) => cb ( null , res ) ) . catch ( ( err ) => cb ( err , null ) ) ; } ) ; } private passThroughEvent ( name ) { this . workerProxy . addListener ( name , ( ... args ) => this . broadcast ( name , ... args ) ) ; } }