import { WebContents } from 'electron' ; export class MainProcessEvents { private registeredWebContents : Map < string , Set < WebContents > > = new Map < string , Set < WebContents > > ( ) ; param eventName param args public publish ( eventName , args ? ) { const allWebContests = this . registeredWebContents . get ( eventName ) ; if ( ! allWebContests ) { return ; } allWebContests . forEach ( ( w ) => { if ( w && ! w . isDestroyed ( ) ) { w . send ( eventName , args ) ; } } ) ; } param eventNames param webContents public subscribeMultipleEvents ( eventNames , webContents ) { eventNames . forEach ( ( e ) => this . subscribe ( e , webContents ) ) ; } param eventNames param webContents public unsubscribeMultipleEvents ( eventNames , webContents ) { eventNames . forEach ( ( e ) => this . unsubscribe ( e , webContents ) ) ; } param eventName param webContents public subscribe ( eventName , webContents ) { if ( ! webContents || webContents . isDestroyed ( ) ) { return ; } const registeredWebContents = this . registeredWebContents . get ( eventName ) ; if ( registeredWebContents ) { const isRegistered = registeredWebContents . has ( webContents ) ; if ( isRegistered ) { return ; } registeredWebContents . add ( webContents ) ; return ; } this . registeredWebContents . set ( eventName , new Set < WebContents > ( ) ) ; this . registeredWebContents . get ( eventName ) ! . add ( webContents ) ; } param eventName param webContents public unsubscribe ( eventName , webContents ) { if ( ! webContents || webContents . isDestroyed ( ) ) { return ; } const registeredWebContents = this . registeredWebContents . get ( eventName ) ; if ( registeredWebContents ) { if ( registeredWebContents . has ( webContents ) ) { registeredWebContents . delete ( webContents ) ; } } } } const mainEvents = new MainProcessEvents ( ) ; export { mainEvents } ;