Files
SecScore/src/main/hosting/lifetime.ts
T
Fox_block e83f17af56 refactor: 移除WebSocket同步功能及相关代码
feat(theme): 增强侧边栏选中项样式配置
style: 优化主题变量和样式覆盖逻辑
chore: 清理未使用的依赖和配置文件
2026-01-18 11:44:50 +08:00

62 lines
1.8 KiB
TypeScript

import type { awaitable, disposer, hostApplicationLifetime } from './types'
type loggerLike = { error: (...args: any[]) => void }
// 默认的应用生命周期实现,管理启动/停止事件
export class DefaultHostApplicationLifetime implements hostApplicationLifetime {
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('[HostLifetime] handler failed', error as Error) // 记录错误但不中断
}
}
}
}