mm_machine 3.1.1 → 3.1.2

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 CHANGED
@@ -15,12 +15,10 @@ class Index {
15
15
  * @param {object} parent 父对象
16
16
  */
17
17
  constructor(config, parent) {
18
- this.config = Object.assign(
19
- {
20
- name: 'default'
21
- },
22
- config
23
- );
18
+ this.config = {
19
+ name: 'default',
20
+ ...config
21
+ };
24
22
 
25
23
  if (parent) {
26
24
  this.getParent = function () {
@@ -53,8 +51,8 @@ Index.prototype.load = function () {
53
51
  * @param {string} name 模块名
54
52
  * @returns {object|null} 模块实例
55
53
  */
56
- Index.prototype.getModule = function (name) {
57
- return $.registry.getModule(name);
54
+ Index.prototype.getMod = function (name) {
55
+ return $.registry.getMod(name);
58
56
  };
59
57
 
60
58
  /**
@@ -62,8 +60,8 @@ Index.prototype.getModule = function (name) {
62
60
  * @param {string} class_name 模块类名
63
61
  * @returns {Array} 模块实例数组
64
62
  */
65
- Index.prototype.getModules = function (class_name) {
66
- return $.registry.getModules(class_name);
63
+ Index.prototype.getMods = function (class_name) {
64
+ return $.registry.getMods(class_name);
67
65
  };
68
66
 
69
67
  module.exports = {
package/manager.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const { Mod } = require('./mod');
2
2
  const { Drive } = require('./drive.js');
3
3
  const { Scanner } = require('./scanner.js');
4
+ const { Registry } = require('./registry.js');
4
5
 
5
6
  /**
6
7
  * 管理器类
@@ -110,6 +111,8 @@ class Manager extends Mod {
110
111
  this.scanner = new Scanner();
111
112
  // 模块
112
113
  this.mods = mods;
114
+ // 不使用注册表时,用内部缓存记录模块条目信息
115
+ this.infos = [];
113
116
  if (Drive) {
114
117
  this.Drive = Drive;
115
118
  }
@@ -133,7 +136,7 @@ Manager.prototype.getParentKey = function () {
133
136
  * @returns {object[]} 模块配置数组
134
137
  */
135
138
  Manager.prototype.getInfos = function () {
136
- return this._getSortedEntries();
139
+ return this._getInfos();
137
140
  };
138
141
 
139
142
  /**
@@ -142,11 +145,11 @@ Manager.prototype.getInfos = function () {
142
145
  * @returns {object[]} 模块对象数组
143
146
  */
144
147
  Manager.prototype.getList = function (keyword) {
145
- var entries = this._getSortedEntries();
148
+ let entries = this._getInfos();
146
149
 
147
150
  // 先用 entry 做关键词过滤,避免无效实例化
148
151
  if (keyword) {
149
- entries = entries.filter(function (o) {
152
+ entries = entries.filter((o) => {
150
153
  return o.name.includes(keyword) || o.title.includes(keyword) || o.description.includes(keyword);
151
154
  });
152
155
  }
@@ -189,11 +192,15 @@ Manager.prototype.updateInfo = function () {
189
192
  };
190
193
 
191
194
  /**
192
- * 从 registry 获取按排序后的模块配置列表
195
+ * 获取模块配置列表
196
+ * 启用注册表时从 registry 获取;否则从内部缓存 infos 读取
193
197
  * @returns {Array} 排序后的模块配置数组
194
198
  */
195
- Manager.prototype._getSortedEntries = function () {
196
- return $.registry.getMods(this.getParentKey());
199
+ Manager.prototype._getInfos = function () {
200
+ if (this.config.use_registry) {
201
+ return $.registry.getMods(this.getParentKey());
202
+ }
203
+ return this.infos || [];
197
204
  };
198
205
 
199
206
  // ==== 模块管理接口 ====
@@ -204,6 +211,10 @@ Manager.prototype.clearMods = function () {
204
211
  for (let name in this.mods) {
205
212
  delete this.mods[name];
206
213
  }
214
+ // 同步清理内部缓存
215
+ if (!this.config.use_registry && this.infos) {
216
+ this.infos = [];
217
+ }
207
218
  };
208
219
 
209
220
  /**
@@ -215,6 +226,10 @@ Manager.prototype.delMod = function (name) {
215
226
  let mod = this.mods[name];
216
227
  if (mod) {
217
228
  delete this.mods[name];
229
+ // 同步清理内部缓存
230
+ if (!this.config.use_registry && this.infos) {
231
+ this.infos = this.infos.filter((o) => { return o.name !== name; });
232
+ }
218
233
  }
219
234
  return mod;
220
235
  };
@@ -240,13 +255,13 @@ Manager.prototype.getMod = function (name) {
240
255
  var mod = this.mods[name];
241
256
  if (mod) return mod;
242
257
 
243
- // 注册表回退:通过 _getSortedEntries 查找配置并按需实例化
244
- if (this.config.use_registry && $.registry) {
245
- var entries = this._getSortedEntries();
258
+ // 注册表回退:通过 _getInfos 查找配置并按需实例化
259
+ if (this.config.use_registry) {
260
+ var entries = this._getInfos();
246
261
  for (var i = 0; i < entries.length; i++) {
247
262
  if (entries[i].name === name) {
248
263
  var entry = entries[i];
249
- var ModClass = this.config.mod_type === 'script' ? Mod : this.Drive;
264
+ const ModClass = this.config.mod_type === 'script' ? Mod : this.Drive;
250
265
  mod = new ModClass(entry, this);
251
266
  mod.mode = this.config.mode;
252
267
  mod.config_file = entry.config_file;
@@ -324,41 +339,16 @@ Manager.prototype.newMod = function (config_file, config, script) {
324
339
  * @param {object} script 模块对象
325
340
  * @returns {object} 模块对象
326
341
  */
327
- Manager.prototype.registerMod = function (config_file, config = {}, script = {}) {
342
+ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
328
343
  let cg = { ...config };
329
- // 未传 config 时尝试从 registry 缓存获取
330
- if (!cg || !cg.name) {
331
- var cached = this._getRegistryConfig(config_file);
332
- if (cached) {
333
- cg = cached;
334
- }
335
- }
336
-
337
- // 处理数组配置:多模块共用一个配置文件(如 event.json 包含多个事件配置)
338
- if (!cg || !cg.name) {
339
- try {
340
- var text = config_file.loadText();
341
- if (text && text.trim().startsWith('[')) {
342
- var arr = $.loadJson(config_file);
343
- if (Array.isArray(arr)) {
344
- for (var i = 0; i < arr.length; i++) {
345
- this.registerMod(config_file, arr[i], script);
346
- }
347
- return;
348
- }
349
- }
350
- } catch (e) {
351
- // 解析失败,交给 newMod 处理(非数组格式或文件不存在)
352
- }
353
- }
354
344
 
355
345
  var mod = this.newMod(config_file, cg, script);
356
346
  if (!mod) return null;
357
347
  this.setMod(mod.config.name, mod);
358
348
 
359
349
  // 启用注册表时,自动写入 $.registry 供后续实例复用
360
- if (this.config.use_registry && $.registry) {
361
- // 直接通过 registry 的 getMod 判断是否已存在,避免 _getSortedEntries 重复
350
+ if (this.config.use_registry) {
351
+ // 直接通过 registry 的 getMod 判断是否已存在,避免 _getInfos 重复
362
352
  if (!$.registry.getMod(this.getParentKey(), mod.config.name)) {
363
353
  var entry = {
364
354
  name: mod.config.name,
@@ -381,6 +371,19 @@ Manager.prototype.registerMod = function (config_file, config = {}, script = {})
381
371
  // 同步 registry 的内部缓存,确保 Index.getMod() 可查
382
372
  $.registry.setMod(this.getParentKey(), mod.config.name, entry);
383
373
  }
374
+ } else {
375
+ // 不使用注册表时,写入内部缓存
376
+ var entry = {
377
+ name: mod.config.name,
378
+ title: mod.config.title,
379
+ description: mod.config.description,
380
+ sort: mod.config.sort,
381
+ state: mod.config.state,
382
+ config_file: config_file,
383
+ main: mod.config.main || '',
384
+ class_name: this.config.class_name || 'Drive'
385
+ };
386
+ this.infos.push(entry);
384
387
  }
385
388
 
386
389
  return mod;
@@ -430,7 +433,7 @@ Manager.prototype.reloadMod = async function (name) {
430
433
  * @param {string} name 模块名
431
434
  * @returns {Promise<any>} 注销结果
432
435
  */
433
- Manager.prototype.unregisterMod = async function (name) {
436
+ Manager.prototype.unregMod = async function (name) {
434
437
  let mod = this.getMod(name);
435
438
  if (mod) {
436
439
  // 按生命周期顺序停止→卸载,避免状态机拦截
@@ -447,9 +450,9 @@ Manager.prototype.unregisterMod = async function (name) {
447
450
  * @param {object} config 配置项
448
451
  * @param {object} script 模块对象
449
452
  */
450
- Manager.prototype.registerMods = function (files, config = {}, script = {}) {
453
+ Manager.prototype.regMods = function (files, config = {}, script = {}) {
451
454
  for (let file of files) {
452
- this.registerMod(file, config, script);
455
+ this.regMod(file, config, script);
453
456
  }
454
457
  };
455
458
 
@@ -512,7 +515,7 @@ Manager.prototype.update = async function (dir, clear = true) {
512
515
 
513
516
  var search_dir = dir || this.config.dir;
514
517
  var files = await this._findFiles(search_dir);
515
- this.registerMods(files);
518
+ this.regMods(files);
516
519
 
517
520
  if (!this.config.lazy_load) {
518
521
  await this.loadScripts();
@@ -523,42 +526,16 @@ Manager.prototype.update = async function (dir, clear = true) {
523
526
  }
524
527
  };
525
528
 
526
- /**
527
- * 从 registry 缓存中查找匹配配置文件的模块配置
528
- * 使用 Map 索引避免每次 O(n) 扫描
529
- * @param {string} config_file 配置文件路径
530
- * @returns {object|null} 模块配置项
531
- */
532
- Manager.prototype._getRegistryConfig = function (config_file) {
533
- var registry = $.registry;
534
- if (!registry || !registry.module_list) return null;
535
-
536
- // 懒构建 config_file → config 的 Map 索引
537
- if (!registry._config_map) {
538
- registry._config_map = {};
539
- var list = registry.module_list;
540
- for (var i = 0; i < list.length; i++) {
541
- var entry = list[i];
542
- registry._config_map[entry.config_file] = entry;
543
- }
544
- }
545
- return registry._config_map[config_file] || null;
546
- };
547
-
548
529
  /**
549
530
  * 从 registry 缓存加载模块(按 parent 字段匹配)
550
531
  * 从 registry.mod_dict 中按 parent 过滤并创建模块实例
551
532
  * @returns {boolean} 是否加载到模块
552
533
  */
553
534
  Manager.prototype._loadFromRegistry = function () {
554
- var registry = $.registry;
555
- if (!registry || !registry.mod_dict) return false;
556
-
557
- var parent = this.getParentKey();
558
- var lt = registry.getMods(parent);
535
+ let lt = this._getInfos();
559
536
  if (!lt.length) return false;
560
537
 
561
- var ModClass = this.config.mod_type === 'script' ? Mod : this.Drive;
538
+ var mod_class = this.config.mod_type === 'script' ? Mod : this.Drive;
562
539
  var mode = this.config.mode;
563
540
  var mods = this.mods;
564
541
 
@@ -612,12 +589,12 @@ Manager.prototype.main = async function (name, method, ...params) {
612
589
  var result = null;
613
590
  if (name) {
614
591
  var mod = this.mods[name];
615
- // 注册表模式下一次遍历查 entry,避免 getMod 重复 _getSortedEntries
616
- if (!mod && this.config.use_registry && $.registry) {
617
- var entries = this._getSortedEntries();
618
- var entry = entries.find(function (o) { return o.name === name; });
592
+ // 注册表模式下一次遍历查 entry,避免 getMod 重复 _getInfos
593
+ if (!mod && this.config.use_registry) {
594
+ var entries = this._getInfos();
595
+ var entry = entries.find((o) => { return o.name === name; });
619
596
  if (entry && entry.state === 1) {
620
- var ModClass = this.config.mod_type === 'script' ? Mod : this.Drive;
597
+ var mod_class = this.config.mod_type === 'script' ? Mod : this.Drive;
621
598
  mod = new ModClass(entry, this);
622
599
  mod.mode = this.config.mode;
623
600
  mod.config_file = entry.config_file;
@@ -643,7 +620,7 @@ Manager.prototype.main = async function (name, method, ...params) {
643
620
  */
644
621
  Manager.prototype._runAllEnabled = async function (method, ...params) {
645
622
  var result;
646
- var entries = this._getSortedEntries();
623
+ var entries = this._getInfos();
647
624
  for (var i = 0; i < entries.length; i++) {
648
625
  var entry = entries[i];
649
626
  // 直接用 entry.state 过滤,避免无效实例化
@@ -725,7 +702,7 @@ Manager.prototype.getDir = function () {
725
702
  Manager.prototype._loadSources = async function () {
726
703
  // 启用注册表且有缓存数据时,走内存快路径
727
704
  // 按管理器自身 name 过滤,多 Manager 共享 $.registry 时互不污染
728
- if (this.config.use_registry && $.registry && $.registry.mod_dict) {
705
+ if (this.config.use_registry) {
729
706
  // 只有从注册表成功加载到模块时才跳过文件系统扫描,否则回退到文件系统
730
707
  if (this._loadFromRegistry()) {
731
708
  // 后台异步扫描文件系统,确保物理最新
@@ -751,7 +728,7 @@ Manager.prototype._scanForUpdate = async function () {
751
728
  } else {
752
729
  await this.call('update', dir, true);
753
730
  }
754
- if (this.config.use_registry && $.registry) {
731
+ if (this.config.use_registry) {
755
732
  $.registry.save();
756
733
  }
757
734
  } catch (err) {
@@ -843,7 +820,7 @@ Manager.prototype._ensureModLoaded = async function (name) {
843
820
  */
844
821
  Manager.prototype._ensureAllLoaded = async function () {
845
822
  var promises = [];
846
- var entries = this._getSortedEntries();
823
+ var entries = this._getInfos();
847
824
 
848
825
  for (var i = 0; i < entries.length; i++) {
849
826
  promises.push(this._ensureModLoaded(entries[i].name));
@@ -863,7 +840,7 @@ Manager.prototype.runWait = async function (method, ...params) {
863
840
  }
864
841
 
865
842
  let result;
866
- var entries = this._getSortedEntries();
843
+ var entries = this._getInfos();
867
844
 
868
845
  for (var i = 0; i < entries.length; i++) {
869
846
  let mod = await this._ensureModLoaded(entries[i].name);
@@ -900,7 +877,7 @@ Manager.prototype.runAsync = async function (method, ...params) {
900
877
  // 确保所有模块都已加载
901
878
  await this._ensureAllLoaded();
902
879
 
903
- var entries = this._getSortedEntries();
880
+ var entries = this._getInfos();
904
881
 
905
882
  for (var i = 0; i < entries.length; i++) {
906
883
  var mod = this.mods[entries[i].name];
@@ -933,7 +910,7 @@ Manager.prototype.runAll = async function (method, ...params) {
933
910
  await this._ensureAllLoaded();
934
911
 
935
912
  const promises = [];
936
- var entries = this._getSortedEntries();
913
+ var entries = this._getInfos();
937
914
 
938
915
  for (var i = 0; i < entries.length; i++) {
939
916
  var mod = this.mods[entries[i].name];
@@ -960,7 +937,7 @@ Manager.prototype.runRace = async function (method, ...params) {
960
937
  await this._ensureAllLoaded();
961
938
 
962
939
  const promises = [];
963
- var entries = this._getSortedEntries();
940
+ var entries = this._getInfos();
964
941
 
965
942
  for (var i = 0; i < entries.length; i++) {
966
943
  var mod = this.mods[entries[i].name];
@@ -988,7 +965,7 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
988
965
  throw new TypeError('方法名称必须是字符串');
989
966
  }
990
967
 
991
- var entries = this._getSortedEntries();
968
+ var entries = this._getInfos();
992
969
 
993
970
  for (var i = 0; i < entries.length; i++) {
994
971
  var mod = await this._ensureModLoaded(entries[i].name);
@@ -1078,7 +1055,7 @@ Manager.prototype.createFile = function (config, file) {
1078
1055
  */
1079
1056
  Manager.prototype.create = function (config, file = '') {
1080
1057
  let f = this.createFile(config, file);
1081
- let mod = this.registerMod(f, config);
1058
+ let mod = this.regMod(f, config);
1082
1059
  return mod;
1083
1060
  };
1084
1061
 
@@ -1092,7 +1069,7 @@ Manager.prototype.remove = async function (name) {
1092
1069
  if (mod) {
1093
1070
  await mod.remove();
1094
1071
  }
1095
- await this.unregisterMod(name);
1072
+ await this.unregMod(name);
1096
1073
  return mod;
1097
1074
  };
1098
1075
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.1.1",
3
+ "version": "3.1.2",
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": {
package/registry.js CHANGED
@@ -1,3 +1,5 @@
1
+ require('mm_expand');
2
+
1
3
  /**
2
4
  * 注册类 - 注册表模式
3
5
  * 管理模块类的注册、模块配置的持久化、模块实例的懒加载
@@ -425,6 +427,11 @@ Registry.prototype.sort = function () {
425
427
  }
426
428
  };
427
429
 
430
+
431
+ if (!$.registry) {
432
+ $.registry = new Registry({ name: 'default' });
433
+ }
434
+
428
435
  module.exports = {
429
436
  Registry
430
437
  };