mm_expand 2.0.2 → 2.0.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.
package/lib/base.js CHANGED
@@ -20,27 +20,30 @@ class Base extends Event {
20
20
  };
21
21
 
22
22
  /**
23
- * 构造函数
24
- * @param {object} config 配置参数
25
- */
26
- constructor(config) {
23
+ * 构造函数
24
+ * @param {object} config 配置参数
25
+ * @param {object} parent 父模块
26
+ */
27
+ constructor(config, parent = null) {
27
28
  super();
28
29
  /**
29
- * 配置参数
30
- * @type {object}
31
- */
32
- this.config = {...this.constructor.config};
30
+ * 配置参数
31
+ * @type {object}
32
+ */
33
+ this.config = { ...this.constructor.config };
33
34
 
34
35
  /**
35
- * 状态
36
- * @type {string} - 状态, 可选值: created, initing, starting, running, stopped, destroying, destroyed
37
- */
38
- this.status = 'created';
36
+ * 状态
37
+ * @type {string} - 状态, 可选值: creating, created, initing, inited, loading, loaded, starting, running, stopping, stopped, unloading, unloaded, destroying, destroyed
38
+ */
39
+ this.status = 'creating';
39
40
 
40
41
  // 设置配置
41
42
  this.setConfig(config);
42
43
  // 初始化监听
43
44
  this._initListen();
45
+
46
+ this.status = 'created';
44
47
  }
45
48
  }
46
49
 
@@ -48,10 +51,10 @@ class Base extends Event {
48
51
  * 日志输出
49
52
  * @param {string} level 日志级别
50
53
  * @param {string} message 日志消息
51
- * @param {...any} args 日志参数
54
+ * @param {...any} params 日志参数
52
55
  */
53
- Base.prototype.log = function (level, message, ...args) {
54
- this._logger[level](`[${this.constructor.name}] [${this.config.name}] ${message}`, ...args);
56
+ Base.prototype.log = function (level, message, ...params) {
57
+ this._logger[level](`[${this.constructor.name}] [${this.config.name}] ${message}`, ...params);
55
58
  };
56
59
 
57
60
  /**
@@ -67,6 +70,14 @@ Base.prototype._initListen = function () {
67
70
  this.on('init:after', (ctx) => {
68
71
  this.status = 'inited';
69
72
  });
73
+ // 加载
74
+ this.on('load:before', (ctx) => {
75
+ status_last = this.status;
76
+ this.status = 'loading';
77
+ });
78
+ this.on('load:after', (ctx) => {
79
+ this.status = 'loaded';
80
+ });
70
81
  // 启动
71
82
  this.on('start:before', (ctx) => {
72
83
  status_last = this.status;
@@ -89,6 +100,14 @@ Base.prototype._initListen = function () {
89
100
  this.on('stop:error', (ctx) => {
90
101
  this.status = status_last;
91
102
  });
103
+ // 卸载
104
+ this.on('unload:before', (ctx) => {
105
+ status_last = this.status;
106
+ this.status = 'unloading';
107
+ });
108
+ this.on('unload:after', (ctx) => {
109
+ this.status = 'unloaded';
110
+ });
92
111
  // 销毁
93
112
  this.on('destroy:before', (ctx) => {
94
113
  status_last = this.status;
@@ -122,20 +141,20 @@ Base.prototype.setConfig = function (config) {
122
141
 
123
142
  /**
124
143
  * 初始化
125
- * @param {...object} args 参数列表
144
+ * @param {...object} params 参数列表
126
145
  * @returns {object} 返回当前对象
127
146
  */
128
- Base.prototype._initCore = async function (...args) {
147
+ Base.prototype._initCore = async function (...params) {
129
148
  this.status = 'init';
130
149
  return this;
131
150
  };
132
151
 
133
152
  /**
134
153
  * 初始化
135
- * @param {...object} args 参数列表
154
+ * @param {...object} params 参数列表
136
155
  * @returns {object} 返回当前对象
137
156
  */
138
- Base.prototype.init = async function (...args) {
157
+ Base.prototype.init = async function (...params) {
139
158
  return this;
140
159
  };
141
160
 
@@ -184,13 +203,25 @@ Base.prototype.end = async function () {
184
203
  await this.do('destroy');
185
204
  };
186
205
 
206
+
207
+ /**
208
+ * 事件驱动
209
+ * @param {string} method 方法名称
210
+ * @param {...object} params 参数列表
211
+ * @returns {object} 返回执行上下文对象
212
+ */
213
+ Base.prototype.do = async function (method, ...params) {
214
+ let ctx = await this._do(method, ...params);
215
+ return ctx.result;
216
+ };
217
+
187
218
  /**
188
219
  * 事件驱动
189
220
  * @param {string} method 方法名称
190
- * @param {...object} args 参数列表
221
+ * @param {...object} params 参数列表
191
222
  * @returns {object} 返回执行结果
192
223
  */
193
- Base.prototype.do = async function (method, ...args) {
224
+ Base.prototype._do = async function (method, ...params) {
194
225
  // 入参校验
195
226
  if (!method || typeof method !== 'string') {
196
227
  throw new TypeError('方法名必须是非空字符串');
@@ -202,7 +233,7 @@ Base.prototype.do = async function (method, ...args) {
202
233
 
203
234
  let ctx = {
204
235
  method,
205
- params: args,
236
+ params,
206
237
  result: undefined,
207
238
  error: undefined,
208
239
  cancelled: false
@@ -215,26 +246,23 @@ Base.prototype.do = async function (method, ...args) {
215
246
  }
216
247
 
217
248
  await this._doExecuteMethod(method, ctx);
218
-
249
+
219
250
  // 如果执行成功,进行成功处理
220
251
  if (!ctx.error) {
221
252
  await this._doSuccessProcess(method, ctx);
222
253
  }
223
-
224
- return ctx.result;
254
+
255
+ // return ctx.result;
225
256
  } catch (error) {
226
257
  ctx.error = error;
227
- this.log('error', `事件驱动执行异常 [${method}]`, error);
228
-
258
+
229
259
  // 执行错误处理
230
260
  await this._doErrorProcess(method, ctx);
231
-
232
- // 重新抛出错误,让上层能够捕获
233
- throw error;
234
261
  } finally {
235
262
  // 无论成功失败,都要执行后处理
236
263
  await this._doAfterProcess(method, ctx);
237
264
  }
265
+ return ctx;
238
266
  };
239
267
 
240
268
  /**
@@ -343,10 +371,10 @@ Base.prototype._doErrorProcess = async function (method, ctx) {
343
371
  /**
344
372
  * 快速执行方法
345
373
  * @param {string} method 方法名称
346
- * @param {...object} args 参数列表
374
+ * @param {...object} params 参数列表
347
375
  * @returns {object} 返回执行结果
348
376
  */
349
- Base.prototype.doEasy = async function (method, ...args) {
377
+ Base.prototype.doEasy = async function (method, ...params) {
350
378
  // 快速检查方法是否存在
351
379
  if (!this[method]) {
352
380
  throw new Error(`方法${method}不存在`);
@@ -354,7 +382,7 @@ Base.prototype.doEasy = async function (method, ...args) {
354
382
 
355
383
  try {
356
384
  // 直接执行主方法,跳过所有事件处理
357
- return await this[method](...args);
385
+ return await this[method](...params);
358
386
  } catch (error) {
359
387
  // 简单错误处理
360
388
  this.log('error', `快速执行方法${method}出错:`, error);
@@ -365,15 +393,15 @@ Base.prototype.doEasy = async function (method, ...args) {
365
393
  /**
366
394
  * 智能执行方法(自动选择do或doEasy)
367
395
  * @param {string} method 方法名称
368
- * @param {...object} args 参数列表
396
+ * @param {...object} params 参数列表
369
397
  * @param {object} options 执行选项
370
398
  * @returns {object} 返回执行结果
371
399
  */
372
- Base.prototype.doSmart = async function (method, ...args) {
400
+ Base.prototype.doSmart = async function (method, ...params) {
373
401
  // 提取选项参数
374
402
  let options = {};
375
- if (args.length > 0 && typeof args[args.length - 1] === 'object') {
376
- options = args.pop();
403
+ if (params.length > 0 && typeof params[params.length - 1] === 'object') {
404
+ options = params.pop();
377
405
  }
378
406
 
379
407
  // 检查是否有监听器
@@ -382,10 +410,10 @@ Base.prototype.doSmart = async function (method, ...args) {
382
410
  // 根据选项和监听器情况选择执行方式
383
411
  if (options.fast || !has_listeners) {
384
412
  // 快速执行:无监听器或明确要求快速
385
- return await this.doEasy(method, ...args);
413
+ return await this.doEasy(method, ...params);
386
414
  } else {
387
415
  // 完整事件驱动执行
388
- return await this.do(method, ...args);
416
+ return await this.do(method, ...params);
389
417
  }
390
418
  };
391
419
 
@@ -397,45 +425,50 @@ Base.prototype.doSmart = async function (method, ...args) {
397
425
  */
398
426
  Base.prototype._hasEventListeners = function (method) {
399
427
  return this.listenerCount(method + ':before') > 0 ||
400
- this.listenerCount(method + ':check') > 0 ||
401
- this.listenerCount(method + ':render') > 0 ||
402
- this.listenerCount(method + ':after') > 0;
428
+ this.listenerCount(method + ':check') > 0 ||
429
+ this.listenerCount(method + ':render') > 0 ||
430
+ this.listenerCount(method + ':after') > 0;
403
431
  };
404
432
 
405
433
  /**
406
434
  * 主要执行方法
407
- * @param {...any} args 参数列表
435
+ * @param {...any} params 参数列表
408
436
  */
409
- Base.prototype.main = async function (...args) {
410
-
437
+ Base.prototype.main = async function (...params) {
438
+ console.log('main', ...params);
439
+ return null;
411
440
  };
412
441
 
413
442
  /**
414
- * 执行
415
- * @param {...object} args 参数列表
443
+ * 执行主方法
444
+ * @param {...object} params 参数列表
416
445
  * @returns {object} 返回执行结果
417
446
  */
418
- Base.prototype.run = async function (...args) {
419
- return await this.do('main', ...args);
447
+ Base.prototype.run = async function (...params) {
448
+ return await this.do('main', ...params);
420
449
  };
421
450
 
422
451
  /**
423
452
  * 执行方法
424
453
  * @param {string} method 方法名称
425
- * @param {...any} args 参数列表
454
+ * @param {...any} params 参数列表
426
455
  * @returns
427
456
  */
428
- Base.prototype.exec = async function (method, ...args) {
429
- return await this.do(method, ...args);
457
+ Base.prototype.exec = async function (method, ...params) {
458
+ const result = await this._do(method, ...params);
459
+ if (result.error) {
460
+ throw result.error;
461
+ }
462
+ return result;
430
463
  };
431
464
 
432
465
  /**
433
466
  * 执行指令(主要)
434
467
  * @param {string} command 指令
435
- * @param {...object} args 参数列表
468
+ * @param {...object} params 参数列表
436
469
  * @returns {object} 返回执行结果
437
470
  */
438
- Base.prototype.cmd = async function (command, ...args) {
471
+ Base.prototype.cmd = async function (command, ...params) {
439
472
  // 入参校验
440
473
  if (!command || typeof command !== 'string') {
441
474
  this.log('error', '指令必须是非空字符串');
@@ -445,7 +478,7 @@ Base.prototype.cmd = async function (command, ...args) {
445
478
  // 创建执行上下文
446
479
  const ctx = {
447
480
  command,
448
- args,
481
+ params,
449
482
  cmds: [],
450
483
  obj: this,
451
484
  error: null,
@@ -564,7 +597,7 @@ Base.prototype._cmdHandleFunction = async function (ctx, is_last) {
564
597
 
565
598
  if (is_last) {
566
599
  try {
567
- ctx.result = await ctx.obj(...ctx.args);
600
+ ctx.result = await ctx.obj(...ctx.params);
568
601
  return true;
569
602
  } catch (error) {
570
603
  this.log('error', `执行方法${ctx.command}出错:`, error);
@@ -617,12 +650,12 @@ Base.prototype._cmdHandlePrimitive = function (ctx, is_last) {
617
650
 
618
651
  /**
619
652
  * 帮助(主要)
620
- * @param {...object} args 参数列表
653
+ * @param {...object} params 参数列表
621
654
  * @returns {object} 返回查询结果
622
655
  */
623
- Base.prototype.help = async function (...args) {
656
+ Base.prototype.help = async function (...params) {
624
657
  const ctx = {
625
- args,
658
+ params,
626
659
  param: null,
627
660
  result: '',
628
661
  error: null
@@ -646,8 +679,8 @@ Base.prototype.help = async function (...args) {
646
679
  * @private
647
680
  */
648
681
  Base.prototype._helpProcessArgs = function (ctx) {
649
- if (ctx.args.length > 0) {
650
- ctx.param = ctx.args[0];
682
+ if (ctx.params.length > 0) {
683
+ ctx.param = ctx.params[0];
651
684
 
652
685
  if (ctx.param !== undefined && ctx.param !== null && typeof ctx.param !== 'string') {
653
686
  this.log('warn', 'help参数应为字符串类型,将自动转换');
package/lib/validator.js CHANGED
@@ -389,7 +389,7 @@ Validator.prototype.notNaN = function (val) {
389
389
  * @param {*} val 要验证的值
390
390
  * @returns {string} 通过返回空字符串,没通过返回错误信息
391
391
  */
392
- Validator.prototype.not_empty = function (val) {
392
+ Validator.prototype.notEmpty = function (val) {
393
393
  let bl = val !== undefined && val !== null && val !== '';
394
394
  if (!bl) {
395
395
  return `参数必须不是空值,实际值为:${val}`;
@@ -1337,7 +1337,7 @@ Validator.prototype.checkRequired = function (val, rule) {
1337
1337
  return tip;
1338
1338
  }
1339
1339
  if (rule.not_empty) {
1340
- tip = this.not_empty(val);
1340
+ tip = this.notEmpty(val);
1341
1341
  if (tip) {
1342
1342
  return tip;
1343
1343
  }
@@ -1657,9 +1657,9 @@ Number.prototype.isMax = function (max) {
1657
1657
  * 检查字符串是否为空
1658
1658
  * @returns {boolean} 是否为空
1659
1659
  */
1660
- String.prototype.not_empty = function () {
1660
+ String.prototype.notEmpty = function () {
1661
1661
  let str = this + '';
1662
- let tip = validator.not_empty(str);
1662
+ let tip = validator.notEmpty(str);
1663
1663
  if (tip) {
1664
1664
  throw new TypeError(tip);
1665
1665
  }
@@ -1776,7 +1776,7 @@ String.prototype.isName = function () {
1776
1776
  * 检查数组是否为空
1777
1777
  * @returns {Array} 原始数组对象
1778
1778
  */
1779
- Array.prototype.not_empty = function () {
1779
+ Array.prototype.notEmpty = function () {
1780
1780
  if (this.length === 0) {
1781
1781
  throw new TypeError('数组不能为空');
1782
1782
  }
@@ -1810,7 +1810,7 @@ Array.prototype.contains = function (element) {
1810
1810
  };
1811
1811
 
1812
1812
  // Object原型方法 - 设置为不可枚举,避免影响for...in循环
1813
- Object.defineProperty(Object.prototype, 'not_empty', {
1813
+ Object.defineProperty(Object.prototype, 'notEmpty', {
1814
1814
  value: function () {
1815
1815
  if (Object.keys(this).length === 0) {
1816
1816
  throw new TypeError('对象不能为空');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_expand",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
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": {
@@ -33,7 +33,7 @@
33
33
  "homepage": "https://gitee.com/qiuwenwu91/mm_expand#readme",
34
34
  "dependencies": {
35
35
  "colors": "^1.4.0",
36
- "fast-xml-parser": "^5.5.9",
36
+ "fast-xml-parser": "^5.5.10",
37
37
  "json5": "^2.2.3",
38
38
  "ncp": "^2.0.0",
39
39
  "pinyinlite": "^1.2.1",