mm_machine 2.1.2 → 2.1.3

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 (3) hide show
  1. package/index.js +50 -35
  2. package/item.js +23 -11
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -45,41 +45,24 @@ class Index {
45
45
  * 5.热更新+重载模式,改变配置文件重新加载配置和脚本,执行完后重新加载脚本
46
46
  */
47
47
  this.mode = 1;
48
- }
49
-
50
- /**
51
- * 私有方法:执行模块方法
52
- * @param {object} module 模块对象
53
- * @param {string} method 方法名称
54
- * @param {Array} params 参数数组
55
- * @returns {Promise<any>} 执行结果
56
- * @private
57
- */
58
- async _execute(module, method, params = []) {
59
- if (!module || !method) return null;
60
-
61
- try {
62
- // 确保模块已加载
63
- if (!module.complete) {
64
- await module.exec('load');
65
- }
66
-
67
- // 执行方法
68
- const ret = await module.exec(method, ...params);
69
-
70
- // 根据模式决定是否重载
71
- if (this.mode >= 4) {
72
- module.exec('reload');
73
- }
74
48
 
75
- return ret;
76
- } catch (err) {
77
- $.log.error(`[${this.type}] 执行模块方法失败: `, err);
78
- return null;
79
- }
49
+ /**
50
+ * 日志输出
51
+ */
52
+ this._logger = $.log || console;
80
53
  }
81
54
  }
82
55
 
56
+ /**
57
+ * 日志输出
58
+ * @param {string} level - 日志级别(debug/info/warn/error)
59
+ * @param {string} message - 日志消息
60
+ * @param {...*} args - 可选参数
61
+ */
62
+ Index.prototype.log = function (level, message, ...args) {
63
+ this._logger[level](`[${this.constructor.name}.${this.type}] ${message}`, ...args);
64
+ };
65
+
83
66
  /**
84
67
  * 清除接口缓存
85
68
  */
@@ -92,6 +75,38 @@ Index.prototype.clear = function () {
92
75
  */
93
76
  Index.prototype.Drive = Item;
94
77
 
78
+ /**
79
+ * 私有方法:执行模块方法
80
+ * @param {object} module 模块对象
81
+ * @param {string} method 方法名称
82
+ * @param {Array} params 参数数组
83
+ * @returns {Promise<any>} 执行结果
84
+ * @private
85
+ */
86
+ Index.prototype._exec = async function (module, method, params = []) {
87
+ if (!module || !method) return null;
88
+
89
+ try {
90
+ // 确保模块已加载
91
+ if (!module.complete) {
92
+ await module.exec('load');
93
+ }
94
+
95
+ // 执行方法
96
+ const ret = await module.exec(method, ...params);
97
+
98
+ // 根据模式决定是否重载
99
+ if (this.mode >= 4) {
100
+ module.exec('reload');
101
+ }
102
+
103
+ return ret;
104
+ } catch (err) {
105
+ $.log.error(`[${this.type}] 执行模块方法失败: `, err);
106
+ return null;
107
+ }
108
+ }
109
+
95
110
  /**
96
111
  * 加载项
97
112
  * @param {string} dir 文件路径
@@ -566,7 +581,7 @@ Index.prototype.run = async function (name, method, ...params) {
566
581
  if (name) {
567
582
  const module = await this.get(name);
568
583
  if (module && module.config?.state === 1) {
569
- return await this._execute(module, method, params);
584
+ return await this._exec(module, method, params);
570
585
  }
571
586
  return null;
572
587
  } else if (name === null) {
@@ -574,7 +589,7 @@ Index.prototype.run = async function (name, method, ...params) {
574
589
  // 使用函数式编程处理列表,使用some模拟break逻辑
575
590
  await this.list.some(async (module) => {
576
591
  if (module.config?.state === 1) {
577
- result = await this._execute(module, method, params);
592
+ result = await this._exec(module, method, params);
578
593
  // 如果结果不为空且有结束标志,则停止迭代
579
594
  return result && module.config?.end;
580
595
  }
@@ -596,7 +611,7 @@ Index.prototype.exec = async function (name, method, ...params) {
596
611
  if (name) {
597
612
  const module = await this.get(name);
598
613
  if (module) {
599
- return await this._execute(module, method, params);
614
+ return await this._exec(module, method, params);
600
615
  }
601
616
  return null;
602
617
  } else if (name === null) {
@@ -608,7 +623,7 @@ Index.prototype.exec = async function (name, method, ...params) {
608
623
  if (result && module.config?.end) {
609
624
  return;
610
625
  }
611
- const current_result = await this._execute(module, method, params);
626
+ const current_result = await this._exec(module, method, params);
612
627
  if (current_result) {
613
628
  result = current_result;
614
629
  }
package/item.js CHANGED
@@ -80,9 +80,21 @@ class Item {
80
80
 
81
81
  // 加载完成
82
82
  this.complete = false;
83
+
84
+ this._logger = $.log || console;
83
85
  }
84
86
  }
85
87
 
88
+ /**
89
+ * 日志输出
90
+ * @param {string} level - 日志级别(debug/info/warn/error)
91
+ * @param {string} message - 日志消息
92
+ * @param {...*} args - 可选参数
93
+ */
94
+ Item.prototype.log = function (level, message, ...args) {
95
+ this._logger[level](`[${this.config.name}] ${message}`, ...args);
96
+ };
97
+
86
98
  /**
87
99
  * 设置配置
88
100
  * @param {object} config 配置对象
@@ -139,7 +151,7 @@ Item.prototype._remove = function (module) {
139
151
  }
140
152
  }
141
153
  } catch (err) {
142
- $.log.error(`[${this.config.name}] 移除模块失败: `, err);
154
+ this.log('error', `移除模块失败: `, err);
143
155
  }
144
156
  };
145
157
 
@@ -207,7 +219,7 @@ Item.prototype.loadScript = function (file, name = '') {
207
219
  this._setMainMethod(cs, target_name);
208
220
  return cs;
209
221
  } catch (err) {
210
- $.log.error(`[${this.config.name}] 加载脚本失败: `, err);
222
+ this.log('error', `加载脚本失败: `, err);
211
223
  return null;
212
224
  }
213
225
  };
@@ -318,7 +330,7 @@ Item.prototype.loadFile = function (file, name) {
318
330
  this.filename = full_path;
319
331
  return final_config;
320
332
  } catch (err) {
321
- $.log.error(`[${this.config.name}] 加载配置文件失败: `, err);
333
+ this.log('error', `加载配置文件失败: `, err);
322
334
  return null;
323
335
  }
324
336
  };
@@ -351,7 +363,7 @@ Item.prototype._handleHotReload = function (loaded_config, change_type) {
351
363
  this._reloadIfNeeded();
352
364
  }
353
365
  } catch (err) {
354
- $.log.error(`[${this.config.name}] 配置热更新失败: `, err);
366
+ this.log('error', `配置热更新失败: `, err);
355
367
  }
356
368
  };
357
369
 
@@ -403,7 +415,7 @@ Item.prototype.removeFile = function () {
403
415
 
404
416
  error_message = this._removeConfigFile(file, name);
405
417
  } catch (err) {
406
- $.log.error(`[${this.config.name}] 删除文件失败: `, err);
418
+ this.log('error', `删除文件失败: `, err);
407
419
  error_message = `删除失败: ${err.message}`;
408
420
  }
409
421
  return error_message;
@@ -475,7 +487,7 @@ Item.prototype.loadConfig = async function (config_data, name) {
475
487
  await this.setConfig(config);
476
488
  this.setConfigAfter();
477
489
  } catch (err) {
478
- $.log.error(`[${this.config.name}] 载入配置失败: `, err);
490
+ this.log('error', `载入配置失败: `, err);
479
491
  }
480
492
  };
481
493
 
@@ -489,7 +501,7 @@ Item.prototype.loadBefore = async function () {
489
501
  this.complete = true;
490
502
  }
491
503
  } catch (err) {
492
- $.log.error(`[${this.config.name}] 加载前处理失败: `, err);
504
+ this.log('error', `加载前处理失败: `, err);
493
505
  this.complete = false;
494
506
  }
495
507
  };
@@ -539,7 +551,7 @@ Item.prototype.save = function () {
539
551
  // 单对象格式配置,直接保存
540
552
  full_path.saveText(JSON.stringify(this.config, null, 4));
541
553
  } catch (err) {
542
- $.log.error(`[${this.config.name}] 保存配置失败: `, err);
554
+ this.log('error', `保存配置失败: `, err);
543
555
  }
544
556
  };
545
557
 
@@ -579,7 +591,7 @@ Item.prototype.exec = async function (method, ...params) {
579
591
  result = await this._execMain(method_func, params, result);
580
592
  result = await this._execAfter(method, params, result);
581
593
  } catch (err) {
582
- $.log.error(`[${this.config.name}] 执行方法 ${method} 失败: `, err);
594
+ this.log('error', `执行方法 ${method} 失败: `, err);
583
595
  return null;
584
596
  }
585
597
 
@@ -618,7 +630,7 @@ Item.prototype._execBefore = async function (method, params) {
618
630
  }
619
631
  return result;
620
632
  } catch (err) {
621
- $.log.error(`[${this.config.name}] 执行前置钩子 ${before_method} 失败: `, err);
633
+ this.log('error', `执行前置钩子 ${before_method} 失败: `, err);
622
634
  return undefined;
623
635
  }
624
636
  };
@@ -659,7 +671,7 @@ Item.prototype._execAfter = async function (method, params, main_result) {
659
671
 
660
672
  return after_result !== undefined ? after_result : main_result;
661
673
  } catch (err) {
662
- $.log.error(`[${this.config.name}] 执行后置钩子 ${after_method} 失败: `, err);
674
+ this.log('error', `执行后置钩子 ${after_method} 失败: `, err);
663
675
  return main_result;
664
676
  }
665
677
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "2.1.2",
3
+ "version": "2.1.3",
4
4
  "description": "这是超级美眉框架机制构建辅助模块,用于快速构建一个机制,支持动态加载、热更新、模块管理等功能,并具有增强的错误处理和现代JavaScript特性支持。",
5
5
  "main": "index.js",
6
6
  "scripts": {