mm_machine 3.2.2 → 3.2.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/manager.js +22 -57
  2. package/package.json +1 -1
  3. package/registry.js +2 -2
package/manager.js CHANGED
@@ -83,7 +83,7 @@ class Manager extends Mod {
83
83
  * @type {string}
84
84
  */
85
85
  scope: $.val && $.val.scope ? $.val.scope + '' : 'server',
86
- // 触发字段,添加到注册表时注入此字段,可以避免先加载模块,满足触发条件后再加载
86
+ // 事件触发器
87
87
  trigger_key: []
88
88
  };
89
89
 
@@ -259,14 +259,7 @@ Manager.prototype.getMod = function (name) {
259
259
  var entries = this._getInfos();
260
260
  for (var i = 0; i < entries.length; i++) {
261
261
  if (entries[i].name === name) {
262
- var entry = entries[i];
263
- mod = new this.Drive(entry, this);
264
- mod.mode = this.config.mode;
265
- mod.config_file = entry.config_file;
266
- if (mod._preset) {
267
- mod._preset();
268
- }
269
- this.mods[name] = mod;
262
+ mod = this.newMod(entries[i].config_file);
270
263
  return mod;
271
264
  }
272
265
  }
@@ -306,9 +299,6 @@ Manager.prototype.newMod = function (config_file, config, script) {
306
299
  if (config && config.name) {
307
300
  // config 已从 registry 缓存获取,跳过磁盘读
308
301
  mod.config_file = config_file;
309
- if (mod._preset) {
310
- mod._preset();
311
- }
312
302
  } else {
313
303
  mod.loadConfig(config_file);
314
304
  }
@@ -359,10 +349,10 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
359
349
  main: mod.config.main || ''
360
350
  };
361
351
  // 获取触发键
362
- let trigger = this.config.trigger;
363
- if (trigger && trigger.length > 0) {
364
- for (let t of trigger) {
365
- entry.trigger[t] = mod.config[t];
352
+ let keys = this.config.trigger_key;
353
+ if (keys && keys.length > 0) {
354
+ for (let key in keys) {
355
+ entry[key] = mod.config[key];
366
356
  }
367
357
  }
368
358
  // 启用注册表时,自动写入 $.registry 供后续实例复用
@@ -373,9 +363,7 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
373
363
  // 用管理器自身的 name 作为 parent 标签,实现多 Manager 隔离
374
364
  $.registry.setMod(this.getParentKey(), mod.config.name, entry);
375
365
  } catch (err) {
376
- throw new Error(
377
- '注册模块失败: ' + config_file + ' - ' + err.message
378
- );
366
+ throw new Error('注册模块失败: ' + config_file + ' - ' + err.message);
379
367
  }
380
368
  }
381
369
  } else {
@@ -535,21 +523,11 @@ Manager.prototype._loadFromRegistry = function () {
535
523
  let lt = this._getInfos();
536
524
  if (!lt.length) return false;
537
525
 
538
- var mode = this.config.mode;
539
- var mods = this.mods;
540
-
541
526
  for (var i = 0; i < lt.length; i++) {
542
527
  var entry = lt[i];
543
-
544
528
  if (!this.config.lazy_load) {
545
- // 非懒加载模式:立即实例化
546
- var mod = new this.Drive(entry, this);
547
- mod.mode = mode;
548
- mod.config_file = entry.config_file;
549
- if (mod._preset) {
550
- mod._preset();
551
- }
552
- mods[entry.name] = mod;
529
+ let mod = this.newMod(entry.config_file);
530
+ this.mods[entry.name] = mod;
553
531
  }
554
532
  }
555
533
 
@@ -588,17 +566,6 @@ Manager.prototype.main = async function (name, method, ...params) {
588
566
  var result = null;
589
567
  if (name) {
590
568
  var mod = this.mods[name];
591
- // 注册表模式下一次遍历查 entry,避免 getMod 重复 _getInfos
592
- if (!mod && this.config.use_registry) {
593
- var entries = this._getInfos();
594
- var entry = entries.find((o) => { return o.name === name; });
595
- if (entry && entry.state === 1) {
596
- var mod = new this.Drive(entry, this);
597
- mod.config_file = entry.config_file;
598
- if (mod._preset) mod._preset();
599
- this.mods[name] = mod;
600
- }
601
- }
602
569
  if (mod) {
603
570
  result = await this._runMod(mod, method, ...params);
604
571
  }
@@ -847,11 +814,7 @@ Manager.prototype.runWait = async function (method, ...params) {
847
814
  result = ret;
848
815
  }
849
816
  } catch (error) {
850
- this.log(
851
- 'error',
852
- '\u6A21\u5757' + entries[i].name + '\u6267\u884C' + method + '\u65B9\u6CD5\u5931\u8D25',
853
- error
854
- );
817
+ this._logModError(entries[i].name, method, error);
855
818
  }
856
819
  }
857
820
 
@@ -879,11 +842,7 @@ Manager.prototype.runAsync = async function (method, ...params) {
879
842
  if (!mod) continue;
880
843
 
881
844
  mod.do(method, ...params).catch((error) => {
882
- this.log(
883
- 'error',
884
- '\u6A21\u5757' + entries[i].name + '\u6267\u884C' + method + '\u65B9\u6CD5\u5931\u8D25',
885
- error
886
- );
845
+ this._logModError(entries[i].name, method, error);
887
846
  });
888
847
  }
889
848
 
@@ -972,11 +931,7 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
972
931
  result = ret;
973
932
  }
974
933
  } catch (error) {
975
- this.log(
976
- 'error',
977
- '\u6A21\u5757' + entries[i].name + '\u6267\u884C' + method + '\u65B9\u6CD5\u5931\u8D25',
978
- error
979
- );
934
+ this._logModError(entries[i].name, method, error);
980
935
  }
981
936
  }
982
937
 
@@ -1068,6 +1023,16 @@ Manager.prototype.remove = async function (name) {
1068
1023
  return mod;
1069
1024
  };
1070
1025
 
1026
+ /**
1027
+ * 记录模块方法执行失败日志
1028
+ * @param {string} name 模块名称
1029
+ * @param {string} method 方法名称
1030
+ * @param {Error} error 错误对象
1031
+ */
1032
+ Manager.prototype._logModError = function (name, method, error) {
1033
+ this.log('error', '模块' + name + '执行' + method + '方法失败', error);
1034
+ };
1035
+
1071
1036
  module.exports = {
1072
1037
  Manager
1073
1038
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.2.2",
3
+ "version": "3.2.3",
4
4
  "description": "A flexible Node.js plugin mechanism system for dynamic loading, management and execution of modules. Supports hot reload, lifecycle management, and modern JavaScript features.",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/registry.js CHANGED
@@ -68,7 +68,7 @@ Registry.prototype.setMod = function (parent, name, config) {
68
68
  throw new TypeError('模块配置项必须为对象');
69
69
  }
70
70
  this.checkConfig(config);
71
- let cg = this._fullValue(config);
71
+ let cg = this.fullConfig(config);
72
72
 
73
73
  // 按 parent 分组存储到 mod_dict
74
74
  if (!this.mod_dict[parent]) {
@@ -174,7 +174,7 @@ Registry.prototype.checkConfig = function (config) {
174
174
  * @param {object} config 模块配置项
175
175
  * @returns {object} 模块配置项
176
176
  */
177
- Registry.prototype._fullValue = function (config) {
177
+ Registry.prototype.fullConfig = function (config) {
178
178
  let cg = { ...this.newModel(), ...config };
179
179
  let {
180
180
  description,