mm_machine 3.0.5 → 3.0.7

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/manager.js +37 -10
  2. package/package.json +1 -1
package/manager.js CHANGED
@@ -370,6 +370,24 @@ Manager.prototype.registerMod = function (config_file, config = {}, script = {})
370
370
  }
371
371
  }
372
372
 
373
+ // 处理数组配置:多模块共用一个配置文件(如 event.json 包含多个事件配置)
374
+ if (!cg || !cg.name) {
375
+ try {
376
+ var text = config_file.loadText();
377
+ if (text && text.trim().startsWith('[')) {
378
+ var arr = $.loadJson(config_file);
379
+ if (Array.isArray(arr)) {
380
+ for (var i = 0; i < arr.length; i++) {
381
+ this.registerMod(config_file, arr[i], script);
382
+ }
383
+ return;
384
+ }
385
+ }
386
+ } catch (e) {
387
+ // 解析失败,交给 newMod 处理(非数组格式或文件不存在)
388
+ }
389
+ }
390
+
373
391
  var mod = this.newMod(config_file, cg, script);
374
392
  this.setMod(mod.config.name, mod);
375
393
 
@@ -392,7 +410,8 @@ Manager.prototype.registerMod = function (config_file, config = {}, script = {})
392
410
  main: mod.config.main || '',
393
411
  scope: mod.config.scope || '',
394
412
  class_name: mod.config.class_name || 'Drive',
395
- parent: parent_name
413
+ parent: parent_name,
414
+ dir: this.config.dir || ''
396
415
  };
397
416
  list.push(entry);
398
417
  // 同步 registry 的内部缓存,确保 Index.getModule() 可查
@@ -565,12 +584,13 @@ Manager.prototype._getRegistryConfig = function (config_file) {
565
584
  };
566
585
 
567
586
  /**
568
- * 从 registry 缓存加载模块(按 parent 字段匹配)
587
+ * 从 registry 缓存加载模块(按 parent 和 dir 字段匹配)
569
588
  * 内联创建模块并构建 infos,避免 registerMod/_getRegistryConfig/updateInfo 的冗余循环
570
589
  * @param {string} parent 父级标识,空或不传时加载所有模块
590
+ * @param {string} dir 目录路径,用于区分不同目录下的同名管理器(如 agent 的 user/contact/record)
571
591
  * @returns {boolean} 是否加载到模块
572
592
  */
573
- Manager.prototype._loadFromRegistry = function (parent) {
593
+ Manager.prototype._loadFromRegistry = function (parent, dir) {
574
594
  var registry = $.registry;
575
595
  if (!registry || !registry.module_list || !registry.module_list.length) return false;
576
596
 
@@ -586,6 +606,8 @@ Manager.prototype._loadFromRegistry = function (parent) {
586
606
  var entry = list[i];
587
607
  // 按 parent 字段匹配,不传或空时加载所有
588
608
  if (parent && entry.parent !== parent) continue;
609
+ // 按 dir 字段匹配,确保不同目录的管理器互不干扰
610
+ if (dir && entry.dir !== dir) continue;
589
611
 
590
612
  var config = entry;
591
613
  var mod = new ModClass(config, this);
@@ -753,19 +775,25 @@ Manager.prototype._loadSources = async function () {
753
775
  $.registry.module_list.length > 0
754
776
  ) {
755
777
  // 只有从注册表成功加载到模块时才跳过文件系统扫描,否则回退到文件系统
756
- if (this._loadFromRegistry(this.config.name)) {
778
+ var t0 = Date.now();
779
+ if (this._loadFromRegistry(this.config.name, this.config.dir)) {
757
780
  this.updateInfo();
758
781
  return;
759
782
  }
760
783
  }
761
784
 
762
785
  // 无注册表缓存或注册表无匹配模块,走文件系统扫描
786
+ var t1 = Date.now();
763
787
  if (base_dir) {
764
788
  // base_dir 和 dir 并行扫描,互不依赖
765
789
  await Promise.all([this.call('update', base_dir, true), this.call('update', dir, false)]);
766
790
  } else {
767
791
  await this.call('update', dir, true);
768
792
  }
793
+ // 文件扫描完成后持久化注册表,确保后续启动可从注册表加载跳过文件扫描
794
+ if ($.registry) {
795
+ $.registry.save();
796
+ }
769
797
  };
770
798
 
771
799
  /**
@@ -856,7 +884,10 @@ Manager.prototype._ensureAllLoaded = async function () {
856
884
 
857
885
  for (var i = 0; i < infos.length; i++) {
858
886
  var info = infos[i];
859
- promises.push(this._ensureModLoaded(info.name));
887
+ var t = Date.now();
888
+ promises.push(
889
+ this._ensureModLoaded(info.name)
890
+ );
860
891
  }
861
892
  await Promise.all(promises);
862
893
  };
@@ -944,11 +975,7 @@ Manager.prototype.runAll = async function (method, ...params) {
944
975
  let mod = this.getMod(info.name);
945
976
  if (!mod) continue;
946
977
 
947
- promises.push(
948
- mod.do(method, ...params).catch(() => {
949
- return null;
950
- })
951
- );
978
+ promises.push(mod.do(method, ...params));
952
979
  }
953
980
 
954
981
  return await Promise.all(promises);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.0.5",
3
+ "version": "3.0.7",
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": {