mm_machine 1.8.8 → 1.8.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.
@@ -10,7 +10,7 @@
10
10
  /**
11
11
  * 描述, 用于描述该有什么用的
12
12
  */
13
- "description": "用于测试动态加载、更新、卸载、删除脚本22",
13
+ "description": "用于测试动态加载、更新、卸载、删除脚本123",
14
14
  /**
15
15
  * 文件路径, 当调用函数不存在时,会先从文件中加载
16
16
  */
@@ -44,7 +44,7 @@
44
44
  /**
45
45
  * 描述, 用于描述该有什么用的
46
46
  */
47
- "description": "用于测试动态加载、更新、卸载、删除脚本",
47
+ "description": "用于测试动态加载、更新、卸载、删除脚本321",
48
48
  /**
49
49
  * 文件路径, 当调用函数不存在时,会先从文件中加载
50
50
  */
package/index.js CHANGED
@@ -83,6 +83,15 @@ class Item {
83
83
  * 模块目录
84
84
  */
85
85
  this.dir_base = dir_base;
86
+
87
+ /**
88
+ * 模式
89
+ * 1.生产模式,改变文件不会重新加载
90
+ * 2.热更新模式,改变文件重新加载
91
+ * 3.重载模式,执行完后重新加载,避免变量污染
92
+ * 4.热更新+重载模式,改变文件重新加载,执行完后重新加载
93
+ */
94
+ this.mode = 1;
86
95
  }
87
96
  }
88
97
 
@@ -140,7 +149,6 @@ Item.prototype.unloadObj = function(file) {
140
149
  this.remove_module(file || this.config.func_file.fullname(this.dir));
141
150
  }
142
151
 
143
-
144
152
  /**
145
153
  * 卸载
146
154
  * @param {Boolean} remove 是否删除文件
@@ -166,9 +174,14 @@ Item.prototype.loadObj = function(config) {
166
174
  var f = config.func_file;
167
175
  if (f) {
168
176
  var file = f.fullname(this.dir);
169
- if (file.hasFile()) {
170
- var name = config.func_name;
171
- var cs = $.require(file, (cs, way) => {
177
+ if (!file.hasFile()) {
178
+ this.new_script(file);
179
+ }
180
+
181
+ var name = config.func_name;
182
+ var cs;
183
+ if (this.mode === 2 || this.mode === 4) {
184
+ cs = $.require(file, (cs, way) => {
172
185
  if (way == "change" && cs) {
173
186
  if (name) {
174
187
  this.main = cs[name];
@@ -177,15 +190,15 @@ Item.prototype.loadObj = function(config) {
177
190
  }
178
191
  }
179
192
  });
180
- if (cs) {
181
- if (name) {
182
- this.main = cs[name];
183
- } else {
184
- $.push(this, cs, true);
185
- }
186
- }
187
193
  } else {
188
- this.new_script(file);
194
+ cs = require(file);
195
+ }
196
+ if (cs) {
197
+ if (name) {
198
+ this.main = cs[name];
199
+ } else {
200
+ $.push(this, cs, true);
201
+ }
189
202
  }
190
203
  }
191
204
  this.load_after();
@@ -203,25 +216,54 @@ Item.prototype.new_config = function(file) {
203
216
  /**
204
217
  * 加载配置文件
205
218
  * @param {String} file 文件路径
219
+ * @param {String} name 配置项名称
206
220
  * @return {Object} 配置对象
207
221
  */
208
- Item.prototype.loadFile = function(file) {
209
- var obj;
222
+ Item.prototype.loadFile = function(file, name) {
223
+ var config;
210
224
  var f = file.fullname(this.dir);
211
225
  var text = f.loadText();
212
226
  if (!text) {
213
227
  this.new_config(f);
214
228
  }
215
229
  if (text) {
216
- obj = $.loadJson(f, (mod) => {
217
- if (mod && !Array.isArray(mod)) {
218
- this.set_config(mod);
230
+ if (this.mode === 2 || this.mode === 4) {
231
+ config = $.loadJson(f, (conf, way) => {
232
+ if (way == "change" && conf) {
233
+ if (Array.isArray(conf)) {
234
+ var list = conf;
235
+ for (var i = 0; i < list.length; i++) {
236
+ var o = list[i];
237
+ if (this.config.name === o.name) {
238
+ this.set_config(o);
239
+ break;
240
+ }
241
+ }
242
+ } else {
243
+ this.set_config(conf);
244
+ }
245
+ }
246
+ });
247
+ } else {
248
+ config = f.loadJson();
249
+ }
250
+
251
+ if (name) {
252
+ if (Array.isArray(config)) {
253
+ var list = config;
254
+ for (var i = 0; i < list.length; i++) {
255
+ var o = list[i];
256
+ if (name === o.name) {
257
+ config = o;
258
+ break;
259
+ }
260
+ }
219
261
  }
220
- });
262
+ }
221
263
  }
222
264
  this.dir = f.dirname();
223
265
  this.filename = f;
224
- return obj;
266
+ return config;
225
267
  };
226
268
 
227
269
  /**
@@ -229,7 +271,7 @@ Item.prototype.loadFile = function(file) {
229
271
  */
230
272
  Item.prototype.reload = function() {
231
273
  this.unloadObj();
232
- this.load(this.filename);
274
+ this.load(this.filename, this.config.name);
233
275
  }
234
276
 
235
277
  /**
@@ -284,11 +326,12 @@ Item.prototype.remove_file = function() {
284
326
  /**
285
327
  * 载入配置
286
328
  * @param {Object|String} cg 配置对象或配置路径
329
+ * @param {String} name 配置名称
287
330
  */
288
- Item.prototype.load = function(cg) {
331
+ Item.prototype.load = function(cg, name) {
289
332
  var config;
290
333
  if (typeof(cg) === "string") {
291
- config = this.loadFile(cg);
334
+ config = this.loadFile(cg, name);
292
335
  } else {
293
336
  config = cg;
294
337
  }
@@ -426,9 +469,13 @@ class Index {
426
469
  this.dir_base = dir_base;
427
470
 
428
471
  /**
429
- * 模式 0生产模式,1开发模式,2热重载模式
472
+ * 模式
473
+ * 1.生产模式,改变文件不会重新加载
474
+ * 2.热更新模式,改变文件重新加载
475
+ * 3.重载模式,执行完后重新加载,避免变量污染
476
+ * 4.热更新+重载模式,改变文件重新加载,执行完后重新加载
430
477
  */
431
- this.mode = 2;
478
+ this.mode = 1;
432
479
  }
433
480
  }
434
481
 
@@ -454,9 +501,14 @@ Index.prototype.load_item = function(dir, cg, file) {
454
501
  if (this.Drive) {
455
502
  var drive = new this.Drive(dir);
456
503
  if (cg) {
457
- drive.file = file;
458
- drive.loadObj(cg);
504
+ if (this.mode < 3) {
505
+ drive.mode = this.mode;
506
+ } else {
507
+ drive.mode = 2;
508
+ }
509
+ drive.load(file, cg.name);
459
510
  } else {
511
+ drive.mode = this.mode;
460
512
  drive.load(file);
461
513
  }
462
514
  this.list.push(drive);
@@ -550,22 +602,60 @@ Index.prototype.update_after = async function(dir) {
550
602
  // console.log("更新后")
551
603
  }
552
604
 
605
+ /**
606
+ * 更新配置
607
+ * @param {Object} dir
608
+ */
609
+ Index.prototype.update_config = async function(dir) {
610
+ if (!dir) {
611
+ var list = this.list;
612
+ for (var i = 0; i < list.length; i++) {
613
+ var o = list[i];
614
+ var file = o.filename;
615
+ if (file) {
616
+ var config = file.loadJson();
617
+ if (config) {
618
+ if (Array.isArray(config)) {
619
+ var lt = config;
620
+ for (var n = 0; n < lt.length; n++) {
621
+ var cg = lt[n];
622
+ if (cg.name == o.config.name) {
623
+ o.set_config(cg);
624
+ continue;
625
+ }
626
+ }
627
+ } else {
628
+ o.set_config(config);
629
+ }
630
+ }
631
+ }
632
+ }
633
+ }
634
+ }
635
+
553
636
  /**
554
637
  * 更新
638
+ * @param {String} dir 检索的路径
639
+ * @param {Boolean} loadJS 是否加载JS
555
640
  */
556
- Index.prototype.update_main = async function(dir) {
557
- this.clear();
558
- this.load(dir);
641
+ Index.prototype.update_main = async function(dir, loadJS = true) {
642
+ if (loadJS) {
643
+ this.clear();
644
+ this.load(dir);
645
+ } else {
646
+ this.update_config(dir);
647
+ }
559
648
  this.sort();
560
649
  }
561
650
 
562
651
  /**
563
652
  * 更新配置
564
653
  * @param {String} dir 检索的路径
654
+ * @param {Boolean} loadJS 是否加载JS
565
655
  */
566
- Index.prototype.update = async function(dir) {
656
+ Index.prototype.update = async function(dir, loadJS = true) {
567
657
  await this.update_before(dir);
568
- await this.update_main(dir);
658
+ await this.update_main(dir, loadJS);
569
659
  await this.update_after(dir);
570
660
  };
571
661
 
@@ -783,7 +873,7 @@ Index.prototype.run = async function(name, method, ...params) {
783
873
  } else {
784
874
  result = ret;
785
875
  }
786
- if (this.mode) {
876
+ if (this.mode > 2) {
787
877
  o.reload();
788
878
  }
789
879
  }
@@ -822,7 +912,7 @@ Index.prototype.exec = async function(name, method, ...params) {
822
912
  } else {
823
913
  result = ret;
824
914
  }
825
- if (this.mode) {
915
+ if (this.mode > 2) {
826
916
  o.reload();
827
917
  }
828
918
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "1.8.8",
3
+ "version": "1.8.9",
4
4
  "description": "这是超级美眉框架机制构建辅助模块,用于快速构建一个机制",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/test.js CHANGED
@@ -13,8 +13,14 @@ class Drive extends Item {
13
13
  class Engine extends Index {
14
14
  constructor(scope, dir_base) {
15
15
  super(scope, dir_base);
16
- // 模式 0生产模式;1开发模式,会重新加载脚本
17
- this.mode = 2;
16
+ /**
17
+ * 模式
18
+ * 1.不更新重载模式,改变文件不会重新加载,执行完后也不会重新加载
19
+ * 2.热更新模式,改变文件重新加载
20
+ * 3.重载模式,执行完后重新加载,避免变量污染
21
+ * 4.热更新+重载模式,改变文件重新加载,执行完后重新加载
22
+ */
23
+ this.mode = 0;
18
24
  // 机制类型,必须填写,用于检索文件
19
25
  this.type = "demo";
20
26
  }
@@ -35,10 +41,10 @@ async function demo() {
35
41
  if (plug) {
36
42
  plug.loadFile(plug.filename);
37
43
  }
38
-
44
+
39
45
  var i = await engine.run('demo3', 'main');
40
46
  console.log("指明回调demo3", i);
41
-
47
+
42
48
  // console.log("模块数", engine.list.length);
43
49
 
44
50
  // var i = 0;
@@ -86,10 +92,19 @@ async function demo() {
86
92
  // file.saveText(text);
87
93
  // }, 26000)
88
94
 
89
- setInterval(() => {
95
+ // setInterval(() => {
96
+ // var plug = engine.get("demo1");
97
+ // console.log("查看变化1", plug.config);
98
+ // var plug = engine.get("demo3");
99
+ // console.log("查看变化3", plug.config);
100
+ // }, 3000);
101
+ setTimeout(async() => {
102
+ await engine.update(null, false);
90
103
  var plug = engine.get("demo1");
91
- console.log("查看变化", plug);
92
- }, 3000);
104
+ console.log("查看变化1", plug.config);
105
+ var plug = engine.get("demo3");
106
+ console.log("查看变化3", plug.config);
107
+ }, 10000);
93
108
  }
94
109
 
95
110
  /* 调用示例 */