mm_machine 3.2.6 → 3.2.8

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/drive.js CHANGED
@@ -57,6 +57,23 @@ class Drive extends Mod {
57
57
  }
58
58
  }
59
59
 
60
+ /**
61
+ * 监听加载事件
62
+ * @param {object} ctx 上下文对象
63
+ */
64
+ Drive.prototype._onLoad = async function (ctx) {
65
+ // 1. 加载
66
+ this.on('load:before', async (ctx) => {
67
+ this.setState('loading');
68
+ await this.call('loadScript');
69
+ await this._loadCore(...ctx.params);
70
+ });
71
+ this.on('load:after', (ctx) => {
72
+ this.setState('loaded');
73
+ this.isLoaded(true);
74
+ });
75
+ };
76
+
60
77
  /**
61
78
  * 获取事件触发器
62
79
  * @returns {object} 事件触发器
@@ -664,8 +681,6 @@ Drive.prototype.loadBefore = function () {
664
681
  * 加载处理
665
682
  */
666
683
  Drive.prototype._loadCore = function () {
667
- // this.loadConfig();
668
- // this.loadScript();
669
684
  };
670
685
 
671
686
  /**
package/lib/manager.js CHANGED
@@ -139,7 +139,7 @@ Manager.prototype.getInfos = function () {
139
139
  Manager.prototype.getList = function (keyword) {
140
140
  let infos = this._getInfos();
141
141
 
142
- // 先用 entry 做关键词过滤,避免无效实例化
142
+ // 先用 info 做关键词过滤,避免无效实例化
143
143
  if (keyword) {
144
144
  infos = infos.filter((o) => {
145
145
  return o.name.includes(keyword) || o.title.includes(keyword) || o.description.includes(keyword);
@@ -306,7 +306,7 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
306
306
  this.setMod(mod.config.name, mod);
307
307
  }
308
308
 
309
- var entry = {
309
+ var info = {
310
310
  name: mod.config.name,
311
311
  title: mod.config.title,
312
312
  description: mod.config.description,
@@ -320,11 +320,10 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
320
320
  let keys = this.config.trigger_key;
321
321
  if (keys && keys.length > 0) {
322
322
  for (let key in keys) {
323
- entry[key] = mod.config[key];
323
+ info[key] = mod.config[key];
324
324
  }
325
325
  }
326
- this.infos.push(entry);
327
-
326
+ this.infos.push(info);
328
327
  return mod;
329
328
  };
330
329
 
@@ -430,19 +429,6 @@ Manager.prototype.getDirName = function () {
430
429
  return prefix + '_' + name;
431
430
  };
432
431
 
433
- /**
434
- * 加载所有模块的脚本
435
- */
436
- Manager.prototype.loadScripts = async function () {
437
- var promises = [];
438
- let mods = this.getMods();
439
- for (let name in mods) {
440
- let mod = mods[name];
441
- promises.push(mod.call('loadScript'));
442
- }
443
- await Promise.all(promises);
444
- };
445
-
446
432
  /**
447
433
  * 更新配置前的钩子函数
448
434
  */
@@ -469,7 +455,7 @@ Manager.prototype.update = async function (dir, clear = true) {
469
455
  this.regMods(files);
470
456
 
471
457
  if (!this.config.lazy_load) {
472
- await this.loadScripts();
458
+ await this.loadMods();
473
459
  }
474
460
  this._is_loaded = true;
475
461
  } catch (err) {
@@ -529,11 +515,11 @@ Manager.prototype._runAllEnabled = async function (method, ...params) {
529
515
  var result;
530
516
  var infos = this._getInfos();
531
517
  for (var i = 0; i < infos.length; i++) {
532
- var entry = infos[i];
533
- // 直接用 entry.state 过滤,避免无效实例化
534
- if (entry.state !== 1) continue;
518
+ var info = infos[i];
519
+ // 直接用 info.state 过滤,避免无效实例化
520
+ if (info.state !== 1) continue;
535
521
 
536
- var mod = this.getMod(entry.name);
522
+ var mod = this.getMod(info.name);
537
523
  if (!mod) continue;
538
524
 
539
525
  var ret = null;
@@ -567,7 +553,7 @@ Manager.prototype._runMod = async function (mod, method, ...params) {
567
553
  // 确保模块已加载
568
554
  if (!mod.isLoaded()) {
569
555
  try {
570
- await mod.call('loadScript');
556
+ await mod.do('load');
571
557
  } catch (err) {
572
558
  this.log('error', `加载模块脚本失败: `, err);
573
559
  }
@@ -579,7 +565,15 @@ Manager.prototype._runMod = async function (mod, method, ...params) {
579
565
 
580
566
  // 确保模块已初始化
581
567
  if (!mod.isInited() && method !== 'init') {
582
- await mod.do('init');
568
+ try {
569
+ await mod.do('init');
570
+ } catch (err) {
571
+ this.log('error', `初始化模块脚本失败: `, err);
572
+ }
573
+ // 初始化失败则不再执行,避免无方法调用
574
+ if (!mod.isInited()) {
575
+ return null;
576
+ }
583
577
  }
584
578
 
585
579
  // 执行方法
@@ -637,20 +631,25 @@ Manager.prototype._initSources = async function () {
637
631
  // 通过
638
632
  };
639
633
 
634
+ /**
635
+ * 加载核心资源
636
+ */
637
+ Manager.prototype._loadCore = async function () {
638
+ // 初始化管理器
639
+ this._initManager();
640
+ // 加载资源
641
+ await this._loadSources();
642
+ };
643
+
640
644
  /**
641
645
  * 初始化公共属性
642
646
  */
643
647
  Manager.prototype._initCore = async function () {
644
- try {
645
- // 初始化管理器
646
- this._initManager();
647
- // 加载资源
648
- await this._loadSources();
649
- // 初始化AI客户端
650
- await this._initSources();
651
- } catch (err) {
652
- this.log('error', '管理器初始化失败', err);
648
+ if (this.isLoaded()) {
649
+ await this.do('load');
653
650
  }
651
+ // 初始化AI客户端
652
+ await this._initSources();
654
653
  };
655
654
 
656
655
  /**
@@ -690,18 +689,26 @@ Manager.prototype.exec = async function (name, method, ...params) {
690
689
  };
691
690
 
692
691
  /**
693
- * 确保单个模块已加载
694
- * @param {string} name 模块名
692
+ * 确保模块已加载
693
+ * @param {object} mod 模块对象
694
+ * @param {string} method 方法名称
695
695
  * @returns {Promise<object|null>} 模块实例或 null
696
696
  */
697
- Manager.prototype._ensureModLoaded = async function (name) {
698
- let mod = this.getMod(name);
697
+ Manager.prototype._ensureModLoaded = async function (mod, method) {
699
698
  if (!mod) return null;
700
- if (!mod.isLoaded()) {
699
+ if (!mod.isLoaded() && method !== 'load') {
701
700
  try {
702
- await mod.call('loadScript');
701
+ await mod.do('load');
703
702
  } catch (error) {
704
- this.log('error', `模块${name}加载失败`, error);
703
+ this.log('error', `模块${mod.config.name}加载失败`, error);
704
+ return null;
705
+ }
706
+ }
707
+ if (!mod.isInited() && method !== 'init') {
708
+ try {
709
+ await mod.do('init');
710
+ } catch (error) {
711
+ this.log('error', `模块${mod.config.name}初始化失败`, error);
705
712
  return null;
706
713
  }
707
714
  }
@@ -712,13 +719,16 @@ Manager.prototype._ensureModLoaded = async function (name) {
712
719
  * 确保所有模块都已加载
713
720
  * @returns {Promise} 加载完成
714
721
  */
715
- Manager.prototype._ensureAllLoaded = async function () {
722
+ Manager.prototype._ensureAllLoaded = async function (method) {
716
723
  var promises = [];
717
724
  var infos = this._getInfos();
718
725
 
719
726
  for (var i = 0; i < infos.length; i++) {
720
- promises.push(this._ensureModLoaded(infos[i].name));
727
+ let mod = this.getMod(infos[i].name);
728
+ if (!mod) continue;
729
+ promises.push(this._ensureModLoaded(mod, method));
721
730
  }
731
+
722
732
  await Promise.all(promises);
723
733
  };
724
734
 
@@ -737,9 +747,9 @@ Manager.prototype.runWait = async function (method, ...params) {
737
747
  var infos = this._getInfos();
738
748
 
739
749
  for (var i = 0; i < infos.length; i++) {
740
- let mod = await this._ensureModLoaded(infos[i].name);
750
+ let mod = this.getMod(infos[i].name);
741
751
  if (!mod) continue;
742
-
752
+ mod = await this._ensureModLoaded(mod, method);
743
753
  try {
744
754
  let ret = await mod.do(method, ...params);
745
755
  if (ret !== null && ret !== undefined) {
@@ -765,7 +775,7 @@ Manager.prototype.runAsync = async function (method, ...params) {
765
775
  }
766
776
 
767
777
  // 确保所有模块都已加载
768
- await this._ensureAllLoaded();
778
+ await this._ensureAllLoaded(method);
769
779
 
770
780
  var infos = this._getInfos();
771
781
 
@@ -793,7 +803,7 @@ Manager.prototype.runAll = async function (method, ...params) {
793
803
  }
794
804
 
795
805
  // 确保所有模块都已加载
796
- await this._ensureAllLoaded();
806
+ await this._ensureAllLoaded(method);
797
807
 
798
808
  const promises = [];
799
809
  var infos = this._getInfos();
@@ -820,7 +830,7 @@ Manager.prototype.runRace = async function (method, ...params) {
820
830
  }
821
831
 
822
832
  // 确保所有模块都已加载
823
- await this._ensureAllLoaded();
833
+ await this._ensureAllLoaded(method);
824
834
 
825
835
  const promises = [];
826
836
  var infos = this._getInfos();
@@ -854,9 +864,9 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
854
864
  var infos = this._getInfos();
855
865
 
856
866
  for (var i = 0; i < infos.length; i++) {
857
- var mod = await this._ensureModLoaded(infos[i].name);
867
+ let mod = this.getMod(infos[i].name);
858
868
  if (!mod) continue;
859
-
869
+ mod = await this._ensureModLoaded(mod, method);
860
870
  try {
861
871
  let ret = await mod.do(method, result, ...params);
862
872
  if (ret !== null && ret !== undefined) {
@@ -881,10 +891,11 @@ Manager.prototype.runSubMod = async function (name, method, ...params) {
881
891
  if (typeof method !== 'string') {
882
892
  throw new TypeError('方法名称必须是字符串');
883
893
  }
884
- let mod = await this._ensureModLoaded(name);
894
+ let mod = this.getMod(name);
885
895
  if (!mod) {
886
896
  throw new Error(`模块${name}不存在或加载失败`);
887
897
  }
898
+ mod = await this._ensureModLoaded(mod, method);
888
899
  return await mod.do(method, ...params);
889
900
  };
890
901
 
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.2.6",
3
+ "version": "3.2.8",
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": {
7
- "test": "node ./tests/run.js",
7
+ "test": "node ./test.js",
8
8
  "dev": "nodemon ./test.js"
9
9
  },
10
10
  "repository": {
@@ -35,11 +35,11 @@
35
35
  "@prettier/sync": "^0.6.1",
36
36
  "mm_config": "^2.4.5",
37
37
  "mm_hot_reload": "^1.4.2",
38
- "mm_tpl": "^2.6.4"
38
+ "mm_tpl": "^2.6.5"
39
39
  },
40
40
  "devDependencies": {
41
41
  "eslint": "^10.7.0",
42
- "eslint-plugin-jsdoc": "^63.0.13",
42
+ "eslint-plugin-jsdoc": "^63.0.14",
43
43
  "mm_eslint": "^1.7.5"
44
44
  }
45
45
  }