新增ServiceContainer服务容器, 所有服务统一实现 IService 接口

This commit is contained in:
YHH
2025-10-10 18:13:28 +08:00
parent 1d2a3e283e
commit 41bbe23404
11 changed files with 702 additions and 33 deletions

View File

@@ -9,13 +9,14 @@ import { Component } from '../../ECS/Component';
import { ComponentPoolManager } from '../../ECS/Core/ComponentPool';
import { Pool } from '../../Utils/Pool';
import { getComponentInstanceTypeName, getSystemInstanceTypeName } from '../../ECS/Decorators';
import type { IService } from '../../Core/ServiceContainer';
/**
* 调试管理器
*
* 整合所有调试数据收集器,负责收集和发送调试数据
*/
export class DebugManager {
export class DebugManager implements IService {
private config: IECSDebugConfig;
private webSocketManager: WebSocketManager;
private entityCollector: EntityDataCollector;
@@ -821,4 +822,11 @@ export class DebugManager {
// console.error('[ECS Debug] 发送调试数据失败:', error);
}
}
/**
* 释放资源
*/
public dispose(): void {
this.stop();
}
}

View File

@@ -99,20 +99,20 @@ export interface PerformanceThresholds {
};
}
import type { IService } from '../Core/ServiceContainer';
/**
* 高性能监控器
* 用于监控ECS系统的性能表现提供详细的分析和优化建议
*/
export class PerformanceMonitor {
private static _instance: PerformanceMonitor;
export class PerformanceMonitor implements IService {
private _systemData = new Map<string, PerformanceData>();
private _systemStats = new Map<string, PerformanceStats>();
private _warnings: PerformanceWarning[] = [];
private _isEnabled = false;
private _maxRecentSamples = 60; // 保留最近60帧的数据
private _maxWarnings = 100; // 最大警告数量
// 性能阈值配置
private _thresholds: PerformanceThresholds = {
executionTime: { warning: 16.67, critical: 33.33 }, // 60fps和30fps对应的帧时间
@@ -139,18 +139,8 @@ export class PerformanceMonitor {
private _gcCount = 0;
private _lastGcCheck = 0;
private _gcCheckInterval = 1000;
/**
* 获取单例实例
*/
public static get instance(): PerformanceMonitor {
if (!PerformanceMonitor._instance) {
PerformanceMonitor._instance = new PerformanceMonitor();
}
return PerformanceMonitor._instance;
}
private constructor() {}
constructor() {}
/**
* 启用性能监控
@@ -392,7 +382,7 @@ export class PerformanceMonitor {
*/
public setMaxRecentSamples(maxSamples: number): void {
this._maxRecentSamples = maxSamples;
// 裁剪现有数据
for (const stats of this._systemStats.values()) {
while (stats.recentTimes.length > maxSamples) {
@@ -400,4 +390,16 @@ export class PerformanceMonitor {
}
}
}
/**
* 释放资源
*/
public dispose(): void {
this._systemData.clear();
this._systemStats.clear();
this._warnings = [];
this._fpsHistory = [];
this._memoryHistory = [];
this._isEnabled = false;
}
}

View File

@@ -1,11 +1,12 @@
import { GlobalManager } from '../GlobalManager';
import { Timer } from './Timer';
import { ITimer } from './ITimer';
import type { IService } from '../../Core/ServiceContainer';
/**
* 允许动作的延迟和重复执行
*/
export class TimerManager extends GlobalManager {
export class TimerManager extends GlobalManager implements IService {
public _timers: Array<Timer<unknown>> = [];
public override update() {
@@ -31,4 +32,14 @@ export class TimerManager extends GlobalManager {
return timer;
}
/**
* 释放资源
*/
public dispose(): void {
for (const timer of this._timers) {
timer.unload();
}
this._timers = [];
}
}