import { v4 as uuidV4 } from 'uuid'; import WindowFormImpl from '../../window/impl/WindowFormImpl.ts' import type { IProcess } from '@/core/process/IProcess.ts' import type { IProcessInfo } from '@/core/process/IProcessInfo.ts' import type { IWindowForm } from '@/core/window/IWindowForm.ts' import { processManager } from '@/core/process/ProcessManager.ts' import { EventBuilderImpl } from '@/core/events/impl/EventBuilderImpl.ts' import type { IEventBuilder } from '@/core/events/IEventBuilder.ts' import type { IProcessEvent } from '@/core/process/types/ProcessEventTypes.ts' /** * 进程 */ export default class ProcessImpl implements IProcess { private readonly _id: string = uuidV4(); private readonly _processInfo: IProcessInfo; // 当前进程的窗体集合 private _windowForms: Map = new Map(); private _event: IEventBuilder = new EventBuilderImpl() public get id() { return this._id; } public get processInfo() { return this._processInfo; } public get windowForms() { return this._windowForms; } public get event() { return this._event; } constructor(info: IProcessInfo) { console.log(`AppProcess: ${info.name}`) this._processInfo = info; const startName = info.startName; this.initEvent(); processManager.registerProcess(this); // 通过设置 isJustProcess 为 true,则不会创建窗体 if (!info.isJustProcess) { this.openWindowForm(startName) } } private initEvent() { this.event.addEventListener('processWindowFormExit', (id: string) => { this.windowForms.delete(id) if(this.windowForms.size === 0) { processManager.removeProcess(this) } }) } public openWindowForm(startName: string) { const info = this._processInfo.windowFormConfigs.find(item => item.name === startName); if (!info) throw new Error(`未找到窗体:${startName}`); const wf = new WindowFormImpl(this, info); this._windowForms.set(wf.id, wf); } public closeWindowForm(id: string) { try { const wf = this._windowForms.get(id); if (!wf) throw new Error(`未找到窗体:${id}`); wf.destroy(); this.windowForms.delete(id) if(this.windowForms.size === 0) { this.destroy() processManager.removeProcess(this) } } catch (e) { console.log('关闭窗体失败', e) } } public destroy() { this._event.destroy() } }