import { AdobeEventListener , BroadcastMessage } from './api' ; import { Socket , Server , createServer } from 'net' ; const newAdobeAppListener = ( host , port , callback ) : AdobeEventListener => { const callbacks = new Map < string , Function > ( ) ; let server ; let client ; function connectionListener ( socket ) { client = socket ; socket . on ( 'data' , ( buffer ) => { const data = JSON . parse ( buffer . toString ( ) ) ; if ( callbacks . has ( data . command ) ) { callbacks . get ( data . command ) ( data . stdout , data . stderr ) ; } callback ( data . command ) ; } ) ; } function disposeServer ( ) { if ( client ) { client . end ( ) ; } server = null ; console . log ( `Adobe Event Listener has been stopped at port ${ port }` ) ; } return { addEventListener : ( event , callback ) : void => { if ( callbacks . has ( event ) ) { console . warn ( `${ event } listener will be overwritten` ) ; } callbacks . set ( event , callback ) ; } , start : ( ) : void => { if ( server ) return ; server = createServer ( connectionListener ) ; server . listen ( port , host , ( ) => { console . log ( `Adobe Event Listener running at ${ host }:${ port }` ) ; } ) ; } , close : ( ) : void => { server . close ( disposeServer ) ; } } ; } ; export default newAdobeAppListener ;