Files
SecScore/.copying/hosting/lifetime.ts
T
Yukino_fox 18eada8ff3 feat: 实现依赖注入系统、插件架构和窗口管理
- 引入ExamAware的DI系统并适配SecScore项目
- 添加服务注册表和窗口管理器
- 实现基于Koishi的Disposable设计的插件系统
- 集成Winston日志服务
- 分离设置窗口为独立页面
- 将非核心功能改为内置插件
- 更新README文件
- 添加相关hooks和工具函数
2026-04-26 19:39:56 +08:00

62 lines
1.8 KiB
TypeScript

import type { Awaitable, Disposer, PluginHostApplicationLifetime } from "./types"
type LoggerLike = { error: (...args: any[]) => void }
// 默认的应用生命周期实现,管理启动/停止事件
export class DefaultHostApplicationLifetime implements PluginHostApplicationLifetime {
constructor(private readonly logger: LoggerLike = console) {}
private readonly started = new Set<() => Awaitable<void>>() // 启动事件处理器
private readonly stopping = new Set<() => Awaitable<void>>() // 停止事件处理器
private readonly stopped = new Set<() => Awaitable<void>>() // 已停止事件处理器
// 注册启动事件处理器
onStarted(handler: () => Awaitable<void>): Disposer {
this.started.add(handler)
return () => {
this.started.delete(handler)
} // 返回清理函数
}
// 注册停止事件处理器
onStopping(handler: () => Awaitable<void>): Disposer {
this.stopping.add(handler)
return () => {
this.stopping.delete(handler)
}
}
// 注册已停止事件处理器
onStopped(handler: () => Awaitable<void>): Disposer {
this.stopped.add(handler)
return () => {
this.stopped.delete(handler)
}
}
// 通知所有启动事件处理器
async notifyStarted() {
await this.dispatch(this.started)
}
// 通知所有停止事件处理器
async notifyStopping() {
await this.dispatch(this.stopping)
}
// 通知所有已停止事件处理器
async notifyStopped() {
await this.dispatch(this.stopped)
}
// 执行事件处理器列表
private async dispatch(targets: Set<() => Awaitable<void>>) {
for (const handler of Array.from(targets)) {
try {
await handler()
} catch (error) {
this.logger.error("[PluginHostLifetime] handler failed", error as Error) // 记录错误但不中断
}
}
}
}