import { ElectronNotificationData , INotificationData , NotificationActions , } from '../../common/api-interface' ; import { isWindowsOS } from '../../common/env' ; import { notification } from '../../renderer/notification' ; import { windowHandler } from '../window-handler' ; import { ElectronNotification } from './electron-notification' ; class NotificationHelper { private electronNotification : Map < number , ElectronNotification > ; private activeElectronNotification : Map < string , ElectronNotification > ; constructor ( ) { this . electronNotification = new Map < number , ElectronNotification > ( ) ; this . activeElectronNotification = new Map < string , ElectronNotification > ( ) ; } param options { INotificationData } public showNotification ( options ) { if ( options . isElectronNotification ) { options . icon = this . getIcon ( options ) ; if ( this . activeElectronNotification . has ( options . tag ) ) { const electronNotification = this . activeElectronNotification . get ( options . tag ) ; if ( electronNotification ) { electronNotification . close ( ) ; } this . activeElectronNotification . delete ( options . tag ) ; } const electronToast = new ElectronNotification ( options , this . notificationCallback ) ; this . electronNotification . set ( options . id , electronToast ) ; this . activeElectronNotification . set ( options . tag , electronToast ) ; electronToast . show ( ) ; return ; } notification . showNotification ( options , this . notificationCallback ) ; } param id { number } public async closeNotification ( id ) { if ( this . electronNotification . has ( id ) ) { const electronNotification = this . electronNotification . get ( id ) ; if ( electronNotification ) { electronNotification . close ( ) ; } return ; } await notification . hideNotification ( id ) ; } param event { NotificationActions } param data { ElectronNotificationData } param notificationData { ElectronNotificationData } public notificationCallback ( event , data , notificationData ) { const mainWebContents = windowHandler . getMainWebContents ( ) ; if ( mainWebContents && ! mainWebContents . isDestroyed ( ) ) { mainWebContents . send ( 'notification-actions' , { event , data , notificationData , } ) ; } } param options private private getIcon ( options ) { return isWindowsOS ? options . icon : undefined ; } } const notificationHelper = new NotificationHelper ( ) ; export default notificationHelper ;