mm_machine 3.3.4 → 3.3.6

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/lib/manager.js +38 -26
  2. package/package.json +1 -1
package/lib/manager.js CHANGED
@@ -131,6 +131,15 @@ Manager.prototype.getInfos = function () {
131
131
  return this._getInfos();
132
132
  };
133
133
 
134
+ /**
135
+ * 获取模块信息
136
+ * @param {string} name 模块名
137
+ * @returns {object|null} 模块配置对象
138
+ */
139
+ Manager.prototype.getInfo = function (name) {
140
+ return this.infos.getObj({ name });
141
+ };
142
+
134
143
  /**
135
144
  * 获取所有模块对象
136
145
  * @param {string} keyword 查询关键词,判断name、title、description之一是否包含关键词,包含的才返回
@@ -290,16 +299,9 @@ Manager.prototype.newMod = function (config_file, config, script) {
290
299
  * @returns {object} 模块对象
291
300
  */
292
301
  Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
293
- let cg = { ...config };
294
-
295
- let mod = this.newMod(config_file, cg, script);
302
+ let mod = this.newMod(config_file, config, script);
296
303
  if (!mod) return null;
297
-
298
- // 模块已存在时不替换内存实例,避免破坏已初始化的模块
299
- if (!this.getMod(mod.config.name)) {
300
- this.setMod(mod.config.name, mod);
301
- }
302
-
304
+ this.setMod(mod.config.name, mod);
303
305
  let info = { ...mod.config, config_file: config_file };
304
306
  this.infos.push(info);
305
307
  return mod;
@@ -504,7 +506,7 @@ Manager.prototype._runAllEnabled = async function (method, ...params) {
504
506
  for (let i = 0; i < infos.length; i++) {
505
507
  let info = infos[i];
506
508
  // 直接用 info.state 过滤,避免无效实例化
507
- if (info.state !== 1) continue;
509
+ if (!info.state) continue;
508
510
 
509
511
  let mod = this.getMod(info.name);
510
512
  if (!mod) continue;
@@ -539,7 +541,7 @@ Manager.prototype.doAll = async function (method, ...params) {
539
541
  for (let i = 0; i < infos.length; i++) {
540
542
  let info = infos[i];
541
543
  // 直接用 info.state 过滤,避免无效实例化
542
- if (info.state !== 1) continue;
544
+ if (!info.state) continue;
543
545
 
544
546
  let mod = this.getMod(info.name);
545
547
  if (!mod) continue;
@@ -749,12 +751,14 @@ Manager.prototype._ensureAllLoaded = async function (method) {
749
751
  let infos = this._getInfos();
750
752
 
751
753
  for (let i = 0; i < infos.length; i++) {
752
- let mod = this.getMod(infos[i].name);
754
+ let info = infos[i];
755
+ if (!info.state) continue;
756
+ let mod = this.getMod(info.name);
753
757
  if (!mod) continue;
754
758
  try {
755
759
  promises.push(this._ensureModLoaded(mod, method));
756
760
  } catch (error) {
757
- this._logModError(infos[i].name, method, error);
761
+ this._logModError(info.name, method, error);
758
762
  }
759
763
  }
760
764
 
@@ -776,8 +780,9 @@ Manager.prototype.runWait = async function (method, ...params) {
776
780
  let infos = this._getInfos();
777
781
 
778
782
  for (let i = 0; i < infos.length; i++) {
779
- let name = infos[i].name;
780
- let mod = this.getMod(name);
783
+ let info = infos[i];
784
+ if (!info.state) continue;
785
+ let mod = this.getMod(info.name);
781
786
  if (!mod) continue;
782
787
  try {
783
788
  mod = await this._ensureModLoaded(mod, method);
@@ -786,7 +791,7 @@ Manager.prototype.runWait = async function (method, ...params) {
786
791
  result = ret;
787
792
  }
788
793
  } catch (error) {
789
- this._logModError(name, method, error);
794
+ this._logModError(info.name, method, error);
790
795
  }
791
796
  }
792
797
 
@@ -810,11 +815,13 @@ Manager.prototype.runAsync = async function (method, ...params) {
810
815
  let infos = this._getInfos();
811
816
 
812
817
  for (let i = 0; i < infos.length; i++) {
813
- let mod = this.getMod(infos[i].name);
818
+ let info = infos[i];
819
+ if (!info.state) continue;
820
+ let mod = this.getMod(info.name);
814
821
  if (!mod) continue;
815
822
 
816
823
  mod.do(method, ...params).catch((error) => {
817
- this._logModError(infos[i].name, method, error);
824
+ this._logModError(info.name, method, error);
818
825
  });
819
826
  }
820
827
 
@@ -835,13 +842,15 @@ Manager.prototype.runAll = async function (method, ...params) {
835
842
  const promises = [];
836
843
  let infos = this._getInfos();
837
844
  for (let i = 0; i < infos.length; i++) {
838
- let mod = this.getMod(infos[i].name);
845
+ let info = infos[i];
846
+ if (!info.state) continue;
847
+ let mod = this.getMod(info.name);
839
848
  if (!mod) continue;
840
849
  try {
841
850
  mod = await this._ensureModLoaded(mod, method);
842
851
  promises.push(mod.do(method, ...params));
843
852
  } catch (error) {
844
- this._logModError(infos[i].name, method, error);
853
+ this._logModError(info.name, method, error);
845
854
  }
846
855
  }
847
856
 
@@ -866,7 +875,9 @@ Manager.prototype.runRace = async function (method, ...params) {
866
875
  let infos = this._getInfos();
867
876
 
868
877
  for (let i = 0; i < infos.length; i++) {
869
- let mod = this.getMod(infos[i].name);
878
+ let info = infos[i];
879
+ if (!info.state) continue;
880
+ let mod = this.getMod(info.name);
870
881
  if (!mod) continue;
871
882
 
872
883
  promises.push(
@@ -894,7 +905,9 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
894
905
  let infos = this._getInfos();
895
906
 
896
907
  for (let i = 0; i < infos.length; i++) {
897
- let mod = this.getMod(infos[i].name);
908
+ let info = infos[i];
909
+ if (!info.state) continue;
910
+ let mod = this.getMod(info.name);
898
911
  if (!mod) continue;
899
912
  try {
900
913
  mod = await this._ensureModLoaded(mod, method);
@@ -903,7 +916,7 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
903
916
  result = ret;
904
917
  }
905
918
  } catch (error) {
906
- this._logModError(infos[i].name, method, error);
919
+ this._logModError(info.name, method, error);
907
920
  }
908
921
  }
909
922
 
@@ -944,7 +957,7 @@ Manager.prototype.createFile = function (config, file) {
944
957
  if (!config.description) {
945
958
  throw new TypeError('模块描述不能为空');
946
959
  }
947
- if (this.getMod(config.name)) {
960
+ if (this.getInfo(config.name)) {
948
961
  throw new Error(`创建失败,原因:模块${config.name}已存在`);
949
962
  }
950
963
  let mod = new this.Drive(config, this);
@@ -969,8 +982,7 @@ Manager.prototype.createFile = function (config, file) {
969
982
  */
970
983
  Manager.prototype.create = function (config, file = '') {
971
984
  let f = this.createFile(config, file);
972
- let mod = this.regMod(f, config);
973
- return mod;
985
+ return this.regMod(f, config);
974
986
  };
975
987
 
976
988
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.3.4",
3
+ "version": "3.3.6",
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": {