import { IpcMain , ipcMain , ipcRenderer } from 'electron' ; import { Event } from './Events' ; import { IpcRendererEventBus } from './IpcRendererEventBus' ; export interface EventBusInterface { subscribe < MessageType > ( event , callback ) : void ; unsubscribeAll < MessageType > ( event ) : void ; emit < MessageType > ( event , msg ) : void ; unsubscribe < MessageType > ( event , callback ) : void ; } export interface CallbackStore { wrappedCallback : any ; callback : any ; } class IpcMainEventBus implements EventBusInterface { private ipc : IpcMain ; private client : any ; constructor ( ipc ) { this . ipc = ipc ; } public subscribe < MessageType > ( subscribeEvent , callback ) { console . log ( 'subscribing' , subscribeEvent . topic ) ; this . ipc . on ( subscribeEvent . topic , ( event , arg ) => { this . client = event . sender ; callback ( arg ) ; } ) ; } public unsubscribeAll < MessageType > ( event ) { console . log ( 'unsubscribeAll' , event . topic ) ; this . ipc . removeAllListeners ( event . topic ) ; } public unsubscribe < MessageType > ( event , callback ) { throw new Error ( 'Not implemented' ) ; } public emit < MessageType > ( event , msg ) { if ( ! this . client . isDestroyed ( ) ) { this . client . send ( event . topic , msg ) ; } } } export const rendererEvents = new IpcRendererEventBus ( ipcRenderer ) ; export const backendEvents = new IpcMainEventBus ( ipcMain ) ;