mm_machine 3.3.0 → 3.3.2
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 +2 -2
- package/lib/drive.js +36 -24
- package/lib/manager.js +138 -102
- package/lib/mod.js +1 -2
- package/package.json +3 -2
package/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const { Mod } = require('./lib/mod.js');
|
|
2
|
-
const { Drive } = require('./lib/drive.js');
|
|
2
|
+
const { Drive, MODE } = require('./lib/drive.js');
|
|
3
3
|
const { Manager } = require('./lib/manager.js');
|
|
4
4
|
|
|
5
5
|
module.exports = {
|
|
6
|
-
Mod, Drive, Manager
|
|
6
|
+
Mod, Drive, Manager, MODE
|
|
7
7
|
};
|
package/lib/drive.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
const prettier = require('@prettier/sync');
|
|
2
1
|
require('mm_tpl');
|
|
3
2
|
let { conf } = require('mm_config');
|
|
4
3
|
const { Mod } = require('./mod');
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
const MODE = {
|
|
6
|
+
PRODUCTION: 1,
|
|
7
|
+
HOT_CONFIG: 2,
|
|
8
|
+
HOT_RELOAD: 3,
|
|
9
|
+
HOT_BOTH: 4,
|
|
10
|
+
RELOAD_ALWAYS: 5
|
|
11
|
+
};
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* 驱动基础类
|
|
@@ -42,8 +43,6 @@ class Drive extends Mod {
|
|
|
42
43
|
|
|
43
44
|
// 当前脚本文件路径
|
|
44
45
|
script_file = '';
|
|
45
|
-
// 目录缓存
|
|
46
|
-
_dir = undefined;
|
|
47
46
|
/**
|
|
48
47
|
* 构造函数
|
|
49
48
|
* @param {object} config 配置项
|
|
@@ -65,7 +64,7 @@ Drive.prototype._onLoad = async function (ctx) {
|
|
|
65
64
|
// 1. 加载
|
|
66
65
|
this.on('load:before', async (ctx) => {
|
|
67
66
|
this.setState('loading');
|
|
68
|
-
|
|
67
|
+
this.loadScript();
|
|
69
68
|
await this._loadCore(...ctx.params);
|
|
70
69
|
});
|
|
71
70
|
this.on('load:after', (ctx) => {
|
|
@@ -86,10 +85,6 @@ Drive.prototype.getEventer = function () {
|
|
|
86
85
|
* 配置驱动
|
|
87
86
|
*/
|
|
88
87
|
Drive.prototype._preset = function () {
|
|
89
|
-
let file = this.config.main;
|
|
90
|
-
if (file) {
|
|
91
|
-
this.script_file = file.fullname(this.getDir());
|
|
92
|
-
}
|
|
93
88
|
};
|
|
94
89
|
|
|
95
90
|
/**
|
|
@@ -102,11 +97,11 @@ Drive.prototype._loadConfig = function (file) {
|
|
|
102
97
|
this.config_file = file;
|
|
103
98
|
}
|
|
104
99
|
try {
|
|
105
|
-
//
|
|
106
|
-
let config =
|
|
100
|
+
// 加载配置(支持热更新)
|
|
101
|
+
let config = this._loadHotReload(this.config_file);
|
|
107
102
|
if (!config) {
|
|
108
103
|
this.newConfig(this.config_file);
|
|
109
|
-
config =
|
|
104
|
+
config = this._loadHotReload(this.config_file);
|
|
110
105
|
}
|
|
111
106
|
this.setConfig(config);
|
|
112
107
|
return config;
|
|
@@ -204,7 +199,7 @@ Drive.prototype._remove = function () {
|
|
|
204
199
|
if (!file) return;
|
|
205
200
|
|
|
206
201
|
try {
|
|
207
|
-
if (this.mode >=
|
|
202
|
+
if (this.mode >= MODE.HOT_RELOAD) {
|
|
208
203
|
$.mod.unload(file);
|
|
209
204
|
} else {
|
|
210
205
|
// 移除模块缓存
|
|
@@ -271,6 +266,10 @@ Drive.prototype.newScript = function (file) {
|
|
|
271
266
|
this._createScriptFile(file);
|
|
272
267
|
};
|
|
273
268
|
|
|
269
|
+
Drive.prototype.renderTpl = function(tpl, model){
|
|
270
|
+
return $.tpl.render(tpl, model);
|
|
271
|
+
}
|
|
272
|
+
|
|
274
273
|
/**
|
|
275
274
|
* 创建脚本
|
|
276
275
|
* @param {string} file 文件路径
|
|
@@ -278,7 +277,7 @@ Drive.prototype.newScript = function (file) {
|
|
|
278
277
|
*/
|
|
279
278
|
Drive.prototype._createScriptFile = function (file) {
|
|
280
279
|
var tpl = this._getScriptTpl();
|
|
281
|
-
var content =
|
|
280
|
+
var content = this.renderTpl(tpl, this.getModel('script'));
|
|
282
281
|
if (!content) {
|
|
283
282
|
return '';
|
|
284
283
|
}
|
|
@@ -341,7 +340,7 @@ Drive.prototype.loadScript = function () {
|
|
|
341
340
|
let file = this._getScriptFile();
|
|
342
341
|
if (!file) return null;
|
|
343
342
|
try {
|
|
344
|
-
if (this.mode
|
|
343
|
+
if (this.mode === MODE.HOT_RELOAD || this.mode === MODE.HOT_BOTH) {
|
|
345
344
|
var cs = $.require(file, (mod) => {
|
|
346
345
|
this._setMainMethod(mod);
|
|
347
346
|
});
|
|
@@ -424,7 +423,8 @@ Drive.prototype.prettyCode = function (code, type = 'json5') {
|
|
|
424
423
|
options.parser = 'json';
|
|
425
424
|
break;
|
|
426
425
|
}
|
|
427
|
-
|
|
426
|
+
const prettier = require('@prettier/sync');
|
|
427
|
+
let new_code = prettier.format(code, options);
|
|
428
428
|
return new_code;
|
|
429
429
|
};
|
|
430
430
|
|
|
@@ -445,7 +445,7 @@ Drive.prototype.getModel = function (type) {
|
|
|
445
445
|
*/
|
|
446
446
|
Drive.prototype._createConfigFile = function (file, model) {
|
|
447
447
|
var tpl = this._getConfigTpl();
|
|
448
|
-
var content =
|
|
448
|
+
var content = this.renderTpl(tpl, model || this.getModel('config'));
|
|
449
449
|
let code = this.prettyCode(content, 'json');
|
|
450
450
|
file.saveText(code);
|
|
451
451
|
return file;
|
|
@@ -526,7 +526,7 @@ Drive.prototype.loadFile = function (file, name) {
|
|
|
526
526
|
* @returns {object | Array | null} 配置对象
|
|
527
527
|
*/
|
|
528
528
|
Drive.prototype._loadHotReload = function (file) {
|
|
529
|
-
if (this.mode >=
|
|
529
|
+
if (this.mode >= MODE.HOT_CONFIG) {
|
|
530
530
|
return $.loadJson(file, this._handleHotReload.bind(this));
|
|
531
531
|
}
|
|
532
532
|
return file.loadJson();
|
|
@@ -550,7 +550,18 @@ Drive.prototype._findConfigByName = function (config, name) {
|
|
|
550
550
|
* @private
|
|
551
551
|
*/
|
|
552
552
|
Drive.prototype._handleHotReload = function (config) {
|
|
553
|
+
var old_config = { ...this.config };
|
|
553
554
|
this.setConfig(config);
|
|
555
|
+
|
|
556
|
+
// 调用 _onConfigChange 钩子(如果脚本中定义了)
|
|
557
|
+
var on_config_change = this.getMethod('_onConfigChange');
|
|
558
|
+
if (typeof on_config_change === 'function') {
|
|
559
|
+
on_config_change.call(this, old_config, this.config);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
// 触发 config:change 事件
|
|
563
|
+
this.emit('config:change', old_config, this.config);
|
|
564
|
+
|
|
554
565
|
this._reloadIfNeeded();
|
|
555
566
|
return config;
|
|
556
567
|
};
|
|
@@ -559,7 +570,7 @@ Drive.prototype._handleHotReload = function (config) {
|
|
|
559
570
|
* 根据模式决定是否重新加载脚本
|
|
560
571
|
*/
|
|
561
572
|
Drive.prototype._reloadIfNeeded = function () {
|
|
562
|
-
if (this.mode >=
|
|
573
|
+
if (this.mode >= MODE.HOT_RELOAD) {
|
|
563
574
|
this.reloadScript();
|
|
564
575
|
}
|
|
565
576
|
};
|
|
@@ -732,5 +743,6 @@ Drive.prototype.remove = function () {
|
|
|
732
743
|
* @module 导出Drive类
|
|
733
744
|
*/
|
|
734
745
|
module.exports = {
|
|
735
|
-
Drive
|
|
746
|
+
Drive,
|
|
747
|
+
MODE
|
|
736
748
|
};
|
package/lib/manager.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const { Mod } = require('./mod.js');
|
|
2
|
-
const { Drive } = require('./drive.js');
|
|
2
|
+
const { Drive, MODE } = require('./drive.js');
|
|
3
3
|
const { Scanner } = require('./scanner.js');
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -70,7 +70,7 @@ class Manager extends Mod {
|
|
|
70
70
|
* 5.重载模式,执行完后重新加载脚本,避免变量污染
|
|
71
71
|
* @type {number}
|
|
72
72
|
*/
|
|
73
|
-
mode:
|
|
73
|
+
mode: MODE.HOT_RELOAD,
|
|
74
74
|
/**
|
|
75
75
|
* 作用域
|
|
76
76
|
* @type {string}
|
|
@@ -146,9 +146,9 @@ Manager.prototype.getList = function (keyword) {
|
|
|
146
146
|
});
|
|
147
147
|
}
|
|
148
148
|
|
|
149
|
-
|
|
150
|
-
for (
|
|
151
|
-
|
|
149
|
+
let list = [];
|
|
150
|
+
for (let i = 0; i < infos.length; i++) {
|
|
151
|
+
let mod = this.getMod(infos[i].name);
|
|
152
152
|
if (!mod) continue;
|
|
153
153
|
list.push(mod);
|
|
154
154
|
}
|
|
@@ -176,12 +176,6 @@ Manager.prototype.getTplDir = function () {
|
|
|
176
176
|
*/
|
|
177
177
|
Manager.prototype.Drive = Drive;
|
|
178
178
|
|
|
179
|
-
/**
|
|
180
|
-
* 更新模块信息
|
|
181
|
-
*/
|
|
182
|
-
Manager.prototype.updateInfo = function () {
|
|
183
|
-
};
|
|
184
|
-
|
|
185
179
|
/**
|
|
186
180
|
* 获取模块配置列表
|
|
187
181
|
* @returns {Array} 排序后的模块配置数组
|
|
@@ -231,10 +225,10 @@ Manager.prototype.setMod = function (name, mod) {
|
|
|
231
225
|
/**
|
|
232
226
|
* 获取模块
|
|
233
227
|
* @param {string} name 模块名
|
|
234
|
-
* @returns {object
|
|
228
|
+
* @returns {object} 模块对象
|
|
235
229
|
*/
|
|
236
230
|
Manager.prototype.getMod = function (name) {
|
|
237
|
-
return this.mods[name]
|
|
231
|
+
return this.mods[name];
|
|
238
232
|
};
|
|
239
233
|
|
|
240
234
|
/**
|
|
@@ -254,7 +248,7 @@ Manager.prototype.getMods = function () {
|
|
|
254
248
|
*/
|
|
255
249
|
Manager.prototype.newMod = function (config_file, config, script) {
|
|
256
250
|
try {
|
|
257
|
-
|
|
251
|
+
let mod;
|
|
258
252
|
switch (this.config.mod_type) {
|
|
259
253
|
case 'script':
|
|
260
254
|
mod = new Mod(config, this);
|
|
@@ -272,8 +266,8 @@ Manager.prototype.newMod = function (config_file, config, script) {
|
|
|
272
266
|
}
|
|
273
267
|
|
|
274
268
|
if (script) {
|
|
275
|
-
for (
|
|
276
|
-
|
|
269
|
+
for (let key in script) {
|
|
270
|
+
let obj = script[key];
|
|
277
271
|
if (typeof obj == 'function') {
|
|
278
272
|
mod.setMethod(key, obj);
|
|
279
273
|
} else {
|
|
@@ -298,31 +292,15 @@ Manager.prototype.newMod = function (config_file, config, script) {
|
|
|
298
292
|
Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
|
|
299
293
|
let cg = { ...config };
|
|
300
294
|
|
|
301
|
-
|
|
295
|
+
let mod = this.newMod(config_file, cg, script);
|
|
302
296
|
if (!mod) return null;
|
|
303
297
|
|
|
304
298
|
// 模块已存在时不替换内存实例,避免破坏已初始化的模块
|
|
305
|
-
if (!this.
|
|
299
|
+
if (!this.getMod(mod.config.name)) {
|
|
306
300
|
this.setMod(mod.config.name, mod);
|
|
307
301
|
}
|
|
308
302
|
|
|
309
|
-
|
|
310
|
-
name: mod.config.name,
|
|
311
|
-
title: mod.config.title,
|
|
312
|
-
description: mod.config.description,
|
|
313
|
-
sort: mod.config.sort,
|
|
314
|
-
state: mod.config.state,
|
|
315
|
-
config_file: config_file,
|
|
316
|
-
auto_start: mod.config.auto_start || 'auto',
|
|
317
|
-
main: mod.config.main || ''
|
|
318
|
-
};
|
|
319
|
-
// 获取触发键
|
|
320
|
-
let keys = this.config.trigger_key;
|
|
321
|
-
if (keys && keys.length > 0) {
|
|
322
|
-
for (let key in keys) {
|
|
323
|
-
info[key] = mod.config[key];
|
|
324
|
-
}
|
|
325
|
-
}
|
|
303
|
+
let info = { ...mod.config, config_file: config_file };
|
|
326
304
|
this.infos.push(info);
|
|
327
305
|
return mod;
|
|
328
306
|
};
|
|
@@ -410,7 +388,7 @@ Manager.prototype.regMods = function (files, config = {}, script = {}) {
|
|
|
410
388
|
* 加载所有模块
|
|
411
389
|
*/
|
|
412
390
|
Manager.prototype.loadMods = async function () {
|
|
413
|
-
|
|
391
|
+
let promises = [];
|
|
414
392
|
let mods = this.getMods();
|
|
415
393
|
for (let name in mods) {
|
|
416
394
|
let mod = mods[name];
|
|
@@ -438,13 +416,13 @@ Manager.prototype.updateBefore = function () { };
|
|
|
438
416
|
* 更新配置前的钩子函数
|
|
439
417
|
*/
|
|
440
418
|
Manager.prototype.updateAfter = function () {
|
|
441
|
-
this.
|
|
419
|
+
this._sort();
|
|
442
420
|
};
|
|
443
421
|
|
|
444
422
|
/**
|
|
445
423
|
* 排序模块信息
|
|
446
424
|
*/
|
|
447
|
-
Manager.prototype.
|
|
425
|
+
Manager.prototype._sort = function () {
|
|
448
426
|
this.infos.sort((a, b) => {
|
|
449
427
|
// 先按 sort 排序
|
|
450
428
|
let cha = a.sort - b.sort;
|
|
@@ -467,8 +445,8 @@ Manager.prototype.update = async function (dir, clear = true) {
|
|
|
467
445
|
this.clearMods();
|
|
468
446
|
}
|
|
469
447
|
|
|
470
|
-
|
|
471
|
-
|
|
448
|
+
let search_dir = dir || this.config.dir;
|
|
449
|
+
let files = await this._findFiles(search_dir);
|
|
472
450
|
this.regMods(files);
|
|
473
451
|
|
|
474
452
|
if (!this.config.lazy_load) {
|
|
@@ -509,9 +487,9 @@ Manager.prototype._findFiles = async function (dir) {
|
|
|
509
487
|
* @returns {Promise<any>} 执行结果
|
|
510
488
|
*/
|
|
511
489
|
Manager.prototype.main = async function (name, method, ...params) {
|
|
512
|
-
|
|
490
|
+
let result = null;
|
|
513
491
|
if (name) {
|
|
514
|
-
|
|
492
|
+
let mod = this.getMod(name);
|
|
515
493
|
if (mod) {
|
|
516
494
|
result = await this._runMod(mod, method, ...params);
|
|
517
495
|
}
|
|
@@ -522,24 +500,24 @@ Manager.prototype.main = async function (name, method, ...params) {
|
|
|
522
500
|
};
|
|
523
501
|
|
|
524
502
|
/**
|
|
525
|
-
*
|
|
503
|
+
* 执行所有模块方法 - 内部方法
|
|
526
504
|
* @param {string} method 方法名称
|
|
527
505
|
* @param {...any} params 方法参数集合
|
|
528
506
|
* @returns {Promise<any>} 执行结果
|
|
529
507
|
* @private
|
|
530
508
|
*/
|
|
531
509
|
Manager.prototype._runAllEnabled = async function (method, ...params) {
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
for (
|
|
535
|
-
|
|
510
|
+
let result;
|
|
511
|
+
let infos = this._getInfos();
|
|
512
|
+
for (let i = 0; i < infos.length; i++) {
|
|
513
|
+
let info = infos[i];
|
|
536
514
|
// 直接用 info.state 过滤,避免无效实例化
|
|
537
515
|
if (info.state !== 1) continue;
|
|
538
516
|
|
|
539
|
-
|
|
517
|
+
let mod = this.getMod(info.name);
|
|
540
518
|
if (!mod) continue;
|
|
541
519
|
|
|
542
|
-
|
|
520
|
+
let ret = null;
|
|
543
521
|
try {
|
|
544
522
|
ret = await this._runMod(mod, method, ...params);
|
|
545
523
|
} catch (err) {
|
|
@@ -557,7 +535,33 @@ Manager.prototype._runAllEnabled = async function (method, ...params) {
|
|
|
557
535
|
};
|
|
558
536
|
|
|
559
537
|
/**
|
|
560
|
-
*
|
|
538
|
+
* 执行所有模块方法 - 内部方法
|
|
539
|
+
* @param {string} method 方法名称
|
|
540
|
+
* @param {...any} params 方法参数集合
|
|
541
|
+
* @returns {Promise<any>} 执行结果
|
|
542
|
+
* @private
|
|
543
|
+
*/
|
|
544
|
+
Manager.prototype.doAll = async function (method, ...params) {
|
|
545
|
+
let promises = [];
|
|
546
|
+
let infos = this._getInfos();
|
|
547
|
+
for (let i = 0; i < infos.length; i++) {
|
|
548
|
+
let info = infos[i];
|
|
549
|
+
// 直接用 info.state 过滤,避免无效实例化
|
|
550
|
+
if (info.state !== 1) continue;
|
|
551
|
+
|
|
552
|
+
let mod = this.getMod(info.name);
|
|
553
|
+
if (!mod) continue;
|
|
554
|
+
try {
|
|
555
|
+
promises.push(this._runMod(mod, method, ...params));
|
|
556
|
+
} catch (err) {
|
|
557
|
+
this.log('error', `执行模块方法失败: `, err);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
return await Promise.all(promises);
|
|
561
|
+
};
|
|
562
|
+
|
|
563
|
+
/**
|
|
564
|
+
* 执行模块方法 - 内部方法
|
|
561
565
|
* @param {object} mod 模块对象
|
|
562
566
|
* @param {string} method 方法名称
|
|
563
567
|
* @param {...any} params 参数集合
|
|
@@ -565,18 +569,54 @@ Manager.prototype._runAllEnabled = async function (method, ...params) {
|
|
|
565
569
|
* @private
|
|
566
570
|
*/
|
|
567
571
|
Manager.prototype._runMod = async function (mod, method, ...params) {
|
|
568
|
-
mod = this._ensureModLoaded(mod, method);
|
|
572
|
+
mod = await this._ensureModLoaded(mod, method);
|
|
573
|
+
|
|
574
|
+
// 执行方法
|
|
575
|
+
let ret = await mod.do(method, ...params);
|
|
576
|
+
// 根据模式决定是否重载
|
|
577
|
+
if (this.config.mode >= MODE.HOT_BOTH) {
|
|
578
|
+
await mod.do('reload');
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
return ret;
|
|
582
|
+
};
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* 调用模块方法 - 内部方法
|
|
586
|
+
* @param {object} mod 模块对象
|
|
587
|
+
* @param {string} method 方法名称
|
|
588
|
+
* @param {...any} params 参数集合
|
|
589
|
+
* @returns {Promise<any>} 执行结果
|
|
590
|
+
* @private
|
|
591
|
+
*/
|
|
592
|
+
Manager.prototype._callMod = async function (mod, method, ...params) {
|
|
593
|
+
mod = await this._ensureModLoaded(mod, method);
|
|
569
594
|
|
|
570
595
|
// 执行方法
|
|
571
596
|
let ret = await mod.call(method, ...params);
|
|
572
597
|
// 根据模式决定是否重载
|
|
573
|
-
if (this.config.mode >=
|
|
598
|
+
if (this.config.mode >= MODE.HOT_BOTH) {
|
|
574
599
|
await mod.do('reload');
|
|
575
600
|
}
|
|
576
601
|
|
|
577
602
|
return ret;
|
|
578
603
|
};
|
|
579
604
|
|
|
605
|
+
/**
|
|
606
|
+
* 调用模块方法 - 外部方法
|
|
607
|
+
* @param {string} name 模块名
|
|
608
|
+
* @param {string} method 方法名称
|
|
609
|
+
* @param {...any} params 参数集合
|
|
610
|
+
* @returns {Promise<any>} 执行结果
|
|
611
|
+
*/
|
|
612
|
+
Manager.prototype.callMod = async function (name, method, ...params) {
|
|
613
|
+
let mod = this.getMod(name);
|
|
614
|
+
if (!mod) {
|
|
615
|
+
throw new Error(`模块${name}不存在`);
|
|
616
|
+
}
|
|
617
|
+
return await this._callMod(mod, method, ...params);
|
|
618
|
+
};
|
|
619
|
+
|
|
580
620
|
/**
|
|
581
621
|
* 初始化管理器
|
|
582
622
|
*/
|
|
@@ -602,13 +642,13 @@ Manager.prototype._loadSources = async function () {
|
|
|
602
642
|
* 后台扫描文件系统
|
|
603
643
|
*/
|
|
604
644
|
Manager.prototype._scanForUpdate = async function (clear = true) {
|
|
605
|
-
|
|
606
|
-
|
|
645
|
+
let base_dir = this.getBaseDir();
|
|
646
|
+
let dir = this.getDir();
|
|
607
647
|
try {
|
|
608
648
|
if (base_dir) {
|
|
609
|
-
await Promise.all([this.
|
|
649
|
+
await Promise.all([this.update(base_dir, clear), this.update(dir, false)]);
|
|
610
650
|
} else {
|
|
611
|
-
await this.
|
|
651
|
+
await this.update(dir, clear);
|
|
612
652
|
}
|
|
613
653
|
} catch (err) {
|
|
614
654
|
this.log('error', '后台扫描同步失败', err);
|
|
@@ -636,7 +676,7 @@ Manager.prototype._loadCore = async function () {
|
|
|
636
676
|
* 初始化公共属性
|
|
637
677
|
*/
|
|
638
678
|
Manager.prototype._initCore = async function () {
|
|
639
|
-
if (this.isLoaded()) {
|
|
679
|
+
if (!this.isLoaded()) {
|
|
640
680
|
await this.do('load');
|
|
641
681
|
}
|
|
642
682
|
// 初始化AI客户端
|
|
@@ -683,25 +723,25 @@ Manager.prototype.exec = async function (name, method, ...params) {
|
|
|
683
723
|
* 确保模块已加载
|
|
684
724
|
* @param {object} mod 模块对象
|
|
685
725
|
* @param {string} method 方法名称
|
|
686
|
-
* @returns {Promise<object
|
|
726
|
+
* @returns {Promise<object>} 模块实例
|
|
687
727
|
*/
|
|
688
728
|
Manager.prototype._ensureModLoaded = async function (mod, method) {
|
|
689
|
-
if (!mod)
|
|
729
|
+
if (!mod) {
|
|
730
|
+
throw new TypeError(`模块(mod)参数不能为空`);
|
|
731
|
+
}
|
|
690
732
|
if (!['load', 'init'].includes(method)) {
|
|
691
733
|
if (!mod.isLoaded()) {
|
|
692
734
|
try {
|
|
693
735
|
await mod.do('load');
|
|
694
736
|
} catch (error) {
|
|
695
|
-
|
|
696
|
-
return null;
|
|
737
|
+
throw new Error(`模块${mod.config.name}加载失败,无法执行方法 ${method}`);
|
|
697
738
|
}
|
|
698
739
|
}
|
|
699
740
|
if (!mod.isInited()) {
|
|
700
741
|
try {
|
|
701
|
-
await mod.do('init');
|
|
742
|
+
await mod.do('init', this.getParent());
|
|
702
743
|
} catch (error) {
|
|
703
|
-
|
|
704
|
-
return null;
|
|
744
|
+
throw new Error(`模块${mod.config.name}初始化失败,无法执行方法 ${method}`);
|
|
705
745
|
}
|
|
706
746
|
}
|
|
707
747
|
};
|
|
@@ -713,13 +753,17 @@ Manager.prototype._ensureModLoaded = async function (mod, method) {
|
|
|
713
753
|
* @returns {Promise} 加载完成
|
|
714
754
|
*/
|
|
715
755
|
Manager.prototype._ensureAllLoaded = async function (method) {
|
|
716
|
-
|
|
717
|
-
|
|
756
|
+
let promises = [];
|
|
757
|
+
let infos = this._getInfos();
|
|
718
758
|
|
|
719
|
-
for (
|
|
759
|
+
for (let i = 0; i < infos.length; i++) {
|
|
720
760
|
let mod = this.getMod(infos[i].name);
|
|
721
761
|
if (!mod) continue;
|
|
722
|
-
|
|
762
|
+
try {
|
|
763
|
+
promises.push(this._ensureModLoaded(mod, method));
|
|
764
|
+
} catch (error) {
|
|
765
|
+
this._logModError(infos[i].name, method, error);
|
|
766
|
+
}
|
|
723
767
|
}
|
|
724
768
|
|
|
725
769
|
await Promise.all(promises);
|
|
@@ -737,19 +781,20 @@ Manager.prototype.runWait = async function (method, ...params) {
|
|
|
737
781
|
}
|
|
738
782
|
|
|
739
783
|
let result;
|
|
740
|
-
|
|
784
|
+
let infos = this._getInfos();
|
|
741
785
|
|
|
742
|
-
for (
|
|
743
|
-
let
|
|
786
|
+
for (let i = 0; i < infos.length; i++) {
|
|
787
|
+
let name = infos[i].name;
|
|
788
|
+
let mod = this.getMod(name);
|
|
744
789
|
if (!mod) continue;
|
|
745
|
-
mod = await this._ensureModLoaded(mod, method);
|
|
746
790
|
try {
|
|
791
|
+
mod = await this._ensureModLoaded(mod, method);
|
|
747
792
|
let ret = await mod.do(method, ...params);
|
|
748
793
|
if (ret !== null && ret !== undefined) {
|
|
749
794
|
result = ret;
|
|
750
795
|
}
|
|
751
796
|
} catch (error) {
|
|
752
|
-
this._logModError(
|
|
797
|
+
this._logModError(name, method, error);
|
|
753
798
|
}
|
|
754
799
|
}
|
|
755
800
|
|
|
@@ -770,10 +815,10 @@ Manager.prototype.runAsync = async function (method, ...params) {
|
|
|
770
815
|
// 确保所有模块都已加载
|
|
771
816
|
await this._ensureAllLoaded(method);
|
|
772
817
|
|
|
773
|
-
|
|
818
|
+
let infos = this._getInfos();
|
|
774
819
|
|
|
775
|
-
for (
|
|
776
|
-
|
|
820
|
+
for (let i = 0; i < infos.length; i++) {
|
|
821
|
+
let mod = this.getMod(infos[i].name);
|
|
777
822
|
if (!mod) continue;
|
|
778
823
|
|
|
779
824
|
mod.do(method, ...params).catch((error) => {
|
|
@@ -795,17 +840,17 @@ Manager.prototype.runAll = async function (method, ...params) {
|
|
|
795
840
|
throw new TypeError('方法名称必须是字符串');
|
|
796
841
|
}
|
|
797
842
|
|
|
798
|
-
// 确保所有模块都已加载
|
|
799
|
-
await this._ensureAllLoaded(method);
|
|
800
|
-
|
|
801
843
|
const promises = [];
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
var mod = this.mods[infos[i].name];
|
|
844
|
+
let infos = this._getInfos();
|
|
845
|
+
for (let i = 0; i < infos.length; i++) {
|
|
846
|
+
let mod = this.getMod(infos[i].name);
|
|
806
847
|
if (!mod) continue;
|
|
807
|
-
|
|
808
|
-
|
|
848
|
+
try {
|
|
849
|
+
mod = await this._ensureModLoaded(mod, method);
|
|
850
|
+
promises.push(mod.do(method, ...params));
|
|
851
|
+
} catch (error) {
|
|
852
|
+
this._logModError(infos[i].name, method, error);
|
|
853
|
+
}
|
|
809
854
|
}
|
|
810
855
|
|
|
811
856
|
return await Promise.all(promises);
|
|
@@ -826,10 +871,10 @@ Manager.prototype.runRace = async function (method, ...params) {
|
|
|
826
871
|
await this._ensureAllLoaded(method);
|
|
827
872
|
|
|
828
873
|
const promises = [];
|
|
829
|
-
|
|
874
|
+
let infos = this._getInfos();
|
|
830
875
|
|
|
831
|
-
for (
|
|
832
|
-
|
|
876
|
+
for (let i = 0; i < infos.length; i++) {
|
|
877
|
+
let mod = this.getMod(infos[i].name);
|
|
833
878
|
if (!mod) continue;
|
|
834
879
|
|
|
835
880
|
promises.push(
|
|
@@ -854,13 +899,13 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
|
|
|
854
899
|
throw new TypeError('方法名称必须是字符串');
|
|
855
900
|
}
|
|
856
901
|
|
|
857
|
-
|
|
902
|
+
let infos = this._getInfos();
|
|
858
903
|
|
|
859
|
-
for (
|
|
904
|
+
for (let i = 0; i < infos.length; i++) {
|
|
860
905
|
let mod = this.getMod(infos[i].name);
|
|
861
906
|
if (!mod) continue;
|
|
862
|
-
mod = await this._ensureModLoaded(mod, method);
|
|
863
907
|
try {
|
|
908
|
+
mod = await this._ensureModLoaded(mod, method);
|
|
864
909
|
let ret = await mod.do(method, result, ...params);
|
|
865
910
|
if (ret !== null && ret !== undefined) {
|
|
866
911
|
result = ret;
|
|
@@ -880,7 +925,7 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
|
|
|
880
925
|
* @param {...any} params 事件参数
|
|
881
926
|
* @returns {Promise} 事件触发结果
|
|
882
927
|
*/
|
|
883
|
-
Manager.prototype.
|
|
928
|
+
Manager.prototype.runMod = async function (name, method, ...params) {
|
|
884
929
|
if (typeof method !== 'string') {
|
|
885
930
|
throw new TypeError('方法名称必须是字符串');
|
|
886
931
|
}
|
|
@@ -888,16 +933,7 @@ Manager.prototype.runSubMod = async function (name, method, ...params) {
|
|
|
888
933
|
if (!mod) {
|
|
889
934
|
throw new Error(`模块${name}不存在或加载失败`);
|
|
890
935
|
}
|
|
891
|
-
|
|
892
|
-
return await mod.do(method, ...params);
|
|
893
|
-
};
|
|
894
|
-
|
|
895
|
-
/**
|
|
896
|
-
* 移除事件监听
|
|
897
|
-
* @param {string} event 事件名
|
|
898
|
-
*/
|
|
899
|
-
Manager.prototype.offEvent = function (event) {
|
|
900
|
-
this.getEventer()?.off(event);
|
|
936
|
+
return await this._runMod(mod, method, ...params);
|
|
901
937
|
};
|
|
902
938
|
|
|
903
939
|
/**
|
package/lib/mod.js
CHANGED
|
@@ -711,8 +711,7 @@ Mod.prototype.loadDatas = function () {
|
|
|
711
711
|
Mod.prototype.call = async function (method, ...params) {
|
|
712
712
|
let method_func = this.getMethod(method);
|
|
713
713
|
if (typeof method_func !== 'function') {
|
|
714
|
-
|
|
715
|
-
return null;
|
|
714
|
+
throw new Error(`方法 ${method} 未注册,请确保模块已加载`);
|
|
716
715
|
}
|
|
717
716
|
|
|
718
717
|
let result;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_machine",
|
|
3
|
-
"version": "3.3.
|
|
3
|
+
"version": "3.3.2",
|
|
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": {
|
|
@@ -34,12 +34,13 @@
|
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@prettier/sync": "^0.6.1",
|
|
36
36
|
"mm_config": "^2.4.5",
|
|
37
|
+
"mm_expand": "^2.4.5",
|
|
37
38
|
"mm_hot_reload": "^1.4.2",
|
|
38
39
|
"mm_tpl": "^2.6.5"
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|
|
41
42
|
"eslint": "^10.7.0",
|
|
42
|
-
"eslint-plugin-jsdoc": "^63.
|
|
43
|
+
"eslint-plugin-jsdoc": "^63.2.0",
|
|
43
44
|
"mm_eslint": "^1.7.5"
|
|
44
45
|
}
|
|
45
46
|
}
|