mm_machine 2.4.3 → 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.
Files changed (4) hide show
  1. package/drive.js +9 -3
  2. package/index.js +260 -53
  3. package/mod.js +58 -16
  4. package/package.json +1 -1
package/drive.js CHANGED
@@ -3,6 +3,7 @@ require('mm_tpl');
3
3
  let {
4
4
  conf
5
5
  } = require('mm_config');
6
+ // const { Word } = require('./ulits/word.js');
6
7
  const {
7
8
  Mod
8
9
  } = require('./mod');
@@ -617,7 +618,12 @@ Drive.prototype._delArray = function (file, config, name) {
617
618
  * @returns {string} 配置文件名称
618
619
  */
619
620
  Drive.prototype._getName = function (file) {
620
- return file.dirname().basename();
621
+ let name = file.dirname().basename();
622
+ return name;
623
+ // // 提取文件名作为模块名
624
+ // var word = new Word();
625
+ // // 将文件名改为驼峰命名法
626
+ // return word.toCamelCase(name);
621
627
  };
622
628
 
623
629
  /**
@@ -639,8 +645,8 @@ Drive.prototype.loadBefore = function () {
639
645
  * 加载处理
640
646
  */
641
647
  Drive.prototype._loadCore = function () {
642
- this.loadConfig();
643
- this.loadScript();
648
+ // this.loadConfig();
649
+ // this.loadScript();
644
650
  };
645
651
 
646
652
  /**
package/index.js CHANGED
@@ -86,8 +86,10 @@ class Manager extends Mod {
86
86
  * 构造函数
87
87
  * @param {object} config 配置项
88
88
  * @param {object} parent 父项对象
89
+ * @param {object} mods 模块对象列表
90
+ * @param {object} Drive 驱动对象
89
91
  */
90
- constructor(config, parent) {
92
+ constructor(config, parent, mods = {}, Drive) {
91
93
  super({
92
94
  ...Manager.config,
93
95
  ...config
@@ -96,7 +98,10 @@ class Manager extends Mod {
96
98
  // 模块信息列表 { name: '', sort: 0, available: true, enabled: true }
97
99
  this.infos = [];
98
100
  // 模块
99
- this.mods = {};
101
+ this.mods = mods;
102
+ if (Drive) {
103
+ this.Drive = Drive;
104
+ }
100
105
  }
101
106
  }
102
107
 
@@ -496,7 +501,7 @@ Manager.prototype.main = async function (name, method, ...params) {
496
501
  if (name) {
497
502
  let mod = this.getMod(name);
498
503
  if (mod && mod.config?.state === 1) {
499
- result = await this._runSub(mod, method, ...params);
504
+ result = await this._run(mod, method, ...params);
500
505
  }
501
506
  } else if (name === null) {
502
507
  result = await this._runAllEnabled(method, ...params);
@@ -517,7 +522,7 @@ Manager.prototype._runAllEnabled = async function (method, ...params) {
517
522
  let config = this.infos[i];
518
523
  let mod = this.getMod(config.name);
519
524
  if (mod && mod.config?.state === 1) {
520
- var ret = await this._runSub(mod, method, ...params);
525
+ var ret = await this._run(mod, method, ...params);
521
526
  if (ret !== null && ret !== undefined) {
522
527
  result = ret;
523
528
  // 如果结果不为空且有结束标志,则停止迭代
@@ -530,49 +535,6 @@ Manager.prototype._runAllEnabled = async function (method, ...params) {
530
535
  return result;
531
536
  };
532
537
 
533
- /**
534
- * 执行方法
535
- * @param {string} name 模块名称
536
- * @param {string} method 方法名称
537
- * @param {...any} params 方法参数集合
538
- * @returns {Promise<any>} 执行结果
539
- */
540
- Manager.prototype.runAsync = async function (name, method, ...params) {
541
- // 使用mods的顺序(按配置顺序调用)
542
- var result = null;
543
- if (name) {
544
- let mod = await this.getMod(name);
545
- if (mod) {
546
- result = await this._runSub(mod, method, ...params);
547
- }
548
- } else if (name === null) {
549
- result = await this._runAll(method, ...params);
550
- }
551
- return result;
552
- };
553
-
554
- /**
555
- * 私有方法:执行所有模块方法
556
- * @param {string} method 方法名称
557
- * @param {...any} params 方法参数集合
558
- * @returns {Promise<any>} 执行结果
559
- * @private
560
- */
561
- Manager.prototype._runAll = async function (method, ...params) {
562
- var result = null;
563
- for (let i = 0; i < this.infos.length; i++) {
564
- let config = this.infos[i];
565
- let mod = this.getMod(config.name);
566
- if (mod) {
567
- var ret = await this._runSub(mod, method, ...params);
568
- if (ret !== null && ret !== undefined) {
569
- result = ret;
570
- }
571
- }
572
- }
573
- return result;
574
- };
575
-
576
538
  /**
577
539
  * 私有方法:执行模块方法
578
540
  * @param {object} mod 模块对象
@@ -581,7 +543,7 @@ Manager.prototype._runAll = async function (method, ...params) {
581
543
  * @returns {Promise<any>} 执行结果
582
544
  * @private
583
545
  */
584
- Manager.prototype._runSub = async function (mod, method, ...params) {
546
+ Manager.prototype._run = async function (mod, method, ...params) {
585
547
  if (!mod || !method) return null;
586
548
 
587
549
  try {
@@ -614,25 +576,270 @@ Manager.prototype._initManager = function () {
614
576
  /**
615
577
  * 加载资源
616
578
  */
617
- Manager.prototype._loadSources = function () {
579
+ Manager.prototype._loadSources = async function () {
580
+ // 通过更新函数加载资源信息
581
+ await this.call('update');
618
582
  };
619
583
 
620
584
  /**
621
585
  * 初始化资源
622
586
  */
623
- Manager.prototype._initSources = function () {
587
+ Manager.prototype._initSources = async function () {
588
+ // 通过
624
589
  };
625
590
 
626
591
  /**
627
592
  * 初始化公共属性
628
593
  */
629
- Manager.prototype._initCore = function () {
594
+ Manager.prototype._initCore = async function () {
630
595
  // 初始化管理器
631
596
  this._initManager();
632
597
  // 加载资源
633
- this._loadSources();
598
+ await this._loadSources();
634
599
  // 初始化AI客户端
635
- this._initSources();
600
+ await this._initSources();
601
+ };
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;
636
843
  };
637
844
 
638
845
  module.exports = {
package/mod.js CHANGED
@@ -238,11 +238,19 @@ Mod.prototype._onDestroy = async function (ctx) {
238
238
  });
239
239
  };
240
240
 
241
+ /**
242
+ * 加载 - 内部方法
243
+ * @private
244
+ */
245
+ Mod.prototype._load = async function () {
246
+ return this;
247
+ };
248
+
241
249
  /**
242
250
  * 加载
243
251
  */
244
252
  Mod.prototype.load = async function () {
245
-
253
+ return await this._load();
246
254
  };
247
255
 
248
256
  /**
@@ -253,38 +261,65 @@ Mod.prototype._loadCore = async function () {
253
261
  };
254
262
 
255
263
  /**
256
- * 启动
264
+ * 启动 - 内部方法
265
+ * @private
266
+ * @param {...any} args 初始化参数
257
267
  */
258
- Mod.prototype.start = async function () {
268
+ Mod.prototype._start = async function (...args) {
269
+ return this;
270
+ };
259
271
 
272
+ /**
273
+ * 启动
274
+ */
275
+ Mod.prototype.start = async function (...args) {
276
+ return await this._start(...args);
260
277
  };
261
278
 
262
279
  /**
263
280
  * 启动核心
281
+ * @private
264
282
  */
265
283
  Mod.prototype._startCore = async function () {
266
284
 
267
285
  };
268
286
 
287
+ /**
288
+ * 停止 - 内部方法
289
+ * @private
290
+ */
291
+ Mod.prototype._stop = async function () {
292
+ return this;
293
+ };
294
+
269
295
  /**
270
296
  * 停止
271
297
  */
272
298
  Mod.prototype.stop = async function () {
273
-
299
+ return await this._stop();
274
300
  };
275
301
 
276
302
  /**
277
303
  * 停止核心
304
+ * @private
278
305
  */
279
306
  Mod.prototype._stopCore = async function () {
280
307
 
281
308
  };
282
309
 
310
+ /**
311
+ * 卸载 - 内部方法
312
+ * @private
313
+ */
314
+ Mod.prototype._unload = async function () {
315
+ return this;
316
+ };
317
+
283
318
  /**
284
319
  * 卸载
285
320
  */
286
321
  Mod.prototype.unload = async function () {
287
-
322
+ return await this._unload();
288
323
  };
289
324
 
290
325
  /**
@@ -330,21 +365,28 @@ Mod.prototype.end = async function () {
330
365
  };
331
366
 
332
367
  /**
333
- * 触发事件
334
- * @param {string} event 事件名
335
- * @param {...any} args 事件参数
368
+ * 获取事件管理器
369
+ * @returns {Eventer|null} 事件管理器
336
370
  */
337
- Mod.prototype.emitEvent = function (event, ...args) {
338
- this._eventer?.emit(event, ...args);
371
+ Mod.prototype.getEventer = function () {
372
+ return this._eventer || $.eventer;
339
373
  };
340
374
 
341
375
  /**
342
- * 运行方法
343
- * @param {...any} args 方法参数
344
- * @returns {Promise<any>} 方法执行结果
376
+ * 设置事件管理器
377
+ * @param {Eventer} eventer 事件管理器
345
378
  */
346
- Mod.prototype.run = async function (...args) {
347
- return this.main(...args);
379
+ Mod.prototype.setEventer = function (eventer) {
380
+ this._eventer = eventer;
381
+ };
382
+
383
+ /**
384
+ * 触发事件
385
+ * @param {string} event 事件名
386
+ * @param {...any} args 事件参数
387
+ */
388
+ Mod.prototype.emitEvent = function (event, ...args) {
389
+ this.getEventer()?.emit(event, ...args);
348
390
  };
349
391
 
350
392
  /**
@@ -453,7 +495,7 @@ Mod.prototype._callBefore = async function (method, params) {
453
495
  * @returns {Promise<any>} 主方法结果
454
496
  */
455
497
  Mod.prototype._callMain = async function (method_func, params, result) {
456
- let ret = await method_func.call(this._mod || this, ...params);
498
+ let ret = await method_func.call(this, ...params);
457
499
  // 修复:如果主方法返回undefined,则使用前置钩子的结果
458
500
  return ret !== undefined ? ret : result;
459
501
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "2.4.3",
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": {