import { CallbackStore , EventBusInterface } from './EventBus' ; import { Event } from './Events' ; import { IpcRenderer } from 'electron' ; export class IpcRendererEventBus implements EventBusInterface { private ipc : IpcRenderer ; private callbacks : Array < CallbackStore > = [ ] ; constructor ( ipc ) { this . ipc = ipc ; } public subscribe < MessageType > ( event , callback ) { const wrappedCallback = ( _ , arg ) => { callback ( arg ) ; } ; this . ipc . on ( event . topic , wrappedCallback ) ; this . callbacks . push ( { callback , wrappedCallback } ) ; } public unsubscribeAll < MessageType > ( event ) { this . ipc . removeAllListeners ( event . topic ) ; } public unsubscribe < MessageType > ( event , callback ) { const item = this . callbacks . find ( store => store . callback === callback ) ; if ( ! item ) { return ; } this . ipc . removeListener ( event . topic , item . wrappedCallback ) ; this . callbacks = this . callbacks . filter ( a => a !== item ) ; } public emit < MessageType > ( event , msg ) { this . ipc . send ( event . topic , msg ) ; } }