mm_expand 2.3.2 → 2.3.4

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 (2) hide show
  1. package/lib/eventer.js +54 -7
  2. package/package.json +2 -2
package/lib/eventer.js CHANGED
@@ -1203,6 +1203,53 @@ Eventer.prototype.emitRace = async function (key, ...args) {
1203
1203
  }
1204
1204
  };
1205
1205
 
1206
+ /**
1207
+ * 异步触发事件,返回第一个有返回值的结果(不阻塞其他监听器)
1208
+ * @param {string} key 事件关键字,支持namespace:key格式
1209
+ * @param {...any} args 事件参数
1210
+ * @returns {Promise} - 第一个有返回值(非 undefined)的监听器结果
1211
+ * @description 所有监听器并行执行,返回第一个有返回值(非 undefined)的结果,
1212
+ * 返回 undefined 的监听器被跳过,其他监听器继续在后台执行不受影响
1213
+ */
1214
+ Eventer.prototype.emitFirst = async function (key, ...args) {
1215
+ try {
1216
+ const parsed = this._parseKey(key);
1217
+ const namespace = parsed.namespace;
1218
+ const event_key = parsed.key;
1219
+
1220
+ const list = this._dict[namespace] && this._dict[namespace][event_key];
1221
+ if (!list || list.length === 0) {
1222
+ return undefined;
1223
+ }
1224
+
1225
+ return await new Promise(resolve => {
1226
+ let settled = false;
1227
+ let pending = list.length;
1228
+
1229
+ for (const handler of list) {
1230
+ Promise.resolve(handler.func(...args)).then(result => {
1231
+ if (!settled && result !== undefined) {
1232
+ settled = true;
1233
+ resolve(result);
1234
+ }
1235
+ pending--;
1236
+ if (pending === 0 && !settled) {
1237
+ resolve(undefined);
1238
+ }
1239
+ }).catch(() => {
1240
+ pending--;
1241
+ if (pending === 0 && !settled) {
1242
+ resolve(undefined);
1243
+ }
1244
+ });
1245
+ }
1246
+ });
1247
+ } catch (error) {
1248
+ this.log('error', `首个结果事件触发失败 [${key}]`, error);
1249
+ return undefined;
1250
+ }
1251
+ };
1252
+
1206
1253
  /**
1207
1254
  * 异步触发事件,瀑布流执行
1208
1255
  * @param {string} key 事件关键字,支持namespace:key格式
@@ -1315,12 +1362,6 @@ Eventer.prototype.emitAllNamespaces = async function (key, ...params) {
1315
1362
  return results;
1316
1363
  };
1317
1364
 
1318
- /**
1319
- * 删除事件
1320
- * @param {string} key 事件关键字,支持namespace:key格式
1321
- * @param {number | string} index_or_name 事件集索引或名称
1322
- * @returns {boolean} 删除成功返回true,否则返回false
1323
- */
1324
1365
  /**
1325
1366
  * 删除单个事件监听器
1326
1367
  * @private
@@ -1352,7 +1393,7 @@ Eventer.prototype._removeSingleEvent = function (events, index_or_name) {
1352
1393
  * @private
1353
1394
  * @param {string} namespace 命名空间
1354
1395
  * @param {string} key 事件关键字
1355
- * @param event_key
1396
+ * @param {string} event_key 事件键
1356
1397
  * @param {string} full_key 完整事件关键字
1357
1398
  * @returns {boolean} 删除成功返回true
1358
1399
  */
@@ -1413,6 +1454,12 @@ Eventer.prototype._autoCleanEvents = function (namespace, key, events, index_or_
1413
1454
  }
1414
1455
  };
1415
1456
 
1457
+ /**
1458
+ * 删除事件
1459
+ * @param {string} key 事件关键字,支持namespace:key格式
1460
+ * @param {number | string} index_or_name 事件集索引或名称
1461
+ * @returns {boolean} 删除成功返回true,否则返回false
1462
+ */
1416
1463
  Eventer.prototype.off = function (key, index_or_name) {
1417
1464
  // 入参校验
1418
1465
  if (!key || typeof key !== 'string') {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mm_expand",
3
- "version": "2.3.2",
4
- "description": "Super Meimei Prototype Function Extension Module - Enhanced operations for string, array, object, date manipulation with error prevention and simplified business logic.",
3
+ "version": "2.3.4",
4
+ "description": "超级美眉原型函数扩展模块 - 增强的字符串、数组、对象、日期操作,具备错误预防功能并简化业务逻辑",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "test": "node tests/index.js",