interface Callback { ( ... args ) : void ; } const registrations = { } as { [ eventName ] : Callback [ ] ; } ; export function on ( eventName , callback ) { if ( ! ( eventName in registrations ) ) { registrations [ eventName ] = [ ] ; } registrations [ eventName ] . push ( callback ) ; } export function off ( eventName , callback ) { const group = registrations [ eventName ] ; const index = group . indexOf ( callback ) ; if ( index >= 0 ) { group . splice ( index , 1 ) ; } } export function once ( eventName , callback ) { const callbackOnce = ( ... args ) => { off ( eventName , callbackOnce ) ; callback . apply ( null , args ) ; } ; on ( eventName , callbackOnce ) ; } export function send ( eventName , args ) { ( window as any ) . external . notify ( `ipc:${ eventName } ${ JSON . stringify ( args ) }` ) ; } ( window as any ) . _invokeIpc = function ( eventName , argsJson ) { const group = registrations [ eventName ] ; if ( group ) { const args = JSON . parse ( argsJson ) ; group . forEach ( callback => callback . apply ( null , args ) ) ; } } ;