mm_machine 3.2.1 → 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 +33 -70
  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,15 +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
- const ModClass = this.config.mod_type === 'script' ? Mod : this.Drive;
264
- mod = new ModClass(entry, this);
265
- mod.mode = this.config.mode;
266
- mod.config_file = entry.config_file;
267
- if (mod._preset) {
268
- mod._preset();
269
- }
270
- this.mods[name] = mod;
262
+ mod = this.newMod(entries[i].config_file);
271
263
  return mod;
272
264
  }
273
265
  }
@@ -307,9 +299,6 @@ Manager.prototype.newMod = function (config_file, config, script) {
307
299
  if (config && config.name) {
308
300
  // config 已从 registry 缓存获取,跳过磁盘读
309
301
  mod.config_file = config_file;
310
- if (mod._preset) {
311
- mod._preset();
312
- }
313
302
  } else {
314
303
  mod.loadConfig(config_file);
315
304
  }
@@ -343,7 +332,11 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
343
332
 
344
333
  var mod = this.newMod(config_file, cg, script);
345
334
  if (!mod) return null;
346
- this.setMod(mod.config.name, mod);
335
+
336
+ // 模块已存在时不替换内存实例,避免破坏已初始化的模块
337
+ if (!this.mods[mod.config.name]) {
338
+ this.setMod(mod.config.name, mod);
339
+ }
347
340
 
348
341
  var entry = {
349
342
  name: mod.config.name,
@@ -356,10 +349,10 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
356
349
  main: mod.config.main || ''
357
350
  };
358
351
  // 获取触发键
359
- let trigger = this.config.trigger;
360
- if (trigger && trigger.length > 0) {
361
- for (let t of trigger) {
362
- 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];
363
356
  }
364
357
  }
365
358
  // 启用注册表时,自动写入 $.registry 供后续实例复用
@@ -370,9 +363,7 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
370
363
  // 用管理器自身的 name 作为 parent 标签,实现多 Manager 隔离
371
364
  $.registry.setMod(this.getParentKey(), mod.config.name, entry);
372
365
  } catch (err) {
373
- throw new Error(
374
- '注册模块失败: ' + config_file + ' - ' + err.message
375
- );
366
+ throw new Error('注册模块失败: ' + config_file + ' - ' + err.message);
376
367
  }
377
368
  }
378
369
  } else {
@@ -532,22 +523,11 @@ Manager.prototype._loadFromRegistry = function () {
532
523
  let lt = this._getInfos();
533
524
  if (!lt.length) return false;
534
525
 
535
- var mod_class = this.config.mod_type === 'script' ? Mod : this.Drive;
536
- var mode = this.config.mode;
537
- var mods = this.mods;
538
-
539
526
  for (var i = 0; i < lt.length; i++) {
540
527
  var entry = lt[i];
541
-
542
528
  if (!this.config.lazy_load) {
543
- // 非懒加载模式:立即实例化
544
- var mod = new ModClass(entry, this);
545
- mod.mode = mode;
546
- mod.config_file = entry.config_file;
547
- if (mod._preset) {
548
- mod._preset();
549
- }
550
- mods[entry.name] = mod;
529
+ let mod = this.newMod(entry.config_file);
530
+ this.mods[entry.name] = mod;
551
531
  }
552
532
  }
553
533
 
@@ -586,19 +566,6 @@ Manager.prototype.main = async function (name, method, ...params) {
586
566
  var result = null;
587
567
  if (name) {
588
568
  var mod = this.mods[name];
589
- // 注册表模式下一次遍历查 entry,避免 getMod 重复 _getInfos
590
- if (!mod && this.config.use_registry) {
591
- var entries = this._getInfos();
592
- var entry = entries.find((o) => { return o.name === name; });
593
- if (entry && entry.state === 1) {
594
- var mod_class = this.config.mod_type === 'script' ? Mod : this.Drive;
595
- mod = new ModClass(entry, this);
596
- mod.mode = this.config.mode;
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
  }
@@ -697,31 +664,29 @@ Manager.prototype.getDir = function () {
697
664
  * 同时在后台异步扫描文件系统,确保注册表数据为物理最新。
698
665
  */
699
666
  Manager.prototype._loadSources = async function () {
700
- // 启用注册表且有缓存数据时,走内存快路径
701
- // 按管理器自身 name 过滤,多 Manager 共享 $.registry 时互不污染
667
+ // 启用注册表且有缓存数据时,走内存快路径,快速启动;
668
+ // 后台异步扫描文件系统更新注册表,确保物理最新。
702
669
  if (this.config.use_registry) {
703
- // 只有从注册表成功加载到模块时才跳过文件系统扫描,否则回退到文件系统
704
670
  if (this._loadFromRegistry()) {
705
- // 后台异步扫描文件系统,确保物理最新
706
671
  setImmediate(() => this._scanForUpdate(false));
707
672
  return;
708
673
  }
709
674
  }
710
675
 
711
- // 注册表无匹配,走文件系统扫描
712
- await this._scanForUpdate(false);
676
+ // 注册表无缓存,同步扫描文件系统
677
+ await this._scanForUpdate();
713
678
  };
714
679
 
715
680
  /**
716
681
  * 后台扫描文件系统并同步到注册表
717
682
  * 不阻塞启动,让注册表始终与磁盘保持同步
718
683
  */
719
- Manager.prototype._scanForUpdate = async function (clear = false) {
684
+ Manager.prototype._scanForUpdate = async function (clear = true) {
720
685
  var base_dir = this.getBaseDir();
721
686
  var dir = this.getDir();
722
687
  try {
723
688
  if (base_dir) {
724
- await Promise.all([this.call('update', base_dir, clear), this.call('update', dir, clear)]);
689
+ await Promise.all([this.call('update', base_dir, clear), this.call('update', dir, false)]);
725
690
  } else {
726
691
  await this.call('update', dir, clear);
727
692
  }
@@ -849,11 +814,7 @@ Manager.prototype.runWait = async function (method, ...params) {
849
814
  result = ret;
850
815
  }
851
816
  } catch (error) {
852
- this.log(
853
- 'error',
854
- '\u6A21\u5757' + entries[i].name + '\u6267\u884C' + method + '\u65B9\u6CD5\u5931\u8D25',
855
- error
856
- );
817
+ this._logModError(entries[i].name, method, error);
857
818
  }
858
819
  }
859
820
 
@@ -881,11 +842,7 @@ Manager.prototype.runAsync = async function (method, ...params) {
881
842
  if (!mod) continue;
882
843
 
883
844
  mod.do(method, ...params).catch((error) => {
884
- this.log(
885
- 'error',
886
- '\u6A21\u5757' + entries[i].name + '\u6267\u884C' + method + '\u65B9\u6CD5\u5931\u8D25',
887
- error
888
- );
845
+ this._logModError(entries[i].name, method, error);
889
846
  });
890
847
  }
891
848
 
@@ -974,11 +931,7 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
974
931
  result = ret;
975
932
  }
976
933
  } catch (error) {
977
- this.log(
978
- 'error',
979
- '\u6A21\u5757' + entries[i].name + '\u6267\u884C' + method + '\u65B9\u6CD5\u5931\u8D25',
980
- error
981
- );
934
+ this._logModError(entries[i].name, method, error);
982
935
  }
983
936
  }
984
937
 
@@ -1070,6 +1023,16 @@ Manager.prototype.remove = async function (name) {
1070
1023
  return mod;
1071
1024
  };
1072
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
+
1073
1036
  module.exports = {
1074
1037
  Manager
1075
1038
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.2.1",
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,