mm_machine 2.1.2 → 2.1.3
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/index.js +50 -35
- package/item.js +23 -11
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -45,41 +45,24 @@ class Index {
|
|
|
45
45
|
* 5.热更新+重载模式,改变配置文件重新加载配置和脚本,执行完后重新加载脚本
|
|
46
46
|
*/
|
|
47
47
|
this.mode = 1;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* 私有方法:执行模块方法
|
|
52
|
-
* @param {object} module 模块对象
|
|
53
|
-
* @param {string} method 方法名称
|
|
54
|
-
* @param {Array} params 参数数组
|
|
55
|
-
* @returns {Promise<any>} 执行结果
|
|
56
|
-
* @private
|
|
57
|
-
*/
|
|
58
|
-
async _execute(module, method, params = []) {
|
|
59
|
-
if (!module || !method) return null;
|
|
60
|
-
|
|
61
|
-
try {
|
|
62
|
-
// 确保模块已加载
|
|
63
|
-
if (!module.complete) {
|
|
64
|
-
await module.exec('load');
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// 执行方法
|
|
68
|
-
const ret = await module.exec(method, ...params);
|
|
69
|
-
|
|
70
|
-
// 根据模式决定是否重载
|
|
71
|
-
if (this.mode >= 4) {
|
|
72
|
-
module.exec('reload');
|
|
73
|
-
}
|
|
74
48
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
49
|
+
/**
|
|
50
|
+
* 日志输出
|
|
51
|
+
*/
|
|
52
|
+
this._logger = $.log || console;
|
|
80
53
|
}
|
|
81
54
|
}
|
|
82
55
|
|
|
56
|
+
/**
|
|
57
|
+
* 日志输出
|
|
58
|
+
* @param {string} level - 日志级别(debug/info/warn/error)
|
|
59
|
+
* @param {string} message - 日志消息
|
|
60
|
+
* @param {...*} args - 可选参数
|
|
61
|
+
*/
|
|
62
|
+
Index.prototype.log = function (level, message, ...args) {
|
|
63
|
+
this._logger[level](`[${this.constructor.name}.${this.type}] ${message}`, ...args);
|
|
64
|
+
};
|
|
65
|
+
|
|
83
66
|
/**
|
|
84
67
|
* 清除接口缓存
|
|
85
68
|
*/
|
|
@@ -92,6 +75,38 @@ Index.prototype.clear = function () {
|
|
|
92
75
|
*/
|
|
93
76
|
Index.prototype.Drive = Item;
|
|
94
77
|
|
|
78
|
+
/**
|
|
79
|
+
* 私有方法:执行模块方法
|
|
80
|
+
* @param {object} module 模块对象
|
|
81
|
+
* @param {string} method 方法名称
|
|
82
|
+
* @param {Array} params 参数数组
|
|
83
|
+
* @returns {Promise<any>} 执行结果
|
|
84
|
+
* @private
|
|
85
|
+
*/
|
|
86
|
+
Index.prototype._exec = async function (module, method, params = []) {
|
|
87
|
+
if (!module || !method) return null;
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
// 确保模块已加载
|
|
91
|
+
if (!module.complete) {
|
|
92
|
+
await module.exec('load');
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// 执行方法
|
|
96
|
+
const ret = await module.exec(method, ...params);
|
|
97
|
+
|
|
98
|
+
// 根据模式决定是否重载
|
|
99
|
+
if (this.mode >= 4) {
|
|
100
|
+
module.exec('reload');
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return ret;
|
|
104
|
+
} catch (err) {
|
|
105
|
+
$.log.error(`[${this.type}] 执行模块方法失败: `, err);
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
95
110
|
/**
|
|
96
111
|
* 加载项
|
|
97
112
|
* @param {string} dir 文件路径
|
|
@@ -566,7 +581,7 @@ Index.prototype.run = async function (name, method, ...params) {
|
|
|
566
581
|
if (name) {
|
|
567
582
|
const module = await this.get(name);
|
|
568
583
|
if (module && module.config?.state === 1) {
|
|
569
|
-
return await this.
|
|
584
|
+
return await this._exec(module, method, params);
|
|
570
585
|
}
|
|
571
586
|
return null;
|
|
572
587
|
} else if (name === null) {
|
|
@@ -574,7 +589,7 @@ Index.prototype.run = async function (name, method, ...params) {
|
|
|
574
589
|
// 使用函数式编程处理列表,使用some模拟break逻辑
|
|
575
590
|
await this.list.some(async (module) => {
|
|
576
591
|
if (module.config?.state === 1) {
|
|
577
|
-
result = await this.
|
|
592
|
+
result = await this._exec(module, method, params);
|
|
578
593
|
// 如果结果不为空且有结束标志,则停止迭代
|
|
579
594
|
return result && module.config?.end;
|
|
580
595
|
}
|
|
@@ -596,7 +611,7 @@ Index.prototype.exec = async function (name, method, ...params) {
|
|
|
596
611
|
if (name) {
|
|
597
612
|
const module = await this.get(name);
|
|
598
613
|
if (module) {
|
|
599
|
-
return await this.
|
|
614
|
+
return await this._exec(module, method, params);
|
|
600
615
|
}
|
|
601
616
|
return null;
|
|
602
617
|
} else if (name === null) {
|
|
@@ -608,7 +623,7 @@ Index.prototype.exec = async function (name, method, ...params) {
|
|
|
608
623
|
if (result && module.config?.end) {
|
|
609
624
|
return;
|
|
610
625
|
}
|
|
611
|
-
const current_result = await this.
|
|
626
|
+
const current_result = await this._exec(module, method, params);
|
|
612
627
|
if (current_result) {
|
|
613
628
|
result = current_result;
|
|
614
629
|
}
|
package/item.js
CHANGED
|
@@ -80,9 +80,21 @@ class Item {
|
|
|
80
80
|
|
|
81
81
|
// 加载完成
|
|
82
82
|
this.complete = false;
|
|
83
|
+
|
|
84
|
+
this._logger = $.log || console;
|
|
83
85
|
}
|
|
84
86
|
}
|
|
85
87
|
|
|
88
|
+
/**
|
|
89
|
+
* 日志输出
|
|
90
|
+
* @param {string} level - 日志级别(debug/info/warn/error)
|
|
91
|
+
* @param {string} message - 日志消息
|
|
92
|
+
* @param {...*} args - 可选参数
|
|
93
|
+
*/
|
|
94
|
+
Item.prototype.log = function (level, message, ...args) {
|
|
95
|
+
this._logger[level](`[${this.config.name}] ${message}`, ...args);
|
|
96
|
+
};
|
|
97
|
+
|
|
86
98
|
/**
|
|
87
99
|
* 设置配置
|
|
88
100
|
* @param {object} config 配置对象
|
|
@@ -139,7 +151,7 @@ Item.prototype._remove = function (module) {
|
|
|
139
151
|
}
|
|
140
152
|
}
|
|
141
153
|
} catch (err) {
|
|
142
|
-
|
|
154
|
+
this.log('error', `移除模块失败: `, err);
|
|
143
155
|
}
|
|
144
156
|
};
|
|
145
157
|
|
|
@@ -207,7 +219,7 @@ Item.prototype.loadScript = function (file, name = '') {
|
|
|
207
219
|
this._setMainMethod(cs, target_name);
|
|
208
220
|
return cs;
|
|
209
221
|
} catch (err) {
|
|
210
|
-
|
|
222
|
+
this.log('error', `加载脚本失败: `, err);
|
|
211
223
|
return null;
|
|
212
224
|
}
|
|
213
225
|
};
|
|
@@ -318,7 +330,7 @@ Item.prototype.loadFile = function (file, name) {
|
|
|
318
330
|
this.filename = full_path;
|
|
319
331
|
return final_config;
|
|
320
332
|
} catch (err) {
|
|
321
|
-
|
|
333
|
+
this.log('error', `加载配置文件失败: `, err);
|
|
322
334
|
return null;
|
|
323
335
|
}
|
|
324
336
|
};
|
|
@@ -351,7 +363,7 @@ Item.prototype._handleHotReload = function (loaded_config, change_type) {
|
|
|
351
363
|
this._reloadIfNeeded();
|
|
352
364
|
}
|
|
353
365
|
} catch (err) {
|
|
354
|
-
|
|
366
|
+
this.log('error', `配置热更新失败: `, err);
|
|
355
367
|
}
|
|
356
368
|
};
|
|
357
369
|
|
|
@@ -403,7 +415,7 @@ Item.prototype.removeFile = function () {
|
|
|
403
415
|
|
|
404
416
|
error_message = this._removeConfigFile(file, name);
|
|
405
417
|
} catch (err) {
|
|
406
|
-
|
|
418
|
+
this.log('error', `删除文件失败: `, err);
|
|
407
419
|
error_message = `删除失败: ${err.message}`;
|
|
408
420
|
}
|
|
409
421
|
return error_message;
|
|
@@ -475,7 +487,7 @@ Item.prototype.loadConfig = async function (config_data, name) {
|
|
|
475
487
|
await this.setConfig(config);
|
|
476
488
|
this.setConfigAfter();
|
|
477
489
|
} catch (err) {
|
|
478
|
-
|
|
490
|
+
this.log('error', `载入配置失败: `, err);
|
|
479
491
|
}
|
|
480
492
|
};
|
|
481
493
|
|
|
@@ -489,7 +501,7 @@ Item.prototype.loadBefore = async function () {
|
|
|
489
501
|
this.complete = true;
|
|
490
502
|
}
|
|
491
503
|
} catch (err) {
|
|
492
|
-
|
|
504
|
+
this.log('error', `加载前处理失败: `, err);
|
|
493
505
|
this.complete = false;
|
|
494
506
|
}
|
|
495
507
|
};
|
|
@@ -539,7 +551,7 @@ Item.prototype.save = function () {
|
|
|
539
551
|
// 单对象格式配置,直接保存
|
|
540
552
|
full_path.saveText(JSON.stringify(this.config, null, 4));
|
|
541
553
|
} catch (err) {
|
|
542
|
-
|
|
554
|
+
this.log('error', `保存配置失败: `, err);
|
|
543
555
|
}
|
|
544
556
|
};
|
|
545
557
|
|
|
@@ -579,7 +591,7 @@ Item.prototype.exec = async function (method, ...params) {
|
|
|
579
591
|
result = await this._execMain(method_func, params, result);
|
|
580
592
|
result = await this._execAfter(method, params, result);
|
|
581
593
|
} catch (err) {
|
|
582
|
-
|
|
594
|
+
this.log('error', `执行方法 ${method} 失败: `, err);
|
|
583
595
|
return null;
|
|
584
596
|
}
|
|
585
597
|
|
|
@@ -618,7 +630,7 @@ Item.prototype._execBefore = async function (method, params) {
|
|
|
618
630
|
}
|
|
619
631
|
return result;
|
|
620
632
|
} catch (err) {
|
|
621
|
-
|
|
633
|
+
this.log('error', `执行前置钩子 ${before_method} 失败: `, err);
|
|
622
634
|
return undefined;
|
|
623
635
|
}
|
|
624
636
|
};
|
|
@@ -659,7 +671,7 @@ Item.prototype._execAfter = async function (method, params, main_result) {
|
|
|
659
671
|
|
|
660
672
|
return after_result !== undefined ? after_result : main_result;
|
|
661
673
|
} catch (err) {
|
|
662
|
-
|
|
674
|
+
this.log('error', `执行后置钩子 ${after_method} 失败: `, err);
|
|
663
675
|
return main_result;
|
|
664
676
|
}
|
|
665
677
|
};
|