@nimbus2d/core 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/dist/index.mjs +18433 -0
  2. package/package.json +39 -0
  3. package/src/base/EventEmitter.js +187 -0
  4. package/src/base/Subsystem.js +183 -0
  5. package/src/base/index.js +6 -0
  6. package/src/core/Application/AppState.js +27 -0
  7. package/src/core/Application/Application.js +480 -0
  8. package/src/core/Application/DefaultConfig.js +63 -0
  9. package/src/core/Application/index.js +7 -0
  10. package/src/core/AssetManager/AssetManager.js +711 -0
  11. package/src/core/AssetManager/AssetTypes.js +108 -0
  12. package/src/core/AssetManager/adapters/IResourceAdapter.js +50 -0
  13. package/src/core/AssetManager/adapters/PixiAdapter.js +90 -0
  14. package/src/core/AssetManager/core/RefCounter.js +167 -0
  15. package/src/core/AssetManager/core/ResourceCache.js +281 -0
  16. package/src/core/AssetManager/core/ResourceEntry.js +191 -0
  17. package/src/core/AssetManager/core/ResourceEventBus.js +127 -0
  18. package/src/core/AssetManager/handlers/IResourceHandler.js +45 -0
  19. package/src/core/AssetManager/index.js +26 -0
  20. package/src/core/AssetManager/plugins/IResourcePlugin.js +56 -0
  21. package/src/core/Camera/Camera.js +534 -0
  22. package/src/core/Camera/CameraTypes.js +59 -0
  23. package/src/core/Camera/index.js +13 -0
  24. package/src/core/ConfigSystem/ConfigLayer.js +230 -0
  25. package/src/core/ConfigSystem/ConfigLayerType.js +31 -0
  26. package/src/core/ConfigSystem/ConfigMerger.js +50 -0
  27. package/src/core/ConfigSystem/ConfigPath.js +96 -0
  28. package/src/core/ConfigSystem/ConfigSystem.js +378 -0
  29. package/src/core/ConfigSystem/ConfigTypes.js +29 -0
  30. package/src/core/ConfigSystem/ConfigWatcher.js +84 -0
  31. package/src/core/ConfigSystem/index.js +19 -0
  32. package/src/core/EventBus/EventBus.js +797 -0
  33. package/src/core/EventBus/Interceptor.js +26 -0
  34. package/src/core/EventBus/PerformanceStats.js +105 -0
  35. package/src/core/EventBus/index.js +7 -0
  36. package/src/core/Lifecycle/Lifecycle.js +265 -0
  37. package/src/core/Lifecycle/LifecycleState.js +31 -0
  38. package/src/core/Lifecycle/index.js +6 -0
  39. package/src/core/Logger/ConsoleHandler.js +55 -0
  40. package/src/core/Logger/LogHandler.js +34 -0
  41. package/src/core/Logger/LogLevel.js +38 -0
  42. package/src/core/Logger/Logger.js +234 -0
  43. package/src/core/Logger/index.js +8 -0
  44. package/src/core/PluginSystem/Plugin.js +156 -0
  45. package/src/core/PluginSystem/PluginEntry.js +102 -0
  46. package/src/core/PluginSystem/PluginState.js +30 -0
  47. package/src/core/PluginSystem/PluginSystem.js +530 -0
  48. package/src/core/PluginSystem/index.js +8 -0
  49. package/src/core/PoolManager/DefaultConfig.js +19 -0
  50. package/src/core/PoolManager/ObjectPool.js +326 -0
  51. package/src/core/PoolManager/PoolManager.js +253 -0
  52. package/src/core/PoolManager/PoolTypes.js +92 -0
  53. package/src/core/PoolManager/index.js +15 -0
  54. package/src/core/SceneManager/FadeTransition.js +64 -0
  55. package/src/core/SceneManager/MaskTransition.js +234 -0
  56. package/src/core/SceneManager/Scene.js +221 -0
  57. package/src/core/SceneManager/SceneManager.js +658 -0
  58. package/src/core/SceneManager/SceneState.js +29 -0
  59. package/src/core/SceneManager/SlideTransition.js +113 -0
  60. package/src/core/SceneManager/Transition.js +102 -0
  61. package/src/core/SceneManager/ZoomTransition.js +119 -0
  62. package/src/core/SceneManager/index.js +12 -0
  63. package/src/core/SpriteGPULayer/SpriteGPULayer.js +321 -0
  64. package/src/core/SpriteGPULayer/SpriteGPULayerTypes.js +49 -0
  65. package/src/core/SpriteGPULayer/index.js +11 -0
  66. package/src/core/Timeline/Clip.js +101 -0
  67. package/src/core/Timeline/DefaultConfig.js +41 -0
  68. package/src/core/Timeline/Easing.js +176 -0
  69. package/src/core/Timeline/Timeline.js +458 -0
  70. package/src/core/Timeline/TimelineTypes.js +89 -0
  71. package/src/core/Timeline/Track.js +202 -0
  72. package/src/core/Timeline/index.js +22 -0
  73. package/src/errors/ConfigError.js +47 -0
  74. package/src/errors/LifecycleError.js +58 -0
  75. package/src/errors/NimbusError.js +43 -0
  76. package/src/errors/PluginError.js +138 -0
  77. package/src/errors/ResourceError.js +166 -0
  78. package/src/errors/SceneError.js +124 -0
  79. package/src/errors/SubsystemError.js +68 -0
  80. package/src/errors/index.js +53 -0
  81. package/src/index.js +86 -0
  82. package/src/types/index.js +141 -0
  83. package/src/utils/MemoryChecker.js +374 -0
  84. package/src/utils/Validator.js +60 -0
  85. package/src/utils/index.js +84 -0
@@ -0,0 +1,26 @@
1
+ // ============================================================================
2
+ // Nimbus2D - EventBus 拦截器辅助类
3
+ // ----------------------------------------------------------------------------
4
+ // 封装拦截器信息和取消注册逻辑
5
+ // ============================================================================
6
+
7
+ /**
8
+ * 拦截器辅助类
9
+ * 封装拦截器函数、作用范围和取消注册方法
10
+ */
11
+ class Interceptor {
12
+ /**
13
+ * 构造函数
14
+ * @param {Function} fn - 拦截器函数
15
+ * @param {string|null} [eventType=null] - 事件类型(null 表示全局拦截器)
16
+ */
17
+ constructor(fn, eventType = null) {
18
+ /** @type {Function} 拦截器函数 */
19
+ this.fn = fn
20
+
21
+ /** @type {string|null} 事件类型(null 表示全局拦截器) */
22
+ this.eventType = eventType
23
+ }
24
+ }
25
+
26
+ export { Interceptor }
@@ -0,0 +1,105 @@
1
+ // ============================================================================
2
+ // Nimbus2D - EventBus 性能统计
3
+ // ----------------------------------------------------------------------------
4
+ // 记录事件总线的性能指标
5
+ // ============================================================================
6
+
7
+ /**
8
+ * 性能统计类
9
+ * 跟踪事件总线的触发次数、拦截次数、监听器数量等指标
10
+ */
11
+ class PerformanceStats {
12
+ /**
13
+ * 构造函数
14
+ */
15
+ constructor() {
16
+ /** @type {number} 总事件数(emit 调用次数) */
17
+ this.totalEvents = 0
18
+
19
+ /** @type {number} 已触发数(成功传播到监听器的事件数) */
20
+ this.emitted = 0
21
+
22
+ /** @type {number} 被拦截数(被拦截器阻止的事件数) */
23
+ this.blocked = 0
24
+
25
+ /** @type {number} 监听器总数 */
26
+ this.listenersCount = 0
27
+
28
+ /** @type {number} 拦截器总数 */
29
+ this.interceptorsCount = 0
30
+
31
+ /** @type {number} 累计触发耗时(ms) */
32
+ this._totalTime = 0
33
+ }
34
+
35
+ /**
36
+ * 平均触发耗时(ms)
37
+ * @type {number}
38
+ */
39
+ get averageTime() {
40
+ return this.emitted > 0 ? this._totalTime / this.emitted : 0
41
+ }
42
+
43
+ /**
44
+ * 记录一次触发
45
+ * @param {number} elapsedMs - 本次触发耗时(ms)
46
+ */
47
+ recordEmit(elapsedMs) {
48
+ this.totalEvents++
49
+ this.emitted++
50
+ this._totalTime += elapsedMs
51
+ }
52
+
53
+ /**
54
+ * 记录一次拦截
55
+ */
56
+ recordBlock() {
57
+ this.totalEvents++
58
+ this.blocked++
59
+ }
60
+
61
+ /**
62
+ * 更新监听器计数
63
+ * @param {number} count - 监听器数量
64
+ */
65
+ setListenersCount(count) {
66
+ this.listenersCount = count
67
+ }
68
+
69
+ /**
70
+ * 更新拦截器计数
71
+ * @param {number} count - 拦截器数量
72
+ */
73
+ setInterceptorsCount(count) {
74
+ this.interceptorsCount = count
75
+ }
76
+
77
+ /**
78
+ * 获取统计快照
79
+ * @returns {Object} 统计数据快照
80
+ */
81
+ getStats() {
82
+ return {
83
+ totalEvents: this.totalEvents,
84
+ emitted: this.emitted,
85
+ blocked: this.blocked,
86
+ listenersCount: this.listenersCount,
87
+ interceptorsCount: this.interceptorsCount,
88
+ averageTime: this.averageTime
89
+ }
90
+ }
91
+
92
+ /**
93
+ * 重置所有统计数据
94
+ */
95
+ reset() {
96
+ this.totalEvents = 0
97
+ this.emitted = 0
98
+ this.blocked = 0
99
+ this.listenersCount = 0
100
+ this.interceptorsCount = 0
101
+ this._totalTime = 0
102
+ }
103
+ }
104
+
105
+ export { PerformanceStats }
@@ -0,0 +1,7 @@
1
+ // ============================================================================
2
+ // Nimbus2D - EventBus 模块统一导出
3
+ // ============================================================================
4
+
5
+ export { EventBus } from './EventBus.js'
6
+ export { Interceptor } from './Interceptor.js'
7
+ export { PerformanceStats } from './PerformanceStats.js'
@@ -0,0 +1,265 @@
1
+ // ============================================================================
2
+ // Nimbus2D - Lifecycle 生命周期管理
3
+ // ----------------------------------------------------------------------------
4
+ // 职责:
5
+ // 1. 集中调度型:调用所有子系统的 initialize/update/destroy
6
+ // 2. 状态机:CREATED → INITIALIZING → INITIALIZED → ACTIVE ⇄ PAUSED → DESTROYING → DESTROYED
7
+ // 3. activate()/pause() 仅改变 Lifecycle 自身状态(不调用子系统的 activate/deactivate)
8
+ // 4. update() 不检查子系统 isActive(直接调用所有子系统的 update)
9
+ // 5. 销毁阶段不抛异常(记录到 Logger 后继续)
10
+ //
11
+ // 继承关系:
12
+ // EventEmitter → Lifecycle(调度者,不被其他模块调度)
13
+ // ============================================================================
14
+
15
+ import { EventEmitter } from '../../base/EventEmitter.js'
16
+ import { LifecycleState } from './LifecycleState.js'
17
+ import { LifecycleStateError } from '../../errors/LifecycleError.js'
18
+
19
+ class Lifecycle extends EventEmitter {
20
+ /**
21
+ * 构造函数
22
+ * @param {import('../EventBus/EventBus.js').EventBus} eventBus - 全局事件总线
23
+ * @param {import('../Logger/Logger.js').Logger} logger - 日志系统实例(用于 _safeLog 防御性日志)
24
+ */
25
+ constructor(eventBus, logger) {
26
+ super()
27
+
28
+ /** @type {import('../EventBus/EventBus.js').EventBus} 全局事件总线 */
29
+ this._eventBus = eventBus
30
+
31
+ /** @type {import('../Logger/Logger.js').Logger} 日志系统实例 */
32
+ this._logger = logger
33
+
34
+ /** @type {LifecycleState} 当前状态 */
35
+ this._state = LifecycleState.CREATED
36
+
37
+ /** @type {import('../../base/Subsystem.js').Subsystem[]} 子系统列表(按注册顺序) */
38
+ this._subsystems = []
39
+ }
40
+
41
+ // --------------------------------------------------------------------------
42
+ // 子系统管理
43
+ // --------------------------------------------------------------------------
44
+
45
+ /**
46
+ * 注册子系统
47
+ * @param {import('../../base/Subsystem.js').Subsystem} subsystem - 子系统实例
48
+ * @returns {Lifecycle} - this(链式)
49
+ */
50
+ registerSubsystem(subsystem) {
51
+ // 检查是否已注册(按名称去重)
52
+ const existing = this._subsystems.find((s) => s.name === subsystem.name)
53
+ if (existing) {
54
+ this._safeLog('warn', `子系统 ${subsystem.name} 已注册,跳过重复注册`)
55
+ return this
56
+ }
57
+
58
+ this._subsystems.push(subsystem)
59
+ this.emit('subsystem:registered', { name: subsystem.name, subsystem })
60
+ return this
61
+ }
62
+
63
+ /**
64
+ * 注销子系统(不销毁,仅从调度列表移除)
65
+ * @param {string} name - 子系统名称
66
+ * @returns {boolean} - 是否注销成功
67
+ */
68
+ unregisterSubsystem(name) {
69
+ const index = this._subsystems.findIndex((s) => s.name === name)
70
+ if (index === -1) return false
71
+
72
+ // 从列表移除
73
+ const [subsystem] = this._subsystems.splice(index, 1)
74
+ this.emit('subsystem:unregistered', { name: subsystem.name })
75
+ return true
76
+ }
77
+
78
+ // --------------------------------------------------------------------------
79
+ // 生命周期方法
80
+ // --------------------------------------------------------------------------
81
+
82
+ /**
83
+ * 初始化所有子系统(按注册顺序)
84
+ * @returns {Promise<void>}
85
+ * @throws {LifecycleStateError} 状态不合法时
86
+ */
87
+ async initialize() {
88
+ // 状态检查:必须为 CREATED
89
+ if (this._state !== LifecycleState.CREATED) {
90
+ throw new LifecycleStateError(this._state, [LifecycleState.CREATED])
91
+ }
92
+
93
+ // 转换到 INITIALIZING 状态
94
+ const oldState = this._state
95
+ this._state = LifecycleState.INITIALIZING
96
+ this.emit('state:change', { from: oldState, to: LifecycleState.INITIALIZING })
97
+
98
+ // 按注册顺序初始化所有子系统
99
+ for (const subsystem of this._subsystems) {
100
+ try {
101
+ await subsystem.initialize()
102
+ this.emit('subsystem:initialized', { name: subsystem.name, subsystem })
103
+ } catch (e) {
104
+ // 初始化阶段错误向上抛出
105
+ this.emit('subsystem:error', { name: subsystem.name, error: e, phase: 'initialize' })
106
+ this._safeLog('error', `子系统 ${subsystem.name} 初始化失败`, { error: e })
107
+ throw e
108
+ }
109
+ }
110
+
111
+ // 转换到 INITIALIZED 状态
112
+ const prevState = this._state
113
+ this._state = LifecycleState.INITIALIZED
114
+ this.emit('state:change', { from: prevState, to: LifecycleState.INITIALIZED })
115
+ this.emit('initialized')
116
+ }
117
+
118
+ /**
119
+ * 激活 Lifecycle(使 update 生效)
120
+ * 仅改变 Lifecycle 自身状态,不调用子系统的 activate/deactivate
121
+ * @returns {Lifecycle} - this
122
+ * @throws {LifecycleStateError} 状态不合法时
123
+ */
124
+ activate() {
125
+ // 状态检查:必须为 INITIALIZED 或 PAUSED
126
+ if (this._state !== LifecycleState.INITIALIZED && this._state !== LifecycleState.PAUSED) {
127
+ throw new LifecycleStateError(this._state, [LifecycleState.INITIALIZED, LifecycleState.PAUSED])
128
+ }
129
+
130
+ const oldState = this._state
131
+ this._state = LifecycleState.ACTIVE
132
+ this.emit('state:change', { from: oldState, to: LifecycleState.ACTIVE })
133
+ this.emit('activated')
134
+ return this
135
+ }
136
+
137
+ /**
138
+ * 暂停 Lifecycle(不调用 update)
139
+ * 仅改变 Lifecycle 自身状态,不调用子系统的 activate/deactivate
140
+ * @returns {Lifecycle} - this
141
+ * @throws {LifecycleStateError} 状态不合法时
142
+ */
143
+ pause() {
144
+ // 状态检查:必须为 ACTIVE
145
+ if (this._state !== LifecycleState.ACTIVE) {
146
+ throw new LifecycleStateError(this._state, [LifecycleState.ACTIVE])
147
+ }
148
+
149
+ const oldState = this._state
150
+ this._state = LifecycleState.PAUSED
151
+ this.emit('state:change', { from: oldState, to: LifecycleState.PAUSED })
152
+ this.emit('paused')
153
+ return this
154
+ }
155
+
156
+ /**
157
+ * 调用所有子系统的 update(按注册顺序)
158
+ * 仅在 ACTIVE 状态下执行,update 不检查子系统 isActive
159
+ * @param {number} delta - 帧间隔(秒)
160
+ */
161
+ update(delta) {
162
+ // 只有 ACTIVE 状态才调用 update
163
+ if (this._state !== LifecycleState.ACTIVE) return
164
+
165
+ for (const subsystem of this._subsystems) {
166
+ try {
167
+ subsystem.update(delta)
168
+ } catch (e) {
169
+ this.emit('subsystem:error', { name: subsystem.name, error: e, phase: 'update' })
170
+ this._safeLog('error', `子系统 ${subsystem.name} 更新失败`, { error: e })
171
+ }
172
+ }
173
+ }
174
+
175
+ /**
176
+ * 销毁所有子系统(反向顺序,不抛异常)
177
+ * @returns {Promise<void>}
178
+ */
179
+ async destroy() {
180
+ if (this._state === LifecycleState.DESTROYED) return
181
+
182
+ const oldState = this._state
183
+ this._state = LifecycleState.DESTROYING
184
+ this.emit('state:change', { from: oldState, to: LifecycleState.DESTROYING })
185
+
186
+ // 反向销毁所有子系统(错误不中断流程)
187
+ const subsystems = [...this._subsystems].reverse()
188
+ for (const subsystem of subsystems) {
189
+ try {
190
+ await subsystem.destroy()
191
+ } catch (e) {
192
+ // 销毁阶段错误不抛出,记录到 Logger(若可用)
193
+ this.emit('subsystem:error', { name: subsystem.name, error: e, phase: 'destroy' })
194
+ this._safeLog('error', `子系统 ${subsystem.name} 销毁失败`, { error: e })
195
+ }
196
+ }
197
+
198
+ // 转换到 DESTROYED 状态
199
+ const prevState = this._state
200
+ this._state = LifecycleState.DESTROYED
201
+ this.emit('state:change', { from: prevState, to: LifecycleState.DESTROYED })
202
+ this.emit('destroyed')
203
+
204
+ // 清理引用
205
+ this._subsystems = []
206
+ this._eventBus = null
207
+ this._logger = null
208
+
209
+ // 调用父类 destroy 设置 _destroyed = true 并清理监听器
210
+ super.destroy()
211
+ }
212
+
213
+ // --------------------------------------------------------------------------
214
+ // 查询方法
215
+ // --------------------------------------------------------------------------
216
+
217
+ /**
218
+ * 当前状态
219
+ * @type {LifecycleState}
220
+ */
221
+ get state() {
222
+ return this._state
223
+ }
224
+
225
+ /**
226
+ * 所有子系统数组(返回副本)
227
+ * @type {import('../../base/Subsystem.js').Subsystem[]}
228
+ */
229
+ get subsystems() {
230
+ return [...this._subsystems]
231
+ }
232
+
233
+ /**
234
+ * 按名称获取子系统
235
+ * @param {string} name - 子系统名称
236
+ * @returns {import('../../base/Subsystem.js').Subsystem|null} 子系统实例,未找到返回 null
237
+ */
238
+ getSubsystem(name) {
239
+ return this._subsystems.find((s) => s.name === name) || null
240
+ }
241
+
242
+ // --------------------------------------------------------------------------
243
+ // 私有方法
244
+ // --------------------------------------------------------------------------
245
+
246
+ /**
247
+ * 防御性日志方法
248
+ * 处理 Logger 可能已销毁的情况
249
+ * @param {string} level - 日志级别('debug'|'info'|'warn'|'error')
250
+ * @param {string} message - 日志消息
251
+ * @param {Object} [context={}] - 日志上下文
252
+ * @private
253
+ */
254
+ _safeLog(level, message, context = {}) {
255
+ try {
256
+ if (this._logger && !this._logger.isDestroyed) {
257
+ this._logger[level](message, context)
258
+ }
259
+ } catch (e) {
260
+ // 完全忽略日志错误
261
+ }
262
+ }
263
+ }
264
+
265
+ export { Lifecycle }
@@ -0,0 +1,31 @@
1
+ // ============================================================================
2
+ // Nimbus2D - Lifecycle 状态枚举
3
+ // ----------------------------------------------------------------------------
4
+ // 定义 Lifecycle 生命周期管理器的所有合法状态。
5
+ //
6
+ // 状态转换流程:
7
+ // CREATED → INITIALIZING → INITIALIZED → ACTIVE ⇄ PAUSED → DESTROYING → DESTROYED
8
+ // ============================================================================
9
+
10
+ /**
11
+ * Lifecycle 状态枚举
12
+ * @enum {string}
13
+ */
14
+ const LifecycleState = {
15
+ /** 已创建(初始状态) */
16
+ CREATED: 'CREATED',
17
+ /** 初始化中 */
18
+ INITIALIZING: 'INITIALIZING',
19
+ /** 已初始化(等待激活) */
20
+ INITIALIZED: 'INITIALIZED',
21
+ /** 已激活(update 生效) */
22
+ ACTIVE: 'ACTIVE',
23
+ /** 已暂停(update 不执行) */
24
+ PAUSED: 'PAUSED',
25
+ /** 销毁中 */
26
+ DESTROYING: 'DESTROYING',
27
+ /** 已销毁(终态) */
28
+ DESTROYED: 'DESTROYED'
29
+ }
30
+
31
+ export { LifecycleState }
@@ -0,0 +1,6 @@
1
+ // ============================================================================
2
+ // Nimbus2D - Lifecycle 模块统一导出
3
+ // ============================================================================
4
+
5
+ export { Lifecycle } from './Lifecycle.js'
6
+ export { LifecycleState } from './LifecycleState.js'
@@ -0,0 +1,55 @@
1
+ // ============================================================================
2
+ // Nimbus2D - ConsoleHandler 控制台日志处理器
3
+ // ----------------------------------------------------------------------------
4
+ // 默认的日志处理器,输出到浏览器控制台
5
+ // ============================================================================
6
+
7
+ import { LogHandler } from './LogHandler.js'
8
+ import { LogLevel, logLevelToString } from './LogLevel.js'
9
+
10
+ /**
11
+ * 控制台日志处理器
12
+ * 继承 LogHandler,输出格式化的日志到浏览器控制台
13
+ */
14
+ class ConsoleHandler extends LogHandler {
15
+ /**
16
+ * 构造函数
17
+ * @param {number} [level=LogLevel.DEBUG] - 最低处理级别
18
+ */
19
+ constructor(level = LogLevel.DEBUG) {
20
+ super(level)
21
+ }
22
+
23
+ /**
24
+ * 处理日志(继承自 LogHandler 的 handle 方法)
25
+ * @param {number} level - 日志级别
26
+ * @param {string} message - 消息
27
+ * @param {Object} [context={}] - 上下文
28
+ */
29
+ handle(level, message, context) {
30
+ if (level < this.level) return
31
+
32
+ const levelName = logLevelToString(level)
33
+ const time = new Date().toISOString()
34
+ const prefix = `[${time}] [${levelName}]`
35
+
36
+ switch (level) {
37
+ case LogLevel.DEBUG:
38
+ console.debug(prefix, message, context)
39
+ break
40
+ case LogLevel.INFO:
41
+ console.info(prefix, message, context)
42
+ break
43
+ case LogLevel.WARN:
44
+ console.warn(prefix, message, context)
45
+ break
46
+ case LogLevel.ERROR:
47
+ console.error(prefix, message, context)
48
+ break
49
+ default:
50
+ console.log(prefix, message, context)
51
+ }
52
+ }
53
+ }
54
+
55
+ export { ConsoleHandler }
@@ -0,0 +1,34 @@
1
+ // ============================================================================
2
+ // Nimbus2D - LogHandler 日志处理器基类
3
+ // ----------------------------------------------------------------------------
4
+ // 所有日志处理器的基类,子类重写 handle 方法
5
+ // ============================================================================
6
+
7
+ import { LogLevel } from './LogLevel.js'
8
+
9
+ /**
10
+ * 日志处理器基类
11
+ * 子类必须重写 handle(level, message, context) 方法
12
+ */
13
+ class LogHandler {
14
+ /**
15
+ * 构造函数
16
+ * @param {number} [level=LogLevel.DEBUG] - 最低处理级别
17
+ */
18
+ constructor(level = LogLevel.DEBUG) {
19
+ /** @type {number} 最低处理级别 */
20
+ this.level = level
21
+ }
22
+
23
+ /**
24
+ * 处理日志(子类重写)
25
+ * @param {number} level - 日志级别
26
+ * @param {string} message - 消息
27
+ * @param {Object} [context={}] - 上下文
28
+ */
29
+ handle(level, message, context) {
30
+ // 基类空实现,子类必须重写
31
+ }
32
+ }
33
+
34
+ export { LogHandler }
@@ -0,0 +1,38 @@
1
+ // ============================================================================
2
+ // Nimbus2D - 日志级别枚举
3
+ // ----------------------------------------------------------------------------
4
+ // DEBUG=0, INFO=1, WARN=2, ERROR=3, NONE=4
5
+ // ============================================================================
6
+
7
+ /**
8
+ * 日志级别枚举
9
+ * @enum {number}
10
+ */
11
+ export const LogLevel = {
12
+ /** 调试级别(最详细) */
13
+ DEBUG: 0,
14
+ /** 信息级别 */
15
+ INFO: 1,
16
+ /** 警告级别 */
17
+ WARN: 2,
18
+ /** 错误级别 */
19
+ ERROR: 3,
20
+ /** 关闭日志 */
21
+ NONE: 4
22
+ }
23
+
24
+ /**
25
+ * 日志级别名称映射
26
+ * @param {number} level - 日志级别
27
+ * @returns {string} 级别名称
28
+ */
29
+ export function logLevelToString(level) {
30
+ switch (level) {
31
+ case LogLevel.DEBUG: return 'DEBUG'
32
+ case LogLevel.INFO: return 'INFO'
33
+ case LogLevel.WARN: return 'WARN'
34
+ case LogLevel.ERROR: return 'ERROR'
35
+ case LogLevel.NONE: return 'NONE'
36
+ default: return 'UNKNOWN'
37
+ }
38
+ }