mm_machine 3.0.8 → 3.0.9

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/drive.js CHANGED
@@ -566,7 +566,7 @@ Drive.prototype._reloadIfNeeded = function () {
566
566
  Drive.prototype.delDir = function () {
567
567
  let main = this.config.main;
568
568
  if (main && $.dir && $.dir.del) {
569
- $.dir.del(this.dir);
569
+ $.dir.del(this.getDir());
570
570
  }
571
571
  };
572
572
 
@@ -696,7 +696,7 @@ Drive.prototype.save = function () {
696
696
  let config = text.toJson();
697
697
  if (Array.isArray(config)) {
698
698
  // 查找并更新现有配置项
699
- let index = config.findIndex((Drive) => Drive.name === this.config.name);
699
+ let index = config.findIndex((item) => item.name === this.config.name);
700
700
  if (index !== -1) {
701
701
  config[index] = this.config;
702
702
  } else {
package/manager.js CHANGED
@@ -389,6 +389,7 @@ Manager.prototype.registerMod = function (config_file, config = {}, script = {})
389
389
  }
390
390
 
391
391
  var mod = this.newMod(config_file, cg, script);
392
+ if (!mod) return null;
392
393
  this.setMod(mod.config.name, mod);
393
394
 
394
395
  // 启用注册表时,自动写入 $.registry 供后续实例复用
@@ -622,7 +623,10 @@ Manager.prototype._loadFromRegistry = function (parent, dir) {
622
623
  sort: config.sort || 100,
623
624
  state: config.state,
624
625
  enabled: true,
625
- file: entry.config_file || ''
626
+ file: entry.config_file || '',
627
+ get available() {
628
+ return this.state == true && this.enabled;
629
+ }
626
630
  });
627
631
  count++;
628
632
  }
@@ -755,6 +759,7 @@ Manager.prototype._initManager = function () {};
755
759
  * @returns {string} 二次开发模块目录
756
760
  */
757
761
  Manager.prototype.getDir = function () {
762
+ if (!this.getParent) return this.config.dir || '';
758
763
  return this.config.dir || this.getParent().getDir();
759
764
  };
760
765
 
@@ -775,7 +780,6 @@ Manager.prototype._loadSources = async function () {
775
780
  $.registry.module_list.length > 0
776
781
  ) {
777
782
  // 只有从注册表成功加载到模块时才跳过文件系统扫描,否则回退到文件系统
778
- var t0 = Date.now();
779
783
  if (this._loadFromRegistry(this.config.name, this.config.dir)) {
780
784
  this.updateInfo();
781
785
  return;
@@ -783,7 +787,6 @@ Manager.prototype._loadSources = async function () {
783
787
  }
784
788
 
785
789
  // 无注册表缓存或注册表无匹配模块,走文件系统扫描
786
- var t1 = Date.now();
787
790
  if (base_dir) {
788
791
  // base_dir 和 dir 并行扫描,互不依赖
789
792
  await Promise.all([this.call('update', base_dir, true), this.call('update', dir, false)]);
@@ -1125,7 +1128,7 @@ Manager.prototype.remove = async function (name) {
1125
1128
  if (mod) {
1126
1129
  await mod.remove();
1127
1130
  }
1128
- this.unregisterMod(name);
1131
+ await this.unregisterMod(name);
1129
1132
  return mod;
1130
1133
  };
1131
1134
 
package/mod.js CHANGED
@@ -367,8 +367,9 @@ Mod.prototype._unloadCore = async function () {
367
367
  */
368
368
  Mod.prototype.reload = async function () {
369
369
  try {
370
- await this.do('unload');
371
- await this.do('load');
370
+ this._unloadCore();
371
+ this._is_loaded = false;
372
+ this._script_keys = null;
372
373
  } catch (err) {
373
374
  this.log('error', '重载失败', err);
374
375
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.0.8",
3
+ "version": "3.0.9",
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
@@ -252,6 +252,12 @@ Registry.prototype.set = function (config) {
252
252
  if (!has) {
253
253
  this.module_list.push(cg);
254
254
  }
255
+ // 同步更新 _module_cache,避免 get() 读到脏数据
256
+ if (this._module_cache[cg.name]) {
257
+ this._module_cache[cg.name].config = cg;
258
+ } else {
259
+ this.registerModule(cg);
260
+ }
255
261
  // 清除外部缓存索引
256
262
  this._config_map = null;
257
263
  return cg;
@@ -288,6 +294,18 @@ Registry.prototype.del = function (name) {
288
294
  break;
289
295
  }
290
296
  }
297
+ // 同步清理内存缓存,避免 get() 读到已删除数据
298
+ if (obj) {
299
+ var class_name = obj.class_name || 'Drive';
300
+ var names = this._class_index[class_name];
301
+ if (names) {
302
+ var idx = names.indexOf(name);
303
+ if (idx !== -1) {
304
+ names.splice(idx, 1);
305
+ }
306
+ }
307
+ delete this._module_cache[name];
308
+ }
291
309
  // 清除外部缓存索引
292
310
  this._config_map = null;
293
311
  return obj;
@@ -337,7 +355,8 @@ Registry.prototype.checkConfig = function (config) {
337
355
  if (!config_file) {
338
356
  throw new TypeError('模块配置文件不能为空');
339
357
  }
340
- if (!['true', 'false', 'auto'].includes(auto_start)) {
358
+ var valid_auto = ['true', 'false', 'auto'];
359
+ if (valid_auto.indexOf(auto_start) === -1 && ![true, false].includes(auto_start)) {
341
360
  throw new TypeError('模块自动启动配置错误,只能为true、false、auto');
342
361
  }
343
362
  if (!this.class_dict[class_name]) {