mm_machine 2.4.4 → 2.4.5
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/index.js +250 -47
- package/mod.js +1 -10
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -87,8 +87,9 @@ class Manager extends Mod {
|
|
|
87
87
|
* @param {object} config 配置项
|
|
88
88
|
* @param {object} parent 父项对象
|
|
89
89
|
* @param {object} mods 模块对象列表
|
|
90
|
+
* @param {object} Drive 驱动对象
|
|
90
91
|
*/
|
|
91
|
-
constructor(config, parent, mods = {}) {
|
|
92
|
+
constructor(config, parent, mods = {}, Drive) {
|
|
92
93
|
super({
|
|
93
94
|
...Manager.config,
|
|
94
95
|
...config
|
|
@@ -98,6 +99,9 @@ class Manager extends Mod {
|
|
|
98
99
|
this.infos = [];
|
|
99
100
|
// 模块
|
|
100
101
|
this.mods = mods;
|
|
102
|
+
if (Drive) {
|
|
103
|
+
this.Drive = Drive;
|
|
104
|
+
}
|
|
101
105
|
}
|
|
102
106
|
}
|
|
103
107
|
|
|
@@ -497,7 +501,7 @@ Manager.prototype.main = async function (name, method, ...params) {
|
|
|
497
501
|
if (name) {
|
|
498
502
|
let mod = this.getMod(name);
|
|
499
503
|
if (mod && mod.config?.state === 1) {
|
|
500
|
-
result = await this.
|
|
504
|
+
result = await this._run(mod, method, ...params);
|
|
501
505
|
}
|
|
502
506
|
} else if (name === null) {
|
|
503
507
|
result = await this._runAllEnabled(method, ...params);
|
|
@@ -518,7 +522,7 @@ Manager.prototype._runAllEnabled = async function (method, ...params) {
|
|
|
518
522
|
let config = this.infos[i];
|
|
519
523
|
let mod = this.getMod(config.name);
|
|
520
524
|
if (mod && mod.config?.state === 1) {
|
|
521
|
-
var ret = await this.
|
|
525
|
+
var ret = await this._run(mod, method, ...params);
|
|
522
526
|
if (ret !== null && ret !== undefined) {
|
|
523
527
|
result = ret;
|
|
524
528
|
// 如果结果不为空且有结束标志,则停止迭代
|
|
@@ -531,49 +535,6 @@ Manager.prototype._runAllEnabled = async function (method, ...params) {
|
|
|
531
535
|
return result;
|
|
532
536
|
};
|
|
533
537
|
|
|
534
|
-
/**
|
|
535
|
-
* 执行方法
|
|
536
|
-
* @param {string} name 模块名称
|
|
537
|
-
* @param {string} method 方法名称
|
|
538
|
-
* @param {...any} params 方法参数集合
|
|
539
|
-
* @returns {Promise<any>} 执行结果
|
|
540
|
-
*/
|
|
541
|
-
Manager.prototype.runAsync = async function (name, method, ...params) {
|
|
542
|
-
// 使用mods的顺序(按配置顺序调用)
|
|
543
|
-
var result = null;
|
|
544
|
-
if (name) {
|
|
545
|
-
let mod = await this.getMod(name);
|
|
546
|
-
if (mod) {
|
|
547
|
-
result = await this._runSub(mod, method, ...params);
|
|
548
|
-
}
|
|
549
|
-
} else if (name === null) {
|
|
550
|
-
result = await this._runAll(method, ...params);
|
|
551
|
-
}
|
|
552
|
-
return result;
|
|
553
|
-
};
|
|
554
|
-
|
|
555
|
-
/**
|
|
556
|
-
* 私有方法:执行所有模块方法
|
|
557
|
-
* @param {string} method 方法名称
|
|
558
|
-
* @param {...any} params 方法参数集合
|
|
559
|
-
* @returns {Promise<any>} 执行结果
|
|
560
|
-
* @private
|
|
561
|
-
*/
|
|
562
|
-
Manager.prototype._runAll = async function (method, ...params) {
|
|
563
|
-
var result = null;
|
|
564
|
-
for (let i = 0; i < this.infos.length; i++) {
|
|
565
|
-
let config = this.infos[i];
|
|
566
|
-
let mod = this.getMod(config.name);
|
|
567
|
-
if (mod) {
|
|
568
|
-
var ret = await this._runSub(mod, method, ...params);
|
|
569
|
-
if (ret !== null && ret !== undefined) {
|
|
570
|
-
result = ret;
|
|
571
|
-
}
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
return result;
|
|
575
|
-
};
|
|
576
|
-
|
|
577
538
|
/**
|
|
578
539
|
* 私有方法:执行模块方法
|
|
579
540
|
* @param {object} mod 模块对象
|
|
@@ -582,7 +543,7 @@ Manager.prototype._runAll = async function (method, ...params) {
|
|
|
582
543
|
* @returns {Promise<any>} 执行结果
|
|
583
544
|
* @private
|
|
584
545
|
*/
|
|
585
|
-
Manager.prototype.
|
|
546
|
+
Manager.prototype._run = async function (mod, method, ...params) {
|
|
586
547
|
if (!mod || !method) return null;
|
|
587
548
|
|
|
588
549
|
try {
|
|
@@ -639,6 +600,248 @@ Manager.prototype._initCore = async function () {
|
|
|
639
600
|
await this._initSources();
|
|
640
601
|
};
|
|
641
602
|
|
|
603
|
+
/**
|
|
604
|
+
* 执行方法
|
|
605
|
+
* @param {string} method 方法名称
|
|
606
|
+
* @param {...object} params 参数列表
|
|
607
|
+
* @returns {object} 返回执行结果
|
|
608
|
+
*/
|
|
609
|
+
Manager.prototype._exec = async function (mod, method, ...params) {
|
|
610
|
+
if (mod[method]) {
|
|
611
|
+
return await mod[method](...params);
|
|
612
|
+
}
|
|
613
|
+
else {
|
|
614
|
+
throw new Error(`方法${method}不存在`);
|
|
615
|
+
}
|
|
616
|
+
};
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* 执行方法
|
|
620
|
+
* @param {string} method 方法名称
|
|
621
|
+
* @param {...any} params 参数列表
|
|
622
|
+
* @returns {object} 返回执行结果
|
|
623
|
+
*/
|
|
624
|
+
Manager.prototype.exec = async function (name, method, ...params) {
|
|
625
|
+
let ret;
|
|
626
|
+
try {
|
|
627
|
+
let mod = this.getMod(name);
|
|
628
|
+
ret = await this._exec(mod, method, ...params);
|
|
629
|
+
}
|
|
630
|
+
catch (error) {
|
|
631
|
+
this.log('error', `${method}方法执行失败!`, error);
|
|
632
|
+
}
|
|
633
|
+
return ret;
|
|
634
|
+
};
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* 确保所有模块都已加载
|
|
638
|
+
* @returns {Promise} 加载完成
|
|
639
|
+
*/
|
|
640
|
+
Manager.prototype._ensureAllModulesLoaded = async function () {
|
|
641
|
+
let infos = this.getInfos();
|
|
642
|
+
|
|
643
|
+
for (let i = 0; i < infos.length; i++) {
|
|
644
|
+
let info = infos[i];
|
|
645
|
+
let mod = this.getMod(info.name);
|
|
646
|
+
|
|
647
|
+
if (!mod.isLoaded()) {
|
|
648
|
+
try {
|
|
649
|
+
await mod.call('loadScript');
|
|
650
|
+
} catch (error) {
|
|
651
|
+
this.log('error', `模块${info.name}加载失败`, error);
|
|
652
|
+
continue; // 跳过加载失败的模块
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
};
|
|
657
|
+
|
|
658
|
+
/**
|
|
659
|
+
* 异步触发事件,按顺序执行等待完成
|
|
660
|
+
* @param {string} method 方法名称
|
|
661
|
+
* @param {...any} params 事件参数
|
|
662
|
+
* @returns {Promise} 事件触发结果
|
|
663
|
+
*/
|
|
664
|
+
Manager.prototype.runWait = async function (method, ...params) {
|
|
665
|
+
// 参数校验
|
|
666
|
+
if (typeof method !== 'string') {
|
|
667
|
+
throw new TypeError('方法名称必须是字符串');
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
let result;
|
|
671
|
+
let infos = this.getInfos();
|
|
672
|
+
|
|
673
|
+
for (let i = 0; i < infos.length; i++) {
|
|
674
|
+
let info = infos[i];
|
|
675
|
+
let mod = this.getMod(info.name);
|
|
676
|
+
|
|
677
|
+
// 确保模块已加载
|
|
678
|
+
if (!mod.isLoaded()) {
|
|
679
|
+
try {
|
|
680
|
+
await mod.call('loadScript');
|
|
681
|
+
} catch (error) {
|
|
682
|
+
this.log('error', `模块${info.name}加载失败`, error);
|
|
683
|
+
continue; // 跳过加载失败的模块
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
// 执行方法
|
|
688
|
+
try {
|
|
689
|
+
let ret = await mod.call(method, ...params);
|
|
690
|
+
if (ret !== null && ret !== undefined) {
|
|
691
|
+
result = ret;
|
|
692
|
+
}
|
|
693
|
+
} catch (error) {
|
|
694
|
+
this.log('error', `模块${info.name}执行${method}方法失败`, error);
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
return result;
|
|
699
|
+
};
|
|
700
|
+
|
|
701
|
+
/**
|
|
702
|
+
* 异步触发事件,并发执行不等待
|
|
703
|
+
* @param {string} method 方法名称
|
|
704
|
+
* @param {...any} params 事件参数
|
|
705
|
+
* @returns {boolean} 是否成功触发事件
|
|
706
|
+
*/
|
|
707
|
+
Manager.prototype.runAsync = async function (method, ...params) {
|
|
708
|
+
// 参数校验
|
|
709
|
+
if (typeof method !== 'string') {
|
|
710
|
+
throw new TypeError('方法名称必须是字符串');
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
// 确保所有模块都已加载
|
|
714
|
+
await this._ensureAllModulesLoaded();
|
|
715
|
+
|
|
716
|
+
let infos = this.getInfos();
|
|
717
|
+
|
|
718
|
+
// 并发执行所有方法,不等待结果
|
|
719
|
+
for (let i = 0; i < infos.length; i++) {
|
|
720
|
+
let info = infos[i];
|
|
721
|
+
let mod = this.getMod(info.name);
|
|
722
|
+
try {
|
|
723
|
+
mod.call(method, ...params);
|
|
724
|
+
} catch (error) {
|
|
725
|
+
this.log('error', `模块${info.name}执行${method}方法失败`, error);
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
return true;
|
|
730
|
+
};
|
|
731
|
+
|
|
732
|
+
/**
|
|
733
|
+
* 异步触发事件,并发执行等待完成
|
|
734
|
+
* @param {string} method 方法名称
|
|
735
|
+
* @param {...any} params 事件参数
|
|
736
|
+
* @returns {Promise} 事件触发结果
|
|
737
|
+
*/
|
|
738
|
+
Manager.prototype.runAll = async function (method, ...params) {
|
|
739
|
+
// 参数校验
|
|
740
|
+
if (typeof method !== 'string') {
|
|
741
|
+
throw new TypeError('方法名称必须是字符串');
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
// 确保所有模块都已加载
|
|
745
|
+
await this._ensureAllModulesLoaded();
|
|
746
|
+
|
|
747
|
+
const promises = [];
|
|
748
|
+
let infos = this.getInfos();
|
|
749
|
+
|
|
750
|
+
// 并发执行所有方法
|
|
751
|
+
for (let i = 0; i < infos.length; i++) {
|
|
752
|
+
let info = infos[i];
|
|
753
|
+
let mod = this.getMod(info.name);
|
|
754
|
+
if (mod.isLoaded()) {
|
|
755
|
+
promises.push(
|
|
756
|
+
mod.call(method, ...params).catch(error => {
|
|
757
|
+
this.log('error', `模块${info.name}执行${method}方法失败`, error);
|
|
758
|
+
return null; // 失败时返回null
|
|
759
|
+
})
|
|
760
|
+
);
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
return await Promise.all(promises);
|
|
765
|
+
};
|
|
766
|
+
|
|
767
|
+
/**
|
|
768
|
+
* 异步触发事件,竞争执行
|
|
769
|
+
* @param {string} method 方法名称
|
|
770
|
+
* @param {...any} params 事件参数
|
|
771
|
+
* @returns {Promise} 第一个完成的结果
|
|
772
|
+
*/
|
|
773
|
+
Manager.prototype.runRace = async function (method, ...params) {
|
|
774
|
+
// 参数校验
|
|
775
|
+
if (typeof method !== 'string') {
|
|
776
|
+
throw new TypeError('方法名称必须是字符串');
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
// 确保所有模块都已加载
|
|
780
|
+
await this._ensureAllModulesLoaded();
|
|
781
|
+
|
|
782
|
+
const promises = [];
|
|
783
|
+
let infos = this.getInfos();
|
|
784
|
+
|
|
785
|
+
// 竞争执行所有方法
|
|
786
|
+
for (let i = 0; i < infos.length; i++) {
|
|
787
|
+
let info = infos[i];
|
|
788
|
+
let mod = this.getMod(info.name);
|
|
789
|
+
if (mod.isLoaded()) {
|
|
790
|
+
promises.push(
|
|
791
|
+
mod.call(method, ...params).catch(error => {
|
|
792
|
+
this.log('error', `模块${info.name}执行${method}方法失败`, error);
|
|
793
|
+
throw error; // 竞争执行中,失败应该抛出错误
|
|
794
|
+
})
|
|
795
|
+
);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
return await Promise.race(promises);
|
|
800
|
+
};
|
|
801
|
+
|
|
802
|
+
/**
|
|
803
|
+
* 异步触发事件,瀑布流执行
|
|
804
|
+
* @param {string} method 方法名称
|
|
805
|
+
* @param {*} result 初始值
|
|
806
|
+
* @param {...any} params 事件参数
|
|
807
|
+
* @returns {Promise} 最后一个事件监听者的结果
|
|
808
|
+
*/
|
|
809
|
+
Manager.prototype.runWaterfall = async function (method, result, ...params) {
|
|
810
|
+
// 参数校验
|
|
811
|
+
if (typeof method !== 'string') {
|
|
812
|
+
throw new TypeError('方法名称必须是字符串');
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
let infos = this.getInfos();
|
|
816
|
+
|
|
817
|
+
for (let i = 0; i < infos.length; i++) {
|
|
818
|
+
let info = infos[i];
|
|
819
|
+
let mod = this.getMod(info.name);
|
|
820
|
+
|
|
821
|
+
// 确保模块已加载
|
|
822
|
+
if (!mod.isLoaded()) {
|
|
823
|
+
try {
|
|
824
|
+
await mod.call('loadScript');
|
|
825
|
+
} catch (error) {
|
|
826
|
+
this.log('error', `模块${info.name}加载失败`, error);
|
|
827
|
+
continue; // 跳过加载失败的模块
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
// 执行方法,将前一个结果作为参数传递给下一个
|
|
832
|
+
try {
|
|
833
|
+
let ret = await mod.call(method, result, ...params);
|
|
834
|
+
if (ret !== null && ret !== undefined) {
|
|
835
|
+
result = ret;
|
|
836
|
+
}
|
|
837
|
+
} catch (error) {
|
|
838
|
+
this.log('error', `模块${info.name}执行${method}方法失败`, error);
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
return result;
|
|
843
|
+
};
|
|
844
|
+
|
|
642
845
|
module.exports = {
|
|
643
846
|
Manager,
|
|
644
847
|
Drive,
|
package/mod.js
CHANGED
|
@@ -389,15 +389,6 @@ Mod.prototype.emitEvent = function (event, ...args) {
|
|
|
389
389
|
this.getEventer()?.emit(event, ...args);
|
|
390
390
|
};
|
|
391
391
|
|
|
392
|
-
/**
|
|
393
|
-
* 运行方法
|
|
394
|
-
* @param {...any} args 方法参数
|
|
395
|
-
* @returns {Promise<any>} 方法执行结果
|
|
396
|
-
*/
|
|
397
|
-
Mod.prototype.run = async function (...args) {
|
|
398
|
-
return this.main(...args);
|
|
399
|
-
};
|
|
400
|
-
|
|
401
392
|
/**
|
|
402
393
|
* 主方法名
|
|
403
394
|
* @param {...any} args 方法参数
|
|
@@ -504,7 +495,7 @@ Mod.prototype._callBefore = async function (method, params) {
|
|
|
504
495
|
* @returns {Promise<any>} 主方法结果
|
|
505
496
|
*/
|
|
506
497
|
Mod.prototype._callMain = async function (method_func, params, result) {
|
|
507
|
-
let ret = await method_func.call(this
|
|
498
|
+
let ret = await method_func.call(this, ...params);
|
|
508
499
|
// 修复:如果主方法返回undefined,则使用前置钩子的结果
|
|
509
500
|
return ret !== undefined ? ret : result;
|
|
510
501
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_machine",
|
|
3
|
-
"version": "2.4.
|
|
3
|
+
"version": "2.4.5",
|
|
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": {
|