mm_machine 3.2.8 → 3.3.0

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 (2) hide show
  1. package/lib/manager.js +36 -43
  2. package/package.json +2 -2
package/lib/manager.js CHANGED
@@ -437,7 +437,24 @@ Manager.prototype.updateBefore = function () { };
437
437
  /**
438
438
  * 更新配置前的钩子函数
439
439
  */
440
- Manager.prototype.updateAfter = function () { };
440
+ Manager.prototype.updateAfter = function () {
441
+ this.sort();
442
+ };
443
+
444
+ /**
445
+ * 排序模块信息
446
+ */
447
+ Manager.prototype.sort = function () {
448
+ this.infos.sort((a, b) => {
449
+ // 先按 sort 排序
450
+ let cha = a.sort - b.sort;
451
+ if (cha !== 0) {
452
+ return cha;
453
+ }
454
+ // 再按 name 排序
455
+ return a.name.localeCompare(b.name);
456
+ });
457
+ };
441
458
 
442
459
  /**
443
460
  * 更新配置
@@ -548,33 +565,7 @@ Manager.prototype._runAllEnabled = async function (method, ...params) {
548
565
  * @private
549
566
  */
550
567
  Manager.prototype._runMod = async function (mod, method, ...params) {
551
- if (!mod || !method) return null;
552
-
553
- // 确保模块已加载
554
- if (!mod.isLoaded()) {
555
- try {
556
- await mod.do('load');
557
- } catch (err) {
558
- this.log('error', `加载模块脚本失败: `, err);
559
- }
560
- // 加载失败则不再执行,避免无方法调用
561
- if (!mod.isLoaded()) {
562
- return null;
563
- }
564
- }
565
-
566
- // 确保模块已初始化
567
- if (!mod.isInited() && method !== '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
- }
577
- }
568
+ mod = this._ensureModLoaded(mod, method);
578
569
 
579
570
  // 执行方法
580
571
  let ret = await mod.call(method, ...params);
@@ -696,24 +687,26 @@ Manager.prototype.exec = async function (name, method, ...params) {
696
687
  */
697
688
  Manager.prototype._ensureModLoaded = async function (mod, method) {
698
689
  if (!mod) return null;
699
- if (!mod.isLoaded() && method !== 'load') {
700
- try {
701
- await mod.do('load');
702
- } catch (error) {
703
- this.log('error', `模块${mod.config.name}加载失败`, error);
704
- return null;
690
+ if (!['load', 'init'].includes(method)) {
691
+ if (!mod.isLoaded()) {
692
+ try {
693
+ await mod.do('load');
694
+ } catch (error) {
695
+ this.log('error', `模块${mod.config.name}加载失败`, error);
696
+ return null;
697
+ }
705
698
  }
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);
712
- return null;
699
+ if (!mod.isInited()) {
700
+ try {
701
+ await mod.do('init');
702
+ } catch (error) {
703
+ this.log('error', `模块${mod.config.name}初始化失败`, error);
704
+ return null;
705
+ }
713
706
  }
714
- }
707
+ };
715
708
  return mod;
716
- };
709
+ }
717
710
 
718
711
  /**
719
712
  * 确保所有模块都已加载
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.2.8",
3
+ "version": "3.3.0",
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": {
@@ -39,7 +39,7 @@
39
39
  },
40
40
  "devDependencies": {
41
41
  "eslint": "^10.7.0",
42
- "eslint-plugin-jsdoc": "^63.0.14",
42
+ "eslint-plugin-jsdoc": "^63.1.0",
43
43
  "mm_eslint": "^1.7.5"
44
44
  }
45
45
  }