mm_machine 3.2.3 → 3.2.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 +15 -28
  2. package/manager.js +53 -36
  3. package/mod.js +57 -16
  4. package/package.json +1 -1
package/drive.js CHANGED
@@ -37,7 +37,13 @@ class Drive extends Mod {
37
37
  // 是否结束,可选,默认false
38
38
  end: false
39
39
  };
40
+ // 当前配置文件路径
41
+ config_file = '';
40
42
 
43
+ // 当前脚本文件路径
44
+ script_file = '';
45
+ // 目录缓存
46
+ _dir = undefined;
41
47
  /**
42
48
  * 构造函数
43
49
  * @param {object} config 配置项
@@ -46,12 +52,6 @@ class Drive extends Mod {
46
52
  */
47
53
  constructor(config, parent) {
48
54
  super({ ...Drive.config, ...config }, parent);
49
- // 当前配置文件路径
50
- this.config_file = '';
51
-
52
- // 当前脚本文件路径
53
- this.script_file = '';
54
-
55
55
  // 模式: 1.生产模式 2.热更新模式 3.热重载模式 4.热更新+重载模式 5.重载模式
56
56
  this.mode = 1;
57
57
  }
@@ -116,7 +116,7 @@ Drive.prototype.setConfig = function (config) {
116
116
  if (config) {
117
117
  // 如果main改变,重置加载状态
118
118
  if (config.main !== this.config.main) {
119
- this._is_loaded = false;
119
+ this.isLoaded(false);
120
120
  }
121
121
 
122
122
  if (this.config_file) {
@@ -132,6 +132,10 @@ Drive.prototype.setConfig = function (config) {
132
132
  Object.assign(this.config, config);
133
133
  }
134
134
  }
135
+ let file = this.config.main;
136
+ if (file) {
137
+ this.script_file = file.fullname(this.getDir());
138
+ }
135
139
  this._preset();
136
140
  return this.config;
137
141
  };
@@ -203,8 +207,7 @@ Drive.prototype._remove = function () {
203
207
  */
204
208
  Drive.prototype.unloadScript = function () {
205
209
  this._remove();
206
- this._script_keys = null;
207
- this._is_loaded = false;
210
+ this.isLoaded(false);
208
211
  };
209
212
 
210
213
  /**
@@ -348,22 +351,6 @@ Drive.prototype._setMainMethod = function (mod) {
348
351
  if (name && typeof mod[name] === 'function') {
349
352
  this.main = mod[name];
350
353
  }
351
- var new_keys = Object.keys(mod);
352
- // 热重载时如果脚本未变化则跳过重建
353
- if (this._script_keys && this._script_keys.length === new_keys.length) {
354
- var same = true;
355
- for (var i = 0; i < new_keys.length; i++) {
356
- if (this._script_keys[i] !== new_keys[i]) {
357
- same = false;
358
- break;
359
- }
360
- }
361
- if (same) {
362
- this._is_loaded = true;
363
- return;
364
- }
365
- }
366
- this._script_keys = new_keys;
367
354
  for (var key in mod) {
368
355
  var obj = mod[key];
369
356
  if (typeof obj === 'function') {
@@ -372,7 +359,7 @@ Drive.prototype._setMainMethod = function (mod) {
372
359
  this.setData(key, obj);
373
360
  }
374
361
  }
375
- this._is_loaded = true;
362
+ this.isLoaded(true);
376
363
  };
377
364
 
378
365
  /**
@@ -665,11 +652,11 @@ Drive.prototype.loadBefore = function () {
665
652
  try {
666
653
  let mod = this.loadScript();
667
654
  if (mod) {
668
- this._is_loaded = true;
655
+ this.isLoaded(true);
669
656
  }
670
657
  } catch (err) {
671
658
  this.log('error', `加载前处理失败`, this._wrapError(err));
672
- this._is_loaded = false;
659
+ this.isLoaded(false);
673
660
  }
674
661
  };
675
662
 
package/manager.js CHANGED
@@ -144,18 +144,18 @@ Manager.prototype.getInfos = function () {
144
144
  * @returns {object[]} 模块对象数组
145
145
  */
146
146
  Manager.prototype.getList = function (keyword) {
147
- let entries = this._getInfos();
147
+ let infos = this._getInfos();
148
148
 
149
149
  // 先用 entry 做关键词过滤,避免无效实例化
150
150
  if (keyword) {
151
- entries = entries.filter((o) => {
151
+ infos = infos.filter((o) => {
152
152
  return o.name.includes(keyword) || o.title.includes(keyword) || o.description.includes(keyword);
153
153
  });
154
154
  }
155
155
 
156
156
  var list = [];
157
- for (var i = 0; i < entries.length; i++) {
158
- var mod = this.getMod(entries[i].name);
157
+ for (var i = 0; i < infos.length; i++) {
158
+ var mod = this.getMod(infos[i].name);
159
159
  if (!mod) continue;
160
160
  list.push(mod);
161
161
  }
@@ -237,11 +237,9 @@ Manager.prototype.delMod = function (name) {
237
237
  * 设置模块
238
238
  * @param {string} name 模块名
239
239
  * @param {object} mod 模块对象
240
- * @returns {object|null} 设置的模块对象
241
240
  */
242
241
  Manager.prototype.setMod = function (name, mod) {
243
242
  this.mods[name] = mod;
244
- return this.getMod(name);
245
243
  };
246
244
 
247
245
  /**
@@ -256,10 +254,14 @@ Manager.prototype.getMod = function (name) {
256
254
 
257
255
  // 注册表回退:通过 _getInfos 查找配置并按需实例化
258
256
  if (this.config.use_registry) {
259
- var entries = this._getInfos();
260
- for (var i = 0; i < entries.length; i++) {
261
- if (entries[i].name === name) {
262
- mod = this.newMod(entries[i].config_file);
257
+ var infos = this._getInfos();
258
+ for (var i = 0; i < infos.length; i++) {
259
+ if (infos[i].name === name) {
260
+ mod = this.newMod(infos[i].config_file);
261
+ // 保存到内存集合,避免后续访问时再次创建
262
+ if (mod) {
263
+ this.setMod(mod.config.name, mod);
264
+ }
263
265
  return mod;
264
266
  }
265
267
  }
@@ -386,6 +388,14 @@ Manager.prototype.loadMod = async function (name) {
386
388
  return null;
387
389
  };
388
390
 
391
+ /**
392
+ * 初始化模块
393
+ * @param {object} mod 模块对象
394
+ */
395
+ Manager.prototype._initMod = async function (mod) {
396
+ await mod.do('init', this.getParent());
397
+ };
398
+
389
399
  /**
390
400
  * 卸载模块
391
401
  * @param {string} name 模块名
@@ -527,7 +537,9 @@ Manager.prototype._loadFromRegistry = function () {
527
537
  var entry = lt[i];
528
538
  if (!this.config.lazy_load) {
529
539
  let mod = this.newMod(entry.config_file);
530
- this.mods[entry.name] = mod;
540
+ if (mod) {
541
+ this.setMod(mod.config.name, mod);
542
+ }
531
543
  }
532
544
  }
533
545
 
@@ -584,9 +596,9 @@ Manager.prototype.main = async function (name, method, ...params) {
584
596
  */
585
597
  Manager.prototype._runAllEnabled = async function (method, ...params) {
586
598
  var result;
587
- var entries = this._getInfos();
588
- for (var i = 0; i < entries.length; i++) {
589
- var entry = entries[i];
599
+ var infos = this._getInfos();
600
+ for (var i = 0; i < infos.length; i++) {
601
+ var entry = infos[i];
590
602
  // 直接用 entry.state 过滤,避免无效实例化
591
603
  if (entry.state !== 1) continue;
592
604
 
@@ -634,6 +646,11 @@ Manager.prototype._runMod = async function (mod, method, ...params) {
634
646
  }
635
647
  }
636
648
 
649
+ // 确保模块已初始化
650
+ if (!mod.isInited() && method !== 'init') {
651
+ await mod.do('init');
652
+ }
653
+
637
654
  // 执行方法
638
655
  let ret = await mod.call(method, ...params);
639
656
  // 根据模式决定是否重载
@@ -668,7 +685,7 @@ Manager.prototype._loadSources = async function () {
668
685
  // 后台异步扫描文件系统更新注册表,确保物理最新。
669
686
  if (this.config.use_registry) {
670
687
  if (this._loadFromRegistry()) {
671
- setImmediate(() => this._scanForUpdate(false));
688
+ // setImmediate(() => this._scanForUpdate(false));
672
689
  return;
673
690
  }
674
691
  }
@@ -782,10 +799,10 @@ Manager.prototype._ensureModLoaded = async function (name) {
782
799
  */
783
800
  Manager.prototype._ensureAllLoaded = async function () {
784
801
  var promises = [];
785
- var entries = this._getInfos();
802
+ var infos = this._getInfos();
786
803
 
787
- for (var i = 0; i < entries.length; i++) {
788
- promises.push(this._ensureModLoaded(entries[i].name));
804
+ for (var i = 0; i < infos.length; i++) {
805
+ promises.push(this._ensureModLoaded(infos[i].name));
789
806
  }
790
807
  await Promise.all(promises);
791
808
  };
@@ -802,10 +819,10 @@ Manager.prototype.runWait = async function (method, ...params) {
802
819
  }
803
820
 
804
821
  let result;
805
- var entries = this._getInfos();
822
+ var infos = this._getInfos();
806
823
 
807
- for (var i = 0; i < entries.length; i++) {
808
- let mod = await this._ensureModLoaded(entries[i].name);
824
+ for (var i = 0; i < infos.length; i++) {
825
+ let mod = await this._ensureModLoaded(infos[i].name);
809
826
  if (!mod) continue;
810
827
 
811
828
  try {
@@ -814,7 +831,7 @@ Manager.prototype.runWait = async function (method, ...params) {
814
831
  result = ret;
815
832
  }
816
833
  } catch (error) {
817
- this._logModError(entries[i].name, method, error);
834
+ this._logModError(infos[i].name, method, error);
818
835
  }
819
836
  }
820
837
 
@@ -835,14 +852,14 @@ Manager.prototype.runAsync = async function (method, ...params) {
835
852
  // 确保所有模块都已加载
836
853
  await this._ensureAllLoaded();
837
854
 
838
- var entries = this._getInfos();
855
+ var infos = this._getInfos();
839
856
 
840
- for (var i = 0; i < entries.length; i++) {
841
- var mod = this.mods[entries[i].name];
857
+ for (var i = 0; i < infos.length; i++) {
858
+ var mod = this.mods[infos[i].name];
842
859
  if (!mod) continue;
843
860
 
844
861
  mod.do(method, ...params).catch((error) => {
845
- this._logModError(entries[i].name, method, error);
862
+ this._logModError(infos[i].name, method, error);
846
863
  });
847
864
  }
848
865
 
@@ -864,10 +881,10 @@ Manager.prototype.runAll = async function (method, ...params) {
864
881
  await this._ensureAllLoaded();
865
882
 
866
883
  const promises = [];
867
- var entries = this._getInfos();
884
+ var infos = this._getInfos();
868
885
 
869
- for (var i = 0; i < entries.length; i++) {
870
- var mod = this.mods[entries[i].name];
886
+ for (var i = 0; i < infos.length; i++) {
887
+ var mod = this.mods[infos[i].name];
871
888
  if (!mod) continue;
872
889
 
873
890
  promises.push(mod.do(method, ...params));
@@ -891,10 +908,10 @@ Manager.prototype.runRace = async function (method, ...params) {
891
908
  await this._ensureAllLoaded();
892
909
 
893
910
  const promises = [];
894
- var entries = this._getInfos();
911
+ var infos = this._getInfos();
895
912
 
896
- for (var i = 0; i < entries.length; i++) {
897
- var mod = this.mods[entries[i].name];
913
+ for (var i = 0; i < infos.length; i++) {
914
+ var mod = this.mods[infos[i].name];
898
915
  if (!mod) continue;
899
916
 
900
917
  promises.push(
@@ -919,10 +936,10 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
919
936
  throw new TypeError('方法名称必须是字符串');
920
937
  }
921
938
 
922
- var entries = this._getInfos();
939
+ var infos = this._getInfos();
923
940
 
924
- for (var i = 0; i < entries.length; i++) {
925
- var mod = await this._ensureModLoaded(entries[i].name);
941
+ for (var i = 0; i < infos.length; i++) {
942
+ var mod = await this._ensureModLoaded(infos[i].name);
926
943
  if (!mod) continue;
927
944
 
928
945
  try {
@@ -931,7 +948,7 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
931
948
  result = ret;
932
949
  }
933
950
  } catch (error) {
934
- this._logModError(entries[i].name, method, error);
951
+ this._logModError(infos[i].name, method, error);
935
952
  }
936
953
  }
937
954
 
package/mod.js CHANGED
@@ -75,6 +75,9 @@ class Mod extends Base {
75
75
  cache_file: ''
76
76
  };
77
77
 
78
+ _is_loaded = false;
79
+ _is_inited = false;
80
+
78
81
  /**
79
82
  * 构造函数
80
83
  * @param {object} config 配置对象
@@ -91,17 +94,37 @@ class Mod extends Base {
91
94
  this._methods = {};
92
95
  // 脚本数据
93
96
  this._datas = {};
94
- this._is_loaded = false;
95
97
  }
96
- }
97
98
 
98
- /**
99
- * 检查模块是否已加载
100
- * @returns {boolean} 是否已加载
101
- */
102
- Mod.prototype.isLoaded = function () {
103
- return this._is_loaded;
104
- };
99
+ /**
100
+ * 是否已加载
101
+ * @param {boolean} bl 是否已加载
102
+ * @returns {boolean} 是否已加载
103
+ */
104
+ isLoaded(bl) {
105
+ if (bl === undefined) {
106
+ return this._is_loaded;
107
+ }
108
+ this._is_loaded = bl;
109
+ if (!bl) {
110
+ this._is_inited = false;
111
+ }
112
+ return bl;
113
+ }
114
+
115
+ /**
116
+ * 是否已初始化
117
+ * @param {boolean} bl 是否已初始化
118
+ * @returns {boolean} 是否已初始化
119
+ */
120
+ isInited(bl) {
121
+ if (bl === undefined) {
122
+ return this._is_inited;
123
+ }
124
+ this._is_inited = bl;
125
+ return bl;
126
+ }
127
+ }
105
128
 
106
129
  /**
107
130
  * 日志输出
@@ -176,7 +199,24 @@ Mod.prototype._onLoad = async function (ctx) {
176
199
  });
177
200
  this.on('load:after', (ctx) => {
178
201
  this.setState('loaded');
179
- this._is_loaded = true;
202
+ this.isLoaded(true);
203
+ });
204
+ };
205
+
206
+ /**
207
+ * 监听初始化事件
208
+ */
209
+ Base.prototype._onInit = function () {
210
+ // 初始化
211
+ this.on('init:before', async (ctx) => {
212
+ this.setState('initing');
213
+ await this._initCore(...ctx.params);
214
+ });
215
+ this.on('init:success', (ctx) => {
216
+ this.isInited(true);
217
+ });
218
+ this.on('init:after', (ctx) => {
219
+ this.setState('inited');
180
220
  });
181
221
  };
182
222
 
@@ -269,7 +309,7 @@ Mod.prototype._onUnload = async function (ctx) {
269
309
  this.on('unload:after', async (ctx) => {
270
310
  this.setState('unloaded');
271
311
  await this._unloadCore(...ctx.params);
272
- this._is_loaded = false;
312
+ this.isLoaded(false);
273
313
  });
274
314
  };
275
315
 
@@ -297,7 +337,9 @@ Mod.prototype.load = async function (...args) {
297
337
  * @private
298
338
  * @param {...any} args 加载参数
299
339
  */
300
- Mod.prototype._loadCore = async function (...args) {};
340
+ Mod.prototype._loadCore = async function (...args) {
341
+
342
+ };
301
343
 
302
344
  /**
303
345
  * 启动 - 内部方法
@@ -323,7 +365,7 @@ Mod.prototype.start = async function (...args) {
323
365
  * @private
324
366
  * @param {...any} args 初始化参数
325
367
  */
326
- Mod.prototype._startCore = async function (...args) {};
368
+ Mod.prototype._startCore = async function (...args) { };
327
369
 
328
370
  /**
329
371
  * 停止 - 内部方法
@@ -346,7 +388,7 @@ Mod.prototype.stop = async function () {
346
388
  * 停止核心
347
389
  * @private
348
390
  */
349
- Mod.prototype._stopCore = async function () {};
391
+ Mod.prototype._stopCore = async function () { };
350
392
 
351
393
  /**
352
394
  * 销毁核心
@@ -401,8 +443,7 @@ Mod.prototype._unloadCore = async function () {
401
443
  Mod.prototype.reload = async function () {
402
444
  try {
403
445
  this._unloadCore();
404
- this._is_loaded = false;
405
- this._script_keys = null;
446
+ this.isLoaded(false);
406
447
  } catch (err) {
407
448
  this.log('error', '重载失败', err);
408
449
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.2.3",
3
+ "version": "3.2.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": {