import EventEmitter from "events" ; import { CurrentWindow } from ".." ; import { OCSEventTypes , OCSEvents } from "../../types" ; import { Logger } from "../logger" ; /**
 * ocs公用事件处理器
 * 可以通过一个方法同时输出到本地
 *
 */ export class OCSEvent extends EventEmitter { logger : Logger ; constructor ( name ) { super ( ) ; this . logger = Logger . of ( name ) ; } info ( ... msg ) { this . logger . log ( OCSEventTypes . INFO , msg ) ; this . emit ( OCSEventTypes . INFO , msg ) ; } success ( ... msg ) { this . logger . log ( OCSEventTypes . SUCCESS , msg ) ; this . emit ( OCSEventTypes . SUCCESS , msg ) ; } error ( ... msg ) { console . error ( msg ) ; this . logger . log ( OCSEventTypes . ERROR , msg ) ; this . emit ( OCSEventTypes . ERROR , msg ) ; } warn ( ... msg ) { console . log ( msg ) ; this . logger . log ( OCSEventTypes . WARN , msg ) ; this . emit ( OCSEventTypes . WARN , msg ) ; } onInfo ( listener ) { this . on ( OCSEventTypes . INFO , listener ) ; } onError ( listener ) { this . on ( OCSEventTypes . ERROR , listener ) ; } onSuccess ( listener ) { this . on ( OCSEventTypes . SUCCESS , listener ) ; } onWarning ( listener ) { this . on ( OCSEventTypes . WARN , listener ) ; } } type eventHandler = ( msg : string [ ] ) => void ; /**
 * 信息
 */ export class OCSMessage extends OCSEvent { constructor ( public name ) { super ( name ) ; } info ( ... msg ) { super . info ( ... msg ) ; sendToRender ( OCSEventTypes . INFO , msg . join ( "," ) ) ; } success ( ... msg ) { super . info ( ... msg ) ; sendToRender ( OCSEventTypes . SUCCESS , msg . join ( "," ) ) ; } error ( ... msg ) { super . info ( ... msg ) ; sendToRender ( OCSEventTypes . ERROR , msg . join ( "," ) ) ; } warn ( ... msg ) { super . info ( ... msg ) ; sendToRender ( OCSEventTypes . WARN , msg . join ( "," ) ) ; } } /**
 * 通知
 */ export class OCSNotify extends OCSEvent { constructor ( public name , private title ) { super ( name ) ; } notify ( { type , message } ) { const notify = { type , title : this . title , message , name : this . name } ; this . logger . log ( OCSEventTypes . NOTIFY , notify ) ; this . emit ( OCSEventTypes . NOTIFY , notify ) ; CurrentWindow ?. webContents . send ( OCSEventTypes . NOTIFY , notify ) ; } info ( ... msg ) { this . notify ( { type : "info" , message : msg } ) ; } success ( ... msg ) { this . notify ( { type : "success" , message : msg } ) ; } error ( ... msg ) { this . notify ( { type : "error" , message : msg } ) ; } warn ( ... msg ) { this . notify ( { type : "warn" , message : msg } ) ; } } param eventName param event export function sendToRender < T extends keyof OCSEvents > ( eventName , event ) { CurrentWindow ?. webContents . send ( eventName , event ) ; }