mm_machine 3.5.5 → 3.5.7
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/example/index.js +1 -4
- package/lib/drive.js +19 -81
- package/lib/manager.js +253 -170
- package/lib/mod.js +3 -241
- package/package.json +8 -7
- package/lib/scanner.js +0 -225
package/lib/manager.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
const { Manager: Core } = require('mm_mod');
|
|
2
|
-
const { hasDir, fullname } = require('mm_helper');
|
|
2
|
+
const { hasDir, hasFile, fullname, basename, dirname } = require('mm_helper');
|
|
3
|
+
const { Loader } = require('mm_loader');
|
|
3
4
|
const { Mod, addModMethods } = require('./mod.js');
|
|
4
5
|
const { Drive, MODE } = require('./drive.js');
|
|
5
|
-
const { Scanner } = require('./scanner.js');
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* 管理器类
|
|
@@ -54,11 +54,6 @@ class Manager extends Core {
|
|
|
54
54
|
* @type {string}
|
|
55
55
|
*/
|
|
56
56
|
dir: '', // './ai/manager'.fullname(),
|
|
57
|
-
/**
|
|
58
|
-
* 搜索模式 dir按目录搜索 | file按文件名搜索
|
|
59
|
-
* @type {string}
|
|
60
|
-
*/
|
|
61
|
-
search_way: 'file',
|
|
62
57
|
/**
|
|
63
58
|
* 类型 script脚本 | json配置 | drive驱动
|
|
64
59
|
* @type {string}
|
|
@@ -84,11 +79,6 @@ class Manager extends Core {
|
|
|
84
79
|
* @type {string}
|
|
85
80
|
*/
|
|
86
81
|
scope: 'server',
|
|
87
|
-
/**
|
|
88
|
-
* 最大搜索深度 -1表示不限制深度
|
|
89
|
-
* @type {number}
|
|
90
|
-
*/
|
|
91
|
-
max_depth: 3,
|
|
92
82
|
/**
|
|
93
83
|
* 忽略目录数组
|
|
94
84
|
* @type {Array}
|
|
@@ -101,9 +91,8 @@ class Manager extends Core {
|
|
|
101
91
|
* @param {object} config 配置项
|
|
102
92
|
* @param {object} parent 父项对象
|
|
103
93
|
* @param {object} [mods] 模块对象列表,传入时作为受管记录的实例表(保持外部引用)
|
|
104
|
-
* @param {object} [Drive] 驱动对象
|
|
105
94
|
*/
|
|
106
|
-
constructor(config, parent, mods
|
|
95
|
+
constructor(config, parent, mods) {
|
|
107
96
|
super(
|
|
108
97
|
{
|
|
109
98
|
...Manager.config,
|
|
@@ -117,22 +106,24 @@ class Manager extends Core {
|
|
|
117
106
|
return parent;
|
|
118
107
|
};
|
|
119
108
|
}
|
|
120
|
-
//
|
|
121
|
-
this.
|
|
122
|
-
//
|
|
123
|
-
this.
|
|
109
|
+
// 管理器自身的加载标记(受管模块状态由状态存储器管理)
|
|
110
|
+
this._loaded = false;
|
|
111
|
+
// 目录监听器,键为目录路径,值为该目录的配置加载器
|
|
112
|
+
this._dir_watchers = new Map();
|
|
124
113
|
// 外部传入实例表时,直接作为受管记录的实例表,保证外部引用同步
|
|
125
114
|
if (mods) {
|
|
126
115
|
this._getRecord().mods = mods;
|
|
127
116
|
}
|
|
128
|
-
if (Drive) {
|
|
129
|
-
this.Drive = Drive;
|
|
130
|
-
}
|
|
131
117
|
// 监听事件
|
|
132
118
|
this._listen();
|
|
133
119
|
}
|
|
134
120
|
}
|
|
135
121
|
|
|
122
|
+
/**
|
|
123
|
+
* 默认驱动
|
|
124
|
+
*/
|
|
125
|
+
Manager.prototype.Drive = Drive;
|
|
126
|
+
|
|
136
127
|
/**
|
|
137
128
|
* 获取父项键名
|
|
138
129
|
* @returns {string} 父项键名
|
|
@@ -146,7 +137,7 @@ Manager.prototype.getParentKey = function () {
|
|
|
146
137
|
* @returns {object[]} 模块配置数组
|
|
147
138
|
*/
|
|
148
139
|
Manager.prototype.getInfos = function () {
|
|
149
|
-
return this.
|
|
140
|
+
return this.configs;
|
|
150
141
|
};
|
|
151
142
|
|
|
152
143
|
/**
|
|
@@ -155,7 +146,7 @@ Manager.prototype.getInfos = function () {
|
|
|
155
146
|
* @returns {object|null} 模块配置对象
|
|
156
147
|
*/
|
|
157
148
|
Manager.prototype.getInfo = function (name) {
|
|
158
|
-
return this.
|
|
149
|
+
return this.configs.find((o) => o.name === name) || null;
|
|
159
150
|
};
|
|
160
151
|
|
|
161
152
|
/**
|
|
@@ -164,7 +155,7 @@ Manager.prototype.getInfo = function (name) {
|
|
|
164
155
|
* @returns {object[]} 模块对象数组
|
|
165
156
|
*/
|
|
166
157
|
Manager.prototype.getList = function (keyword) {
|
|
167
|
-
let infos = this.
|
|
158
|
+
let infos = this.configs;
|
|
168
159
|
|
|
169
160
|
// 先用 info 做关键词过滤,避免无效实例化
|
|
170
161
|
if (keyword) {
|
|
@@ -199,78 +190,20 @@ Manager.prototype.getTplDir = function () {
|
|
|
199
190
|
};
|
|
200
191
|
|
|
201
192
|
/**
|
|
202
|
-
*
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* 创建受管记录
|
|
208
|
-
* @description 在受管实例表之外,额外记录模块条目信息与插件集合
|
|
209
|
-
* @returns {object} 受管记录
|
|
210
|
-
*/
|
|
211
|
-
Manager.prototype._createManagedRecord = function () {
|
|
212
|
-
let record = Core.prototype._createManagedRecord.call(this);
|
|
213
|
-
record.infos = [];
|
|
214
|
-
record.plugins = {};
|
|
215
|
-
return record;
|
|
216
|
-
};
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* 获取插件集合
|
|
220
|
-
* @description 兼容旧记录,缺失时惰性补建
|
|
221
|
-
* @returns {object} 插件集合(应用名 => 插件对象)
|
|
222
|
-
*/
|
|
223
|
-
Manager.prototype._getPlugins = function () {
|
|
224
|
-
let record = this._getRecord();
|
|
225
|
-
if (!record.plugins) {
|
|
226
|
-
record.plugins = {};
|
|
227
|
-
}
|
|
228
|
-
return record.plugins;
|
|
229
|
-
};
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* 注册插件对象
|
|
233
|
-
* @param {string} name 应用名称
|
|
234
|
-
* @param {object} obj 插件对象
|
|
235
|
-
* @returns {Manager} 当前管理器
|
|
236
|
-
*/
|
|
237
|
-
Manager.prototype.usePlugin = function (name, obj) {
|
|
238
|
-
this._getPlugins()[name] = obj;
|
|
239
|
-
return this;
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* 插件集合
|
|
244
|
-
* @type {object}
|
|
245
|
-
*/
|
|
246
|
-
Object.defineProperty(Manager.prototype, 'plugins', {
|
|
247
|
-
/**
|
|
248
|
-
* 获取插件集合
|
|
249
|
-
* @returns {object} 插件集合
|
|
250
|
-
*/
|
|
251
|
-
get: function () {
|
|
252
|
-
return this._getPlugins();
|
|
253
|
-
}
|
|
254
|
-
});
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* 获取文件扫描器
|
|
258
|
-
* @returns {Scanner} 文件扫描器
|
|
259
|
-
*/
|
|
260
|
-
Manager.prototype._getScanner = function () {
|
|
261
|
-
return new Scanner(this.config.ignore, this.config.max_depth);
|
|
262
|
-
};
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* 获取模块配置列表
|
|
266
|
-
* @returns {Array} 排序后的模块配置数组
|
|
193
|
+
* 获取配置加载器
|
|
194
|
+
* @returns {Loader} 配置加载器
|
|
195
|
+
* @private
|
|
267
196
|
*/
|
|
268
|
-
Manager.prototype.
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
197
|
+
Manager.prototype._getLoader = function () {
|
|
198
|
+
return new Loader({
|
|
199
|
+
resources: {
|
|
200
|
+
mod: {
|
|
201
|
+
ext: this.config.filename,
|
|
202
|
+
hot: false
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
ignores: this.config.ignore
|
|
206
|
+
});
|
|
274
207
|
};
|
|
275
208
|
|
|
276
209
|
// ==== 模块管理接口 ====
|
|
@@ -279,10 +212,8 @@ Manager.prototype._getInfos = function () {
|
|
|
279
212
|
* @returns {Promise<void>} 无返回值
|
|
280
213
|
*/
|
|
281
214
|
Manager.prototype.clearMods = async function () {
|
|
282
|
-
//
|
|
215
|
+
// 统一由受管记录释放模块实例,同时清空配置列表
|
|
283
216
|
await this.clear();
|
|
284
|
-
let infos = this._getInfos();
|
|
285
|
-
infos.length = 0;
|
|
286
217
|
};
|
|
287
218
|
|
|
288
219
|
/**
|
|
@@ -295,7 +226,7 @@ Manager.prototype.delMod = function (name) {
|
|
|
295
226
|
let mod = record.mods[name];
|
|
296
227
|
if (mod) {
|
|
297
228
|
delete record.mods[name];
|
|
298
|
-
let infos = this.
|
|
229
|
+
let infos = this.configs;
|
|
299
230
|
let index = infos.findIndex((o) => o.name === name);
|
|
300
231
|
if (index >= 0) {
|
|
301
232
|
infos.splice(index, 1);
|
|
@@ -330,6 +261,30 @@ Manager.prototype.getMods = function () {
|
|
|
330
261
|
return this._getRecord().mods;
|
|
331
262
|
};
|
|
332
263
|
|
|
264
|
+
/**
|
|
265
|
+
* 读取模块配置
|
|
266
|
+
* @description 配置文件由管理器统一读取,调用方已提供名称时直接使用传入配置
|
|
267
|
+
* @param {string} config_file 配置文件路径
|
|
268
|
+
* @param {object} config 配置项
|
|
269
|
+
* @returns {object} 包含名称的配置项
|
|
270
|
+
* @private
|
|
271
|
+
*/
|
|
272
|
+
Manager.prototype._readModConfig = function (config_file, config = {}) {
|
|
273
|
+
let mod_config = { ...config };
|
|
274
|
+
if (!mod_config.name && config_file) {
|
|
275
|
+
let file_config = hasFile(config_file) ? this._getLoader().load(config_file) : null;
|
|
276
|
+
mod_config = { ...file_config, ...mod_config };
|
|
277
|
+
if (!mod_config.name) {
|
|
278
|
+
mod_config.name = basename(dirname(config_file));
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
// 驱动目录默认为配置文件所在目录,供子类解析主脚本与参数、SQL配置的相对路径
|
|
282
|
+
if (!mod_config.dir && config_file) {
|
|
283
|
+
mod_config.dir = dirname(config_file);
|
|
284
|
+
}
|
|
285
|
+
return mod_config;
|
|
286
|
+
};
|
|
287
|
+
|
|
333
288
|
/**
|
|
334
289
|
* 创建模块
|
|
335
290
|
* @param {string} config_file 配置文件路径
|
|
@@ -340,20 +295,27 @@ Manager.prototype.getMods = function () {
|
|
|
340
295
|
Manager.prototype.newMod = function (config_file, config, script) {
|
|
341
296
|
try {
|
|
342
297
|
let mod;
|
|
298
|
+
let mod_config = null;
|
|
343
299
|
switch (this.config.mod_type) {
|
|
344
300
|
case 'script':
|
|
345
301
|
mod = new Mod(config, this.getParent() || this);
|
|
346
302
|
break;
|
|
347
303
|
default:
|
|
348
|
-
|
|
304
|
+
// 配置由管理器读取,模块构造时即具备完整配置
|
|
305
|
+
mod_config = this._readModConfig(config_file, config);
|
|
306
|
+
mod = new this.Drive(mod_config, this.getParent() || this);
|
|
349
307
|
break;
|
|
350
308
|
}
|
|
351
309
|
mod.mode = this.config.mode;
|
|
352
310
|
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
311
|
+
// 仅 Drive 子类提供配置文件应用与热更新能力,Mod 子类(如 api、event、plugin)构造时已持有完整配置
|
|
312
|
+
if (mod_config && config_file) {
|
|
313
|
+
if (typeof mod.applyConfig === 'function') {
|
|
314
|
+
mod.applyConfig(config_file, mod_config);
|
|
315
|
+
}
|
|
316
|
+
if (typeof mod.watchConfig === 'function') {
|
|
317
|
+
mod.watchConfig(config_file);
|
|
318
|
+
}
|
|
357
319
|
}
|
|
358
320
|
|
|
359
321
|
if (script) {
|
|
@@ -385,7 +347,7 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
|
|
|
385
347
|
if (!mod) return null;
|
|
386
348
|
this.setMod(mod.config.name, mod);
|
|
387
349
|
let info = { ...mod.config, config_file: config_file };
|
|
388
|
-
this.
|
|
350
|
+
this.configs.push(info);
|
|
389
351
|
return mod;
|
|
390
352
|
};
|
|
391
353
|
|
|
@@ -473,25 +435,20 @@ Manager.prototype.loadMods = async function () {
|
|
|
473
435
|
await Promise.all(promises);
|
|
474
436
|
};
|
|
475
437
|
|
|
476
|
-
/**
|
|
477
|
-
* 获取目录名
|
|
478
|
-
* @returns {string} 目录名
|
|
479
|
-
*/
|
|
480
|
-
Manager.prototype.getDirName = function () {
|
|
481
|
-
let prefix = this.constructor.name.toLowerCase();
|
|
482
|
-
let name = this.config.name.toLowerCase();
|
|
483
|
-
return prefix + '_' + name;
|
|
484
|
-
};
|
|
485
|
-
|
|
486
438
|
/**
|
|
487
439
|
* 更新配置前的钩子函数
|
|
440
|
+
* @param {string} dir 检索的路径
|
|
441
|
+
* @param {boolean} clear 是否清除缓存
|
|
442
|
+
* @returns {Promise<void>} 无返回值
|
|
488
443
|
*/
|
|
489
|
-
Manager.prototype.updateBefore = function () { };
|
|
444
|
+
Manager.prototype.updateBefore = async function (dir, clear) { };
|
|
490
445
|
|
|
491
446
|
/**
|
|
492
|
-
*
|
|
447
|
+
* 更新配置后的钩子函数
|
|
448
|
+
* @param {string} dir 检索的路径
|
|
449
|
+
* @returns {Promise<void>} 无返回值
|
|
493
450
|
*/
|
|
494
|
-
Manager.prototype.updateAfter = function () {
|
|
451
|
+
Manager.prototype.updateAfter = async function (dir) {
|
|
495
452
|
this._sort();
|
|
496
453
|
};
|
|
497
454
|
|
|
@@ -499,7 +456,7 @@ Manager.prototype.updateAfter = function () {
|
|
|
499
456
|
* 排序模块信息
|
|
500
457
|
*/
|
|
501
458
|
Manager.prototype._sort = function () {
|
|
502
|
-
this.
|
|
459
|
+
this.configs.sort((a, b) => {
|
|
503
460
|
// 先按 sort 排序
|
|
504
461
|
let cha = a.sort - b.sort;
|
|
505
462
|
if (cha !== 0) {
|
|
@@ -517,6 +474,8 @@ Manager.prototype._sort = function () {
|
|
|
517
474
|
*/
|
|
518
475
|
Manager.prototype.update = async function (dir, clear = true) {
|
|
519
476
|
try {
|
|
477
|
+
await this.updateBefore(dir, clear);
|
|
478
|
+
|
|
520
479
|
if (clear) {
|
|
521
480
|
await this.clearMods();
|
|
522
481
|
}
|
|
@@ -528,7 +487,10 @@ Manager.prototype.update = async function (dir, clear = true) {
|
|
|
528
487
|
if (!this.config.lazy_load) {
|
|
529
488
|
await this.loadMods();
|
|
530
489
|
}
|
|
531
|
-
|
|
490
|
+
|
|
491
|
+
await this.updateAfter(search_dir);
|
|
492
|
+
// 检索目录开启监听,配置文件变化后增量维护模块
|
|
493
|
+
this._watchDir(search_dir);
|
|
532
494
|
} catch (err) {
|
|
533
495
|
this.log('error', '更新配置失败', err);
|
|
534
496
|
}
|
|
@@ -548,10 +510,111 @@ Manager.prototype._findFiles = async function (dir) {
|
|
|
548
510
|
if (!hasDir(dir)) {
|
|
549
511
|
return [];
|
|
550
512
|
}
|
|
551
|
-
|
|
552
|
-
|
|
513
|
+
let files = this._getLoader().search(dir);
|
|
514
|
+
let filename = this.config.filename;
|
|
515
|
+
if (!filename) {
|
|
516
|
+
return files;
|
|
517
|
+
}
|
|
518
|
+
return files.filter((file) => basename(file) === filename);
|
|
519
|
+
};
|
|
520
|
+
|
|
521
|
+
// ==== 目录监听 ====
|
|
522
|
+
/**
|
|
523
|
+
* 监听目录中配置文件的变化
|
|
524
|
+
* @description 只扫描一次,之后由文件事件增量维护模块
|
|
525
|
+
* @param {string} dir 目录路径
|
|
526
|
+
* @private
|
|
527
|
+
*/
|
|
528
|
+
Manager.prototype._watchDir = function (dir) {
|
|
529
|
+
if (!dir || this._dir_watchers.has(dir)) return;
|
|
530
|
+
if (!hasDir(dir)) return;
|
|
531
|
+
let loader = this._getLoader();
|
|
532
|
+
let success = loader.watch(dir, (changes) => {
|
|
533
|
+
this._applyChanges(changes);
|
|
534
|
+
});
|
|
535
|
+
if (success) {
|
|
536
|
+
this._dir_watchers.set(dir, loader);
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* 应用配置文件变更
|
|
542
|
+
* @param {object[]} changes 变更列表,每项含 type 与 file
|
|
543
|
+
* @private
|
|
544
|
+
*/
|
|
545
|
+
Manager.prototype._applyChanges = function (changes) {
|
|
546
|
+
this._applyChangesAsync(changes).catch((err) => {
|
|
547
|
+
this.log('error', '应用配置文件变更失败', err);
|
|
548
|
+
});
|
|
549
|
+
};
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* 按变更列表增量维护模块
|
|
553
|
+
* @description 新增与修改时重新注册模块,删除时注销模块;热更新模式下仅同步配置快照
|
|
554
|
+
* @param {object[]} changes 变更列表,每项含 type 与 file
|
|
555
|
+
* @returns {Promise<void>} 无返回值
|
|
556
|
+
* @private
|
|
557
|
+
*/
|
|
558
|
+
Manager.prototype._applyChangesAsync = async function (changes) {
|
|
559
|
+
let filename = this.config.filename;
|
|
560
|
+
let lazy = this.config.lazy_load;
|
|
561
|
+
let hot = this.config.mode >= MODE.HOT_CONFIG;
|
|
562
|
+
|
|
563
|
+
for (let change of changes) {
|
|
564
|
+
if (filename && basename(change.file) !== filename) continue;
|
|
565
|
+
let info = this._getInfoByFile(change.file);
|
|
566
|
+
|
|
567
|
+
if (change.type === 'unlink') {
|
|
568
|
+
if (info) await this.unregMod(info.name);
|
|
569
|
+
continue;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
if (change.type === 'change' && info && hot) {
|
|
573
|
+
this._syncInfo(info, change.file);
|
|
574
|
+
continue;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
if (info) await this.unregMod(info.name);
|
|
578
|
+
let mod = this.regMod(change.file);
|
|
579
|
+
if (mod && !lazy) {
|
|
580
|
+
await this.loadMod(mod.config.name);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
this._sort();
|
|
585
|
+
};
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* 按配置文件路径获取模块信息
|
|
589
|
+
* @param {string} file 配置文件路径
|
|
590
|
+
* @returns {object|null} 模块信息,未注册时返回null
|
|
591
|
+
* @private
|
|
592
|
+
*/
|
|
593
|
+
Manager.prototype._getInfoByFile = function (file) {
|
|
594
|
+
return this.configs.find((info) => info.config_file === file) || null;
|
|
595
|
+
};
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* 同步模块信息
|
|
599
|
+
* @description 热更新模式下配置与脚本的重载由驱动负责,这里只维护管理器的配置快照
|
|
600
|
+
* @param {object} info 模块信息
|
|
601
|
+
* @param {string} file 配置文件路径
|
|
602
|
+
* @private
|
|
603
|
+
*/
|
|
604
|
+
Manager.prototype._syncInfo = function (info, file) {
|
|
605
|
+
let mod_config = this._readModConfig(file);
|
|
606
|
+
Object.assign(info, mod_config, { config_file: file });
|
|
607
|
+
};
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* 关闭全部目录监听
|
|
611
|
+
* @returns {void}
|
|
612
|
+
*/
|
|
613
|
+
Manager.prototype.unwatch = function () {
|
|
614
|
+
for (let loader of this._dir_watchers.values()) {
|
|
615
|
+
loader.unwatch();
|
|
553
616
|
}
|
|
554
|
-
|
|
617
|
+
this._dir_watchers.clear();
|
|
555
618
|
};
|
|
556
619
|
|
|
557
620
|
// ==== 高性能调用接口 ====
|
|
@@ -584,12 +647,9 @@ Manager.prototype.main = async function (name, method, ...params) {
|
|
|
584
647
|
*/
|
|
585
648
|
Manager.prototype._runAllEnabled = async function (method, ...params) {
|
|
586
649
|
let result;
|
|
587
|
-
let infos = this.
|
|
650
|
+
let infos = this.configs;
|
|
588
651
|
for (let i = 0; i < infos.length; i++) {
|
|
589
652
|
let info = infos[i];
|
|
590
|
-
// 直接用 info.state 过滤,避免无效实例化
|
|
591
|
-
if (!info.state) continue;
|
|
592
|
-
|
|
593
653
|
let mod = this.getMod(info.name);
|
|
594
654
|
if (!mod) continue;
|
|
595
655
|
|
|
@@ -619,12 +679,9 @@ Manager.prototype._runAllEnabled = async function (method, ...params) {
|
|
|
619
679
|
*/
|
|
620
680
|
Manager.prototype.doAll = async function (method, ...params) {
|
|
621
681
|
let promises = [];
|
|
622
|
-
let infos = this.
|
|
682
|
+
let infos = this.configs;
|
|
623
683
|
for (let i = 0; i < infos.length; i++) {
|
|
624
684
|
let info = infos[i];
|
|
625
|
-
// 直接用 info.state 过滤,避免无效实例化
|
|
626
|
-
if (!info.state) continue;
|
|
627
|
-
|
|
628
685
|
let mod = this.getMod(info.name);
|
|
629
686
|
if (!mod) continue;
|
|
630
687
|
try {
|
|
@@ -641,20 +698,11 @@ Manager.prototype.doAll = async function (method, ...params) {
|
|
|
641
698
|
* @param {object} mod 模块对象
|
|
642
699
|
* @param {string} method 方法名称
|
|
643
700
|
* @param {...any} params 参数集合
|
|
644
|
-
* @returns {Promise<any>}
|
|
701
|
+
* @returns {Promise<any>} 执行结果,模块未运行时返回 null
|
|
645
702
|
* @private
|
|
646
703
|
*/
|
|
647
704
|
Manager.prototype._runMod = async function (mod, method, ...params) {
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
// 执行方法
|
|
651
|
-
let ret = await mod.do(method, ...params);
|
|
652
|
-
// 根据模式决定是否重载
|
|
653
|
-
if (this.config.mode >= MODE.HOT_BOTH) {
|
|
654
|
-
await mod.do('reload');
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
return ret;
|
|
705
|
+
return await this._runModWith(mod, 'do', method, ...params);
|
|
658
706
|
};
|
|
659
707
|
|
|
660
708
|
/**
|
|
@@ -662,17 +710,36 @@ Manager.prototype._runMod = async function (mod, method, ...params) {
|
|
|
662
710
|
* @param {object} mod 模块对象
|
|
663
711
|
* @param {string} method 方法名称
|
|
664
712
|
* @param {...any} params 参数集合
|
|
665
|
-
* @returns {Promise<any>}
|
|
713
|
+
* @returns {Promise<any>} 执行结果,模块未运行时返回 null
|
|
666
714
|
* @private
|
|
667
715
|
*/
|
|
668
716
|
Manager.prototype._callMod = async function (mod, method, ...params) {
|
|
669
|
-
|
|
717
|
+
return await this._runModWith(mod, 'call', method, ...params);
|
|
718
|
+
};
|
|
719
|
+
|
|
720
|
+
/**
|
|
721
|
+
* 按指定执行器运行模块方法 - 内部方法
|
|
722
|
+
* @param {object} mod 模块对象
|
|
723
|
+
* @param {string} handler 执行器名称(do/call)
|
|
724
|
+
* @param {string} method 方法名称
|
|
725
|
+
* @param {...any} params 参数集合
|
|
726
|
+
* @returns {Promise<any>} 执行结果,模块未运行时返回 null
|
|
727
|
+
* @private
|
|
728
|
+
*/
|
|
729
|
+
Manager.prototype._runModWith = async function (mod, handler, method, ...params) {
|
|
730
|
+
// 状态由管理器统一判断:模块未运行则跳过
|
|
731
|
+
let name = mod.config.name;
|
|
732
|
+
let is_running = await this.isRunning(name);
|
|
733
|
+
if (!is_running) {
|
|
734
|
+
return null;
|
|
735
|
+
}
|
|
736
|
+
let target = await this._ensureModLoaded(mod, method);
|
|
670
737
|
|
|
671
738
|
// 执行方法
|
|
672
|
-
let ret = await
|
|
739
|
+
let ret = await target[handler](method, ...params);
|
|
673
740
|
// 根据模式决定是否重载
|
|
674
741
|
if (this.config.mode >= MODE.HOT_BOTH) {
|
|
675
|
-
await
|
|
742
|
+
await target.do('reload');
|
|
676
743
|
}
|
|
677
744
|
|
|
678
745
|
return ret;
|
|
@@ -746,16 +813,18 @@ Manager.prototype._loadCore = async function () {
|
|
|
746
813
|
this._initManager();
|
|
747
814
|
// 加载资源
|
|
748
815
|
await this._loadSources();
|
|
816
|
+
// 标记管理器已完成加载
|
|
817
|
+
this._loaded = true;
|
|
749
818
|
};
|
|
750
819
|
|
|
751
820
|
/**
|
|
752
821
|
* 初始化公共属性
|
|
753
822
|
*/
|
|
754
823
|
Manager.prototype._initCore = async function () {
|
|
755
|
-
if (!
|
|
824
|
+
if (!this._loaded) {
|
|
756
825
|
await this.do('load');
|
|
757
826
|
}
|
|
758
|
-
//
|
|
827
|
+
// 初始化资源
|
|
759
828
|
await this._initSources();
|
|
760
829
|
};
|
|
761
830
|
|
|
@@ -806,19 +875,22 @@ Manager.prototype._ensureModLoaded = async function (mod, method) {
|
|
|
806
875
|
throw new TypeError(`模块(mod)参数不能为空`);
|
|
807
876
|
}
|
|
808
877
|
if (!['load', 'init'].includes(method)) {
|
|
809
|
-
|
|
878
|
+
let name = mod.config.name;
|
|
879
|
+
// 状态由管理器统一判断:无运行状态记录视为尚未加载与初始化
|
|
880
|
+
let is_running = await this.isRunning(name);
|
|
881
|
+
if (is_running) {
|
|
810
882
|
try {
|
|
811
883
|
await mod.do('load');
|
|
812
884
|
} catch (error) {
|
|
813
|
-
throw new Error(`模块${
|
|
885
|
+
throw new Error(`模块${name}加载失败,无法执行方法 ${method}`);
|
|
814
886
|
}
|
|
815
|
-
}
|
|
816
|
-
if (!(await mod.isInited())) {
|
|
817
887
|
try {
|
|
818
888
|
await mod.do('init');
|
|
819
889
|
} catch (error) {
|
|
820
|
-
throw new Error(`模块${
|
|
890
|
+
throw new Error(`模块${name}初始化失败,无法执行方法 ${method}`);
|
|
821
891
|
}
|
|
892
|
+
// 记录受管模块已完成初始化
|
|
893
|
+
await this.setState(name, 'inited');
|
|
822
894
|
}
|
|
823
895
|
};
|
|
824
896
|
return mod;
|
|
@@ -830,11 +902,10 @@ Manager.prototype._ensureModLoaded = async function (mod, method) {
|
|
|
830
902
|
*/
|
|
831
903
|
Manager.prototype._ensureAllLoaded = async function (method) {
|
|
832
904
|
let promises = [];
|
|
833
|
-
let infos = this.
|
|
905
|
+
let infos = this.configs;
|
|
834
906
|
|
|
835
907
|
for (let i = 0; i < infos.length; i++) {
|
|
836
908
|
let info = infos[i];
|
|
837
|
-
if (!info.state) continue;
|
|
838
909
|
let mod = this.getMod(info.name);
|
|
839
910
|
if (!mod) continue;
|
|
840
911
|
try {
|
|
@@ -859,11 +930,10 @@ Manager.prototype.runWait = async function (method, ...params) {
|
|
|
859
930
|
}
|
|
860
931
|
|
|
861
932
|
let result;
|
|
862
|
-
let infos = this.
|
|
933
|
+
let infos = this.configs;
|
|
863
934
|
|
|
864
935
|
for (let i = 0; i < infos.length; i++) {
|
|
865
936
|
let info = infos[i];
|
|
866
|
-
if (!info.state) continue;
|
|
867
937
|
let mod = this.getMod(info.name);
|
|
868
938
|
if (!mod) continue;
|
|
869
939
|
try {
|
|
@@ -894,11 +964,10 @@ Manager.prototype.runAsync = async function (method, ...params) {
|
|
|
894
964
|
// 确保所有模块都已加载
|
|
895
965
|
await this._ensureAllLoaded(method);
|
|
896
966
|
|
|
897
|
-
let infos = this.
|
|
967
|
+
let infos = this.configs;
|
|
898
968
|
|
|
899
969
|
for (let i = 0; i < infos.length; i++) {
|
|
900
970
|
let info = infos[i];
|
|
901
|
-
if (!info.state) continue;
|
|
902
971
|
let mod = this.getMod(info.name);
|
|
903
972
|
if (!mod) continue;
|
|
904
973
|
|
|
@@ -910,6 +979,27 @@ Manager.prototype.runAsync = async function (method, ...params) {
|
|
|
910
979
|
return true;
|
|
911
980
|
};
|
|
912
981
|
|
|
982
|
+
/**
|
|
983
|
+
* 运行模块生命周期方法并同步记录运行状态
|
|
984
|
+
* @param {object} mod 模块对象
|
|
985
|
+
* @param {string} method 方法名称
|
|
986
|
+
* @param {...any} params 方法参数
|
|
987
|
+
* @returns {Promise<any>} 执行结果
|
|
988
|
+
* @private
|
|
989
|
+
*/
|
|
990
|
+
Manager.prototype._runLifecycle = async function (mod, method, ...params) {
|
|
991
|
+
const name = mod.config.name;
|
|
992
|
+
await this._markState(name, method, 'running');
|
|
993
|
+
try {
|
|
994
|
+
const result = await mod.do(method, ...params);
|
|
995
|
+
await this._markState(name, method, 'done');
|
|
996
|
+
return result;
|
|
997
|
+
} catch (error) {
|
|
998
|
+
await this._safeSetState(name, { state: 'error' });
|
|
999
|
+
throw error;
|
|
1000
|
+
}
|
|
1001
|
+
};
|
|
1002
|
+
|
|
913
1003
|
/**
|
|
914
1004
|
* 异步触发事件,并发执行等待完成
|
|
915
1005
|
* @param {string} method 方法名称
|
|
@@ -922,15 +1012,14 @@ Manager.prototype.runAll = async function (method, ...params) {
|
|
|
922
1012
|
}
|
|
923
1013
|
|
|
924
1014
|
const promises = [];
|
|
925
|
-
let infos = this.
|
|
1015
|
+
let infos = this.configs;
|
|
926
1016
|
for (let i = 0; i < infos.length; i++) {
|
|
927
1017
|
let info = infos[i];
|
|
928
|
-
if (!info.state) continue;
|
|
929
1018
|
let mod = this.getMod(info.name);
|
|
930
1019
|
if (!mod) continue;
|
|
931
1020
|
try {
|
|
932
1021
|
mod = await this._ensureModLoaded(mod, method);
|
|
933
|
-
promises.push(
|
|
1022
|
+
promises.push(this._runLifecycle(mod, method, ...params));
|
|
934
1023
|
} catch (error) {
|
|
935
1024
|
this._logModError(info.name, method, error);
|
|
936
1025
|
}
|
|
@@ -954,11 +1043,10 @@ Manager.prototype.runRace = async function (method, ...params) {
|
|
|
954
1043
|
await this._ensureAllLoaded(method);
|
|
955
1044
|
|
|
956
1045
|
const promises = [];
|
|
957
|
-
let infos = this.
|
|
1046
|
+
let infos = this.configs;
|
|
958
1047
|
|
|
959
1048
|
for (let i = 0; i < infos.length; i++) {
|
|
960
1049
|
let info = infos[i];
|
|
961
|
-
if (!info.state) continue;
|
|
962
1050
|
let mod = this.getMod(info.name);
|
|
963
1051
|
if (!mod) continue;
|
|
964
1052
|
|
|
@@ -984,11 +1072,10 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
|
|
|
984
1072
|
throw new TypeError('方法名称必须是字符串');
|
|
985
1073
|
}
|
|
986
1074
|
|
|
987
|
-
let infos = this.
|
|
1075
|
+
let infos = this.configs;
|
|
988
1076
|
|
|
989
1077
|
for (let i = 0; i < infos.length; i++) {
|
|
990
1078
|
let info = infos[i];
|
|
991
|
-
if (!info.state) continue;
|
|
992
1079
|
let mod = this.getMod(info.name);
|
|
993
1080
|
if (!mod) continue;
|
|
994
1081
|
try {
|
|
@@ -1046,10 +1133,6 @@ Manager.prototype.createFile = function (config, file) {
|
|
|
1046
1133
|
let f = file;
|
|
1047
1134
|
if (!f) {
|
|
1048
1135
|
f = `${config.name}/${this.config.filename}`;
|
|
1049
|
-
if (this.config.search_way === 'dir') {
|
|
1050
|
-
let dir_name = this.getDirName();
|
|
1051
|
-
f = `${dir_name}/${f}`;
|
|
1052
|
-
}
|
|
1053
1136
|
}
|
|
1054
1137
|
f = fullname(f, this.config.dir);
|
|
1055
1138
|
mod.createConfigFile(f, config);
|