import WebSocket from 'ws' ; import { Server } from 'http' ; import { Transport , Args } from '@vvim/nvim' ; import { EventEmitter } from 'events' ; class WsTransport extends EventEmitter implements Transport { ws : WebSocket ; constructor ( ws ) { super ( ) ; this . ws = ws ; this . ws . on ( 'message' , ( data ) => { const [ channel , ... args ] = JSON . parse ( data ) ; this . emit ( channel , ... args ) ; } ) ; } send ( channel , ... args ) { this . ws . send ( JSON . stringify ( [ channel , ... args ] ) ) ; } } /**
 * Init transport between main and renderer via websocket on server side.
 */ const transport = ( { server , onConnect , } ) : void => { const wss = new WebSocket . Server ( { server } ) ; wss . on ( 'connection' , ( ws ) => { onConnect ( new WsTransport ( ws ) ) ; } ) ; } ; export default transport ;