@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.
- package/dist/index.mjs +18433 -0
- package/package.json +39 -0
- package/src/base/EventEmitter.js +187 -0
- package/src/base/Subsystem.js +183 -0
- package/src/base/index.js +6 -0
- package/src/core/Application/AppState.js +27 -0
- package/src/core/Application/Application.js +480 -0
- package/src/core/Application/DefaultConfig.js +63 -0
- package/src/core/Application/index.js +7 -0
- package/src/core/AssetManager/AssetManager.js +711 -0
- package/src/core/AssetManager/AssetTypes.js +108 -0
- package/src/core/AssetManager/adapters/IResourceAdapter.js +50 -0
- package/src/core/AssetManager/adapters/PixiAdapter.js +90 -0
- package/src/core/AssetManager/core/RefCounter.js +167 -0
- package/src/core/AssetManager/core/ResourceCache.js +281 -0
- package/src/core/AssetManager/core/ResourceEntry.js +191 -0
- package/src/core/AssetManager/core/ResourceEventBus.js +127 -0
- package/src/core/AssetManager/handlers/IResourceHandler.js +45 -0
- package/src/core/AssetManager/index.js +26 -0
- package/src/core/AssetManager/plugins/IResourcePlugin.js +56 -0
- package/src/core/Camera/Camera.js +534 -0
- package/src/core/Camera/CameraTypes.js +59 -0
- package/src/core/Camera/index.js +13 -0
- package/src/core/ConfigSystem/ConfigLayer.js +230 -0
- package/src/core/ConfigSystem/ConfigLayerType.js +31 -0
- package/src/core/ConfigSystem/ConfigMerger.js +50 -0
- package/src/core/ConfigSystem/ConfigPath.js +96 -0
- package/src/core/ConfigSystem/ConfigSystem.js +378 -0
- package/src/core/ConfigSystem/ConfigTypes.js +29 -0
- package/src/core/ConfigSystem/ConfigWatcher.js +84 -0
- package/src/core/ConfigSystem/index.js +19 -0
- package/src/core/EventBus/EventBus.js +797 -0
- package/src/core/EventBus/Interceptor.js +26 -0
- package/src/core/EventBus/PerformanceStats.js +105 -0
- package/src/core/EventBus/index.js +7 -0
- package/src/core/Lifecycle/Lifecycle.js +265 -0
- package/src/core/Lifecycle/LifecycleState.js +31 -0
- package/src/core/Lifecycle/index.js +6 -0
- package/src/core/Logger/ConsoleHandler.js +55 -0
- package/src/core/Logger/LogHandler.js +34 -0
- package/src/core/Logger/LogLevel.js +38 -0
- package/src/core/Logger/Logger.js +234 -0
- package/src/core/Logger/index.js +8 -0
- package/src/core/PluginSystem/Plugin.js +156 -0
- package/src/core/PluginSystem/PluginEntry.js +102 -0
- package/src/core/PluginSystem/PluginState.js +30 -0
- package/src/core/PluginSystem/PluginSystem.js +530 -0
- package/src/core/PluginSystem/index.js +8 -0
- package/src/core/PoolManager/DefaultConfig.js +19 -0
- package/src/core/PoolManager/ObjectPool.js +326 -0
- package/src/core/PoolManager/PoolManager.js +253 -0
- package/src/core/PoolManager/PoolTypes.js +92 -0
- package/src/core/PoolManager/index.js +15 -0
- package/src/core/SceneManager/FadeTransition.js +64 -0
- package/src/core/SceneManager/MaskTransition.js +234 -0
- package/src/core/SceneManager/Scene.js +221 -0
- package/src/core/SceneManager/SceneManager.js +658 -0
- package/src/core/SceneManager/SceneState.js +29 -0
- package/src/core/SceneManager/SlideTransition.js +113 -0
- package/src/core/SceneManager/Transition.js +102 -0
- package/src/core/SceneManager/ZoomTransition.js +119 -0
- package/src/core/SceneManager/index.js +12 -0
- package/src/core/SpriteGPULayer/SpriteGPULayer.js +321 -0
- package/src/core/SpriteGPULayer/SpriteGPULayerTypes.js +49 -0
- package/src/core/SpriteGPULayer/index.js +11 -0
- package/src/core/Timeline/Clip.js +101 -0
- package/src/core/Timeline/DefaultConfig.js +41 -0
- package/src/core/Timeline/Easing.js +176 -0
- package/src/core/Timeline/Timeline.js +458 -0
- package/src/core/Timeline/TimelineTypes.js +89 -0
- package/src/core/Timeline/Track.js +202 -0
- package/src/core/Timeline/index.js +22 -0
- package/src/errors/ConfigError.js +47 -0
- package/src/errors/LifecycleError.js +58 -0
- package/src/errors/NimbusError.js +43 -0
- package/src/errors/PluginError.js +138 -0
- package/src/errors/ResourceError.js +166 -0
- package/src/errors/SceneError.js +124 -0
- package/src/errors/SubsystemError.js +68 -0
- package/src/errors/index.js +53 -0
- package/src/index.js +86 -0
- package/src/types/index.js +141 -0
- package/src/utils/MemoryChecker.js +374 -0
- package/src/utils/Validator.js +60 -0
- package/src/utils/index.js +84 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// Nimbus2D - 资源条目
|
|
3
|
+
// ----------------------------------------------------------------------------
|
|
4
|
+
// 封装单个资源的元信息、状态和数据。
|
|
5
|
+
//
|
|
6
|
+
// 设计决策:
|
|
7
|
+
// - 资源 ID 作为唯一标识
|
|
8
|
+
// - 状态机:REGISTERED → LOADING → LOADED → UNLOADED / ERROR
|
|
9
|
+
// - 支持分组、标签、加载策略等元信息
|
|
10
|
+
// ============================================================================
|
|
11
|
+
|
|
12
|
+
import { ResourceState, LoadStrategy } from '../AssetTypes.js'
|
|
13
|
+
|
|
14
|
+
class ResourceEntry {
|
|
15
|
+
/**
|
|
16
|
+
* 构造函数
|
|
17
|
+
* @param {string} id - 资源唯一标识
|
|
18
|
+
* @param {Object} options - 资源注册选项
|
|
19
|
+
* @param {string} options.type - 资源类型(ResourceType 枚举值)
|
|
20
|
+
* @param {string} options.src - 资源路径
|
|
21
|
+
* @param {string} [options.strategy=LoadStrategy.LAZY] - 加载策略
|
|
22
|
+
* @param {string} [options.group] - 分组名称
|
|
23
|
+
* @param {string[]} [options.tags=[]] - 标签列表
|
|
24
|
+
* @param {Object} [options.adapterOptions={}] - 适配器自定义选项
|
|
25
|
+
*/
|
|
26
|
+
constructor(id, options) {
|
|
27
|
+
/** @type {string} 资源唯一标识 */
|
|
28
|
+
this._id = id
|
|
29
|
+
|
|
30
|
+
/** @type {string} 资源类型 */
|
|
31
|
+
this._type = options.type
|
|
32
|
+
|
|
33
|
+
/** @type {string} 资源路径 */
|
|
34
|
+
this._src = options.src
|
|
35
|
+
|
|
36
|
+
/** @type {string|null} 资源别名(用于 PixiJS Assets 缓存键) */
|
|
37
|
+
this._alias = options.alias || null
|
|
38
|
+
|
|
39
|
+
/** @type {LoadStrategy} 加载策略 */
|
|
40
|
+
this._strategy = options.strategy || LoadStrategy.LAZY
|
|
41
|
+
|
|
42
|
+
/** @type {string} 分组名称 */
|
|
43
|
+
this._group = options.group || ''
|
|
44
|
+
|
|
45
|
+
/** @type {string[]} 标签列表 */
|
|
46
|
+
this._tags = options.tags || []
|
|
47
|
+
|
|
48
|
+
/** @type {Object} 适配器自定义选项 */
|
|
49
|
+
this._adapterOptions = options.adapterOptions
|
|
50
|
+
? options.adapterOptions
|
|
51
|
+
: (options.pixiData ? { pixiData: options.pixiData } : {})
|
|
52
|
+
|
|
53
|
+
/** @type {ResourceState} 当前状态 */
|
|
54
|
+
this._state = ResourceState.REGISTERED
|
|
55
|
+
|
|
56
|
+
/** @type {*} 资源数据(加载后填充) */
|
|
57
|
+
this._data = null
|
|
58
|
+
|
|
59
|
+
/** @type {Error|null} 加载错误(加载失败时填充) */
|
|
60
|
+
this._error = null
|
|
61
|
+
|
|
62
|
+
/** @type {number} 引用计数 */
|
|
63
|
+
this._refCount = 0
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// --------------------------------------------------------------------------
|
|
67
|
+
// Getter
|
|
68
|
+
// --------------------------------------------------------------------------
|
|
69
|
+
|
|
70
|
+
/** @type {string} 资源 ID */
|
|
71
|
+
get id() { return this._id }
|
|
72
|
+
|
|
73
|
+
/** @type {string} 资源类型 */
|
|
74
|
+
get type() { return this._type }
|
|
75
|
+
|
|
76
|
+
/** @type {string} 资源路径 */
|
|
77
|
+
get src() { return this._src }
|
|
78
|
+
|
|
79
|
+
/** @type {string|null} 资源别名 */
|
|
80
|
+
get alias() { return this._alias }
|
|
81
|
+
|
|
82
|
+
/** @type {LoadStrategy} 加载策略 */
|
|
83
|
+
get strategy() { return this._strategy }
|
|
84
|
+
|
|
85
|
+
/** @type {string} 分组名称 */
|
|
86
|
+
get group() { return this._group }
|
|
87
|
+
|
|
88
|
+
/** @type {string[]} 标签列表 */
|
|
89
|
+
get tags() { return this._tags }
|
|
90
|
+
|
|
91
|
+
/** @type {Object} 适配器自定义选项 */
|
|
92
|
+
get adapterOptions() { return this._adapterOptions }
|
|
93
|
+
|
|
94
|
+
/** @type {ResourceState} 当前状态 */
|
|
95
|
+
get state() { return this._state }
|
|
96
|
+
|
|
97
|
+
/** @type {*} 资源数据 */
|
|
98
|
+
get data() { return this._data }
|
|
99
|
+
|
|
100
|
+
/** @type {Error|null} 加载错误 */
|
|
101
|
+
get error() { return this._error }
|
|
102
|
+
|
|
103
|
+
/** @type {number} 引用计数 */
|
|
104
|
+
get refCount() { return this._refCount }
|
|
105
|
+
|
|
106
|
+
/** @type {boolean} 是否已加载 */
|
|
107
|
+
get isLoaded() { return this._state === ResourceState.LOADED }
|
|
108
|
+
|
|
109
|
+
// --------------------------------------------------------------------------
|
|
110
|
+
// 状态变更
|
|
111
|
+
// --------------------------------------------------------------------------
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 标记为加载中
|
|
115
|
+
*/
|
|
116
|
+
setLoading() {
|
|
117
|
+
this._state = ResourceState.LOADING
|
|
118
|
+
this._error = null
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 标记为已加载
|
|
123
|
+
* @param {*} data - 资源数据
|
|
124
|
+
*/
|
|
125
|
+
setLoaded(data) {
|
|
126
|
+
this._state = ResourceState.LOADED
|
|
127
|
+
this._data = data
|
|
128
|
+
this._error = null
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 标记为加载失败
|
|
133
|
+
* @param {Error} error - 错误
|
|
134
|
+
*/
|
|
135
|
+
setError(error) {
|
|
136
|
+
this._state = ResourceState.ERROR
|
|
137
|
+
this._error = error
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 标记为已卸载
|
|
142
|
+
*/
|
|
143
|
+
setUnloaded() {
|
|
144
|
+
this._state = ResourceState.UNLOADED
|
|
145
|
+
this._data = null
|
|
146
|
+
this._error = null
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* 设置分组名称(仅当未设置分组时由 ResourceCache.createGroup 调用)
|
|
151
|
+
* @param {string} group - 分组名称
|
|
152
|
+
*/
|
|
153
|
+
setGroup(group) {
|
|
154
|
+
if (!this._group) {
|
|
155
|
+
this._group = group
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// --------------------------------------------------------------------------
|
|
160
|
+
// 引用计数
|
|
161
|
+
// --------------------------------------------------------------------------
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* 增加引用计数
|
|
165
|
+
* @returns {number} 新的引用计数
|
|
166
|
+
*/
|
|
167
|
+
addRef() {
|
|
168
|
+
this._refCount++
|
|
169
|
+
return this._refCount
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* 减少引用计数(最小为 0)
|
|
174
|
+
* @returns {number} 新的引用计数
|
|
175
|
+
*/
|
|
176
|
+
releaseRef() {
|
|
177
|
+
if (this._refCount > 0) {
|
|
178
|
+
this._refCount--
|
|
179
|
+
}
|
|
180
|
+
return this._refCount
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* 重置引用计数为 0
|
|
185
|
+
*/
|
|
186
|
+
resetRef() {
|
|
187
|
+
this._refCount = 0
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export { ResourceEntry }
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// Nimbus2D - 资源内部事件总线
|
|
3
|
+
// ----------------------------------------------------------------------------
|
|
4
|
+
// 资源管理模块的内部事件通信通道,与核心 EventBus 解耦。
|
|
5
|
+
//
|
|
6
|
+
// 设计决策:
|
|
7
|
+
// - 轻量级实现,仅用于资源模块内部通信
|
|
8
|
+
// - 不依赖核心 EventBus(避免循环依赖)
|
|
9
|
+
// - 支持基本事件能力(on/off/emit/removeAllListeners)
|
|
10
|
+
// ============================================================================
|
|
11
|
+
|
|
12
|
+
class ResourceEventBus {
|
|
13
|
+
/**
|
|
14
|
+
* 构造函数
|
|
15
|
+
*/
|
|
16
|
+
constructor() {
|
|
17
|
+
/** @type {Map<string, Array<{fn: Function, once: boolean}>>} 事件监听器映射 */
|
|
18
|
+
this._listeners = new Map()
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 注册事件监听器
|
|
23
|
+
* @param {string} event - 事件名称
|
|
24
|
+
* @param {Function} listener - 监听回调
|
|
25
|
+
* @returns {Function} 取消注册函数
|
|
26
|
+
*/
|
|
27
|
+
on(event, listener) {
|
|
28
|
+
if (!this._listeners.has(event)) {
|
|
29
|
+
this._listeners.set(event, [])
|
|
30
|
+
}
|
|
31
|
+
this._listeners.get(event).push({ fn: listener, once: false })
|
|
32
|
+
return () => this.off(event, listener)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 注册一次性事件监听器
|
|
37
|
+
* @param {string} event - 事件名称
|
|
38
|
+
* @param {Function} listener - 监听回调
|
|
39
|
+
* @returns {Function} 取消注册函数
|
|
40
|
+
*/
|
|
41
|
+
once(event, listener) {
|
|
42
|
+
if (!this._listeners.has(event)) {
|
|
43
|
+
this._listeners.set(event, [])
|
|
44
|
+
}
|
|
45
|
+
this._listeners.get(event).push({ fn: listener, once: true })
|
|
46
|
+
return () => this.off(event, listener)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 移除事件监听器
|
|
51
|
+
* @param {string} event - 事件名称
|
|
52
|
+
* @param {Function} listener - 要移除的监听回调
|
|
53
|
+
* @returns {boolean} 是否移除成功
|
|
54
|
+
*/
|
|
55
|
+
off(event, listener) {
|
|
56
|
+
const list = this._listeners.get(event)
|
|
57
|
+
if (!list) return false
|
|
58
|
+
|
|
59
|
+
const index = list.findIndex((entry) => entry.fn === listener)
|
|
60
|
+
if (index === -1) return false
|
|
61
|
+
|
|
62
|
+
list.splice(index, 1)
|
|
63
|
+
if (list.length === 0) {
|
|
64
|
+
this._listeners.delete(event)
|
|
65
|
+
}
|
|
66
|
+
return true
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 触发事件
|
|
71
|
+
* @param {string} event - 事件名称
|
|
72
|
+
* @param {...*} args - 传递给监听器的参数
|
|
73
|
+
* @returns {boolean} true 表示有监听器被调用
|
|
74
|
+
*/
|
|
75
|
+
emit(event, ...args) {
|
|
76
|
+
const list = this._listeners.get(event)
|
|
77
|
+
if (!list || list.length === 0) return false
|
|
78
|
+
|
|
79
|
+
const snapshot = list.slice()
|
|
80
|
+
const toRemove = []
|
|
81
|
+
|
|
82
|
+
for (const entry of snapshot) {
|
|
83
|
+
try {
|
|
84
|
+
entry.fn(...args)
|
|
85
|
+
} catch (e) {
|
|
86
|
+
// 错误不中断其他监听器
|
|
87
|
+
}
|
|
88
|
+
if (entry.once) {
|
|
89
|
+
toRemove.push(entry)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
for (const entry of toRemove) {
|
|
94
|
+
const index = list.indexOf(entry)
|
|
95
|
+
if (index !== -1) {
|
|
96
|
+
list.splice(index, 1)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (list.length === 0) {
|
|
101
|
+
this._listeners.delete(event)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return true
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 移除指定事件的所有监听器(不传参则移除所有)
|
|
109
|
+
* @param {string} [event] - 事件名称
|
|
110
|
+
*/
|
|
111
|
+
removeAllListeners(event) {
|
|
112
|
+
if (event) {
|
|
113
|
+
this._listeners.delete(event)
|
|
114
|
+
} else {
|
|
115
|
+
this._listeners.clear()
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* 销毁
|
|
121
|
+
*/
|
|
122
|
+
destroy() {
|
|
123
|
+
this._listeners.clear()
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export { ResourceEventBus }
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// Nimbus2D - 资源处理器接口
|
|
3
|
+
// ----------------------------------------------------------------------------
|
|
4
|
+
// 定义资源处理器的契约,处理器在资源加载/卸载完成时执行回调。
|
|
5
|
+
//
|
|
6
|
+
// 设计决策:
|
|
7
|
+
// - 独立设计(不继承任何类)
|
|
8
|
+
// - 两个回调:onLoadComplete、onLoadError、onUnload
|
|
9
|
+
// - 用于资源加载后的后处理(如缓存预热、数据校验等)
|
|
10
|
+
// ============================================================================
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 资源处理器接口(基类)
|
|
14
|
+
* 资源处理器在资源加载完成、加载失败、卸载完成时接收通知,
|
|
15
|
+
* 可用于缓存预热、数据校验、统计记录等场景。
|
|
16
|
+
*/
|
|
17
|
+
class IResourceHandler {
|
|
18
|
+
/**
|
|
19
|
+
* 资源加载完成回调
|
|
20
|
+
* @param {import('../core/ResourceEntry.js').ResourceEntry} entry - 资源条目
|
|
21
|
+
* @param {*} data - 资源数据
|
|
22
|
+
*/
|
|
23
|
+
onLoadComplete(entry, data) {
|
|
24
|
+
// 基类空实现,子类按需重写
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 资源加载失败回调
|
|
29
|
+
* @param {import('../core/ResourceEntry.js').ResourceEntry} entry - 资源条目
|
|
30
|
+
* @param {Error} error - 错误
|
|
31
|
+
*/
|
|
32
|
+
onLoadError(entry, error) {
|
|
33
|
+
// 基类空实现,子类按需重写
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 资源卸载完成回调
|
|
38
|
+
* @param {import('../core/ResourceEntry.js').ResourceEntry} entry - 资源条目
|
|
39
|
+
*/
|
|
40
|
+
onUnload(entry) {
|
|
41
|
+
// 基类空实现,子类按需重写
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { IResourceHandler }
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// Nimbus2D - AssetManager 模块统一导出
|
|
3
|
+
// ============================================================================
|
|
4
|
+
|
|
5
|
+
export { AssetManager } from './AssetManager.js'
|
|
6
|
+
export {
|
|
7
|
+
ResourceType,
|
|
8
|
+
ResourceState,
|
|
9
|
+
LoadStrategy,
|
|
10
|
+
ResourceEventType,
|
|
11
|
+
ResourceError,
|
|
12
|
+
ResourceNotFoundError,
|
|
13
|
+
ResourceDuplicateError,
|
|
14
|
+
ResourceStateError,
|
|
15
|
+
ResourceLoadError,
|
|
16
|
+
AdapterError,
|
|
17
|
+
ResourceDestroyedError
|
|
18
|
+
} from './AssetTypes.js'
|
|
19
|
+
export { ResourceEntry } from './core/ResourceEntry.js'
|
|
20
|
+
export { ResourceCache } from './core/ResourceCache.js'
|
|
21
|
+
export { RefCounter } from './core/RefCounter.js'
|
|
22
|
+
export { ResourceEventBus } from './core/ResourceEventBus.js'
|
|
23
|
+
export { IResourceAdapter } from './adapters/IResourceAdapter.js'
|
|
24
|
+
export { PixiAdapter } from './adapters/PixiAdapter.js'
|
|
25
|
+
export { IResourceHandler } from './handlers/IResourceHandler.js'
|
|
26
|
+
export { IResourcePlugin } from './plugins/IResourcePlugin.js'
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// Nimbus2D - 资源插件接口
|
|
3
|
+
// ----------------------------------------------------------------------------
|
|
4
|
+
// 定义资源插件的契约,插件通过钩子机制扩展资源管理行为。
|
|
5
|
+
//
|
|
6
|
+
// 设计决策:
|
|
7
|
+
// - 独立设计(不继承任何类)
|
|
8
|
+
// - 四个钩子:beforeLoad、afterLoad、beforeUnload、afterUnload
|
|
9
|
+
// - 钩子可返回 Promise,支持异步处理
|
|
10
|
+
// ============================================================================
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 资源插件接口(基类)
|
|
14
|
+
* 资源插件通过 before/after 钩子在资源加载和卸载前后执行自定义逻辑,
|
|
15
|
+
* 可用于数据校验、格式转换、缓存预热、性能监控等。
|
|
16
|
+
*/
|
|
17
|
+
class IResourcePlugin {
|
|
18
|
+
/**
|
|
19
|
+
* 资源加载前钩子
|
|
20
|
+
* 可修改 entry(如添加适配器选项),返回修改后的 entry
|
|
21
|
+
* @param {import('../core/ResourceEntry.js').ResourceEntry} entry - 资源条目
|
|
22
|
+
* @returns {import('../core/ResourceEntry.js').ResourceEntry|Promise<import('../core/ResourceEntry.js').ResourceEntry>}
|
|
23
|
+
*/
|
|
24
|
+
beforeLoad(entry) {
|
|
25
|
+
return entry
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 资源加载后钩子
|
|
30
|
+
* 可修改 data(如数据转换),返回修改后的 data
|
|
31
|
+
* @param {import('../core/ResourceEntry.js').ResourceEntry} entry - 资源条目
|
|
32
|
+
* @param {*} data - 资源数据
|
|
33
|
+
* @returns {*|Promise<*>}
|
|
34
|
+
*/
|
|
35
|
+
afterLoad(entry, data) {
|
|
36
|
+
return data
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 资源卸载前钩子
|
|
41
|
+
* @param {import('../core/ResourceEntry.js').ResourceEntry} entry - 资源条目
|
|
42
|
+
*/
|
|
43
|
+
beforeUnload(entry) {
|
|
44
|
+
// 基类空实现
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 资源卸载后钩子
|
|
49
|
+
* @param {import('../core/ResourceEntry.js').ResourceEntry} entry - 资源条目
|
|
50
|
+
*/
|
|
51
|
+
afterUnload(entry) {
|
|
52
|
+
// 基类空实现
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export { IResourcePlugin }
|