mm_expand 2.0.6 → 2.0.8
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/lib/base.js +7 -1
- package/lib/event.js +41 -41
- package/lib/file.js +1 -1
- package/package.json +1 -1
package/lib/base.js
CHANGED
|
@@ -14,6 +14,12 @@ class Base extends Event {
|
|
|
14
14
|
*/
|
|
15
15
|
constructor(config) {
|
|
16
16
|
super();
|
|
17
|
+
/**
|
|
18
|
+
* 配置参数
|
|
19
|
+
* @type {object}
|
|
20
|
+
*/
|
|
21
|
+
this.config = { ...this.constructor.config };
|
|
22
|
+
|
|
17
23
|
/**
|
|
18
24
|
* 状态
|
|
19
25
|
* @type {string} - 状态, 可选值: creating, created, initing, inited, loading, loaded, starting, running, stopping, stopped, unloading, unloaded, destroying, destroyed
|
|
@@ -76,7 +82,7 @@ Base.prototype.setConfig = function (config) {
|
|
|
76
82
|
if (typeof config !== 'object') {
|
|
77
83
|
throw new TypeError('配置参数必须是对象');
|
|
78
84
|
}
|
|
79
|
-
Object.assign(
|
|
85
|
+
Object.assign(this.config, config);
|
|
80
86
|
}
|
|
81
87
|
return this.config;
|
|
82
88
|
};
|
package/lib/event.js
CHANGED
|
@@ -20,7 +20,7 @@ class Event {
|
|
|
20
20
|
* 配置参数
|
|
21
21
|
* @type {object} - 配置参数
|
|
22
22
|
*/
|
|
23
|
-
this.
|
|
23
|
+
this.event_config = {
|
|
24
24
|
...Event.config,
|
|
25
25
|
...config || {}
|
|
26
26
|
};
|
|
@@ -59,17 +59,17 @@ class Event {
|
|
|
59
59
|
*/
|
|
60
60
|
_getEventType(event_name) {
|
|
61
61
|
if (typeof event_name !== 'string') return 'main';
|
|
62
|
-
|
|
62
|
+
|
|
63
63
|
const parts = event_name.split(':');
|
|
64
64
|
if (parts.length < 2) return 'main';
|
|
65
|
-
|
|
65
|
+
|
|
66
66
|
const suffix = parts[parts.length - 1];
|
|
67
67
|
const special_types = ['before', 'check', 'after', 'error', 'render', 'success'];
|
|
68
|
-
|
|
68
|
+
|
|
69
69
|
if (special_types.includes(suffix)) {
|
|
70
70
|
return suffix;
|
|
71
71
|
}
|
|
72
|
-
|
|
72
|
+
|
|
73
73
|
return 'other';
|
|
74
74
|
}
|
|
75
75
|
|
|
@@ -81,14 +81,14 @@ class Event {
|
|
|
81
81
|
*/
|
|
82
82
|
_getEventStore(event_name) {
|
|
83
83
|
const event_type = this._getEventType(event_name);
|
|
84
|
-
|
|
84
|
+
|
|
85
85
|
if (event_type === 'main') {
|
|
86
86
|
return {
|
|
87
87
|
map: this._events.main,
|
|
88
88
|
type: 'main'
|
|
89
89
|
};
|
|
90
90
|
}
|
|
91
|
-
|
|
91
|
+
|
|
92
92
|
return {
|
|
93
93
|
map: this._events.special[event_type],
|
|
94
94
|
type: 'special',
|
|
@@ -108,21 +108,21 @@ class Event {
|
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
const store = this._getEventStore(event_name);
|
|
111
|
-
|
|
111
|
+
|
|
112
112
|
if (!store.map.has(event_name)) {
|
|
113
113
|
store.map.set(event_name, []);
|
|
114
114
|
}
|
|
115
115
|
|
|
116
116
|
const listeners = store.map.get(event_name);
|
|
117
|
-
|
|
117
|
+
|
|
118
118
|
// 检查监听器数量限制
|
|
119
|
-
if (listeners.length >= this.
|
|
120
|
-
this._logger.warn(`事件 ${event_name} 的监听器数量已达到上限 ${this.
|
|
119
|
+
if (listeners.length >= this.event_config.max_listeners) {
|
|
120
|
+
this._logger.warn(`事件 ${event_name} 的监听器数量已达到上限 ${this.event_config.max_listeners}`);
|
|
121
121
|
return this;
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
listeners.push(listener);
|
|
125
|
-
|
|
125
|
+
|
|
126
126
|
// 更新计数器
|
|
127
127
|
if (store.type === 'special') {
|
|
128
128
|
this._counters.special++;
|
|
@@ -167,19 +167,19 @@ class Event {
|
|
|
167
167
|
*/
|
|
168
168
|
off(event_name, listener) {
|
|
169
169
|
const store = this._getEventStore(event_name);
|
|
170
|
-
|
|
170
|
+
|
|
171
171
|
if (!store.map.has(event_name)) {
|
|
172
172
|
return this;
|
|
173
173
|
}
|
|
174
174
|
|
|
175
175
|
const listeners = store.map.get(event_name);
|
|
176
|
-
|
|
176
|
+
|
|
177
177
|
if (listener) {
|
|
178
178
|
// 移除特定监听器
|
|
179
179
|
const index = listeners.findIndex(fn => fn === listener || fn._original === listener);
|
|
180
180
|
if (index !== -1) {
|
|
181
181
|
listeners.splice(index, 1);
|
|
182
|
-
|
|
182
|
+
|
|
183
183
|
// 更新计数器
|
|
184
184
|
if (store.type === 'special') {
|
|
185
185
|
this._counters.special--;
|
|
@@ -191,7 +191,7 @@ class Event {
|
|
|
191
191
|
// 移除所有监听器
|
|
192
192
|
const count = listeners.length;
|
|
193
193
|
store.map.delete(event_name);
|
|
194
|
-
|
|
194
|
+
|
|
195
195
|
// 更新计数器
|
|
196
196
|
if (store.type === 'special') {
|
|
197
197
|
this._counters.special -= count;
|
|
@@ -201,7 +201,7 @@ class Event {
|
|
|
201
201
|
}
|
|
202
202
|
|
|
203
203
|
// 自动清理空的事件列表
|
|
204
|
-
if (this.
|
|
204
|
+
if (this.event_config.auto_clean && listeners && listeners.length === 0) {
|
|
205
205
|
store.map.delete(event_name);
|
|
206
206
|
}
|
|
207
207
|
|
|
@@ -216,13 +216,13 @@ class Event {
|
|
|
216
216
|
*/
|
|
217
217
|
emit(event_name, ...args) {
|
|
218
218
|
const store = this._getEventStore(event_name);
|
|
219
|
-
|
|
219
|
+
|
|
220
220
|
if (!store.map.has(event_name)) {
|
|
221
221
|
return false;
|
|
222
222
|
}
|
|
223
223
|
|
|
224
224
|
const listeners = store.map.get(event_name).slice();
|
|
225
|
-
|
|
225
|
+
|
|
226
226
|
for (const listener of listeners) {
|
|
227
227
|
try {
|
|
228
228
|
listener(...args);
|
|
@@ -242,13 +242,13 @@ class Event {
|
|
|
242
242
|
*/
|
|
243
243
|
async emitWait(event_name, ...args) {
|
|
244
244
|
const store = this._getEventStore(event_name);
|
|
245
|
-
|
|
245
|
+
|
|
246
246
|
if (!store.map.has(event_name)) {
|
|
247
247
|
return;
|
|
248
248
|
}
|
|
249
249
|
|
|
250
250
|
const listeners = store.map.get(event_name).slice();
|
|
251
|
-
|
|
251
|
+
|
|
252
252
|
for (const listener of listeners) {
|
|
253
253
|
try {
|
|
254
254
|
await listener(...args);
|
|
@@ -266,13 +266,13 @@ class Event {
|
|
|
266
266
|
*/
|
|
267
267
|
emitAsync(event_name, ...args) {
|
|
268
268
|
const store = this._getEventStore(event_name);
|
|
269
|
-
|
|
269
|
+
|
|
270
270
|
if (!store.map.has(event_name)) {
|
|
271
271
|
return false;
|
|
272
272
|
}
|
|
273
273
|
|
|
274
274
|
const listeners = store.map.get(event_name).slice();
|
|
275
|
-
|
|
275
|
+
|
|
276
276
|
listeners.forEach(listener => {
|
|
277
277
|
try {
|
|
278
278
|
Promise.resolve(listener(...args))
|
|
@@ -295,7 +295,7 @@ class Event {
|
|
|
295
295
|
*/
|
|
296
296
|
async emitAll(event_name, ...args) {
|
|
297
297
|
const store = this._getEventStore(event_name);
|
|
298
|
-
|
|
298
|
+
|
|
299
299
|
if (!store.map.has(event_name)) {
|
|
300
300
|
return [];
|
|
301
301
|
}
|
|
@@ -320,7 +320,7 @@ class Event {
|
|
|
320
320
|
*/
|
|
321
321
|
async emitRace(event_name, ...args) {
|
|
322
322
|
const store = this._getEventStore(event_name);
|
|
323
|
-
|
|
323
|
+
|
|
324
324
|
if (!store.map.has(event_name)) {
|
|
325
325
|
return null;
|
|
326
326
|
}
|
|
@@ -344,7 +344,7 @@ class Event {
|
|
|
344
344
|
*/
|
|
345
345
|
async emitWaterfall(event_name, value, ...args) {
|
|
346
346
|
const store = this._getEventStore(event_name);
|
|
347
|
-
|
|
347
|
+
|
|
348
348
|
if (!store.map.has(event_name)) {
|
|
349
349
|
return value;
|
|
350
350
|
}
|
|
@@ -371,7 +371,7 @@ class Event {
|
|
|
371
371
|
*/
|
|
372
372
|
listenerCount(event_name) {
|
|
373
373
|
const store = this._getEventStore(event_name);
|
|
374
|
-
|
|
374
|
+
|
|
375
375
|
if (!store.map.has(event_name)) {
|
|
376
376
|
return 0;
|
|
377
377
|
}
|
|
@@ -386,7 +386,7 @@ class Event {
|
|
|
386
386
|
*/
|
|
387
387
|
listeners(event_name) {
|
|
388
388
|
const store = this._getEventStore(event_name);
|
|
389
|
-
|
|
389
|
+
|
|
390
390
|
if (!store.map.has(event_name)) {
|
|
391
391
|
return [];
|
|
392
392
|
}
|
|
@@ -402,11 +402,11 @@ class Event {
|
|
|
402
402
|
removeAllListeners(event_name) {
|
|
403
403
|
if (event_name) {
|
|
404
404
|
const store = this._getEventStore(event_name);
|
|
405
|
-
|
|
405
|
+
|
|
406
406
|
if (store.map.has(event_name)) {
|
|
407
407
|
const count = store.map.get(event_name).length;
|
|
408
408
|
store.map.delete(event_name);
|
|
409
|
-
|
|
409
|
+
|
|
410
410
|
// 更新计数器
|
|
411
411
|
if (store.type === 'special') {
|
|
412
412
|
this._counters.special -= count;
|
|
@@ -431,19 +431,19 @@ class Event {
|
|
|
431
431
|
*/
|
|
432
432
|
eventNames() {
|
|
433
433
|
const event_names = [];
|
|
434
|
-
|
|
434
|
+
|
|
435
435
|
// 添加 main 类型事件
|
|
436
436
|
for (const event_name of this._events.main.keys()) {
|
|
437
437
|
event_names.push(event_name);
|
|
438
438
|
}
|
|
439
|
-
|
|
439
|
+
|
|
440
440
|
// 添加特殊类型事件
|
|
441
441
|
for (const [type, map] of Object.entries(this._events.special)) {
|
|
442
442
|
for (const event_name of map.keys()) {
|
|
443
443
|
event_names.push(event_name);
|
|
444
444
|
}
|
|
445
445
|
}
|
|
446
|
-
|
|
446
|
+
|
|
447
447
|
return event_names;
|
|
448
448
|
}
|
|
449
449
|
|
|
@@ -459,22 +459,22 @@ class Event {
|
|
|
459
459
|
}
|
|
460
460
|
|
|
461
461
|
const store = this._getEventStore(event_name);
|
|
462
|
-
|
|
462
|
+
|
|
463
463
|
if (!store.map.has(event_name)) {
|
|
464
464
|
store.map.set(event_name, []);
|
|
465
465
|
}
|
|
466
466
|
|
|
467
467
|
const listeners = store.map.get(event_name);
|
|
468
|
-
|
|
468
|
+
|
|
469
469
|
// 检查监听器数量限制
|
|
470
|
-
if (listeners.length >= this.
|
|
471
|
-
this._logger.warn(`事件 ${event_name} 的监听器数量已达到上限 ${this.
|
|
470
|
+
if (listeners.length >= this.event_config.max_listeners) {
|
|
471
|
+
this._logger.warn(`事件 ${event_name} 的监听器数量已达到上限 ${this.event_config.max_listeners}`);
|
|
472
472
|
return this;
|
|
473
473
|
}
|
|
474
474
|
|
|
475
475
|
// 前置添加监听器
|
|
476
476
|
listeners.unshift(listener);
|
|
477
|
-
|
|
477
|
+
|
|
478
478
|
// 更新计数器
|
|
479
479
|
if (store.type === 'special') {
|
|
480
480
|
this._counters.special++;
|
|
@@ -508,7 +508,7 @@ class Event {
|
|
|
508
508
|
*/
|
|
509
509
|
rawListeners(event_name) {
|
|
510
510
|
const store = this._getEventStore(event_name);
|
|
511
|
-
|
|
511
|
+
|
|
512
512
|
if (!store.map.has(event_name)) {
|
|
513
513
|
return [];
|
|
514
514
|
}
|
|
@@ -545,9 +545,9 @@ class Event {
|
|
|
545
545
|
* @private
|
|
546
546
|
*/
|
|
547
547
|
_handleError(error, event_name, args) {
|
|
548
|
-
if (this.
|
|
548
|
+
if (this.event_config.error_handler) {
|
|
549
549
|
try {
|
|
550
|
-
this.
|
|
550
|
+
this.event_config.error_handler(error, event_name, args);
|
|
551
551
|
} catch (handler_error) {
|
|
552
552
|
this._logger.error('错误处理函数执行失败:', handler_error);
|
|
553
553
|
}
|
package/lib/file.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_expand",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.8",
|
|
4
4
|
"description": "Super Meimei Prototype Function Extension Module - Enhanced operations for string, array, object, date manipulation with error prevention and simplified business logic.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|