mm_expand 2.3.6 → 2.3.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/array.js +1 -1
- package/lib/date.js +6 -6
- package/lib/eventer.js +177 -184
- package/lib/number.js +2 -0
- package/lib/object.js +9 -0
- package/lib/string.js +6 -9
- package/package.json +1 -1
package/lib/array.js
CHANGED
|
@@ -155,7 +155,7 @@ function createDescCnSort(field) {
|
|
|
155
155
|
/**
|
|
156
156
|
* 列表转树形列表
|
|
157
157
|
* @param {string | object} idOrConfig 主键字段名或配置对象
|
|
158
|
-
* @param id_or_config
|
|
158
|
+
* @param {string} id_or_config 主键字段名
|
|
159
159
|
* @param {number} value 根节点值
|
|
160
160
|
* @param {string} father_id 父节点字段名
|
|
161
161
|
* @param {string} sub 子节点字段名
|
package/lib/date.js
CHANGED
|
@@ -18,14 +18,14 @@ Date.prototype.toStr = function (fm) {
|
|
|
18
18
|
'q+': Math.floor((t.getMonth() + 3) / 3),
|
|
19
19
|
'S': t.getMilliseconds()
|
|
20
20
|
};
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
format = format.replace(
|
|
21
|
+
var yearMatch = format.match(/(y+)/);
|
|
22
|
+
if (yearMatch) {
|
|
23
|
+
format = format.replace(yearMatch[1], (t.getFullYear() + '').substring(4 - yearMatch[1].length));
|
|
24
24
|
}
|
|
25
25
|
for (var k in obj) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
format = format.replace(
|
|
26
|
+
var match = format.match(new RegExp('(' + k + ')'));
|
|
27
|
+
if (match) {
|
|
28
|
+
format = format.replace(match[1], match[1].length == 1 ? obj[k] : ('00' + obj[k]).substring(('' +
|
|
29
29
|
obj[k]).length));
|
|
30
30
|
}
|
|
31
31
|
}
|
package/lib/eventer.js
CHANGED
|
@@ -33,17 +33,17 @@ class Eventer {
|
|
|
33
33
|
// 事件中间件
|
|
34
34
|
this._middleware = [];
|
|
35
35
|
// 命名空间映射
|
|
36
|
-
this.
|
|
37
|
-
// 事件链映射 { 'event1': ['event2', 'event3'], '
|
|
36
|
+
this._space = {};
|
|
37
|
+
// 事件链映射 { 'event1': ['event2', 'event3'], 'space:event1': ['event4'] }
|
|
38
38
|
this._chain = {};
|
|
39
39
|
// 进行中的任务映射,用于取消操作
|
|
40
40
|
this._tasks = new Map();
|
|
41
41
|
// 暂停状态管理
|
|
42
42
|
this._paused = new Set(); // 暂停的单个事件
|
|
43
|
-
this.
|
|
43
|
+
this._paused_space = new Set(); // 暂停的命名空间
|
|
44
44
|
this._paused_global = false; // 全局暂停状态
|
|
45
45
|
// 初始化默认命名空间
|
|
46
|
-
this.
|
|
46
|
+
this._space[this.config.default_name] = true;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
/**
|
|
@@ -95,14 +95,14 @@ Eventer.prototype.init = async function (...args) {
|
|
|
95
95
|
|
|
96
96
|
/**
|
|
97
97
|
* 监听事件(支持命名空间和优先级)
|
|
98
|
-
* @param {string} key 事件关键字,支持
|
|
98
|
+
* @param {string} key 事件关键字,支持space:key格式
|
|
99
99
|
* @param {object} func 触发函数
|
|
100
100
|
* @param {string} name 增加名称,方便删除,当出现相同名称时会被覆盖
|
|
101
101
|
* @param {number} times 执行次数 -1为无限次执行
|
|
102
102
|
* @param {number} priority 优先级,数字越小优先级越高,默认为0
|
|
103
103
|
* @param {object} options 其他选项,如throttle、debounce、timeout等
|
|
104
104
|
* @param {string} ns 可选的显式命名空间,优先级高于key中的命名空间
|
|
105
|
-
* @returns {object} 返回事件信息对象 {
|
|
105
|
+
* @returns {object} 返回事件信息对象 {space, index, name, remove},remove为移除该事件的函数
|
|
106
106
|
*/
|
|
107
107
|
Eventer.prototype._validateOnParams = function (key, func) {
|
|
108
108
|
if (!key || typeof key !== 'string') {
|
|
@@ -115,19 +115,19 @@ Eventer.prototype._validateOnParams = function (key, func) {
|
|
|
115
115
|
|
|
116
116
|
Eventer.prototype._parseEventKey = function (key, ns) {
|
|
117
117
|
let parsed = this._parseKey(key);
|
|
118
|
-
let
|
|
118
|
+
let space = ns || parsed.space;
|
|
119
119
|
let event_key = parsed.key;
|
|
120
|
-
let full_key = `${
|
|
121
|
-
return {
|
|
120
|
+
let full_key = `${space}:${event_key}`;
|
|
121
|
+
return { space, event_key, full_key };
|
|
122
122
|
};
|
|
123
123
|
|
|
124
|
-
Eventer.prototype._initNamespace = function (
|
|
125
|
-
if (!this._dict[
|
|
126
|
-
this._dict[
|
|
127
|
-
this.
|
|
124
|
+
Eventer.prototype._initNamespace = function (space, event_key) {
|
|
125
|
+
if (!this._dict[space]) {
|
|
126
|
+
this._dict[space] = {};
|
|
127
|
+
this._space[space] = true;
|
|
128
128
|
}
|
|
129
|
-
if (!this._dict[
|
|
130
|
-
this._dict[
|
|
129
|
+
if (!this._dict[space][event_key]) {
|
|
130
|
+
this._dict[space][event_key] = [];
|
|
131
131
|
}
|
|
132
132
|
};
|
|
133
133
|
|
|
@@ -179,16 +179,16 @@ Eventer.prototype._findHandlerIndex = function (list, name) {
|
|
|
179
179
|
return -1;
|
|
180
180
|
};
|
|
181
181
|
|
|
182
|
-
Eventer.prototype._checkListenerLimit = function (
|
|
182
|
+
Eventer.prototype._checkListenerLimit = function (space, event_key, list) {
|
|
183
183
|
let max_list = this.config.max_list;
|
|
184
184
|
if (max_list > 0 && list.length >= max_list) {
|
|
185
|
-
this.log('warn', `警告: '${
|
|
185
|
+
this.log('warn', `警告: '${space}:${event_key}' 事件的监听器数量已达到 ${max_list},可能导致内存泄漏。使用 set_max_listeners() 方法可以增加限制。`);
|
|
186
186
|
}
|
|
187
187
|
};
|
|
188
188
|
|
|
189
|
-
Eventer.prototype._createHandlerResult = function (
|
|
189
|
+
Eventer.prototype._createHandlerResult = function (space, index, name, full_key) {
|
|
190
190
|
return {
|
|
191
|
-
|
|
191
|
+
space,
|
|
192
192
|
index,
|
|
193
193
|
name,
|
|
194
194
|
/**
|
|
@@ -200,11 +200,11 @@ Eventer.prototype._createHandlerResult = function (namespace, index, name, full_
|
|
|
200
200
|
|
|
201
201
|
/**
|
|
202
202
|
* 监听事件(支持命名空间和优先级)
|
|
203
|
-
* @param {string} key 事件关键字,支持
|
|
203
|
+
* @param {string} key 事件关键字,支持space:key格式
|
|
204
204
|
* @param {object} func 触发函数
|
|
205
205
|
* @param {string} name 增加名称,方便删除,当出现相同名称时会被覆盖
|
|
206
206
|
* @param {object} options 其他选项,如throttle、debounce、timeout等
|
|
207
|
-
* @returns {object} 返回事件信息对象 {
|
|
207
|
+
* @returns {object} 返回事件信息对象 {space, index, name, remove},remove为移除该事件的函数
|
|
208
208
|
*/
|
|
209
209
|
Eventer.prototype.on = function (key, func, name, options) {
|
|
210
210
|
// 处理参数
|
|
@@ -241,10 +241,10 @@ Eventer.prototype._addEventHandler = function (params) {
|
|
|
241
241
|
times: eventTimes, priority: eventPriority, ns } = params;
|
|
242
242
|
|
|
243
243
|
// 解析命名空间
|
|
244
|
-
const {
|
|
244
|
+
const { space, event_key, full_key } = this._parseEventKey(eventKey, ns);
|
|
245
245
|
|
|
246
246
|
// 初始化命名空间和事件数组
|
|
247
|
-
this._initNamespace(
|
|
247
|
+
this._initNamespace(space, event_key);
|
|
248
248
|
|
|
249
249
|
// 设置默认名称
|
|
250
250
|
let final_name = eventName || this._generateDefaultName(eventFunc);
|
|
@@ -262,7 +262,7 @@ Eventer.prototype._addEventHandler = function (params) {
|
|
|
262
262
|
});
|
|
263
263
|
|
|
264
264
|
// 处理事件添加逻辑
|
|
265
|
-
return this._handleEventAddition({
|
|
265
|
+
return this._handleEventAddition({ space, event_key, full_key, final_name, obj });
|
|
266
266
|
};
|
|
267
267
|
|
|
268
268
|
Eventer.prototype._extractOnParams = function (config) {
|
|
@@ -298,8 +298,8 @@ Eventer.prototype._processOnceParams = function (key, func, name, options) {
|
|
|
298
298
|
};
|
|
299
299
|
|
|
300
300
|
Eventer.prototype._handleEventAddition = function (config) {
|
|
301
|
-
const {
|
|
302
|
-
let list = this._dict[
|
|
301
|
+
const { space, event_key, full_key, final_name, obj } = config;
|
|
302
|
+
let list = this._dict[space][event_key];
|
|
303
303
|
let idx = this._findHandlerIndex(list, final_name);
|
|
304
304
|
|
|
305
305
|
if (idx >= 0) {
|
|
@@ -307,26 +307,26 @@ Eventer.prototype._handleEventAddition = function (config) {
|
|
|
307
307
|
Object.assign(list[idx], obj);
|
|
308
308
|
// 重新排序
|
|
309
309
|
this._sort(list);
|
|
310
|
-
return this._createHandlerResult(
|
|
310
|
+
return this._createHandlerResult(space, idx, final_name, full_key);
|
|
311
311
|
} else {
|
|
312
312
|
// 检查监听器数量是否超过限制
|
|
313
|
-
this._checkListenerLimit(
|
|
313
|
+
this._checkListenerLimit(space, event_key, list);
|
|
314
314
|
|
|
315
315
|
// 添加新事件
|
|
316
316
|
list.push(obj);
|
|
317
317
|
// 按优先级排序
|
|
318
318
|
this._sort(list);
|
|
319
|
-
return this._createHandlerResult(
|
|
319
|
+
return this._createHandlerResult(space, list.length - 1, final_name, full_key);
|
|
320
320
|
}
|
|
321
321
|
};
|
|
322
322
|
|
|
323
323
|
/**
|
|
324
324
|
* 监听事件(仅执行一次)
|
|
325
|
-
* @param {string | object} key 事件关键,支持
|
|
325
|
+
* @param {string | object} key 事件关键,支持space:key格式,或配置对象
|
|
326
326
|
* @param {object} func 触发函数
|
|
327
327
|
* @param {string} name 增加名称,方便删除,当出现相同名称时会被覆盖
|
|
328
328
|
* @param {object} options 其他选项
|
|
329
|
-
* @returns {object} 返回事件信息对象 {
|
|
329
|
+
* @returns {object} 返回事件信息对象 {space, idx, name}
|
|
330
330
|
*/
|
|
331
331
|
Eventer.prototype.once = function (key, func, name, options) {
|
|
332
332
|
// 处理参数
|
|
@@ -341,7 +341,7 @@ Eventer.prototype.once = function (key, func, name, options) {
|
|
|
341
341
|
|
|
342
342
|
/**
|
|
343
343
|
* 统一的事件执行引擎
|
|
344
|
-
* @param {string} key 事件关键字,支持
|
|
344
|
+
* @param {string} key 事件关键字,支持space:key格式
|
|
345
345
|
* @param {object} options 执行选项
|
|
346
346
|
* @param {...any} params 传递参数
|
|
347
347
|
* @returns {Promise<object>} 返回包含results和cancel方法的对象
|
|
@@ -355,13 +355,13 @@ Eventer.prototype._validateExec = function (key) {
|
|
|
355
355
|
|
|
356
356
|
Eventer.prototype._createCtx = function (key, params) {
|
|
357
357
|
let parsed = this._parseKey(key);
|
|
358
|
-
let
|
|
358
|
+
let space = parsed.space;
|
|
359
359
|
let event_key = parsed.key;
|
|
360
|
-
let full_key = `${
|
|
360
|
+
let full_key = `${space}:${event_key}`;
|
|
361
361
|
let start_time = Date.now();
|
|
362
362
|
|
|
363
363
|
const ctx = {
|
|
364
|
-
|
|
364
|
+
space,
|
|
365
365
|
event: event_key,
|
|
366
366
|
full_event: full_key,
|
|
367
367
|
params,
|
|
@@ -379,7 +379,7 @@ Eventer.prototype._createCtx = function (key, params) {
|
|
|
379
379
|
}
|
|
380
380
|
};
|
|
381
381
|
|
|
382
|
-
return { ctx,
|
|
382
|
+
return { ctx, space, event_key, full_key, start_time };
|
|
383
383
|
};
|
|
384
384
|
|
|
385
385
|
Eventer.prototype._cancelTasks = function (full_key) {
|
|
@@ -419,7 +419,7 @@ Eventer.prototype._runBeforeMiddleware = async function (ctx, full_key) {
|
|
|
419
419
|
}
|
|
420
420
|
return false;
|
|
421
421
|
} catch (error) {
|
|
422
|
-
this.log('error', `事件中间件执行失败 [${ctx.
|
|
422
|
+
this.log('error', `事件中间件执行失败 [${ctx.space}:${ctx.event}]`, error);
|
|
423
423
|
if (this.config.error_handler && typeof this.config.error_handler === 'function') {
|
|
424
424
|
this.config.error_handler(error, { type: 'middleware', stage: 'before', ctx });
|
|
425
425
|
}
|
|
@@ -459,28 +459,28 @@ Eventer.prototype._executeStrategy = async function (config) {
|
|
|
459
459
|
}
|
|
460
460
|
};
|
|
461
461
|
|
|
462
|
-
Eventer.prototype._removeEvents = function (
|
|
462
|
+
Eventer.prototype._removeEvents = function (space, key, arr) {
|
|
463
463
|
try {
|
|
464
464
|
for (let i = 0; i < arr.length; i++) {
|
|
465
|
-
this.del(`${
|
|
465
|
+
this.del(`${space}:${key}`, arr[i]);
|
|
466
466
|
}
|
|
467
467
|
} catch (remove_error) {
|
|
468
|
-
this.log('error', `事件移除失败 [${
|
|
468
|
+
this.log('error', `事件移除失败 [${space}:${key}]`, remove_error);
|
|
469
469
|
}
|
|
470
470
|
};
|
|
471
471
|
|
|
472
|
-
Eventer.prototype._autoCleanup = function (
|
|
472
|
+
Eventer.prototype._autoCleanup = function (space, key) {
|
|
473
473
|
try {
|
|
474
|
-
if (this.config.auto_clean && this._dict[
|
|
475
|
-
this._dict[
|
|
476
|
-
delete this._dict[
|
|
477
|
-
if (this._dict[
|
|
478
|
-
delete this._dict[
|
|
479
|
-
delete this.
|
|
474
|
+
if (this.config.auto_clean && this._dict[space] && this._dict[space][key] &&
|
|
475
|
+
this._dict[space][key].length === 0) {
|
|
476
|
+
delete this._dict[space][key];
|
|
477
|
+
if (this._dict[space] && Object.keys(this._dict[space]).length === 0) {
|
|
478
|
+
delete this._dict[space];
|
|
479
|
+
delete this._space[space];
|
|
480
480
|
}
|
|
481
481
|
}
|
|
482
482
|
} catch (cleanup_error) {
|
|
483
|
-
this.log('error', `自动清理失败 [${
|
|
483
|
+
this.log('error', `自动清理失败 [${space}:${key}]`, cleanup_error);
|
|
484
484
|
}
|
|
485
485
|
};
|
|
486
486
|
|
|
@@ -490,7 +490,7 @@ Eventer.prototype._runAfterMiddleware = async function (ctx, start_time) {
|
|
|
490
490
|
ctx.duration = ctx.end_time - start_time;
|
|
491
491
|
await this._runMiddleware('after', ctx);
|
|
492
492
|
} catch (error) {
|
|
493
|
-
this.log('error', `事件后置中间件执行失败 [${ctx.
|
|
493
|
+
this.log('error', `事件后置中间件执行失败 [${ctx.space}:${ctx.event}]`, error);
|
|
494
494
|
if (this.config.error_handler && typeof this.config.error_handler === 'function') {
|
|
495
495
|
this.config.error_handler(error, { type: 'middleware', stage: 'after', ctx });
|
|
496
496
|
}
|
|
@@ -537,18 +537,18 @@ Eventer.prototype._buildSafeResult = function (ctx) {
|
|
|
537
537
|
Eventer.prototype._execute = async function (key, options = {}, ...params) {
|
|
538
538
|
this._validateExec(key);
|
|
539
539
|
|
|
540
|
-
const { ctx,
|
|
540
|
+
const { ctx, space, event_key, full_key, start_time } = this._createCtx(key, params);
|
|
541
541
|
|
|
542
542
|
if (this.isPaused(full_key)) {
|
|
543
543
|
return this._createPausedResult();
|
|
544
544
|
}
|
|
545
545
|
|
|
546
|
-
let list = this._dict[
|
|
546
|
+
let list = this._dict[space] && this._dict[space][event_key];
|
|
547
547
|
const arr = [];
|
|
548
548
|
|
|
549
549
|
if (list && list.length > 0) {
|
|
550
550
|
await this._executeEventList({
|
|
551
|
-
list, params, ctx, full_key, arr, options,
|
|
551
|
+
list, params, ctx, full_key, arr, options, space, event_key, start_time
|
|
552
552
|
});
|
|
553
553
|
}
|
|
554
554
|
|
|
@@ -582,13 +582,13 @@ Eventer.prototype._createPausedResult = function () {
|
|
|
582
582
|
* @param {string} config.full_key 完整键名
|
|
583
583
|
* @param {Array} config.arr 数组
|
|
584
584
|
* @param {object} config.options 选项
|
|
585
|
-
* @param {string} config.
|
|
585
|
+
* @param {string} config.space 命名空间
|
|
586
586
|
* @param {string} config.event_key 事件键
|
|
587
587
|
* @param {number} config.start_time 开始时间
|
|
588
588
|
* @private
|
|
589
589
|
*/
|
|
590
590
|
Eventer.prototype._executeEventList = async function (config) {
|
|
591
|
-
const { list, params, ctx, full_key, arr, options,
|
|
591
|
+
const { list, params, ctx, full_key, arr, options, space, event_key, start_time } = config;
|
|
592
592
|
|
|
593
593
|
this._initTaskList(full_key);
|
|
594
594
|
|
|
@@ -604,8 +604,8 @@ Eventer.prototype._executeEventList = async function (config) {
|
|
|
604
604
|
await this._executeStrategy({ list, params, ctx, full_key, arr, options });
|
|
605
605
|
|
|
606
606
|
this._cleanupTasks(full_key);
|
|
607
|
-
this._removeEvents(
|
|
608
|
-
this._autoCleanup(
|
|
607
|
+
this._removeEvents(space, event_key, arr);
|
|
608
|
+
this._autoCleanup(space, event_key);
|
|
609
609
|
await this._runAfterMiddleware(ctx, start_time);
|
|
610
610
|
};
|
|
611
611
|
|
|
@@ -685,7 +685,7 @@ Eventer.prototype._handleSuccessResult = function (config) {
|
|
|
685
685
|
|
|
686
686
|
Eventer.prototype._handleTaskError = function (e, o, ctx, task) {
|
|
687
687
|
ctx.error_count++;
|
|
688
|
-
this.log('error', `事件执行失败 [${ctx.
|
|
688
|
+
this.log('error', `事件执行失败 [${ctx.space}:${ctx.event}] - ${o.name}`, e);
|
|
689
689
|
|
|
690
690
|
const error_result = {
|
|
691
691
|
_error: true,
|
|
@@ -828,7 +828,7 @@ Eventer.prototype._handleWfSuccess = function (config) {
|
|
|
828
828
|
Eventer.prototype._handleWfError = function (config) {
|
|
829
829
|
const { e, obj, ctx, task } = config;
|
|
830
830
|
ctx.error_count++;
|
|
831
|
-
this.log('error', `事件执行失败 [${ctx.
|
|
831
|
+
this.log('error', `事件执行失败 [${ctx.space}:${ctx.event}] - ${obj.name}`, e);
|
|
832
832
|
|
|
833
833
|
const error_result = {
|
|
834
834
|
_error: true,
|
|
@@ -935,7 +935,7 @@ Eventer.prototype._handleRaceSuccess = function (config) {
|
|
|
935
935
|
|
|
936
936
|
Eventer.prototype._handleRaceError = function (e, obj, ctx, task) {
|
|
937
937
|
ctx.error_count++;
|
|
938
|
-
this.log('error', `事件执行失败 [${ctx.
|
|
938
|
+
this.log('error', `事件执行失败 [${ctx.space}:${ctx.event}] - ${obj.name}`, e);
|
|
939
939
|
|
|
940
940
|
const error_result = {
|
|
941
941
|
_error: true,
|
|
@@ -1039,7 +1039,7 @@ Eventer.prototype._handleTaskError = function (config) {
|
|
|
1039
1039
|
|
|
1040
1040
|
/**
|
|
1041
1041
|
* 执行事件(支持中间件和命名空间)
|
|
1042
|
-
* @param {string} key 事件关键字,支持
|
|
1042
|
+
* @param {string} key 事件关键字,支持space:key格式
|
|
1043
1043
|
* @param {object} params 传递参数
|
|
1044
1044
|
* @returns {Promise<object>} 返回包含results和cancel方法的对象
|
|
1045
1045
|
*/
|
|
@@ -1049,7 +1049,7 @@ Eventer.prototype.run = async function (key, ...params) {
|
|
|
1049
1049
|
|
|
1050
1050
|
/**
|
|
1051
1051
|
* 触发事件
|
|
1052
|
-
* @param {string} key 事件关键,支持
|
|
1052
|
+
* @param {string} key 事件关键,支持space:key格式
|
|
1053
1053
|
* @param {object} params 传递参数
|
|
1054
1054
|
* @returns {Promise<object>} 返回包含results和cancel方法的对象
|
|
1055
1055
|
*/
|
|
@@ -1089,7 +1089,7 @@ Eventer.prototype.emit = function (key, ...params) {
|
|
|
1089
1089
|
|
|
1090
1090
|
/**
|
|
1091
1091
|
* 异步触发事件,按顺序执行等待完成
|
|
1092
|
-
* @param {string} key 事件关键字,支持
|
|
1092
|
+
* @param {string} key 事件关键字,支持space:key格式
|
|
1093
1093
|
* @param {...any} args 事件参数
|
|
1094
1094
|
* @returns {Promise} - 事件触发结果
|
|
1095
1095
|
*/
|
|
@@ -1105,7 +1105,7 @@ Eventer.prototype.emitWait = async function (key, ...args) {
|
|
|
1105
1105
|
|
|
1106
1106
|
/**
|
|
1107
1107
|
* 异步触发事件,并发执行不等待
|
|
1108
|
-
* @param {string} key 事件关键字,支持
|
|
1108
|
+
* @param {string} key 事件关键字,支持space:key格式
|
|
1109
1109
|
* @param {...any} args 事件参数
|
|
1110
1110
|
* @returns {boolean} - 是否有监听者
|
|
1111
1111
|
*/
|
|
@@ -1121,12 +1121,12 @@ Eventer.prototype.emitAsync = async function (key, ...args) {
|
|
|
1121
1121
|
Eventer.prototype._execAsync = function (key, args) {
|
|
1122
1122
|
// 解析命名空间
|
|
1123
1123
|
let parsed = this._parseKey(key);
|
|
1124
|
-
let
|
|
1124
|
+
let space = parsed.space;
|
|
1125
1125
|
let event_key = parsed.key;
|
|
1126
|
-
let full_key = `${
|
|
1126
|
+
let full_key = `${space}:${event_key}`;
|
|
1127
1127
|
|
|
1128
1128
|
// 获取事件列表
|
|
1129
|
-
let list = this._dict[
|
|
1129
|
+
let list = this._dict[space] && this._dict[space][event_key];
|
|
1130
1130
|
|
|
1131
1131
|
if (!list || list.length === 0) {
|
|
1132
1132
|
return false;
|
|
@@ -1174,7 +1174,7 @@ Eventer.prototype._handleSyncError = function (sync_error, full_key, args) {
|
|
|
1174
1174
|
|
|
1175
1175
|
/**
|
|
1176
1176
|
* 异步触发事件,并发执行等待完成
|
|
1177
|
-
* @param {string} key 事件关键字,支持
|
|
1177
|
+
* @param {string} key 事件关键字,支持space:key格式
|
|
1178
1178
|
* @param {...any} args 事件参数
|
|
1179
1179
|
* @returns {Promise<Array>} - 所有监听器的执行结果
|
|
1180
1180
|
*/
|
|
@@ -1190,7 +1190,7 @@ Eventer.prototype.emitAll = async function (key, ...args) {
|
|
|
1190
1190
|
|
|
1191
1191
|
/**
|
|
1192
1192
|
* 异步触发事件,竞争执行
|
|
1193
|
-
* @param {string} key 事件关键字,支持
|
|
1193
|
+
* @param {string} key 事件关键字,支持space:key格式
|
|
1194
1194
|
* @param {...any} args 事件参数
|
|
1195
1195
|
* @returns {Promise} - 第一个完成的结果
|
|
1196
1196
|
*/
|
|
@@ -1206,19 +1206,18 @@ Eventer.prototype.emitRace = async function (key, ...args) {
|
|
|
1206
1206
|
|
|
1207
1207
|
/**
|
|
1208
1208
|
* 异步触发事件,返回第一个有返回值的结果(不阻塞其他监听器)
|
|
1209
|
-
* @param {string} key 事件关键字,支持
|
|
1209
|
+
* @param {string} key 事件关键字,支持space:key格式
|
|
1210
1210
|
* @param {...any} args 事件参数
|
|
1211
1211
|
* @returns {Promise} - 第一个有返回值(非 undefined)的监听器结果
|
|
1212
|
-
* 所有监听器并行执行,返回第一个有返回值(非 undefined
|
|
1213
|
-
* 返回 undefined 的监听器被跳过,其他监听器继续在后台执行不受影响
|
|
1212
|
+
* 所有监听器并行执行,返回第一个有返回值(非 undefined)的结果,返回 undefined 的监听器被跳过,其他监听器继续在后台执行不受影响
|
|
1214
1213
|
*/
|
|
1215
1214
|
Eventer.prototype.emitFirst = async function (key, ...args) {
|
|
1216
1215
|
try {
|
|
1217
1216
|
let parsed = this._parseKey(key);
|
|
1218
|
-
let
|
|
1217
|
+
let space = parsed.space;
|
|
1219
1218
|
let event_key = parsed.key;
|
|
1220
1219
|
|
|
1221
|
-
let list = this._dict[
|
|
1220
|
+
let list = this._dict[space] && this._dict[space][event_key];
|
|
1222
1221
|
if (!list || list.length === 0) {
|
|
1223
1222
|
return undefined;
|
|
1224
1223
|
}
|
|
@@ -1253,7 +1252,7 @@ Eventer.prototype.emitFirst = async function (key, ...args) {
|
|
|
1253
1252
|
|
|
1254
1253
|
/**
|
|
1255
1254
|
* 异步触发事件,瀑布流执行
|
|
1256
|
-
* @param {string} key 事件关键字,支持
|
|
1255
|
+
* @param {string} key 事件关键字,支持space:key格式
|
|
1257
1256
|
* @param {*} value 初始值
|
|
1258
1257
|
* @param {...any} args 事件参数
|
|
1259
1258
|
* @returns {Promise} - 最后一个事件监听者的结果
|
|
@@ -1271,20 +1270,14 @@ Eventer.prototype.emitWaterfall = async function (key, value, ...args) {
|
|
|
1271
1270
|
}
|
|
1272
1271
|
};
|
|
1273
1272
|
|
|
1274
|
-
/**
|
|
1275
|
-
* 触发所有命名空间的特定事件
|
|
1276
|
-
* @param {string} key 事件关键字(不含命名空间)
|
|
1277
|
-
* @param {object} params 传递参数
|
|
1278
|
-
* @returns {Promise<object>} 返回各命名空间的执行结果 {namespace1: results1, namespace2: results2}
|
|
1279
|
-
*/
|
|
1280
1273
|
/**
|
|
1281
1274
|
* 确保命名空间在映射中
|
|
1282
1275
|
* @private
|
|
1283
1276
|
* @param {string} name 命名空间名称
|
|
1284
1277
|
*/
|
|
1285
1278
|
Eventer.prototype._ensureNs = function (name) {
|
|
1286
|
-
if (!this.
|
|
1287
|
-
this.
|
|
1279
|
+
if (!this._space[name]) {
|
|
1280
|
+
this._space[name] = true;
|
|
1288
1281
|
}
|
|
1289
1282
|
};
|
|
1290
1283
|
|
|
@@ -1298,7 +1291,7 @@ Eventer.prototype._ensureNs = function (name) {
|
|
|
1298
1291
|
*/
|
|
1299
1292
|
Eventer.prototype._execNsEvents = async function (name, key, params) {
|
|
1300
1293
|
let list = this._dict[name][key];
|
|
1301
|
-
const
|
|
1294
|
+
const space_results = [];
|
|
1302
1295
|
const arr = []; // 存储需要移除的事件
|
|
1303
1296
|
|
|
1304
1297
|
// 执行事件处理函数
|
|
@@ -1307,7 +1300,7 @@ Eventer.prototype._execNsEvents = async function (name, key, params) {
|
|
|
1307
1300
|
let obj = list[i];
|
|
1308
1301
|
// 执行事件处理函数
|
|
1309
1302
|
let result = await obj.func.apply(obj, params);
|
|
1310
|
-
|
|
1303
|
+
space_results.push(result);
|
|
1311
1304
|
|
|
1312
1305
|
// 处理执行次数限制
|
|
1313
1306
|
if (obj.times >= 0) {
|
|
@@ -1319,7 +1312,7 @@ Eventer.prototype._execNsEvents = async function (name, key, params) {
|
|
|
1319
1312
|
} catch (e) {
|
|
1320
1313
|
// 记录错误但不中断执行
|
|
1321
1314
|
this.log('error', `事件执行失败 [${name}:${key}] - ${list[i].name}`, e);
|
|
1322
|
-
|
|
1315
|
+
space_results.push({
|
|
1323
1316
|
_error: true,
|
|
1324
1317
|
error: e,
|
|
1325
1318
|
handler: list[i].name
|
|
@@ -1347,7 +1340,7 @@ Eventer.prototype.emitAllNamespaces = async function (key, ...params) {
|
|
|
1347
1340
|
const results = {};
|
|
1348
1341
|
// 确保遍历所有命名空间,包括动态创建的
|
|
1349
1342
|
for (let name in this._dict) {
|
|
1350
|
-
// 确保命名空间在
|
|
1343
|
+
// 确保命名空间在space映射中
|
|
1351
1344
|
this._ensureNs(name);
|
|
1352
1345
|
|
|
1353
1346
|
// 检查该命名空间下是否存在该事件
|
|
@@ -1392,20 +1385,20 @@ Eventer.prototype._removeSingleEvent = function (events, index_or_name) {
|
|
|
1392
1385
|
/**
|
|
1393
1386
|
* 删除整个事件
|
|
1394
1387
|
* @private
|
|
1395
|
-
* @param {string}
|
|
1388
|
+
* @param {string} space 命名空间
|
|
1396
1389
|
* @param {string} key 事件关键字
|
|
1397
1390
|
* @param {string} event_key 事件键
|
|
1398
1391
|
* @param {string} full_key 完整事件关键字
|
|
1399
1392
|
* @returns {boolean} 删除成功返回true
|
|
1400
1393
|
*/
|
|
1401
|
-
Eventer.prototype._removeEntireEvent = function (
|
|
1394
|
+
Eventer.prototype._removeEntireEvent = function (space, event_key, full_key) {
|
|
1402
1395
|
// 安全检查:确保命名空间和事件存在
|
|
1403
|
-
if (!this._dict[
|
|
1396
|
+
if (!this._dict[space] || !this._dict[space][event_key]) {
|
|
1404
1397
|
return false;
|
|
1405
1398
|
}
|
|
1406
1399
|
|
|
1407
1400
|
// 删除整个事件
|
|
1408
|
-
delete this._dict[
|
|
1401
|
+
delete this._dict[space][event_key];
|
|
1409
1402
|
|
|
1410
1403
|
// 清理相关的事件链
|
|
1411
1404
|
if (this._chain) {
|
|
@@ -1425,39 +1418,39 @@ Eventer.prototype._removeEntireEvent = function (namespace, event_key, full_key)
|
|
|
1425
1418
|
/**
|
|
1426
1419
|
* 自动清理空的事件列表和命名空间
|
|
1427
1420
|
* @private
|
|
1428
|
-
* @param {string}
|
|
1421
|
+
* @param {string} space 命名空间
|
|
1429
1422
|
* @param {string} key 事件关键字
|
|
1430
1423
|
* @param {Array} events 事件列表
|
|
1431
1424
|
* @param {string | number} index_or_name 事件索引或名称
|
|
1432
1425
|
*/
|
|
1433
|
-
Eventer.prototype._autoCleanEvents = function (
|
|
1426
|
+
Eventer.prototype._autoCleanEvents = function (space, key, events, index_or_name) {
|
|
1434
1427
|
if (!this.config.auto_clean) return;
|
|
1435
1428
|
|
|
1436
1429
|
// 安全检查:确保命名空间存在
|
|
1437
|
-
if (!this._dict[
|
|
1430
|
+
if (!this._dict[space]) {
|
|
1438
1431
|
return;
|
|
1439
1432
|
}
|
|
1440
1433
|
|
|
1441
1434
|
// 清理空的事件列表
|
|
1442
1435
|
if (index_or_name && events && events.length === 0) {
|
|
1443
1436
|
// 安全检查:确保事件键存在
|
|
1444
|
-
if (this._dict[
|
|
1445
|
-
delete this._dict[
|
|
1437
|
+
if (this._dict[space][key]) {
|
|
1438
|
+
delete this._dict[space][key];
|
|
1446
1439
|
}
|
|
1447
1440
|
}
|
|
1448
1441
|
|
|
1449
1442
|
// 如果命名空间为空,则删除命名空间
|
|
1450
|
-
if (this._dict[
|
|
1451
|
-
delete this._dict[
|
|
1452
|
-
if (this.
|
|
1453
|
-
delete this.
|
|
1443
|
+
if (this._dict[space] && Object.keys(this._dict[space]).length === 0) {
|
|
1444
|
+
delete this._dict[space];
|
|
1445
|
+
if (this._space && this._space[space]) {
|
|
1446
|
+
delete this._space[space];
|
|
1454
1447
|
}
|
|
1455
1448
|
}
|
|
1456
1449
|
};
|
|
1457
1450
|
|
|
1458
1451
|
/**
|
|
1459
1452
|
* 删除事件
|
|
1460
|
-
* @param {string} key 事件关键字,支持
|
|
1453
|
+
* @param {string} key 事件关键字,支持space:key格式
|
|
1461
1454
|
* @param {number | string} index_or_name 事件集索引或名称
|
|
1462
1455
|
* @returns {boolean} 删除成功返回true,否则返回false
|
|
1463
1456
|
*/
|
|
@@ -1469,16 +1462,16 @@ Eventer.prototype.off = function (key, index_or_name) {
|
|
|
1469
1462
|
|
|
1470
1463
|
// 解析命名空间
|
|
1471
1464
|
let parsed = this._parseKey(key);
|
|
1472
|
-
let
|
|
1465
|
+
let space = parsed.space;
|
|
1473
1466
|
let event_key = parsed.key;
|
|
1474
|
-
let full_key = `${
|
|
1467
|
+
let full_key = `${space}:${event_key}`;
|
|
1475
1468
|
|
|
1476
1469
|
// 检查事件是否存在
|
|
1477
|
-
if (!this._dict[
|
|
1470
|
+
if (!this._dict[space] || !this._dict[space][event_key]) {
|
|
1478
1471
|
return false;
|
|
1479
1472
|
}
|
|
1480
1473
|
|
|
1481
|
-
let events = this._dict[
|
|
1474
|
+
let events = this._dict[space][event_key];
|
|
1482
1475
|
let removed = false;
|
|
1483
1476
|
|
|
1484
1477
|
if (index_or_name) {
|
|
@@ -1486,12 +1479,12 @@ Eventer.prototype.off = function (key, index_or_name) {
|
|
|
1486
1479
|
removed = this._removeSingleEvent(events, index_or_name);
|
|
1487
1480
|
} else {
|
|
1488
1481
|
// 删除整个事件
|
|
1489
|
-
removed = this._removeEntireEvent(
|
|
1482
|
+
removed = this._removeEntireEvent(space, event_key, full_key);
|
|
1490
1483
|
}
|
|
1491
1484
|
|
|
1492
1485
|
// 自动清理
|
|
1493
1486
|
if (removed) {
|
|
1494
|
-
this._autoCleanEvents(
|
|
1487
|
+
this._autoCleanEvents(space, event_key, events, index_or_name);
|
|
1495
1488
|
}
|
|
1496
1489
|
|
|
1497
1490
|
return removed;
|
|
@@ -1510,22 +1503,22 @@ Eventer.prototype.clearNamespace = function (name) {
|
|
|
1510
1503
|
return false;
|
|
1511
1504
|
}
|
|
1512
1505
|
delete this._dict[name];
|
|
1513
|
-
delete this.
|
|
1506
|
+
delete this._space[name];
|
|
1514
1507
|
return true;
|
|
1515
1508
|
};
|
|
1516
1509
|
|
|
1517
1510
|
/**
|
|
1518
1511
|
* 获取事件列表
|
|
1519
|
-
* @param {string} key 事件关键字,如果为空则获取所有事件,支持
|
|
1512
|
+
* @param {string} key 事件关键字,如果为空则获取所有事件,支持space:key格式
|
|
1520
1513
|
* @returns {Array | object} 返回事件列表或所有事件对象
|
|
1521
1514
|
*/
|
|
1522
1515
|
Eventer.prototype.getEvents = function (key) {
|
|
1523
1516
|
if (key) {
|
|
1524
1517
|
// 解析命名空间
|
|
1525
|
-
const {
|
|
1526
|
-
return this._dict[
|
|
1518
|
+
const { space, key: event_key } = this._parseKey(key);
|
|
1519
|
+
return this._dict[space] &&
|
|
1527
1520
|
|
|
1528
|
-
this._dict[
|
|
1521
|
+
this._dict[space][event_key] ? [...this._dict[space][event_key]] : [];
|
|
1529
1522
|
}
|
|
1530
1523
|
// 返回深拷贝的所有事件
|
|
1531
1524
|
const result = {};
|
|
@@ -1543,15 +1536,15 @@ Eventer.prototype.getEvents = function (key) {
|
|
|
1543
1536
|
* @returns {Array} 返回所有命名空间数组
|
|
1544
1537
|
*/
|
|
1545
1538
|
Eventer.prototype.getNamespace = function () {
|
|
1546
|
-
return Object.keys(this.
|
|
1539
|
+
return Object.keys(this._space);
|
|
1547
1540
|
};
|
|
1548
1541
|
|
|
1549
1542
|
/**
|
|
1550
1543
|
* 清空所有事件和资源
|
|
1551
|
-
* @param {boolean}
|
|
1544
|
+
* @param {boolean} keep_space 是否保留命名空间定义,默认false
|
|
1552
1545
|
* @returns {Eventer} 返回当前实例,支持链式调用
|
|
1553
1546
|
*/
|
|
1554
|
-
Eventer.prototype.clear = function (
|
|
1547
|
+
Eventer.prototype.clear = function (keep_space = false) {
|
|
1555
1548
|
// 取消所有进行中的任务
|
|
1556
1549
|
if (this._tasks) {
|
|
1557
1550
|
this._tasks.forEach((tasks, key) => {
|
|
@@ -1574,16 +1567,16 @@ Eventer.prototype.clear = function (keepnamespace = false) {
|
|
|
1574
1567
|
if (this._paused) {
|
|
1575
1568
|
this._paused.clear();
|
|
1576
1569
|
}
|
|
1577
|
-
if (this.
|
|
1578
|
-
this.
|
|
1570
|
+
if (this._paused_space) {
|
|
1571
|
+
this._paused_space.clear();
|
|
1579
1572
|
}
|
|
1580
1573
|
this._paused_global = false;
|
|
1581
1574
|
|
|
1582
1575
|
// 保留命名空间定义
|
|
1583
|
-
if (!
|
|
1584
|
-
this.
|
|
1576
|
+
if (!keep_space) {
|
|
1577
|
+
this._space = {};
|
|
1585
1578
|
// 重新初始化默认命名空间
|
|
1586
|
-
this.
|
|
1579
|
+
this._space[this.config.default_name] = true;
|
|
1587
1580
|
}
|
|
1588
1581
|
|
|
1589
1582
|
return this;
|
|
@@ -1591,22 +1584,22 @@ Eventer.prototype.clear = function (keepnamespace = false) {
|
|
|
1591
1584
|
|
|
1592
1585
|
/**
|
|
1593
1586
|
* 检查事件是否存在
|
|
1594
|
-
* @param {string} key 事件关键字,支持
|
|
1587
|
+
* @param {string} key 事件关键字,支持space:key格式
|
|
1595
1588
|
* @param {string} name 事件名称(可选)
|
|
1596
1589
|
* @returns {boolean} 事件存在返回true,否则返回false
|
|
1597
1590
|
*/
|
|
1598
1591
|
Eventer.prototype.has = function (key, name) {
|
|
1599
1592
|
// 解析命名空间
|
|
1600
1593
|
let parsed = this._parseKey(key);
|
|
1601
|
-
let
|
|
1594
|
+
let space = parsed.space;
|
|
1602
1595
|
let event_key = parsed.key;
|
|
1603
1596
|
|
|
1604
|
-
if (!this._dict[
|
|
1597
|
+
if (!this._dict[space] || !this._dict[space][event_key]) {
|
|
1605
1598
|
return false;
|
|
1606
1599
|
}
|
|
1607
1600
|
|
|
1608
1601
|
if (name) {
|
|
1609
|
-
let list = this._dict[
|
|
1602
|
+
let list = this._dict[space][event_key];
|
|
1610
1603
|
for (let i = 0; i < list.length; i++) {
|
|
1611
1604
|
if (list[i].name === name) {
|
|
1612
1605
|
return true;
|
|
@@ -1620,7 +1613,7 @@ Eventer.prototype.has = function (key, name) {
|
|
|
1620
1613
|
|
|
1621
1614
|
/**
|
|
1622
1615
|
* 获取事件数量
|
|
1623
|
-
* @param {string} key 事件关键字,如果为空则获取所有事件数量,支持
|
|
1616
|
+
* @param {string} key 事件关键字,如果为空则获取所有事件数量,支持space:key格式或space:*格式
|
|
1624
1617
|
* @returns {number} 返回事件数量
|
|
1625
1618
|
*/
|
|
1626
1619
|
Eventer.prototype.count = function (key) {
|
|
@@ -1628,11 +1621,11 @@ Eventer.prototype.count = function (key) {
|
|
|
1628
1621
|
// 解析可能的命名空间模式
|
|
1629
1622
|
if (key.ends_with(':*')) {
|
|
1630
1623
|
// 统计命名空间下所有事件
|
|
1631
|
-
let
|
|
1632
|
-
if (this._dict[
|
|
1624
|
+
let space = key.split(':')[0];
|
|
1625
|
+
if (this._dict[space]) {
|
|
1633
1626
|
let total = 0;
|
|
1634
|
-
for (let key in this._dict[
|
|
1635
|
-
total += this._dict[
|
|
1627
|
+
for (let key in this._dict[space]) {
|
|
1628
|
+
total += this._dict[space][key].length;
|
|
1636
1629
|
}
|
|
1637
1630
|
return total;
|
|
1638
1631
|
}
|
|
@@ -1640,10 +1633,10 @@ Eventer.prototype.count = function (key) {
|
|
|
1640
1633
|
}
|
|
1641
1634
|
|
|
1642
1635
|
// 解析具体事件
|
|
1643
|
-
const {
|
|
1644
|
-
return this._dict[
|
|
1636
|
+
const { space, key: event_key } = this._parseKey(key);
|
|
1637
|
+
return this._dict[space] &&
|
|
1645
1638
|
|
|
1646
|
-
this._dict[
|
|
1639
|
+
this._dict[space][event_key] ? this._dict[space][event_key].length : 0;
|
|
1647
1640
|
}
|
|
1648
1641
|
|
|
1649
1642
|
// 统计所有事件
|
|
@@ -1679,7 +1672,7 @@ Eventer.prototype.use = function (type, middleware) {
|
|
|
1679
1672
|
|
|
1680
1673
|
/**
|
|
1681
1674
|
* 添加事件链式触发关系
|
|
1682
|
-
* @param {string} from_event 源事件,支持
|
|
1675
|
+
* @param {string} from_event 源事件,支持space:key格式
|
|
1683
1676
|
* @param {string | Array} to_events 目标事件,可以是单个事件或事件数组
|
|
1684
1677
|
* @returns {Eventer} 返回当前实例,支持链式调用
|
|
1685
1678
|
*/
|
|
@@ -1706,8 +1699,8 @@ Eventer.prototype._validateChainInput = function (from_event, to_events) {
|
|
|
1706
1699
|
};
|
|
1707
1700
|
|
|
1708
1701
|
Eventer.prototype._getFullEventKey = function (event) {
|
|
1709
|
-
const {
|
|
1710
|
-
return `${
|
|
1702
|
+
const { space, key } = this._parseKey(event);
|
|
1703
|
+
return `${space}:${key}`;
|
|
1711
1704
|
};
|
|
1712
1705
|
|
|
1713
1706
|
Eventer.prototype._initEventChain = function (full_from_event) {
|
|
@@ -1741,7 +1734,7 @@ Eventer.prototype._addTargetEvent = function (full_from_event, to_event) {
|
|
|
1741
1734
|
|
|
1742
1735
|
/**
|
|
1743
1736
|
* 移除事件链式触发关系
|
|
1744
|
-
* @param {string} from_event 源事件,支持
|
|
1737
|
+
* @param {string} from_event 源事件,支持space:key格式
|
|
1745
1738
|
* @param {string | Array} to_events 目标事件,可以是单个事件或事件数组(可选,不提供则移除所有关联)
|
|
1746
1739
|
* @returns {Eventer} 返回当前实例,支持链式调用
|
|
1747
1740
|
*/
|
|
@@ -1752,8 +1745,8 @@ Eventer.prototype.unchain = function (from_event, to_events) {
|
|
|
1752
1745
|
}
|
|
1753
1746
|
|
|
1754
1747
|
// 解析源事件
|
|
1755
|
-
const {
|
|
1756
|
-
let full_from_event = `${
|
|
1748
|
+
const { space: from_space, key: fromkey } = this._parseKey(from_event);
|
|
1749
|
+
let full_from_event = `${from_space}:${fromkey}`;
|
|
1757
1750
|
|
|
1758
1751
|
// 如果源事件不存在事件链,直接返回
|
|
1759
1752
|
if (!this._chain[full_from_event]) {
|
|
@@ -1789,8 +1782,8 @@ Eventer.prototype._removeTargetEvent = function (full_from_event, to_event) {
|
|
|
1789
1782
|
}
|
|
1790
1783
|
|
|
1791
1784
|
// 解析目标事件
|
|
1792
|
-
const {
|
|
1793
|
-
let full_to_event = `${
|
|
1785
|
+
const { space: to_space, key: tokey } = this._parseKey(to_event);
|
|
1786
|
+
let full_to_event = `${to_space}:${tokey}`;
|
|
1794
1787
|
|
|
1795
1788
|
// 检查事件链是否存在
|
|
1796
1789
|
if (!this._chain[full_from_event]) {
|
|
@@ -1810,7 +1803,7 @@ Eventer.prototype._removeTargetEvent = function (full_from_event, to_event) {
|
|
|
1810
1803
|
|
|
1811
1804
|
/**
|
|
1812
1805
|
* 获取事件链
|
|
1813
|
-
* @param {string} event 事件关键字,支持
|
|
1806
|
+
* @param {string} event 事件关键字,支持space:key格式
|
|
1814
1807
|
* @returns {Array} 返回事件链数组
|
|
1815
1808
|
*/
|
|
1816
1809
|
Eventer.prototype.getChains = function (event) {
|
|
@@ -1819,8 +1812,8 @@ Eventer.prototype.getChains = function (event) {
|
|
|
1819
1812
|
}
|
|
1820
1813
|
|
|
1821
1814
|
// 解析事件
|
|
1822
|
-
const {
|
|
1823
|
-
let full_event = `${
|
|
1815
|
+
const { space, key } = this._parseKey(event);
|
|
1816
|
+
let full_event = `${space}:${key}`;
|
|
1824
1817
|
|
|
1825
1818
|
return this._chain[full_event] ? [...this._chain[full_event]] : [];
|
|
1826
1819
|
};
|
|
@@ -1852,12 +1845,12 @@ Eventer.prototype._triggerChain = async function (current_event, params) {
|
|
|
1852
1845
|
*/
|
|
1853
1846
|
Eventer.prototype._execChainItem = async function (key, params) {
|
|
1854
1847
|
// 解析命名空间
|
|
1855
|
-
const {
|
|
1856
|
-
let list = this._dict[
|
|
1848
|
+
const { space, key: innerkey } = this._parseKey(key);
|
|
1849
|
+
let list = this._dict[space] && this._dict[space][innerkey];
|
|
1857
1850
|
const results = [];
|
|
1858
1851
|
|
|
1859
1852
|
// 创建上下文对象
|
|
1860
|
-
let ctx = this._createChainContext(
|
|
1853
|
+
let ctx = this._createChainContext(space, innerkey, key, params);
|
|
1861
1854
|
|
|
1862
1855
|
// 如果有事件处理函数,执行它们
|
|
1863
1856
|
if (list && list.length > 0) {
|
|
@@ -1872,10 +1865,10 @@ Eventer.prototype._execChainItem = async function (key, params) {
|
|
|
1872
1865
|
return results;
|
|
1873
1866
|
};
|
|
1874
1867
|
|
|
1875
|
-
Eventer.prototype._createChainContext = function (
|
|
1868
|
+
Eventer.prototype._createChainContext = function (space, innerkey, key, params) {
|
|
1876
1869
|
let start_time = Date.now();
|
|
1877
1870
|
return {
|
|
1878
|
-
|
|
1871
|
+
space,
|
|
1879
1872
|
event: innerkey,
|
|
1880
1873
|
full_event: key,
|
|
1881
1874
|
params,
|
|
@@ -1905,7 +1898,7 @@ Eventer.prototype._runChainMiddleware = async function (type, ctx) {
|
|
|
1905
1898
|
}
|
|
1906
1899
|
await this._runMiddleware(type, ctx);
|
|
1907
1900
|
} catch (error) {
|
|
1908
|
-
this.log('error', `事件链${type}中间件执行失败 [${ctx.
|
|
1901
|
+
this.log('error', `事件链${type}中间件执行失败 [${ctx.space}:${ctx.event}]`, error);
|
|
1909
1902
|
}
|
|
1910
1903
|
};
|
|
1911
1904
|
|
|
@@ -1935,14 +1928,14 @@ Eventer.prototype._handleChainTimes = function (obj, ctx) {
|
|
|
1935
1928
|
if (obj.times >= 0) {
|
|
1936
1929
|
obj.times--;
|
|
1937
1930
|
if (obj.times <= 0) {
|
|
1938
|
-
this.del(`${ctx.
|
|
1931
|
+
this.del(`${ctx.space}:${ctx.event}`, obj.name);
|
|
1939
1932
|
}
|
|
1940
1933
|
}
|
|
1941
1934
|
};
|
|
1942
1935
|
|
|
1943
1936
|
Eventer.prototype._handleChainError = function (e, obj, ctx, results) {
|
|
1944
1937
|
ctx.error_count++;
|
|
1945
|
-
this.log('error', `事件链执行失败 [${ctx.
|
|
1938
|
+
this.log('error', `事件链执行失败 [${ctx.space}:${ctx.event}] - ${obj.name}`, e);
|
|
1946
1939
|
|
|
1947
1940
|
const error_result = {
|
|
1948
1941
|
_error: true,
|
|
@@ -1956,7 +1949,7 @@ Eventer.prototype._handleChainError = function (e, obj, ctx, results) {
|
|
|
1956
1949
|
|
|
1957
1950
|
/**
|
|
1958
1951
|
* 并行执行事件(提高性能)
|
|
1959
|
-
* @param {string} key 事件关键字,支持
|
|
1952
|
+
* @param {string} key 事件关键字,支持space:key格式
|
|
1960
1953
|
* @param {object} params 传递参数
|
|
1961
1954
|
* @param {object} options 执行选项,如concurrency(并发数限制)
|
|
1962
1955
|
* @returns {Promise<object>} 返回包含results和cancel方法的对象
|
|
@@ -1975,7 +1968,7 @@ Eventer.prototype.runParallel = async function (key, ...params) {
|
|
|
1975
1968
|
|
|
1976
1969
|
/**
|
|
1977
1970
|
* 并行触发事件
|
|
1978
|
-
* @param {string} key 事件关键字,支持
|
|
1971
|
+
* @param {string} key 事件关键字,支持space:key格式
|
|
1979
1972
|
* @param {object} params 传递参数
|
|
1980
1973
|
* @param {object} options 执行选项,如concurrency(并发数限制)
|
|
1981
1974
|
* @returns {Promise<object>} 返回包含results和cancel方法的对象
|
|
@@ -2032,7 +2025,7 @@ Eventer.prototype.onMany = function (events_map) {
|
|
|
2032
2025
|
/**
|
|
2033
2026
|
* 解析事件键,提取命名空间和事件键
|
|
2034
2027
|
* @param {string} key 事件关键字
|
|
2035
|
-
* @returns {object} 包含
|
|
2028
|
+
* @returns {object} 包含space和key的对象
|
|
2036
2029
|
* @private
|
|
2037
2030
|
*/
|
|
2038
2031
|
Eventer.prototype._parseKey = function (key) {
|
|
@@ -2040,19 +2033,19 @@ Eventer.prototype._parseKey = function (key) {
|
|
|
2040
2033
|
if (parts.length === 1) {
|
|
2041
2034
|
// 没有命名空间,使用默认命名空间
|
|
2042
2035
|
return {
|
|
2043
|
-
|
|
2036
|
+
space: this.config.default_name,
|
|
2044
2037
|
key: key
|
|
2045
2038
|
};
|
|
2046
2039
|
} else if (parts.length === 2) {
|
|
2047
2040
|
// 有命名空间
|
|
2048
2041
|
return {
|
|
2049
|
-
|
|
2042
|
+
space: parts[0],
|
|
2050
2043
|
key: parts[1]
|
|
2051
2044
|
};
|
|
2052
2045
|
} else {
|
|
2053
2046
|
// 多个冒号,将第一个作为命名空间,其余作为事件键
|
|
2054
2047
|
return {
|
|
2055
|
-
|
|
2048
|
+
space: parts[0],
|
|
2056
2049
|
key: parts.slice(1).join(':')
|
|
2057
2050
|
};
|
|
2058
2051
|
}
|
|
@@ -2152,11 +2145,11 @@ Eventer.prototype._triggerChainEvent = async function (key, current_event, param
|
|
|
2152
2145
|
};
|
|
2153
2146
|
|
|
2154
2147
|
Eventer.prototype._createChainCtx = function (key, current_event, params) {
|
|
2155
|
-
var {
|
|
2156
|
-
var fullkey = `${
|
|
2148
|
+
var { space, key: innerkey } = this._parseKey(key);
|
|
2149
|
+
var fullkey = `${space}:${innerkey}`;
|
|
2157
2150
|
|
|
2158
2151
|
return {
|
|
2159
|
-
|
|
2152
|
+
space,
|
|
2160
2153
|
event: innerkey,
|
|
2161
2154
|
full_event: fullkey,
|
|
2162
2155
|
params,
|
|
@@ -2242,7 +2235,7 @@ Eventer.prototype._debounce = function (func, wait) {
|
|
|
2242
2235
|
|
|
2243
2236
|
/**
|
|
2244
2237
|
* 暂停事件执行
|
|
2245
|
-
* @param {string} key 事件关键字或命名空间,支持
|
|
2238
|
+
* @param {string} key 事件关键字或命名空间,支持space:key格式,为空表示全局暂停
|
|
2246
2239
|
* @returns {boolean} 是否成功暂停
|
|
2247
2240
|
*/
|
|
2248
2241
|
Eventer.prototype.pause = function (key = '') {
|
|
@@ -2257,14 +2250,14 @@ Eventer.prototype.pause = function (key = '') {
|
|
|
2257
2250
|
|
|
2258
2251
|
// 解析关键字
|
|
2259
2252
|
let parsed = this._parseKey(key);
|
|
2260
|
-
let
|
|
2253
|
+
let space = parsed.space;
|
|
2261
2254
|
let event_key = parsed.key;
|
|
2262
|
-
let full_key = `${
|
|
2255
|
+
let full_key = `${space}:${event_key}`;
|
|
2263
2256
|
|
|
2264
2257
|
if (key) {
|
|
2265
2258
|
return this._pauseEvent(full_key);
|
|
2266
2259
|
} else {
|
|
2267
|
-
return this._pauseNamespace(
|
|
2260
|
+
return this._pauseNamespace(space);
|
|
2268
2261
|
}
|
|
2269
2262
|
};
|
|
2270
2263
|
|
|
@@ -2288,19 +2281,19 @@ Eventer.prototype._pauseEvent = function (full_key) {
|
|
|
2288
2281
|
return true;
|
|
2289
2282
|
};
|
|
2290
2283
|
|
|
2291
|
-
Eventer.prototype._pauseNamespace = function (
|
|
2292
|
-
if (this.
|
|
2293
|
-
this.log('warn', `命名空间 '${
|
|
2284
|
+
Eventer.prototype._pauseNamespace = function (space) {
|
|
2285
|
+
if (this._paused_space.has(space)) {
|
|
2286
|
+
this.log('warn', `命名空间 '${space}' 已处于暂停状态`);
|
|
2294
2287
|
return false;
|
|
2295
2288
|
}
|
|
2296
|
-
this.
|
|
2297
|
-
this.log('info', `命名空间 '${
|
|
2289
|
+
this._paused_space.add(space);
|
|
2290
|
+
this.log('info', `命名空间 '${space}' 已暂停`);
|
|
2298
2291
|
return true;
|
|
2299
2292
|
};
|
|
2300
2293
|
|
|
2301
2294
|
/**
|
|
2302
2295
|
* 恢复事件执行
|
|
2303
|
-
* @param {string} key 事件关键字或命名空间,支持
|
|
2296
|
+
* @param {string} key 事件关键字或命名空间,支持space:key格式,为空表示恢复全局暂停
|
|
2304
2297
|
* @returns {boolean} 是否成功恢复
|
|
2305
2298
|
*/
|
|
2306
2299
|
Eventer.prototype.resume = function (key = '') {
|
|
@@ -2315,14 +2308,14 @@ Eventer.prototype.resume = function (key = '') {
|
|
|
2315
2308
|
|
|
2316
2309
|
// 解析关键字
|
|
2317
2310
|
let parsed = this._parseKey(key);
|
|
2318
|
-
let
|
|
2311
|
+
let space = parsed.space;
|
|
2319
2312
|
let event_key = parsed.key;
|
|
2320
|
-
let full_key = `${
|
|
2313
|
+
let full_key = `${space}:${event_key}`;
|
|
2321
2314
|
|
|
2322
2315
|
if (key) {
|
|
2323
2316
|
return this._resumeEvent(full_key);
|
|
2324
2317
|
} else {
|
|
2325
|
-
return this._resumeNamespace(
|
|
2318
|
+
return this._resumeNamespace(space);
|
|
2326
2319
|
}
|
|
2327
2320
|
};
|
|
2328
2321
|
|
|
@@ -2346,19 +2339,19 @@ Eventer.prototype._resumeEvent = function (full_key) {
|
|
|
2346
2339
|
return true;
|
|
2347
2340
|
};
|
|
2348
2341
|
|
|
2349
|
-
Eventer.prototype._resumeNamespace = function (
|
|
2350
|
-
if (!this.
|
|
2351
|
-
this.log('warn', `命名空间 '${
|
|
2342
|
+
Eventer.prototype._resumeNamespace = function (space) {
|
|
2343
|
+
if (!this._paused_space.has(space)) {
|
|
2344
|
+
this.log('warn', `命名空间 '${space}' 未处于暂停状态`);
|
|
2352
2345
|
return false;
|
|
2353
2346
|
}
|
|
2354
|
-
this.
|
|
2355
|
-
this.log('info', `命名空间 '${
|
|
2347
|
+
this._paused_space.delete(space);
|
|
2348
|
+
this.log('info', `命名空间 '${space}' 已恢复`);
|
|
2356
2349
|
return true;
|
|
2357
2350
|
};
|
|
2358
2351
|
|
|
2359
2352
|
/**
|
|
2360
2353
|
* 检查事件是否被暂停
|
|
2361
|
-
* @param {string} key 事件关键字,支持
|
|
2354
|
+
* @param {string} key 事件关键字,支持space:key格式
|
|
2362
2355
|
* @returns {boolean} 是否被暂停
|
|
2363
2356
|
*/
|
|
2364
2357
|
Eventer.prototype.isPaused = function (key) {
|
|
@@ -2373,12 +2366,12 @@ Eventer.prototype.isPaused = function (key) {
|
|
|
2373
2366
|
|
|
2374
2367
|
// 解析关键字
|
|
2375
2368
|
let parsed = this._parseKey(key);
|
|
2376
|
-
let
|
|
2369
|
+
let space = parsed.space;
|
|
2377
2370
|
let event_key = parsed.key;
|
|
2378
|
-
let full_key = `${
|
|
2371
|
+
let full_key = `${space}:${event_key}`;
|
|
2379
2372
|
|
|
2380
2373
|
// 命名空间暂停检查
|
|
2381
|
-
if (this.
|
|
2374
|
+
if (this._paused_space.has(space)) {
|
|
2382
2375
|
return true;
|
|
2383
2376
|
}
|
|
2384
2377
|
|
|
@@ -2398,7 +2391,7 @@ Eventer.prototype.getPausedStatus = function () {
|
|
|
2398
2391
|
return {
|
|
2399
2392
|
global_paused: this._paused_global,
|
|
2400
2393
|
paused_events: Array.from(this._paused),
|
|
2401
|
-
|
|
2394
|
+
paused_spaces: Array.from(this._paused_space), // eslint-disable-line mm_eslint/variable-name
|
|
2402
2395
|
};
|
|
2403
2396
|
};
|
|
2404
2397
|
|
|
@@ -2424,8 +2417,8 @@ Eventer.prototype.clearPaused = function () {
|
|
|
2424
2417
|
cleared = true;
|
|
2425
2418
|
}
|
|
2426
2419
|
|
|
2427
|
-
if (this.
|
|
2428
|
-
this.
|
|
2420
|
+
if (this._paused_space.size > 0) {
|
|
2421
|
+
this._paused_space.clear();
|
|
2429
2422
|
cleared = true;
|
|
2430
2423
|
}
|
|
2431
2424
|
|
package/lib/number.js
CHANGED
|
@@ -8,6 +8,7 @@ Number.prototype.toFloor = function (len) {
|
|
|
8
8
|
var n = Math.pow(10, len);
|
|
9
9
|
return Math.floor(this * n) / n;
|
|
10
10
|
};
|
|
11
|
+
|
|
11
12
|
/**
|
|
12
13
|
* 进一法
|
|
13
14
|
* @param {number} len 保留长度
|
|
@@ -17,6 +18,7 @@ Number.prototype.toCeil = function (len) {
|
|
|
17
18
|
var n = Math.pow(10, len);
|
|
18
19
|
return Math.ceil(this * n) / n;
|
|
19
20
|
};
|
|
21
|
+
|
|
20
22
|
/**
|
|
21
23
|
* 四舍五入法
|
|
22
24
|
* @param {number} len 保留长度
|
package/lib/object.js
CHANGED
|
@@ -60,6 +60,7 @@ function _convertValueByType(value, target_type) {
|
|
|
60
60
|
return value;
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
|
+
|
|
63
64
|
/**
|
|
64
65
|
* 查询对象属性明细
|
|
65
66
|
* @param {object} obj 目标对象
|
|
@@ -131,6 +132,7 @@ function _getPropertyInfo(obj, prop_path) {
|
|
|
131
132
|
// 使用util.inspect显示详细的属性信息
|
|
132
133
|
return inspect(current, { depth: 1, colors: false });
|
|
133
134
|
}
|
|
135
|
+
|
|
134
136
|
/**
|
|
135
137
|
* 判断对象是否相似
|
|
136
138
|
* @param {object} source 被判断对象
|
|
@@ -160,6 +162,7 @@ function as(source, target, exact, depth = 0) {
|
|
|
160
162
|
}
|
|
161
163
|
return processObject(source, target, exact, depth);
|
|
162
164
|
}
|
|
165
|
+
|
|
163
166
|
/**
|
|
164
167
|
* 处理数组相似性比较
|
|
165
168
|
* @param {Array} source_arr 源数组
|
|
@@ -180,6 +183,7 @@ function processArray(source_arr, target_arr, exact, depth) {
|
|
|
180
183
|
}
|
|
181
184
|
return true;
|
|
182
185
|
}
|
|
186
|
+
|
|
183
187
|
/**
|
|
184
188
|
* 处理对象相似性比较
|
|
185
189
|
* @param {object} source_obj 源对象
|
|
@@ -200,6 +204,7 @@ function processObject(source_obj, target_obj, exact, depth) {
|
|
|
200
204
|
}
|
|
201
205
|
return true;
|
|
202
206
|
}
|
|
207
|
+
|
|
203
208
|
/**
|
|
204
209
|
* 添加对象属性
|
|
205
210
|
* @param {object} target 被添加的对象
|
|
@@ -388,6 +393,7 @@ function toXml(source, format, attributes, cdata = false) {
|
|
|
388
393
|
const builder = new Xml(options);
|
|
389
394
|
return builder.build(source_xml || source);
|
|
390
395
|
};
|
|
396
|
+
|
|
391
397
|
/**
|
|
392
398
|
* 转url字符串
|
|
393
399
|
* @param {object} source 被转换的对象
|
|
@@ -435,6 +441,7 @@ function saveJson(source, target, format = true) {
|
|
|
435
441
|
}
|
|
436
442
|
return json_str;
|
|
437
443
|
};
|
|
444
|
+
|
|
438
445
|
/**
|
|
439
446
|
* 将对象保存为XML文件
|
|
440
447
|
* @param {object} source 源对象
|
|
@@ -1030,6 +1037,7 @@ function objToArr(source, key = 'key') {
|
|
|
1030
1037
|
}
|
|
1031
1038
|
return result;
|
|
1032
1039
|
}
|
|
1040
|
+
|
|
1033
1041
|
/**
|
|
1034
1042
|
* 数值增减操作
|
|
1035
1043
|
* @param {object} source 源对象
|
|
@@ -1057,6 +1065,7 @@ function gain(source, key, value) {
|
|
|
1057
1065
|
}
|
|
1058
1066
|
return source;
|
|
1059
1067
|
}
|
|
1068
|
+
|
|
1060
1069
|
// Object原型方法 - 设置为不可枚举,避免影响for...in循环
|
|
1061
1070
|
Object.defineProperty(Object.prototype, 'as', {
|
|
1062
1071
|
value: function (target, exact, depth = 0) {
|
package/lib/string.js
CHANGED
|
@@ -251,19 +251,16 @@ String.prototype.right = function (delimiter, retain) {
|
|
|
251
251
|
};
|
|
252
252
|
/**
|
|
253
253
|
* 取文本之间
|
|
254
|
-
* @param {string}
|
|
255
|
-
* @param {string}
|
|
256
|
-
* @param left_delimiter
|
|
257
|
-
* @param left_deli
|
|
258
|
-
* @param right_delimiter
|
|
259
|
-
* @param right_deli
|
|
254
|
+
* @param {string} left_str 索引的左边字符
|
|
255
|
+
* @param {string} right_str 索引的右边字符
|
|
260
256
|
* @param {boolean} retain 当索引字符不存在时是否保留右边
|
|
261
257
|
* @returns {string} 截取后的字符串
|
|
262
258
|
*/
|
|
263
|
-
String.prototype.between = function (
|
|
264
|
-
let str = this.right(
|
|
265
|
-
return str.left(
|
|
259
|
+
String.prototype.between = function (left_str, right_str, retain) {
|
|
260
|
+
let str = this.right(left_str, retain);
|
|
261
|
+
return str.left(right_str, retain);
|
|
266
262
|
};
|
|
263
|
+
|
|
267
264
|
/**
|
|
268
265
|
* 替换所有字符串
|
|
269
266
|
* @param {string} search 被替换的字符串
|