mm_machine 3.2.4 → 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 +46 -33
  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
  }
@@ -254,10 +254,10 @@ Manager.prototype.getMod = function (name) {
254
254
 
255
255
  // 注册表回退:通过 _getInfos 查找配置并按需实例化
256
256
  if (this.config.use_registry) {
257
- var entries = this._getInfos();
258
- for (var i = 0; i < entries.length; i++) {
259
- if (entries[i].name === name) {
260
- 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
261
  // 保存到内存集合,避免后续访问时再次创建
262
262
  if (mod) {
263
263
  this.setMod(mod.config.name, mod);
@@ -388,6 +388,14 @@ Manager.prototype.loadMod = async function (name) {
388
388
  return null;
389
389
  };
390
390
 
391
+ /**
392
+ * 初始化模块
393
+ * @param {object} mod 模块对象
394
+ */
395
+ Manager.prototype._initMod = async function (mod) {
396
+ await mod.do('init', this.getParent());
397
+ };
398
+
391
399
  /**
392
400
  * 卸载模块
393
401
  * @param {string} name 模块名
@@ -588,9 +596,9 @@ Manager.prototype.main = async function (name, method, ...params) {
588
596
  */
589
597
  Manager.prototype._runAllEnabled = async function (method, ...params) {
590
598
  var result;
591
- var entries = this._getInfos();
592
- for (var i = 0; i < entries.length; i++) {
593
- var entry = entries[i];
599
+ var infos = this._getInfos();
600
+ for (var i = 0; i < infos.length; i++) {
601
+ var entry = infos[i];
594
602
  // 直接用 entry.state 过滤,避免无效实例化
595
603
  if (entry.state !== 1) continue;
596
604
 
@@ -638,6 +646,11 @@ Manager.prototype._runMod = async function (mod, method, ...params) {
638
646
  }
639
647
  }
640
648
 
649
+ // 确保模块已初始化
650
+ if (!mod.isInited() && method !== 'init') {
651
+ await mod.do('init');
652
+ }
653
+
641
654
  // 执行方法
642
655
  let ret = await mod.call(method, ...params);
643
656
  // 根据模式决定是否重载
@@ -672,7 +685,7 @@ Manager.prototype._loadSources = async function () {
672
685
  // 后台异步扫描文件系统更新注册表,确保物理最新。
673
686
  if (this.config.use_registry) {
674
687
  if (this._loadFromRegistry()) {
675
- setImmediate(() => this._scanForUpdate(false));
688
+ // setImmediate(() => this._scanForUpdate(false));
676
689
  return;
677
690
  }
678
691
  }
@@ -786,10 +799,10 @@ Manager.prototype._ensureModLoaded = async function (name) {
786
799
  */
787
800
  Manager.prototype._ensureAllLoaded = async function () {
788
801
  var promises = [];
789
- var entries = this._getInfos();
802
+ var infos = this._getInfos();
790
803
 
791
- for (var i = 0; i < entries.length; i++) {
792
- promises.push(this._ensureModLoaded(entries[i].name));
804
+ for (var i = 0; i < infos.length; i++) {
805
+ promises.push(this._ensureModLoaded(infos[i].name));
793
806
  }
794
807
  await Promise.all(promises);
795
808
  };
@@ -806,10 +819,10 @@ Manager.prototype.runWait = async function (method, ...params) {
806
819
  }
807
820
 
808
821
  let result;
809
- var entries = this._getInfos();
822
+ var infos = this._getInfos();
810
823
 
811
- for (var i = 0; i < entries.length; i++) {
812
- 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);
813
826
  if (!mod) continue;
814
827
 
815
828
  try {
@@ -818,7 +831,7 @@ Manager.prototype.runWait = async function (method, ...params) {
818
831
  result = ret;
819
832
  }
820
833
  } catch (error) {
821
- this._logModError(entries[i].name, method, error);
834
+ this._logModError(infos[i].name, method, error);
822
835
  }
823
836
  }
824
837
 
@@ -839,14 +852,14 @@ Manager.prototype.runAsync = async function (method, ...params) {
839
852
  // 确保所有模块都已加载
840
853
  await this._ensureAllLoaded();
841
854
 
842
- var entries = this._getInfos();
855
+ var infos = this._getInfos();
843
856
 
844
- for (var i = 0; i < entries.length; i++) {
845
- var mod = this.mods[entries[i].name];
857
+ for (var i = 0; i < infos.length; i++) {
858
+ var mod = this.mods[infos[i].name];
846
859
  if (!mod) continue;
847
860
 
848
861
  mod.do(method, ...params).catch((error) => {
849
- this._logModError(entries[i].name, method, error);
862
+ this._logModError(infos[i].name, method, error);
850
863
  });
851
864
  }
852
865
 
@@ -868,10 +881,10 @@ Manager.prototype.runAll = async function (method, ...params) {
868
881
  await this._ensureAllLoaded();
869
882
 
870
883
  const promises = [];
871
- var entries = this._getInfos();
884
+ var infos = this._getInfos();
872
885
 
873
- for (var i = 0; i < entries.length; i++) {
874
- var mod = this.mods[entries[i].name];
886
+ for (var i = 0; i < infos.length; i++) {
887
+ var mod = this.mods[infos[i].name];
875
888
  if (!mod) continue;
876
889
 
877
890
  promises.push(mod.do(method, ...params));
@@ -895,10 +908,10 @@ Manager.prototype.runRace = async function (method, ...params) {
895
908
  await this._ensureAllLoaded();
896
909
 
897
910
  const promises = [];
898
- var entries = this._getInfos();
911
+ var infos = this._getInfos();
899
912
 
900
- for (var i = 0; i < entries.length; i++) {
901
- var mod = this.mods[entries[i].name];
913
+ for (var i = 0; i < infos.length; i++) {
914
+ var mod = this.mods[infos[i].name];
902
915
  if (!mod) continue;
903
916
 
904
917
  promises.push(
@@ -923,10 +936,10 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
923
936
  throw new TypeError('方法名称必须是字符串');
924
937
  }
925
938
 
926
- var entries = this._getInfos();
939
+ var infos = this._getInfos();
927
940
 
928
- for (var i = 0; i < entries.length; i++) {
929
- 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);
930
943
  if (!mod) continue;
931
944
 
932
945
  try {
@@ -935,7 +948,7 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
935
948
  result = ret;
936
949
  }
937
950
  } catch (error) {
938
- this._logModError(entries[i].name, method, error);
951
+ this._logModError(infos[i].name, method, error);
939
952
  }
940
953
  }
941
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.4",
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": {