import { EventEmitter } from 'events' ; import { app , ipcMain , protocol } from 'electron' ; import { dev , windows } from 'electron-is' ; import { createLogProxy } from '@/common/utils' ; import type { TServiceModule } from '@/services' ; import BrowserManager from './BrowserManager' ; import Logger from './Logger' ; import DataBase from './DataBase' ; import { ServiceStorage } from './ServiceStorage' ; import * as browserItems from '../browserItems' ; const importAll = ( r ) => r . keys ( ) . map ( ( k ) => r ( k ) . default ) ; export type ServiceMap = Map < string , any > ; export class App extends EventEmitter { browserManager : BrowserManager ; /**
     * app 包含的服务能力
     */ services : any = new WeakMap ( ) ; /**
     * 日志服务
     */ logger : Logger ; /**
     * 数据库服务
     */ database : DataBase ; /**
     * 承接 webview fetch 的事件表
     */ serviceEventMap : ServiceMap = new Map ( ) ; constructor ( ) { super ( ) ; const services = importAll ( require . context ( '../services' , false , /.+Service\.ts$/ ) ) ; services . forEach ( ( service ) => this . addService ( service ) ) ; this . serviceEventMap . forEach ( ( serviceInfo , key ) => { const { service , methodName } = serviceInfo ; ipcMain . handle ( key , async ( e , ... data ) => { this . logger . module ( 'Fetch' , key ) ; if ( data ) this . logger . data ( ... data ) ; try { return await service [ methodName ] ( ... data ) ; } catch ( error ) { this . logger . error ( error ) ; return { error : error . message } ; } } ) ; } ) ; this . browserManager = new BrowserManager ( this ) ; this . database = new DataBase ( this ) ; this . logger = new Logger ( ) ; } onActivate = ( ) => { this . browserManager . browsers . get ( 'home' ) ! . show ( ) ; } ; beforeQuit = ( ) => { } ; /**
     * 启动 app
     */ bootstrap = ( ) => { protocol . registerSchemesAsPrivileged ( [ { scheme : 'app' , privileges : { secure : true , standard : true } } , ] ) ; const isSingle = app . requestSingleInstanceLock ( ) ; if ( ! isSingle ) { app . exit ( 0 ) ; } app . whenReady ( ) . then ( ( ) => { this . logger . logSystemInfo ( ) ; this . initBrowsers ( ) ; this . database . connect ( ) ; this . logger . info ( 'app 初始化完毕!' ) ; this . logger . divider ( '🎉' ) ; } ) ; app . on ( 'window-all-closed' , ( ) => { if ( windows ( ) ) { app . quit ( ) ; } } ) ; app . on ( 'activate' , this . onActivate ) ; app . on ( 'before-quit' , ( ) => { this . beforeQuit ( ) ; app . exit ( ) ; } ) ; } ; /**
     * 添加窗口
  
     */ initBrowsers ( ) { Object . values ( browserItems ) . forEach ( ( item ) => { this . browserManager . retrieveOrInitialize ( item ) ; } ) ; } param ServiceClass addService ( ServiceClass ) { const service = new ServiceClass ( this ) ; this . services . set ( ServiceClass , service ) ; ServiceStorage . services . get ( ServiceClass ) ?. forEach ( ( event ) => { this . serviceEventMap . set ( event . name , { service , methodName : event . methodName , } ) ; } ) ; } /**
     * 初始化之前的操作
     */ beforeInit ( ) { if ( ! dev ( ) ) { console . error = createLogProxy ( 'error' , Logger . getLogger ( 'error' ) ) ( console . error ) ; } } }