mm_machine 2.4.4 → 2.4.6
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 -48
- 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
|
|
|
@@ -424,7 +428,6 @@ Manager.prototype._findFiles = function (dir) {
|
|
|
424
428
|
}
|
|
425
429
|
// 检查目录是否存在
|
|
426
430
|
if (!dir.hasDir()) {
|
|
427
|
-
this.log('warn', '目录不存在', dir);
|
|
428
431
|
return [];
|
|
429
432
|
}
|
|
430
433
|
if (this.config.search_way === 'dir') {
|
|
@@ -497,7 +500,7 @@ Manager.prototype.main = async function (name, method, ...params) {
|
|
|
497
500
|
if (name) {
|
|
498
501
|
let mod = this.getMod(name);
|
|
499
502
|
if (mod && mod.config?.state === 1) {
|
|
500
|
-
result = await this.
|
|
503
|
+
result = await this._run(mod, method, ...params);
|
|
501
504
|
}
|
|
502
505
|
} else if (name === null) {
|
|
503
506
|
result = await this._runAllEnabled(method, ...params);
|
|
@@ -518,7 +521,7 @@ Manager.prototype._runAllEnabled = async function (method, ...params) {
|
|
|
518
521
|
let config = this.infos[i];
|
|
519
522
|
let mod = this.getMod(config.name);
|
|
520
523
|
if (mod && mod.config?.state === 1) {
|
|
521
|
-
var ret = await this.
|
|
524
|
+
var ret = await this._run(mod, method, ...params);
|
|
522
525
|
if (ret !== null && ret !== undefined) {
|
|
523
526
|
result = ret;
|
|
524
527
|
// 如果结果不为空且有结束标志,则停止迭代
|
|
@@ -531,49 +534,6 @@ Manager.prototype._runAllEnabled = async function (method, ...params) {
|
|
|
531
534
|
return result;
|
|
532
535
|
};
|
|
533
536
|
|
|
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
537
|
/**
|
|
578
538
|
* 私有方法:执行模块方法
|
|
579
539
|
* @param {object} mod 模块对象
|
|
@@ -582,7 +542,7 @@ Manager.prototype._runAll = async function (method, ...params) {
|
|
|
582
542
|
* @returns {Promise<any>} 执行结果
|
|
583
543
|
* @private
|
|
584
544
|
*/
|
|
585
|
-
Manager.prototype.
|
|
545
|
+
Manager.prototype._run = async function (mod, method, ...params) {
|
|
586
546
|
if (!mod || !method) return null;
|
|
587
547
|
|
|
588
548
|
try {
|
|
@@ -639,6 +599,248 @@ Manager.prototype._initCore = async function () {
|
|
|
639
599
|
await this._initSources();
|
|
640
600
|
};
|
|
641
601
|
|
|
602
|
+
/**
|
|
603
|
+
* 执行方法
|
|
604
|
+
* @param {string} method 方法名称
|
|
605
|
+
* @param {...object} params 参数列表
|
|
606
|
+
* @returns {object} 返回执行结果
|
|
607
|
+
*/
|
|
608
|
+
Manager.prototype._exec = async function (mod, method, ...params) {
|
|
609
|
+
if (mod[method]) {
|
|
610
|
+
return await mod[method](...params);
|
|
611
|
+
}
|
|
612
|
+
else {
|
|
613
|
+
throw new Error(`方法${method}不存在`);
|
|
614
|
+
}
|
|
615
|
+
};
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* 执行方法
|
|
619
|
+
* @param {string} method 方法名称
|
|
620
|
+
* @param {...any} params 参数列表
|
|
621
|
+
* @returns {object} 返回执行结果
|
|
622
|
+
*/
|
|
623
|
+
Manager.prototype.exec = async function (name, method, ...params) {
|
|
624
|
+
let ret;
|
|
625
|
+
try {
|
|
626
|
+
let mod = this.getMod(name);
|
|
627
|
+
ret = await this._exec(mod, method, ...params);
|
|
628
|
+
}
|
|
629
|
+
catch (error) {
|
|
630
|
+
this.log('error', `${method}方法执行失败!`, error);
|
|
631
|
+
}
|
|
632
|
+
return ret;
|
|
633
|
+
};
|
|
634
|
+
|
|
635
|
+
/**
|
|
636
|
+
* 确保所有模块都已加载
|
|
637
|
+
* @returns {Promise} 加载完成
|
|
638
|
+
*/
|
|
639
|
+
Manager.prototype._ensureAllModulesLoaded = async function () {
|
|
640
|
+
let infos = this.getInfos();
|
|
641
|
+
|
|
642
|
+
for (let i = 0; i < infos.length; i++) {
|
|
643
|
+
let info = infos[i];
|
|
644
|
+
let mod = this.getMod(info.name);
|
|
645
|
+
|
|
646
|
+
if (!mod.isLoaded()) {
|
|
647
|
+
try {
|
|
648
|
+
await mod.call('loadScript');
|
|
649
|
+
} catch (error) {
|
|
650
|
+
this.log('error', `模块${info.name}加载失败`, error);
|
|
651
|
+
continue; // 跳过加载失败的模块
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
};
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* 异步触发事件,按顺序执行等待完成
|
|
659
|
+
* @param {string} method 方法名称
|
|
660
|
+
* @param {...any} params 事件参数
|
|
661
|
+
* @returns {Promise} 事件触发结果
|
|
662
|
+
*/
|
|
663
|
+
Manager.prototype.runWait = async function (method, ...params) {
|
|
664
|
+
// 参数校验
|
|
665
|
+
if (typeof method !== 'string') {
|
|
666
|
+
throw new TypeError('方法名称必须是字符串');
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
let result;
|
|
670
|
+
let infos = this.getInfos();
|
|
671
|
+
|
|
672
|
+
for (let i = 0; i < infos.length; i++) {
|
|
673
|
+
let info = infos[i];
|
|
674
|
+
let mod = this.getMod(info.name);
|
|
675
|
+
|
|
676
|
+
// 确保模块已加载
|
|
677
|
+
if (!mod.isLoaded()) {
|
|
678
|
+
try {
|
|
679
|
+
await mod.call('loadScript');
|
|
680
|
+
} catch (error) {
|
|
681
|
+
this.log('error', `模块${info.name}加载失败`, error);
|
|
682
|
+
continue; // 跳过加载失败的模块
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
// 执行方法
|
|
687
|
+
try {
|
|
688
|
+
let ret = await mod.call(method, ...params);
|
|
689
|
+
if (ret !== null && ret !== undefined) {
|
|
690
|
+
result = ret;
|
|
691
|
+
}
|
|
692
|
+
} catch (error) {
|
|
693
|
+
this.log('error', `模块${info.name}执行${method}方法失败`, error);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
return result;
|
|
698
|
+
};
|
|
699
|
+
|
|
700
|
+
/**
|
|
701
|
+
* 异步触发事件,并发执行不等待
|
|
702
|
+
* @param {string} method 方法名称
|
|
703
|
+
* @param {...any} params 事件参数
|
|
704
|
+
* @returns {boolean} 是否成功触发事件
|
|
705
|
+
*/
|
|
706
|
+
Manager.prototype.runAsync = async function (method, ...params) {
|
|
707
|
+
// 参数校验
|
|
708
|
+
if (typeof method !== 'string') {
|
|
709
|
+
throw new TypeError('方法名称必须是字符串');
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
// 确保所有模块都已加载
|
|
713
|
+
await this._ensureAllModulesLoaded();
|
|
714
|
+
|
|
715
|
+
let infos = this.getInfos();
|
|
716
|
+
|
|
717
|
+
// 并发执行所有方法,不等待结果
|
|
718
|
+
for (let i = 0; i < infos.length; i++) {
|
|
719
|
+
let info = infos[i];
|
|
720
|
+
let mod = this.getMod(info.name);
|
|
721
|
+
try {
|
|
722
|
+
mod.call(method, ...params);
|
|
723
|
+
} catch (error) {
|
|
724
|
+
this.log('error', `模块${info.name}执行${method}方法失败`, error);
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
return true;
|
|
729
|
+
};
|
|
730
|
+
|
|
731
|
+
/**
|
|
732
|
+
* 异步触发事件,并发执行等待完成
|
|
733
|
+
* @param {string} method 方法名称
|
|
734
|
+
* @param {...any} params 事件参数
|
|
735
|
+
* @returns {Promise} 事件触发结果
|
|
736
|
+
*/
|
|
737
|
+
Manager.prototype.runAll = async function (method, ...params) {
|
|
738
|
+
// 参数校验
|
|
739
|
+
if (typeof method !== 'string') {
|
|
740
|
+
throw new TypeError('方法名称必须是字符串');
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
// 确保所有模块都已加载
|
|
744
|
+
await this._ensureAllModulesLoaded();
|
|
745
|
+
|
|
746
|
+
const promises = [];
|
|
747
|
+
let infos = this.getInfos();
|
|
748
|
+
|
|
749
|
+
// 并发执行所有方法
|
|
750
|
+
for (let i = 0; i < infos.length; i++) {
|
|
751
|
+
let info = infos[i];
|
|
752
|
+
let mod = this.getMod(info.name);
|
|
753
|
+
if (mod.isLoaded()) {
|
|
754
|
+
promises.push(
|
|
755
|
+
mod.call(method, ...params).catch(error => {
|
|
756
|
+
this.log('error', `模块${info.name}执行${method}方法失败`, error);
|
|
757
|
+
return null; // 失败时返回null
|
|
758
|
+
})
|
|
759
|
+
);
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
return await Promise.all(promises);
|
|
764
|
+
};
|
|
765
|
+
|
|
766
|
+
/**
|
|
767
|
+
* 异步触发事件,竞争执行
|
|
768
|
+
* @param {string} method 方法名称
|
|
769
|
+
* @param {...any} params 事件参数
|
|
770
|
+
* @returns {Promise} 第一个完成的结果
|
|
771
|
+
*/
|
|
772
|
+
Manager.prototype.runRace = async function (method, ...params) {
|
|
773
|
+
// 参数校验
|
|
774
|
+
if (typeof method !== 'string') {
|
|
775
|
+
throw new TypeError('方法名称必须是字符串');
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
// 确保所有模块都已加载
|
|
779
|
+
await this._ensureAllModulesLoaded();
|
|
780
|
+
|
|
781
|
+
const promises = [];
|
|
782
|
+
let infos = this.getInfos();
|
|
783
|
+
|
|
784
|
+
// 竞争执行所有方法
|
|
785
|
+
for (let i = 0; i < infos.length; i++) {
|
|
786
|
+
let info = infos[i];
|
|
787
|
+
let mod = this.getMod(info.name);
|
|
788
|
+
if (mod.isLoaded()) {
|
|
789
|
+
promises.push(
|
|
790
|
+
mod.call(method, ...params).catch(error => {
|
|
791
|
+
this.log('error', `模块${info.name}执行${method}方法失败`, error);
|
|
792
|
+
throw error; // 竞争执行中,失败应该抛出错误
|
|
793
|
+
})
|
|
794
|
+
);
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
return await Promise.race(promises);
|
|
799
|
+
};
|
|
800
|
+
|
|
801
|
+
/**
|
|
802
|
+
* 异步触发事件,瀑布流执行
|
|
803
|
+
* @param {string} method 方法名称
|
|
804
|
+
* @param {*} result 初始值
|
|
805
|
+
* @param {...any} params 事件参数
|
|
806
|
+
* @returns {Promise} 最后一个事件监听者的结果
|
|
807
|
+
*/
|
|
808
|
+
Manager.prototype.runWaterfall = async function (method, result, ...params) {
|
|
809
|
+
// 参数校验
|
|
810
|
+
if (typeof method !== 'string') {
|
|
811
|
+
throw new TypeError('方法名称必须是字符串');
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
let infos = this.getInfos();
|
|
815
|
+
|
|
816
|
+
for (let i = 0; i < infos.length; i++) {
|
|
817
|
+
let info = infos[i];
|
|
818
|
+
let mod = this.getMod(info.name);
|
|
819
|
+
|
|
820
|
+
// 确保模块已加载
|
|
821
|
+
if (!mod.isLoaded()) {
|
|
822
|
+
try {
|
|
823
|
+
await mod.call('loadScript');
|
|
824
|
+
} catch (error) {
|
|
825
|
+
this.log('error', `模块${info.name}加载失败`, error);
|
|
826
|
+
continue; // 跳过加载失败的模块
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
// 执行方法,将前一个结果作为参数传递给下一个
|
|
831
|
+
try {
|
|
832
|
+
let ret = await mod.call(method, result, ...params);
|
|
833
|
+
if (ret !== null && ret !== undefined) {
|
|
834
|
+
result = ret;
|
|
835
|
+
}
|
|
836
|
+
} catch (error) {
|
|
837
|
+
this.log('error', `模块${info.name}执行${method}方法失败`, error);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
return result;
|
|
842
|
+
};
|
|
843
|
+
|
|
642
844
|
module.exports = {
|
|
643
845
|
Manager,
|
|
644
846
|
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.6",
|
|
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": {
|