mm_machine 3.5.5 → 3.5.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.
- package/example/index.js +1 -4
- package/lib/drive.js +19 -81
- package/lib/manager.js +245 -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,27 @@ 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
|
+
return mod_config;
|
|
276
|
+
}
|
|
277
|
+
let file_config = hasFile(config_file) ? this._getLoader().load(config_file) : null;
|
|
278
|
+
mod_config = { ...file_config, ...mod_config };
|
|
279
|
+
if (!mod_config.name) {
|
|
280
|
+
mod_config.name = basename(dirname(config_file));
|
|
281
|
+
}
|
|
282
|
+
return mod_config;
|
|
283
|
+
};
|
|
284
|
+
|
|
333
285
|
/**
|
|
334
286
|
* 创建模块
|
|
335
287
|
* @param {string} config_file 配置文件路径
|
|
@@ -340,20 +292,22 @@ Manager.prototype.getMods = function () {
|
|
|
340
292
|
Manager.prototype.newMod = function (config_file, config, script) {
|
|
341
293
|
try {
|
|
342
294
|
let mod;
|
|
295
|
+
let mod_config = null;
|
|
343
296
|
switch (this.config.mod_type) {
|
|
344
297
|
case 'script':
|
|
345
298
|
mod = new Mod(config, this.getParent() || this);
|
|
346
299
|
break;
|
|
347
300
|
default:
|
|
348
|
-
|
|
301
|
+
// 配置由管理器读取,模块构造时即具备完整配置
|
|
302
|
+
mod_config = this._readModConfig(config_file, config);
|
|
303
|
+
mod = new this.Drive(mod_config, this.getParent() || this);
|
|
349
304
|
break;
|
|
350
305
|
}
|
|
351
306
|
mod.mode = this.config.mode;
|
|
352
307
|
|
|
353
|
-
if (
|
|
354
|
-
mod.config_file
|
|
355
|
-
|
|
356
|
-
mod.loadConfig(config_file);
|
|
308
|
+
if (mod_config && config_file) {
|
|
309
|
+
mod.applyConfig(config_file, mod_config);
|
|
310
|
+
mod.watchConfig(config_file);
|
|
357
311
|
}
|
|
358
312
|
|
|
359
313
|
if (script) {
|
|
@@ -385,7 +339,7 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
|
|
|
385
339
|
if (!mod) return null;
|
|
386
340
|
this.setMod(mod.config.name, mod);
|
|
387
341
|
let info = { ...mod.config, config_file: config_file };
|
|
388
|
-
this.
|
|
342
|
+
this.configs.push(info);
|
|
389
343
|
return mod;
|
|
390
344
|
};
|
|
391
345
|
|
|
@@ -473,25 +427,20 @@ Manager.prototype.loadMods = async function () {
|
|
|
473
427
|
await Promise.all(promises);
|
|
474
428
|
};
|
|
475
429
|
|
|
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
430
|
/**
|
|
487
431
|
* 更新配置前的钩子函数
|
|
432
|
+
* @param {string} dir 检索的路径
|
|
433
|
+
* @param {boolean} clear 是否清除缓存
|
|
434
|
+
* @returns {Promise<void>} 无返回值
|
|
488
435
|
*/
|
|
489
|
-
Manager.prototype.updateBefore = function () { };
|
|
436
|
+
Manager.prototype.updateBefore = async function (dir, clear) { };
|
|
490
437
|
|
|
491
438
|
/**
|
|
492
|
-
*
|
|
439
|
+
* 更新配置后的钩子函数
|
|
440
|
+
* @param {string} dir 检索的路径
|
|
441
|
+
* @returns {Promise<void>} 无返回值
|
|
493
442
|
*/
|
|
494
|
-
Manager.prototype.updateAfter = function () {
|
|
443
|
+
Manager.prototype.updateAfter = async function (dir) {
|
|
495
444
|
this._sort();
|
|
496
445
|
};
|
|
497
446
|
|
|
@@ -499,7 +448,7 @@ Manager.prototype.updateAfter = function () {
|
|
|
499
448
|
* 排序模块信息
|
|
500
449
|
*/
|
|
501
450
|
Manager.prototype._sort = function () {
|
|
502
|
-
this.
|
|
451
|
+
this.configs.sort((a, b) => {
|
|
503
452
|
// 先按 sort 排序
|
|
504
453
|
let cha = a.sort - b.sort;
|
|
505
454
|
if (cha !== 0) {
|
|
@@ -517,6 +466,8 @@ Manager.prototype._sort = function () {
|
|
|
517
466
|
*/
|
|
518
467
|
Manager.prototype.update = async function (dir, clear = true) {
|
|
519
468
|
try {
|
|
469
|
+
await this.updateBefore(dir, clear);
|
|
470
|
+
|
|
520
471
|
if (clear) {
|
|
521
472
|
await this.clearMods();
|
|
522
473
|
}
|
|
@@ -528,7 +479,10 @@ Manager.prototype.update = async function (dir, clear = true) {
|
|
|
528
479
|
if (!this.config.lazy_load) {
|
|
529
480
|
await this.loadMods();
|
|
530
481
|
}
|
|
531
|
-
|
|
482
|
+
|
|
483
|
+
await this.updateAfter(search_dir);
|
|
484
|
+
// 检索目录开启监听,配置文件变化后增量维护模块
|
|
485
|
+
this._watchDir(search_dir);
|
|
532
486
|
} catch (err) {
|
|
533
487
|
this.log('error', '更新配置失败', err);
|
|
534
488
|
}
|
|
@@ -548,10 +502,111 @@ Manager.prototype._findFiles = async function (dir) {
|
|
|
548
502
|
if (!hasDir(dir)) {
|
|
549
503
|
return [];
|
|
550
504
|
}
|
|
551
|
-
|
|
552
|
-
|
|
505
|
+
let files = this._getLoader().search(dir);
|
|
506
|
+
let filename = this.config.filename;
|
|
507
|
+
if (!filename) {
|
|
508
|
+
return files;
|
|
553
509
|
}
|
|
554
|
-
return
|
|
510
|
+
return files.filter((file) => basename(file) === filename);
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
// ==== 目录监听 ====
|
|
514
|
+
/**
|
|
515
|
+
* 监听目录中配置文件的变化
|
|
516
|
+
* @description 只扫描一次,之后由文件事件增量维护模块
|
|
517
|
+
* @param {string} dir 目录路径
|
|
518
|
+
* @private
|
|
519
|
+
*/
|
|
520
|
+
Manager.prototype._watchDir = function (dir) {
|
|
521
|
+
if (!dir || this._dir_watchers.has(dir)) return;
|
|
522
|
+
if (!hasDir(dir)) return;
|
|
523
|
+
let loader = this._getLoader();
|
|
524
|
+
let success = loader.watch(dir, (changes) => {
|
|
525
|
+
this._applyChanges(changes);
|
|
526
|
+
});
|
|
527
|
+
if (success) {
|
|
528
|
+
this._dir_watchers.set(dir, loader);
|
|
529
|
+
}
|
|
530
|
+
};
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* 应用配置文件变更
|
|
534
|
+
* @param {object[]} changes 变更列表,每项含 type 与 file
|
|
535
|
+
* @private
|
|
536
|
+
*/
|
|
537
|
+
Manager.prototype._applyChanges = function (changes) {
|
|
538
|
+
this._applyChangesAsync(changes).catch((err) => {
|
|
539
|
+
this.log('error', '应用配置文件变更失败', err);
|
|
540
|
+
});
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* 按变更列表增量维护模块
|
|
545
|
+
* @description 新增与修改时重新注册模块,删除时注销模块;热更新模式下仅同步配置快照
|
|
546
|
+
* @param {object[]} changes 变更列表,每项含 type 与 file
|
|
547
|
+
* @returns {Promise<void>} 无返回值
|
|
548
|
+
* @private
|
|
549
|
+
*/
|
|
550
|
+
Manager.prototype._applyChangesAsync = async function (changes) {
|
|
551
|
+
let filename = this.config.filename;
|
|
552
|
+
let lazy = this.config.lazy_load;
|
|
553
|
+
let hot = this.config.mode >= MODE.HOT_CONFIG;
|
|
554
|
+
|
|
555
|
+
for (let change of changes) {
|
|
556
|
+
if (filename && basename(change.file) !== filename) continue;
|
|
557
|
+
let info = this._getInfoByFile(change.file);
|
|
558
|
+
|
|
559
|
+
if (change.type === 'unlink') {
|
|
560
|
+
if (info) await this.unregMod(info.name);
|
|
561
|
+
continue;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
if (change.type === 'change' && info && hot) {
|
|
565
|
+
this._syncInfo(info, change.file);
|
|
566
|
+
continue;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
if (info) await this.unregMod(info.name);
|
|
570
|
+
let mod = this.regMod(change.file);
|
|
571
|
+
if (mod && !lazy) {
|
|
572
|
+
await this.loadMod(mod.config.name);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
this._sort();
|
|
577
|
+
};
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* 按配置文件路径获取模块信息
|
|
581
|
+
* @param {string} file 配置文件路径
|
|
582
|
+
* @returns {object|null} 模块信息,未注册时返回null
|
|
583
|
+
* @private
|
|
584
|
+
*/
|
|
585
|
+
Manager.prototype._getInfoByFile = function (file) {
|
|
586
|
+
return this.configs.find((info) => info.config_file === file) || null;
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* 同步模块信息
|
|
591
|
+
* @description 热更新模式下配置与脚本的重载由驱动负责,这里只维护管理器的配置快照
|
|
592
|
+
* @param {object} info 模块信息
|
|
593
|
+
* @param {string} file 配置文件路径
|
|
594
|
+
* @private
|
|
595
|
+
*/
|
|
596
|
+
Manager.prototype._syncInfo = function (info, file) {
|
|
597
|
+
let mod_config = this._readModConfig(file);
|
|
598
|
+
Object.assign(info, mod_config, { config_file: file });
|
|
599
|
+
};
|
|
600
|
+
|
|
601
|
+
/**
|
|
602
|
+
* 关闭全部目录监听
|
|
603
|
+
* @returns {void}
|
|
604
|
+
*/
|
|
605
|
+
Manager.prototype.unwatch = function () {
|
|
606
|
+
for (let loader of this._dir_watchers.values()) {
|
|
607
|
+
loader.unwatch();
|
|
608
|
+
}
|
|
609
|
+
this._dir_watchers.clear();
|
|
555
610
|
};
|
|
556
611
|
|
|
557
612
|
// ==== 高性能调用接口 ====
|
|
@@ -584,12 +639,9 @@ Manager.prototype.main = async function (name, method, ...params) {
|
|
|
584
639
|
*/
|
|
585
640
|
Manager.prototype._runAllEnabled = async function (method, ...params) {
|
|
586
641
|
let result;
|
|
587
|
-
let infos = this.
|
|
642
|
+
let infos = this.configs;
|
|
588
643
|
for (let i = 0; i < infos.length; i++) {
|
|
589
644
|
let info = infos[i];
|
|
590
|
-
// 直接用 info.state 过滤,避免无效实例化
|
|
591
|
-
if (!info.state) continue;
|
|
592
|
-
|
|
593
645
|
let mod = this.getMod(info.name);
|
|
594
646
|
if (!mod) continue;
|
|
595
647
|
|
|
@@ -619,12 +671,9 @@ Manager.prototype._runAllEnabled = async function (method, ...params) {
|
|
|
619
671
|
*/
|
|
620
672
|
Manager.prototype.doAll = async function (method, ...params) {
|
|
621
673
|
let promises = [];
|
|
622
|
-
let infos = this.
|
|
674
|
+
let infos = this.configs;
|
|
623
675
|
for (let i = 0; i < infos.length; i++) {
|
|
624
676
|
let info = infos[i];
|
|
625
|
-
// 直接用 info.state 过滤,避免无效实例化
|
|
626
|
-
if (!info.state) continue;
|
|
627
|
-
|
|
628
677
|
let mod = this.getMod(info.name);
|
|
629
678
|
if (!mod) continue;
|
|
630
679
|
try {
|
|
@@ -641,20 +690,11 @@ Manager.prototype.doAll = async function (method, ...params) {
|
|
|
641
690
|
* @param {object} mod 模块对象
|
|
642
691
|
* @param {string} method 方法名称
|
|
643
692
|
* @param {...any} params 参数集合
|
|
644
|
-
* @returns {Promise<any>}
|
|
693
|
+
* @returns {Promise<any>} 执行结果,模块未运行时返回 null
|
|
645
694
|
* @private
|
|
646
695
|
*/
|
|
647
696
|
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;
|
|
697
|
+
return await this._runModWith(mod, 'do', method, ...params);
|
|
658
698
|
};
|
|
659
699
|
|
|
660
700
|
/**
|
|
@@ -662,17 +702,36 @@ Manager.prototype._runMod = async function (mod, method, ...params) {
|
|
|
662
702
|
* @param {object} mod 模块对象
|
|
663
703
|
* @param {string} method 方法名称
|
|
664
704
|
* @param {...any} params 参数集合
|
|
665
|
-
* @returns {Promise<any>}
|
|
705
|
+
* @returns {Promise<any>} 执行结果,模块未运行时返回 null
|
|
666
706
|
* @private
|
|
667
707
|
*/
|
|
668
708
|
Manager.prototype._callMod = async function (mod, method, ...params) {
|
|
669
|
-
|
|
709
|
+
return await this._runModWith(mod, 'call', method, ...params);
|
|
710
|
+
};
|
|
711
|
+
|
|
712
|
+
/**
|
|
713
|
+
* 按指定执行器运行模块方法 - 内部方法
|
|
714
|
+
* @param {object} mod 模块对象
|
|
715
|
+
* @param {string} handler 执行器名称(do/call)
|
|
716
|
+
* @param {string} method 方法名称
|
|
717
|
+
* @param {...any} params 参数集合
|
|
718
|
+
* @returns {Promise<any>} 执行结果,模块未运行时返回 null
|
|
719
|
+
* @private
|
|
720
|
+
*/
|
|
721
|
+
Manager.prototype._runModWith = async function (mod, handler, method, ...params) {
|
|
722
|
+
// 状态由管理器统一判断:模块未运行则跳过
|
|
723
|
+
let name = mod.config.name;
|
|
724
|
+
let is_running = await this.isRunning(name);
|
|
725
|
+
if (!is_running) {
|
|
726
|
+
return null;
|
|
727
|
+
}
|
|
728
|
+
let target = await this._ensureModLoaded(mod, method);
|
|
670
729
|
|
|
671
730
|
// 执行方法
|
|
672
|
-
let ret = await
|
|
731
|
+
let ret = await target[handler](method, ...params);
|
|
673
732
|
// 根据模式决定是否重载
|
|
674
733
|
if (this.config.mode >= MODE.HOT_BOTH) {
|
|
675
|
-
await
|
|
734
|
+
await target.do('reload');
|
|
676
735
|
}
|
|
677
736
|
|
|
678
737
|
return ret;
|
|
@@ -746,16 +805,18 @@ Manager.prototype._loadCore = async function () {
|
|
|
746
805
|
this._initManager();
|
|
747
806
|
// 加载资源
|
|
748
807
|
await this._loadSources();
|
|
808
|
+
// 标记管理器已完成加载
|
|
809
|
+
this._loaded = true;
|
|
749
810
|
};
|
|
750
811
|
|
|
751
812
|
/**
|
|
752
813
|
* 初始化公共属性
|
|
753
814
|
*/
|
|
754
815
|
Manager.prototype._initCore = async function () {
|
|
755
|
-
if (!
|
|
816
|
+
if (!this._loaded) {
|
|
756
817
|
await this.do('load');
|
|
757
818
|
}
|
|
758
|
-
//
|
|
819
|
+
// 初始化资源
|
|
759
820
|
await this._initSources();
|
|
760
821
|
};
|
|
761
822
|
|
|
@@ -806,19 +867,22 @@ Manager.prototype._ensureModLoaded = async function (mod, method) {
|
|
|
806
867
|
throw new TypeError(`模块(mod)参数不能为空`);
|
|
807
868
|
}
|
|
808
869
|
if (!['load', 'init'].includes(method)) {
|
|
809
|
-
|
|
870
|
+
let name = mod.config.name;
|
|
871
|
+
// 状态由管理器统一判断:无运行状态记录视为尚未加载与初始化
|
|
872
|
+
let is_running = await this.isRunning(name);
|
|
873
|
+
if (is_running) {
|
|
810
874
|
try {
|
|
811
875
|
await mod.do('load');
|
|
812
876
|
} catch (error) {
|
|
813
|
-
throw new Error(`模块${
|
|
877
|
+
throw new Error(`模块${name}加载失败,无法执行方法 ${method}`);
|
|
814
878
|
}
|
|
815
|
-
}
|
|
816
|
-
if (!(await mod.isInited())) {
|
|
817
879
|
try {
|
|
818
880
|
await mod.do('init');
|
|
819
881
|
} catch (error) {
|
|
820
|
-
throw new Error(`模块${
|
|
882
|
+
throw new Error(`模块${name}初始化失败,无法执行方法 ${method}`);
|
|
821
883
|
}
|
|
884
|
+
// 记录受管模块已完成初始化
|
|
885
|
+
await this.setState(name, 'inited');
|
|
822
886
|
}
|
|
823
887
|
};
|
|
824
888
|
return mod;
|
|
@@ -830,11 +894,10 @@ Manager.prototype._ensureModLoaded = async function (mod, method) {
|
|
|
830
894
|
*/
|
|
831
895
|
Manager.prototype._ensureAllLoaded = async function (method) {
|
|
832
896
|
let promises = [];
|
|
833
|
-
let infos = this.
|
|
897
|
+
let infos = this.configs;
|
|
834
898
|
|
|
835
899
|
for (let i = 0; i < infos.length; i++) {
|
|
836
900
|
let info = infos[i];
|
|
837
|
-
if (!info.state) continue;
|
|
838
901
|
let mod = this.getMod(info.name);
|
|
839
902
|
if (!mod) continue;
|
|
840
903
|
try {
|
|
@@ -859,11 +922,10 @@ Manager.prototype.runWait = async function (method, ...params) {
|
|
|
859
922
|
}
|
|
860
923
|
|
|
861
924
|
let result;
|
|
862
|
-
let infos = this.
|
|
925
|
+
let infos = this.configs;
|
|
863
926
|
|
|
864
927
|
for (let i = 0; i < infos.length; i++) {
|
|
865
928
|
let info = infos[i];
|
|
866
|
-
if (!info.state) continue;
|
|
867
929
|
let mod = this.getMod(info.name);
|
|
868
930
|
if (!mod) continue;
|
|
869
931
|
try {
|
|
@@ -894,11 +956,10 @@ Manager.prototype.runAsync = async function (method, ...params) {
|
|
|
894
956
|
// 确保所有模块都已加载
|
|
895
957
|
await this._ensureAllLoaded(method);
|
|
896
958
|
|
|
897
|
-
let infos = this.
|
|
959
|
+
let infos = this.configs;
|
|
898
960
|
|
|
899
961
|
for (let i = 0; i < infos.length; i++) {
|
|
900
962
|
let info = infos[i];
|
|
901
|
-
if (!info.state) continue;
|
|
902
963
|
let mod = this.getMod(info.name);
|
|
903
964
|
if (!mod) continue;
|
|
904
965
|
|
|
@@ -910,6 +971,27 @@ Manager.prototype.runAsync = async function (method, ...params) {
|
|
|
910
971
|
return true;
|
|
911
972
|
};
|
|
912
973
|
|
|
974
|
+
/**
|
|
975
|
+
* 运行模块生命周期方法并同步记录运行状态
|
|
976
|
+
* @param {object} mod 模块对象
|
|
977
|
+
* @param {string} method 方法名称
|
|
978
|
+
* @param {...any} params 方法参数
|
|
979
|
+
* @returns {Promise<any>} 执行结果
|
|
980
|
+
* @private
|
|
981
|
+
*/
|
|
982
|
+
Manager.prototype._runLifecycle = async function (mod, method, ...params) {
|
|
983
|
+
const name = mod.config.name;
|
|
984
|
+
await this._markState(name, method, 'running');
|
|
985
|
+
try {
|
|
986
|
+
const result = await mod.do(method, ...params);
|
|
987
|
+
await this._markState(name, method, 'done');
|
|
988
|
+
return result;
|
|
989
|
+
} catch (error) {
|
|
990
|
+
await this._safeSetState(name, { state: 'error' });
|
|
991
|
+
throw error;
|
|
992
|
+
}
|
|
993
|
+
};
|
|
994
|
+
|
|
913
995
|
/**
|
|
914
996
|
* 异步触发事件,并发执行等待完成
|
|
915
997
|
* @param {string} method 方法名称
|
|
@@ -922,15 +1004,14 @@ Manager.prototype.runAll = async function (method, ...params) {
|
|
|
922
1004
|
}
|
|
923
1005
|
|
|
924
1006
|
const promises = [];
|
|
925
|
-
let infos = this.
|
|
1007
|
+
let infos = this.configs;
|
|
926
1008
|
for (let i = 0; i < infos.length; i++) {
|
|
927
1009
|
let info = infos[i];
|
|
928
|
-
if (!info.state) continue;
|
|
929
1010
|
let mod = this.getMod(info.name);
|
|
930
1011
|
if (!mod) continue;
|
|
931
1012
|
try {
|
|
932
1013
|
mod = await this._ensureModLoaded(mod, method);
|
|
933
|
-
promises.push(
|
|
1014
|
+
promises.push(this._runLifecycle(mod, method, ...params));
|
|
934
1015
|
} catch (error) {
|
|
935
1016
|
this._logModError(info.name, method, error);
|
|
936
1017
|
}
|
|
@@ -954,11 +1035,10 @@ Manager.prototype.runRace = async function (method, ...params) {
|
|
|
954
1035
|
await this._ensureAllLoaded(method);
|
|
955
1036
|
|
|
956
1037
|
const promises = [];
|
|
957
|
-
let infos = this.
|
|
1038
|
+
let infos = this.configs;
|
|
958
1039
|
|
|
959
1040
|
for (let i = 0; i < infos.length; i++) {
|
|
960
1041
|
let info = infos[i];
|
|
961
|
-
if (!info.state) continue;
|
|
962
1042
|
let mod = this.getMod(info.name);
|
|
963
1043
|
if (!mod) continue;
|
|
964
1044
|
|
|
@@ -984,11 +1064,10 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
|
|
|
984
1064
|
throw new TypeError('方法名称必须是字符串');
|
|
985
1065
|
}
|
|
986
1066
|
|
|
987
|
-
let infos = this.
|
|
1067
|
+
let infos = this.configs;
|
|
988
1068
|
|
|
989
1069
|
for (let i = 0; i < infos.length; i++) {
|
|
990
1070
|
let info = infos[i];
|
|
991
|
-
if (!info.state) continue;
|
|
992
1071
|
let mod = this.getMod(info.name);
|
|
993
1072
|
if (!mod) continue;
|
|
994
1073
|
try {
|
|
@@ -1046,10 +1125,6 @@ Manager.prototype.createFile = function (config, file) {
|
|
|
1046
1125
|
let f = file;
|
|
1047
1126
|
if (!f) {
|
|
1048
1127
|
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
1128
|
}
|
|
1054
1129
|
f = fullname(f, this.config.dir);
|
|
1055
1130
|
mod.createConfigFile(f, config);
|