mm_machine 3.2.2 → 3.2.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 (3) hide show
  1. package/manager.js +26 -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
 
@@ -237,11 +237,9 @@ Manager.prototype.delMod = function (name) {
237
237
  * 设置模块
238
238
  * @param {string} name 模块名
239
239
  * @param {object} mod 模块对象
240
- * @returns {object|null} 设置的模块对象
241
240
  */
242
241
  Manager.prototype.setMod = function (name, mod) {
243
242
  this.mods[name] = mod;
244
- return this.getMod(name);
245
243
  };
246
244
 
247
245
  /**
@@ -259,14 +257,11 @@ Manager.prototype.getMod = function (name) {
259
257
  var entries = this._getInfos();
260
258
  for (var i = 0; i < entries.length; i++) {
261
259
  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();
260
+ mod = this.newMod(entries[i].config_file);
261
+ // 保存到内存集合,避免后续访问时再次创建
262
+ if (mod) {
263
+ this.setMod(mod.config.name, mod);
268
264
  }
269
- this.mods[name] = mod;
270
265
  return mod;
271
266
  }
272
267
  }
@@ -306,9 +301,6 @@ Manager.prototype.newMod = function (config_file, config, script) {
306
301
  if (config && config.name) {
307
302
  // config 已从 registry 缓存获取,跳过磁盘读
308
303
  mod.config_file = config_file;
309
- if (mod._preset) {
310
- mod._preset();
311
- }
312
304
  } else {
313
305
  mod.loadConfig(config_file);
314
306
  }
@@ -359,10 +351,10 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
359
351
  main: mod.config.main || ''
360
352
  };
361
353
  // 获取触发键
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];
354
+ let keys = this.config.trigger_key;
355
+ if (keys && keys.length > 0) {
356
+ for (let key in keys) {
357
+ entry[key] = mod.config[key];
366
358
  }
367
359
  }
368
360
  // 启用注册表时,自动写入 $.registry 供后续实例复用
@@ -373,9 +365,7 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
373
365
  // 用管理器自身的 name 作为 parent 标签,实现多 Manager 隔离
374
366
  $.registry.setMod(this.getParentKey(), mod.config.name, entry);
375
367
  } catch (err) {
376
- throw new Error(
377
- '注册模块失败: ' + config_file + ' - ' + err.message
378
- );
368
+ throw new Error('注册模块失败: ' + config_file + ' - ' + err.message);
379
369
  }
380
370
  }
381
371
  } else {
@@ -535,21 +525,13 @@ Manager.prototype._loadFromRegistry = function () {
535
525
  let lt = this._getInfos();
536
526
  if (!lt.length) return false;
537
527
 
538
- var mode = this.config.mode;
539
- var mods = this.mods;
540
-
541
528
  for (var i = 0; i < lt.length; i++) {
542
529
  var entry = lt[i];
543
-
544
530
  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();
531
+ let mod = this.newMod(entry.config_file);
532
+ if (mod) {
533
+ this.setMod(mod.config.name, mod);
551
534
  }
552
- mods[entry.name] = mod;
553
535
  }
554
536
  }
555
537
 
@@ -588,17 +570,6 @@ Manager.prototype.main = async function (name, method, ...params) {
588
570
  var result = null;
589
571
  if (name) {
590
572
  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
573
  if (mod) {
603
574
  result = await this._runMod(mod, method, ...params);
604
575
  }
@@ -847,11 +818,7 @@ Manager.prototype.runWait = async function (method, ...params) {
847
818
  result = ret;
848
819
  }
849
820
  } catch (error) {
850
- this.log(
851
- 'error',
852
- '\u6A21\u5757' + entries[i].name + '\u6267\u884C' + method + '\u65B9\u6CD5\u5931\u8D25',
853
- error
854
- );
821
+ this._logModError(entries[i].name, method, error);
855
822
  }
856
823
  }
857
824
 
@@ -879,11 +846,7 @@ Manager.prototype.runAsync = async function (method, ...params) {
879
846
  if (!mod) continue;
880
847
 
881
848
  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
- );
849
+ this._logModError(entries[i].name, method, error);
887
850
  });
888
851
  }
889
852
 
@@ -972,11 +935,7 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
972
935
  result = ret;
973
936
  }
974
937
  } catch (error) {
975
- this.log(
976
- 'error',
977
- '\u6A21\u5757' + entries[i].name + '\u6267\u884C' + method + '\u65B9\u6CD5\u5931\u8D25',
978
- error
979
- );
938
+ this._logModError(entries[i].name, method, error);
980
939
  }
981
940
  }
982
941
 
@@ -1068,6 +1027,16 @@ Manager.prototype.remove = async function (name) {
1068
1027
  return mod;
1069
1028
  };
1070
1029
 
1030
+ /**
1031
+ * 记录模块方法执行失败日志
1032
+ * @param {string} name 模块名称
1033
+ * @param {string} method 方法名称
1034
+ * @param {Error} error 错误对象
1035
+ */
1036
+ Manager.prototype._logModError = function (name, method, error) {
1037
+ this.log('error', '模块' + name + '执行' + method + '方法失败', error);
1038
+ };
1039
+
1071
1040
  module.exports = {
1072
1041
  Manager
1073
1042
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.2.2",
3
+ "version": "3.2.4",
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,