mm_machine 2.9.6 → 2.9.8
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/drive.js +480 -675
- package/index.js +48 -1497
- package/mod.js +452 -494
- package/package.json +3 -4
- package/scanner.js +62 -0
- package/dir_scanner.js +0 -262
- package/id_gen.js +0 -138
- package/registry.js +0 -634
- package/word.js +0 -209
package/mod.js
CHANGED
|
@@ -1,333 +1,398 @@
|
|
|
1
|
+
const { Base } = require('mm_expand');
|
|
2
|
+
|
|
3
|
+
require('mm_hot_reload');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 增强require函数,专门用于脚本文件(.js)热重载
|
|
7
|
+
* 当脚本文件内容发生变化时,自动重新加载并触发回调
|
|
8
|
+
* @param {string} file 脚本文件路径(.js)
|
|
9
|
+
* @param {Function} func 热更新回调函数,文件变化时触发
|
|
10
|
+
* @returns {object} 加载的模块对象
|
|
11
|
+
* @throws {TypeError} 文件路径不是字符串或回调不是函数时抛出
|
|
12
|
+
*/
|
|
13
|
+
$.require = function (file, func) {
|
|
14
|
+
// 参数校验
|
|
15
|
+
if (typeof file !== 'string') {
|
|
16
|
+
throw new TypeError('第一个参数(file)必须是字符串');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (func && typeof func !== 'function') {
|
|
20
|
+
throw new TypeError('第二个参数(func)必须是函数');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// 确保文件是.js文件
|
|
24
|
+
if (!file.endsWith('.js')) {
|
|
25
|
+
throw new TypeError('$.require 只能用于.js脚本文件');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return $.mod.load(file, func);
|
|
29
|
+
};
|
|
30
|
+
|
|
1
31
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* @
|
|
5
|
-
* @
|
|
32
|
+
* 增强JSON加载函数,专门用于配置文件(.json)热重载
|
|
33
|
+
* 当配置文件内容发生变化时,自动重新加载并触发回调
|
|
34
|
+
* @param {string} file 配置文件路径(.json)
|
|
35
|
+
* @param {Function} func 热更新回调函数,文件变化时触发
|
|
36
|
+
* @returns {object} 解析的JSON对象
|
|
37
|
+
* @throws {TypeError} 文件路径不是字符串或回调不是函数时抛出
|
|
6
38
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
39
|
+
$.loadJson = function (file, func) {
|
|
40
|
+
// 参数校验
|
|
41
|
+
if (typeof file !== 'string') {
|
|
42
|
+
throw new TypeError('第一个参数(file)必须是字符串');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (func && typeof func !== 'function') {
|
|
46
|
+
throw new TypeError('第二个参数(func)必须是函数');
|
|
47
|
+
}
|
|
9
48
|
|
|
49
|
+
// 确保文件是.json文件
|
|
50
|
+
if (!file.endsWith('.json')) {
|
|
51
|
+
throw new TypeError('$.loadJson 只能用于.json配置文件');
|
|
52
|
+
}
|
|
53
|
+
return $.mod.load(file, func);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 通用模块类
|
|
58
|
+
*/
|
|
10
59
|
class Mod extends Base {
|
|
11
60
|
/**
|
|
12
|
-
*
|
|
13
|
-
* @param {object} config 配置参数
|
|
14
|
-
* @param {string} [config.name] 模块名称
|
|
15
|
-
* @param {string} [config.lifecycle_state='created'] 初始生命周期状态
|
|
61
|
+
* 配置项
|
|
16
62
|
*/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
63
|
+
static config = {
|
|
64
|
+
// 模块名称(唯一标识)
|
|
65
|
+
name: '',
|
|
66
|
+
// 模块标题(中文名称)
|
|
67
|
+
title: '',
|
|
68
|
+
// 模块描述
|
|
69
|
+
description: '',
|
|
70
|
+
// 状态, 可选值: 1-启用, 0-禁用
|
|
71
|
+
state: 1,
|
|
72
|
+
// 是否需要缓存数据
|
|
73
|
+
need_cache: false,
|
|
74
|
+
// 缓存文件路径
|
|
75
|
+
cache_file: ''
|
|
76
|
+
};
|
|
23
77
|
|
|
24
|
-
|
|
25
|
-
|
|
78
|
+
/**
|
|
79
|
+
* 构造函数
|
|
80
|
+
* @param {object} config 配置对象
|
|
81
|
+
* @param {object} parent 父模块
|
|
82
|
+
*/
|
|
83
|
+
constructor(config, parent) {
|
|
84
|
+
super(config, parent);
|
|
85
|
+
if (parent) {
|
|
26
86
|
this.getParent = function () {
|
|
27
|
-
return
|
|
87
|
+
return parent;
|
|
28
88
|
};
|
|
29
89
|
}
|
|
90
|
+
// 脚本方法
|
|
91
|
+
this._methods = {};
|
|
92
|
+
// 脚本数据
|
|
93
|
+
this._datas = {};
|
|
94
|
+
this._is_loaded = false;
|
|
30
95
|
}
|
|
31
96
|
}
|
|
32
97
|
|
|
33
|
-
// ==================== 生命周期公共方法 ====================
|
|
34
|
-
|
|
35
98
|
/**
|
|
36
|
-
*
|
|
37
|
-
* @returns {
|
|
99
|
+
* 检查模块是否已加载
|
|
100
|
+
* @returns {boolean} 是否已加载
|
|
38
101
|
*/
|
|
39
|
-
Mod.prototype.
|
|
40
|
-
return this.
|
|
102
|
+
Mod.prototype.isLoaded = function () {
|
|
103
|
+
return this._is_loaded;
|
|
41
104
|
};
|
|
42
105
|
|
|
43
106
|
/**
|
|
44
|
-
*
|
|
45
|
-
* @
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
107
|
+
* 日志输出
|
|
108
|
+
* @param {string} level 日志级别
|
|
109
|
+
* @param {string} message 日志消息
|
|
110
|
+
* @param {...any} params 日志参数
|
|
111
|
+
*/
|
|
112
|
+
Mod.prototype.log = function (level, message, ...params) {
|
|
113
|
+
this.getLogger()[level](`[${this.constructor.name}.${this.config.name}] ${message}`, ...params);
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 监听事件
|
|
118
|
+
*/
|
|
119
|
+
Mod.prototype._listen = function () {
|
|
120
|
+
// 触发时机:模块被加载到内存时(注册后、实例化后)
|
|
121
|
+
this._onLoad();
|
|
122
|
+
// 触发时机:模块启动前准备(依赖已解析,但未启动)
|
|
123
|
+
this._onInit();
|
|
124
|
+
// 触发时机:模块正式启动(业务功能可用)
|
|
125
|
+
this._onStart();
|
|
126
|
+
// 触发时机:模块停止(业务功能不可用)
|
|
127
|
+
this._onStop();
|
|
128
|
+
// 触发时机:销毁前清理资源
|
|
129
|
+
this._onDestroy();
|
|
130
|
+
// 触发时机:模块从内存中卸载(销毁后)
|
|
131
|
+
this._onUnload();
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* 监听加载事件
|
|
136
|
+
* @param {object} ctx 上下文对象
|
|
137
|
+
*/
|
|
138
|
+
Mod.prototype._onLoad = async function (ctx) {
|
|
139
|
+
// 1. 加载
|
|
140
|
+
this.on('load:before', async (ctx) => {
|
|
141
|
+
this.setState('loading');
|
|
142
|
+
await this._loadCore(...ctx.params);
|
|
143
|
+
});
|
|
144
|
+
this.on('load:after', (ctx) => {
|
|
145
|
+
this.setState('loaded');
|
|
146
|
+
this._is_loaded = true;
|
|
147
|
+
});
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* 监听启动事件
|
|
152
|
+
* @param {object} ctx 上下文对象
|
|
153
|
+
*/
|
|
154
|
+
Mod.prototype._onStart = async function (ctx) {
|
|
155
|
+
let status_last = 'inited';
|
|
156
|
+
// 3. 启动
|
|
157
|
+
this.on('start:before', async (ctx) => {
|
|
158
|
+
status_last = this.getState();
|
|
159
|
+
this.setState('starting');
|
|
160
|
+
await this._startCore(...ctx.params);
|
|
161
|
+
});
|
|
162
|
+
this.on('start:success', (ctx) => {
|
|
163
|
+
this.setState('running');
|
|
164
|
+
});
|
|
165
|
+
this.on('start:error', (ctx) => {
|
|
166
|
+
this.setState(status_last);
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* 监听停止事件
|
|
172
|
+
* @param {object} ctx 上下文对象
|
|
173
|
+
*/
|
|
174
|
+
Mod.prototype._onStop = async function (ctx) {
|
|
175
|
+
let status_last = 'running';
|
|
176
|
+
// 4. 停止
|
|
177
|
+
this.on('stop:before', async (ctx) => {
|
|
178
|
+
status_last = this.getState();
|
|
179
|
+
this.setState('stopping');
|
|
180
|
+
});
|
|
181
|
+
this.on('stop:after', async (ctx) => {
|
|
182
|
+
await this._stopCore(...ctx.params);
|
|
183
|
+
});
|
|
184
|
+
this.on('stop:success', async (ctx) => {
|
|
185
|
+
this.setState('stopped');
|
|
186
|
+
});
|
|
187
|
+
this.on('stop:error', (ctx) => {
|
|
188
|
+
this.setState(status_last);
|
|
189
|
+
});
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* 监听销毁事件
|
|
194
|
+
*/
|
|
195
|
+
Mod.prototype._onDestroy = function () {
|
|
196
|
+
let status_last = 'stopped';
|
|
197
|
+
// 销毁
|
|
198
|
+
this.on('destroy:before', async (ctx) => {
|
|
199
|
+
status_last = this.getState();
|
|
200
|
+
this.setState('destroying');
|
|
201
|
+
});
|
|
202
|
+
this.on('destroy:check', (ctx) => {
|
|
203
|
+
if (status_last !== 'unloaded') {
|
|
204
|
+
this.setState(status_last);
|
|
205
|
+
ctx.error = new Error('销毁前必须先卸载');
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
this.on('destroy:after', async (ctx) => {
|
|
209
|
+
await this._destroyCore(...ctx.params);
|
|
210
|
+
});
|
|
211
|
+
this.on('destroy:success', (ctx) => {
|
|
212
|
+
this.setState('destroyed');
|
|
213
|
+
});
|
|
214
|
+
this.on('destroy:error', (ctx) => {
|
|
215
|
+
this.setState(status_last);
|
|
216
|
+
});
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* 监听卸载事件
|
|
221
|
+
* @param {object} ctx 上下文对象
|
|
222
|
+
*/
|
|
223
|
+
Mod.prototype._onUnload = async function (ctx) {
|
|
224
|
+
let status_last = 'stopped';
|
|
225
|
+
// 5. 卸载
|
|
226
|
+
this.on('unload:before', (ctx) => {
|
|
227
|
+
status_last = this.getState();
|
|
228
|
+
this.setState('unloading');
|
|
229
|
+
});
|
|
230
|
+
this.on('unload:check', (ctx) => {
|
|
231
|
+
if (status_last !== 'stopped') {
|
|
232
|
+
status_last = this.getState();
|
|
233
|
+
ctx.error = new Error('卸载前必须先停止');
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
this.on('unload:after', async (ctx) => {
|
|
237
|
+
this.setState('unloaded');
|
|
238
|
+
await this._unloadCore(...ctx.params);
|
|
239
|
+
this._is_loaded = false;
|
|
240
|
+
});
|
|
49
241
|
};
|
|
50
242
|
|
|
51
243
|
/**
|
|
52
|
-
*
|
|
53
|
-
* @
|
|
244
|
+
* 加载 - 内部方法
|
|
245
|
+
* @param {...any} args 加载参数
|
|
246
|
+
* @returns {object} 模块对象
|
|
247
|
+
* @private
|
|
54
248
|
*/
|
|
55
|
-
Mod.prototype.
|
|
56
|
-
return this
|
|
249
|
+
Mod.prototype._load = async function (...args) {
|
|
250
|
+
return this;
|
|
57
251
|
};
|
|
58
252
|
|
|
59
253
|
/**
|
|
60
|
-
*
|
|
61
|
-
* @
|
|
254
|
+
* 加载
|
|
255
|
+
* @param {...any} args 加载参数
|
|
256
|
+
* @returns {object} 模块对象
|
|
62
257
|
*/
|
|
63
|
-
Mod.prototype.
|
|
64
|
-
return this.
|
|
258
|
+
Mod.prototype.load = async function (...args) {
|
|
259
|
+
return await this._load(...args);
|
|
65
260
|
};
|
|
66
261
|
|
|
67
262
|
/**
|
|
68
|
-
*
|
|
69
|
-
* @
|
|
263
|
+
* 加载核心
|
|
264
|
+
* @private
|
|
265
|
+
* @param {...any} args 加载参数
|
|
70
266
|
*/
|
|
71
|
-
Mod.prototype.
|
|
72
|
-
return this._resumeCore();
|
|
73
|
-
};
|
|
267
|
+
Mod.prototype._loadCore = async function (...args) {};
|
|
74
268
|
|
|
75
269
|
/**
|
|
76
|
-
*
|
|
77
|
-
* @
|
|
270
|
+
* 启动 - 内部方法
|
|
271
|
+
* @private
|
|
272
|
+
* @param {...any} args 初始化参数
|
|
273
|
+
* @returns {object} 模块对象
|
|
78
274
|
*/
|
|
79
|
-
Mod.prototype.
|
|
80
|
-
return this
|
|
275
|
+
Mod.prototype._start = async function (...args) {
|
|
276
|
+
return this;
|
|
81
277
|
};
|
|
82
278
|
|
|
83
279
|
/**
|
|
84
|
-
*
|
|
85
|
-
* @
|
|
280
|
+
* 启动
|
|
281
|
+
* @param {...any} args 初始化参数
|
|
282
|
+
* @returns {object} 模块对象
|
|
86
283
|
*/
|
|
87
|
-
Mod.prototype.
|
|
88
|
-
return this.
|
|
284
|
+
Mod.prototype.start = async function (...args) {
|
|
285
|
+
return await this._start(...args);
|
|
89
286
|
};
|
|
90
287
|
|
|
91
|
-
// ==================== 生命周期 Core 方法 ====================
|
|
92
|
-
|
|
93
288
|
/**
|
|
94
|
-
*
|
|
95
|
-
* 由 Drive 子类重写以添加脚本加载逻辑
|
|
96
|
-
* @returns {Promise<void>}
|
|
289
|
+
* 启动核心
|
|
97
290
|
* @private
|
|
291
|
+
* @param {...any} args 初始化参数
|
|
98
292
|
*/
|
|
99
|
-
Mod.prototype.
|
|
100
|
-
if (this.lifecycle_state !== 'created') {
|
|
101
|
-
throw new Error('模块未创建,无法加载');
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
this.emit('load:before');
|
|
105
|
-
|
|
106
|
-
try {
|
|
107
|
-
// 执行加载操作,由 Drive 子类重写
|
|
108
|
-
this.lifecycle_state = 'loaded';
|
|
109
|
-
this.emit('load:success');
|
|
110
|
-
} catch (err) {
|
|
111
|
-
this.emit('load:error', err);
|
|
112
|
-
throw err;
|
|
113
|
-
}
|
|
114
|
-
};
|
|
293
|
+
Mod.prototype._startCore = async function (...args) {};
|
|
115
294
|
|
|
116
295
|
/**
|
|
117
|
-
*
|
|
118
|
-
* 调用 _init 钩子,Manager 类型自动调用 boot()
|
|
119
|
-
* @returns {Promise<void>}
|
|
296
|
+
* 停止 - 内部方法
|
|
120
297
|
* @private
|
|
298
|
+
* @returns {object} 模块对象
|
|
121
299
|
*/
|
|
122
|
-
Mod.prototype.
|
|
123
|
-
|
|
124
|
-
if (this.lifecycle_state === 'inited') {
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
if (this.lifecycle_state !== 'loaded') {
|
|
128
|
-
throw new Error('模块未加载,无法初始化');
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
try {
|
|
132
|
-
var init_hook = this.getMethod('_init');
|
|
133
|
-
if (init_hook) {
|
|
134
|
-
var result = init_hook(this);
|
|
135
|
-
if (result instanceof Promise) {
|
|
136
|
-
var timeout = new Promise((resolve, reject) => {
|
|
137
|
-
setTimeout(() => {
|
|
138
|
-
reject(new Error('初始化超时'));
|
|
139
|
-
}, 30000);
|
|
140
|
-
});
|
|
141
|
-
await Promise.race([result, timeout]);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// Manager 类型模块自动调用 boot()(在设置状态前执行)
|
|
146
|
-
if (typeof this.boot === 'function') {
|
|
147
|
-
await this.boot();
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
this.lifecycle_state = 'inited';
|
|
151
|
-
this.emit('init:success');
|
|
152
|
-
} catch (err) {
|
|
153
|
-
this.emit('init:error', err);
|
|
154
|
-
throw err;
|
|
155
|
-
}
|
|
300
|
+
Mod.prototype._stop = async function () {
|
|
301
|
+
return this;
|
|
156
302
|
};
|
|
157
303
|
|
|
158
304
|
/**
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
* @returns {Promise<void>}
|
|
162
|
-
* @private
|
|
305
|
+
* 停止
|
|
306
|
+
* @returns {object} 模块对象
|
|
163
307
|
*/
|
|
164
|
-
Mod.prototype.
|
|
165
|
-
|
|
166
|
-
if (this.lifecycle_state === 'started') {
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
169
|
-
throw new Error('模块未初始化,无法启动');
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
this.emit('start:before');
|
|
173
|
-
|
|
174
|
-
try {
|
|
175
|
-
var start_hook = this.getMethod('_start');
|
|
176
|
-
if (start_hook) {
|
|
177
|
-
var result = start_hook(this);
|
|
178
|
-
if (result instanceof Promise) {
|
|
179
|
-
var timeout = new Promise((resolve, reject) => {
|
|
180
|
-
setTimeout(() => {
|
|
181
|
-
reject(new Error('启动超时'));
|
|
182
|
-
}, 30000);
|
|
183
|
-
});
|
|
184
|
-
await Promise.race([result, timeout]);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
this.lifecycle_state = 'started';
|
|
189
|
-
this.emit('start:success');
|
|
190
|
-
} catch (err) {
|
|
191
|
-
this.emit('start:error', err);
|
|
192
|
-
throw err;
|
|
193
|
-
}
|
|
308
|
+
Mod.prototype.stop = async function () {
|
|
309
|
+
return await this._stop();
|
|
194
310
|
};
|
|
195
311
|
|
|
196
312
|
/**
|
|
197
|
-
*
|
|
198
|
-
* 调用 _pause 钩子,传播到子模块
|
|
199
|
-
* @returns {Promise<void>}
|
|
313
|
+
* 停止核心
|
|
200
314
|
* @private
|
|
201
315
|
*/
|
|
202
|
-
Mod.prototype.
|
|
203
|
-
if (this.lifecycle_state !== 'started') {
|
|
204
|
-
throw new Error('模块未启动,无法暂停');
|
|
205
|
-
}
|
|
316
|
+
Mod.prototype._stopCore = async function () {};
|
|
206
317
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
this.lifecycle_state = 'paused';
|
|
216
|
-
this.emit('pause:success');
|
|
217
|
-
} catch (err) {
|
|
218
|
-
this.emit('pause:error', err);
|
|
219
|
-
throw err;
|
|
318
|
+
/**
|
|
319
|
+
* 销毁核心
|
|
320
|
+
* @returns {object} 返回当前对象
|
|
321
|
+
*/
|
|
322
|
+
Mod.prototype._destroyCore = async function () {
|
|
323
|
+
if (this.config.need_cache) {
|
|
324
|
+
this.saveDatas();
|
|
220
325
|
}
|
|
326
|
+
this._datas = {};
|
|
327
|
+
this._methods = {};
|
|
221
328
|
};
|
|
222
329
|
|
|
223
330
|
/**
|
|
224
|
-
*
|
|
225
|
-
* 调用 _resume 钩子,传播到子模块
|
|
226
|
-
* @returns {Promise<void>}
|
|
331
|
+
* 卸载 - 内部方法
|
|
227
332
|
* @private
|
|
333
|
+
* @returns {object} 模块对象
|
|
228
334
|
*/
|
|
229
|
-
Mod.prototype.
|
|
230
|
-
|
|
231
|
-
throw new Error('模块未暂停,无法恢复');
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
this.emit('resume:before');
|
|
235
|
-
|
|
236
|
-
try {
|
|
237
|
-
var resume_hook = this.getMethod('_resume');
|
|
238
|
-
if (resume_hook) {
|
|
239
|
-
resume_hook(this);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
this.lifecycle_state = 'started';
|
|
243
|
-
this.emit('resume:success');
|
|
244
|
-
} catch (err) {
|
|
245
|
-
this.emit('resume:error', err);
|
|
246
|
-
throw err;
|
|
247
|
-
}
|
|
335
|
+
Mod.prototype._unload = async function () {
|
|
336
|
+
return this;
|
|
248
337
|
};
|
|
249
338
|
|
|
250
339
|
/**
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
* @returns {Promise<void>}
|
|
254
|
-
* @private
|
|
340
|
+
* 卸载
|
|
341
|
+
* @returns {object} 模块对象
|
|
255
342
|
*/
|
|
256
|
-
Mod.prototype.
|
|
257
|
-
|
|
258
|
-
throw new Error('模块未启动或暂停,无法结束');
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
this.emit('end:before');
|
|
262
|
-
|
|
263
|
-
try {
|
|
264
|
-
var end_hook = this.getMethod('_end');
|
|
265
|
-
if (end_hook) {
|
|
266
|
-
end_hook(this);
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
this.lifecycle_state = 'ended';
|
|
270
|
-
this.emit('end:success');
|
|
271
|
-
} catch (err) {
|
|
272
|
-
this.emit('end:error', err);
|
|
273
|
-
throw err;
|
|
274
|
-
}
|
|
343
|
+
Mod.prototype.unload = async function () {
|
|
344
|
+
return await this._unload();
|
|
275
345
|
};
|
|
276
346
|
|
|
277
347
|
/**
|
|
278
|
-
*
|
|
279
|
-
* 调用 _onDestroy 钩子,释放引用
|
|
280
|
-
* @returns {Promise<void>}
|
|
281
|
-
* @private
|
|
348
|
+
* 卸载核心
|
|
282
349
|
*/
|
|
283
|
-
Mod.prototype.
|
|
284
|
-
if (this.
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
this.emit('destroy:before');
|
|
289
|
-
|
|
290
|
-
try {
|
|
291
|
-
var destroy_hook = this.getMethod('_onDestroy');
|
|
292
|
-
if (destroy_hook) {
|
|
293
|
-
destroy_hook(this);
|
|
350
|
+
Mod.prototype._unloadCore = async function () {
|
|
351
|
+
if (this._methods) {
|
|
352
|
+
for (let key in this._methods) {
|
|
353
|
+
delete this[key];
|
|
294
354
|
}
|
|
295
|
-
|
|
296
355
|
this._methods = {};
|
|
356
|
+
}
|
|
357
|
+
if (this._datas) {
|
|
358
|
+
for (let key in this._datas) {
|
|
359
|
+
delete this[key];
|
|
360
|
+
}
|
|
297
361
|
this._datas = {};
|
|
298
|
-
this.lifecycle_state = 'destroyed';
|
|
299
|
-
this.emit('destroy:success');
|
|
300
|
-
} catch (err) {
|
|
301
|
-
this.emit('destroy:error', err);
|
|
302
|
-
throw err;
|
|
303
362
|
}
|
|
304
363
|
};
|
|
305
364
|
|
|
306
|
-
|
|
365
|
+
/**
|
|
366
|
+
* 重载
|
|
367
|
+
*/
|
|
368
|
+
Mod.prototype.reload = async function () {
|
|
369
|
+
await this.do('unload');
|
|
370
|
+
await this.do('load');
|
|
371
|
+
};
|
|
307
372
|
|
|
308
373
|
/**
|
|
309
|
-
*
|
|
310
|
-
* @returns {boolean} 是否已加载
|
|
374
|
+
* 重启
|
|
311
375
|
*/
|
|
312
|
-
Mod.prototype.
|
|
313
|
-
|
|
376
|
+
Mod.prototype.restart = async function () {
|
|
377
|
+
await this.do('stop');
|
|
378
|
+
await this.do('start');
|
|
314
379
|
};
|
|
315
380
|
|
|
316
381
|
/**
|
|
317
|
-
*
|
|
318
|
-
* @
|
|
319
|
-
* @param {string} message 日志消息
|
|
320
|
-
* @param {...any} params 日志参数
|
|
382
|
+
* 是否运行中
|
|
383
|
+
* @returns {boolean} 是否运行中
|
|
321
384
|
*/
|
|
322
|
-
Mod.prototype.
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
385
|
+
Mod.prototype.isRunning = function () {
|
|
386
|
+
return this.getState() === 'running';
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* 结束
|
|
391
|
+
*/
|
|
392
|
+
Mod.prototype.end = async function () {
|
|
393
|
+
await this.do('stop');
|
|
394
|
+
await this.do('unload');
|
|
395
|
+
await this.do('destroy');
|
|
331
396
|
};
|
|
332
397
|
|
|
333
398
|
/**
|
|
@@ -335,7 +400,7 @@ Mod.prototype.log = function (level, message) {
|
|
|
335
400
|
* @returns {object} 事件管理器
|
|
336
401
|
*/
|
|
337
402
|
Mod.prototype.getEventer = function () {
|
|
338
|
-
return this._eventer ||
|
|
403
|
+
return this._eventer || $.eventer;
|
|
339
404
|
};
|
|
340
405
|
|
|
341
406
|
/**
|
|
@@ -351,15 +416,8 @@ Mod.prototype.setEventer = function (eventer) {
|
|
|
351
416
|
* @param {string} event 事件名
|
|
352
417
|
* @param {...any} args 事件参数
|
|
353
418
|
*/
|
|
354
|
-
Mod.prototype.emitEvent =
|
|
355
|
-
|
|
356
|
-
if (eventer && eventer.emitWait) {
|
|
357
|
-
await eventer.emitWait(event);
|
|
358
|
-
} else if (eventer && eventer.emit) {
|
|
359
|
-
await eventer.emit(event);
|
|
360
|
-
} else if (this.emit) {
|
|
361
|
-
this.emit(event);
|
|
362
|
-
}
|
|
419
|
+
Mod.prototype.emitEvent = function (event, ...args) {
|
|
420
|
+
this.getEventer()?.emit(event, ...args);
|
|
363
421
|
};
|
|
364
422
|
|
|
365
423
|
/**
|
|
@@ -368,12 +426,8 @@ Mod.prototype.emitEvent = async function (event) {
|
|
|
368
426
|
* @param {...any} args 事件参数
|
|
369
427
|
* @returns {Promise<any>} 事件触发结果
|
|
370
428
|
*/
|
|
371
|
-
Mod.prototype.emitEventRace = async function (event) {
|
|
372
|
-
|
|
373
|
-
if (eventer && eventer.emitRace) {
|
|
374
|
-
return await eventer.emitRace(event);
|
|
375
|
-
}
|
|
376
|
-
return null;
|
|
429
|
+
Mod.prototype.emitEventRace = async function (event, ...args) {
|
|
430
|
+
return await this.getEventer()?.emitRace(event, ...args);
|
|
377
431
|
};
|
|
378
432
|
|
|
379
433
|
/**
|
|
@@ -382,12 +436,8 @@ Mod.prototype.emitEventRace = async function (event) {
|
|
|
382
436
|
* @param {...any} args 事件参数
|
|
383
437
|
* @returns {Promise<any>} 事件触发结果
|
|
384
438
|
*/
|
|
385
|
-
Mod.prototype.emitEventFirst = async function (event) {
|
|
386
|
-
|
|
387
|
-
if (eventer && eventer.emitFirst) {
|
|
388
|
-
return await eventer.emitFirst(event);
|
|
389
|
-
}
|
|
390
|
-
return null;
|
|
439
|
+
Mod.prototype.emitEventFirst = async function (event, ...args) {
|
|
440
|
+
return await this.getEventer()?.emitFirst(event, ...args);
|
|
391
441
|
};
|
|
392
442
|
|
|
393
443
|
/**
|
|
@@ -396,10 +446,7 @@ Mod.prototype.emitEventFirst = async function (event) {
|
|
|
396
446
|
* @param {Function} callback 事件回调函数
|
|
397
447
|
*/
|
|
398
448
|
Mod.prototype.onEvent = function (event, callback) {
|
|
399
|
-
|
|
400
|
-
if (eventer && eventer.on) {
|
|
401
|
-
eventer.on(event, callback);
|
|
402
|
-
}
|
|
449
|
+
this.getEventer()?.on(event, callback);
|
|
403
450
|
};
|
|
404
451
|
|
|
405
452
|
/**
|
|
@@ -407,70 +454,7 @@ Mod.prototype.onEvent = function (event, callback) {
|
|
|
407
454
|
* @param {string} event 事件名
|
|
408
455
|
*/
|
|
409
456
|
Mod.prototype.offEvent = function (event) {
|
|
410
|
-
|
|
411
|
-
if (eventer && eventer.off) {
|
|
412
|
-
eventer.off(event);
|
|
413
|
-
}
|
|
414
|
-
};
|
|
415
|
-
|
|
416
|
-
/**
|
|
417
|
-
* 是否运行中
|
|
418
|
-
* @returns {boolean} 是否运行中
|
|
419
|
-
*/
|
|
420
|
-
Mod.prototype.isRunning = function () {
|
|
421
|
-
return this.lifecycle_state === 'started';
|
|
422
|
-
};
|
|
423
|
-
|
|
424
|
-
/**
|
|
425
|
-
* 重载
|
|
426
|
-
* 记录当前状态,清除缓存,重新执行生命周期到原状态
|
|
427
|
-
* @returns {Promise<void>}
|
|
428
|
-
*/
|
|
429
|
-
Mod.prototype.reload = async function () {
|
|
430
|
-
var state = this.lifecycle_state;
|
|
431
|
-
var priority = 0;
|
|
432
|
-
if (state === 'loaded') {
|
|
433
|
-
priority = 1;
|
|
434
|
-
} else if (state === 'inited') {
|
|
435
|
-
priority = 2;
|
|
436
|
-
} else if (state === 'started' || state === 'paused') {
|
|
437
|
-
priority = 3;
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
// 结束当前状态
|
|
441
|
-
if (this.lifecycle_state === 'started' || this.lifecycle_state === 'paused') {
|
|
442
|
-
await this._endCore();
|
|
443
|
-
}
|
|
444
|
-
if (this.lifecycle_state === 'ended') {
|
|
445
|
-
await this._destroyCore();
|
|
446
|
-
}
|
|
447
|
-
// 恢复为 created 以重新加载
|
|
448
|
-
this.lifecycle_state = 'created';
|
|
449
|
-
|
|
450
|
-
// 重新加载
|
|
451
|
-
if (priority >= 1) {
|
|
452
|
-
await this.load();
|
|
453
|
-
}
|
|
454
|
-
if (priority >= 2) {
|
|
455
|
-
await this.init();
|
|
456
|
-
}
|
|
457
|
-
if (priority >= 3) {
|
|
458
|
-
await this.start();
|
|
459
|
-
}
|
|
460
|
-
};
|
|
461
|
-
|
|
462
|
-
/**
|
|
463
|
-
* 重启
|
|
464
|
-
* 从 started 重新执行 start
|
|
465
|
-
* @returns {Promise<void>}
|
|
466
|
-
*/
|
|
467
|
-
Mod.prototype.restart = async function () {
|
|
468
|
-
if (this.lifecycle_state !== 'started') {
|
|
469
|
-
throw new Error('模块未启动,无法重启');
|
|
470
|
-
}
|
|
471
|
-
await this._endCore();
|
|
472
|
-
this.lifecycle_state = 'inited';
|
|
473
|
-
await this._startCore();
|
|
457
|
+
this.getEventer()?.off(event);
|
|
474
458
|
};
|
|
475
459
|
|
|
476
460
|
/**
|
|
@@ -478,209 +462,181 @@ Mod.prototype.restart = async function () {
|
|
|
478
462
|
* @param {...any} args 方法参数
|
|
479
463
|
* @returns {Promise<any>} 方法执行结果
|
|
480
464
|
*/
|
|
481
|
-
Mod.prototype.main = async function () {
|
|
482
|
-
|
|
483
|
-
|
|
465
|
+
Mod.prototype.main = async function (...args) {
|
|
466
|
+
let method = args[0];
|
|
467
|
+
let func = this.getMethod(method);
|
|
484
468
|
if (!func) {
|
|
485
|
-
throw new Error(
|
|
469
|
+
throw new Error(`方法${method}不存在`);
|
|
486
470
|
}
|
|
487
|
-
|
|
488
|
-
return await func
|
|
471
|
+
let params = args.slice(1);
|
|
472
|
+
return await func(...params);
|
|
489
473
|
};
|
|
490
474
|
|
|
491
|
-
// ==================== 数据管理 ====================
|
|
492
|
-
|
|
493
475
|
/**
|
|
494
|
-
*
|
|
476
|
+
* 设置脚本方法
|
|
495
477
|
* @param {object} methods 脚本方法对象
|
|
496
478
|
*/
|
|
497
479
|
Mod.prototype.setMethods = function (methods) {
|
|
498
480
|
if (typeof methods !== 'object') {
|
|
499
|
-
throw new Error(
|
|
481
|
+
throw new Error(`参数(methods)必须是对象`);
|
|
500
482
|
}
|
|
501
483
|
if (!methods) return;
|
|
502
|
-
|
|
484
|
+
if (!this._methods) {
|
|
485
|
+
this._methods = {};
|
|
486
|
+
}
|
|
487
|
+
for (let key in methods) {
|
|
503
488
|
this.setMethod(key, methods[key]);
|
|
504
489
|
}
|
|
505
490
|
};
|
|
506
491
|
|
|
507
492
|
/**
|
|
508
|
-
*
|
|
509
|
-
* @param {string}
|
|
510
|
-
* @
|
|
493
|
+
* 设置脚本方法
|
|
494
|
+
* @param {string} method 方法名
|
|
495
|
+
* @param {Function} func 方法函数
|
|
511
496
|
*/
|
|
512
|
-
Mod.prototype.
|
|
513
|
-
|
|
497
|
+
Mod.prototype.setMethod = function (method, func) {
|
|
498
|
+
if (typeof method !== 'string') {
|
|
499
|
+
throw new Error(`第一个参数(method)必须是字符串`);
|
|
500
|
+
}
|
|
501
|
+
if (method === '_datas') {
|
|
502
|
+
throw new Error(`第一个参数(method)不能为_datas键`);
|
|
503
|
+
}
|
|
504
|
+
if (method === '_methods') {
|
|
505
|
+
throw new Error(`第一个参数(method)不能为_methods键`);
|
|
506
|
+
}
|
|
507
|
+
if (!func) throw new Error(`第二个参数(func)不能为空`);
|
|
508
|
+
if (typeof func !== 'function') {
|
|
509
|
+
throw new Error(`第二个参数(func)必须是函数`);
|
|
510
|
+
}
|
|
511
|
+
if (!this._methods) {
|
|
512
|
+
this._methods = {};
|
|
513
|
+
}
|
|
514
|
+
this._methods[method] = true;
|
|
515
|
+
this[method] = func;
|
|
514
516
|
};
|
|
515
517
|
|
|
516
518
|
/**
|
|
517
|
-
*
|
|
518
|
-
* @param {string}
|
|
519
|
-
* @
|
|
519
|
+
* 获取方法函数
|
|
520
|
+
* @param {string} method 方法名
|
|
521
|
+
* @returns {Function} 方法函数
|
|
520
522
|
*/
|
|
521
|
-
Mod.prototype.
|
|
522
|
-
if (typeof
|
|
523
|
-
throw new Error(
|
|
523
|
+
Mod.prototype.getMethod = function (method) {
|
|
524
|
+
if (typeof method !== 'string') {
|
|
525
|
+
throw new Error(`第一个参数(method)必须是字符串`);
|
|
524
526
|
}
|
|
525
|
-
|
|
526
|
-
throw new Error('key 不能为保留键');
|
|
527
|
-
}
|
|
528
|
-
if (value === undefined || value === null) {
|
|
529
|
-
throw new Error('第二个参数(value)不能为undefined或null');
|
|
530
|
-
}
|
|
531
|
-
if (typeof value === 'function') {
|
|
532
|
-
throw new Error('第二个参数(value)不能为函数');
|
|
533
|
-
}
|
|
534
|
-
this._datas[key] = typeof value;
|
|
535
|
-
this[key] = value;
|
|
527
|
+
return this[method];
|
|
536
528
|
};
|
|
537
529
|
|
|
538
530
|
/**
|
|
539
|
-
*
|
|
531
|
+
* 获取数据
|
|
540
532
|
* @returns {object} 数据对象
|
|
541
533
|
*/
|
|
542
534
|
Mod.prototype.getDatas = function () {
|
|
543
|
-
|
|
544
|
-
|
|
535
|
+
let datas = this._datas;
|
|
536
|
+
let result = {};
|
|
537
|
+
for (let key in datas) {
|
|
545
538
|
result[key] = this[key];
|
|
546
539
|
}
|
|
547
540
|
return result;
|
|
548
541
|
};
|
|
549
542
|
|
|
550
543
|
/**
|
|
551
|
-
*
|
|
552
|
-
* @param {
|
|
544
|
+
* 获取数据
|
|
545
|
+
* @param {string} key 数据键
|
|
546
|
+
* @returns {object} 数据对象
|
|
553
547
|
*/
|
|
554
|
-
Mod.prototype.
|
|
555
|
-
|
|
556
|
-
throw new Error('参数(datas)必须是对象');
|
|
557
|
-
}
|
|
558
|
-
if (!datas) return;
|
|
559
|
-
for (var key in datas) {
|
|
560
|
-
// eslint-disable-next-line mm_eslint/function-name
|
|
561
|
-
this.setData(key, datas[key]);
|
|
562
|
-
}
|
|
548
|
+
Mod.prototype.getData = function (key) {
|
|
549
|
+
return this[key];
|
|
563
550
|
};
|
|
564
551
|
|
|
565
552
|
/**
|
|
566
|
-
*
|
|
553
|
+
* 设置数据
|
|
554
|
+
* @param {object} datas 数据对象
|
|
567
555
|
*/
|
|
568
|
-
Mod.prototype.
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
if (!
|
|
573
|
-
|
|
574
|
-
process.cwd(),
|
|
575
|
-
'cache',
|
|
576
|
-
(this.config && this.config.name) || 'mod',
|
|
577
|
-
'data.json'
|
|
578
|
-
);
|
|
556
|
+
Mod.prototype.setDatas = function (datas) {
|
|
557
|
+
if (typeof datas !== 'object') {
|
|
558
|
+
throw new Error(`参数(datas)必须是对象`);
|
|
559
|
+
}
|
|
560
|
+
if (!this._datas) {
|
|
561
|
+
this._datas = {};
|
|
579
562
|
}
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
563
|
+
for (let key in datas) {
|
|
564
|
+
this.setData(key, datas[key]);
|
|
583
565
|
}
|
|
584
|
-
var data = this.getDatas();
|
|
585
|
-
fs.writeFileSync(file, JSON.stringify(data, null, 2), 'utf8');
|
|
586
566
|
};
|
|
587
567
|
|
|
588
568
|
/**
|
|
589
|
-
*
|
|
569
|
+
* 设置数据
|
|
570
|
+
* @param {string} key 数据键
|
|
571
|
+
* @param {*} value 数据值
|
|
590
572
|
*/
|
|
591
|
-
Mod.prototype.
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
var file = (this.config && this.config.cache_file) || '';
|
|
595
|
-
if (!file) {
|
|
596
|
-
file = path.resolve(
|
|
597
|
-
process.cwd(),
|
|
598
|
-
'cache',
|
|
599
|
-
(this.config && this.config.name) || 'mod',
|
|
600
|
-
'data.json'
|
|
601
|
-
);
|
|
573
|
+
Mod.prototype.setData = function (key, value) {
|
|
574
|
+
if (typeof key !== 'string') {
|
|
575
|
+
throw new Error(`第一个参数(key)必须是字符串`);
|
|
602
576
|
}
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
var content = fs.readFileSync(file, 'utf8');
|
|
606
|
-
var data = JSON.parse(content);
|
|
607
|
-
this.setDatas(data);
|
|
608
|
-
}
|
|
609
|
-
} catch {
|
|
610
|
-
// 文件不存在或解析失败
|
|
577
|
+
if (key === '_datas') {
|
|
578
|
+
throw new Error(`第一个参数(key)不能为_datas键`);
|
|
611
579
|
}
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
// ==================== 方法管理 ====================
|
|
615
|
-
|
|
616
|
-
/**
|
|
617
|
-
* 注册方法
|
|
618
|
-
* @param {string} name 方法名
|
|
619
|
-
* @param {Function} func 函数
|
|
620
|
-
*/
|
|
621
|
-
Mod.prototype.setMethod = function (name, func) {
|
|
622
|
-
if (typeof name !== 'string') {
|
|
623
|
-
throw new TypeError('方法名必须是字符串');
|
|
580
|
+
if (key === '_methods') {
|
|
581
|
+
throw new Error(`第一个参数(key)不能为_methods键`);
|
|
624
582
|
}
|
|
625
|
-
if (
|
|
626
|
-
throw new
|
|
583
|
+
if (value === undefined || value === null)
|
|
584
|
+
throw new Error(`第二个参数(value)不能为undefined或null`);
|
|
585
|
+
if (typeof value == 'function') {
|
|
586
|
+
throw new Error(`第二个参数(value)不能为函数`);
|
|
627
587
|
}
|
|
628
|
-
this.
|
|
629
|
-
this[
|
|
588
|
+
this._datas[key] = true;
|
|
589
|
+
this[key] = value;
|
|
630
590
|
};
|
|
631
591
|
|
|
632
592
|
/**
|
|
633
|
-
*
|
|
634
|
-
* @
|
|
635
|
-
* @returns {Function|null} 方法函数,不存在返回 null
|
|
593
|
+
* 获取缓存文件路径
|
|
594
|
+
* @returns {string} 缓存文件路径
|
|
636
595
|
*/
|
|
637
|
-
Mod.prototype.
|
|
638
|
-
return this.
|
|
596
|
+
Mod.prototype.getCacheFile = function () {
|
|
597
|
+
return this.config.cache_file || `/cache/${this.constructor.name}_${this.config.name}.json`;
|
|
639
598
|
};
|
|
640
599
|
|
|
641
600
|
/**
|
|
642
|
-
*
|
|
643
|
-
* @param {string} name 方法名
|
|
601
|
+
* 保存数据
|
|
644
602
|
*/
|
|
645
|
-
Mod.prototype.
|
|
646
|
-
|
|
603
|
+
Mod.prototype.saveDatas = function () {
|
|
604
|
+
let datas = this.getDatas();
|
|
605
|
+
let file = this.getCacheFile();
|
|
606
|
+
file.saveJson(datas);
|
|
647
607
|
};
|
|
648
608
|
|
|
649
609
|
/**
|
|
650
|
-
*
|
|
651
|
-
* @param {string} name 方法名
|
|
652
|
-
* @returns {boolean} 是否存在
|
|
610
|
+
* 加载数据
|
|
653
611
|
*/
|
|
654
|
-
Mod.prototype.
|
|
655
|
-
|
|
612
|
+
Mod.prototype.loadDatas = function () {
|
|
613
|
+
let file = this.getCacheFile();
|
|
614
|
+
let datas = file.loadJson();
|
|
615
|
+
this.setDatas(datas);
|
|
656
616
|
};
|
|
657
617
|
|
|
658
618
|
/**
|
|
659
|
-
*
|
|
660
|
-
*
|
|
661
|
-
* @param {
|
|
662
|
-
* @
|
|
663
|
-
* @returns {Promise<*>} 方法执行结果
|
|
619
|
+
* 调用函数(核心执行方法)
|
|
620
|
+
* @param {string} method 函数名
|
|
621
|
+
* @param {*} params 参数集合
|
|
622
|
+
* @returns {Promise<any>} 执行结果
|
|
664
623
|
*/
|
|
665
|
-
Mod.prototype.call = async function (
|
|
666
|
-
|
|
667
|
-
if (
|
|
668
|
-
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
if (this.lifecycle_state !== 'started' && this.lifecycle_state !== 'inited') {
|
|
624
|
+
Mod.prototype.call = async function (method, ...params) {
|
|
625
|
+
let method_func = this.getMethod(method);
|
|
626
|
+
if (typeof method_func !== 'function') {
|
|
627
|
+
this.log('warn', `方法 ${method} 未注册,跳过执行`);
|
|
672
628
|
return null;
|
|
673
629
|
}
|
|
674
630
|
|
|
675
|
-
|
|
631
|
+
let result;
|
|
676
632
|
|
|
677
633
|
try {
|
|
678
|
-
|
|
679
|
-
if (
|
|
680
|
-
result = await this._callMain(
|
|
681
|
-
result = await this._callAfter(
|
|
634
|
+
var before_result = await this._callBefore(method, params);
|
|
635
|
+
if (before_result) return before_result;
|
|
636
|
+
result = await this._callMain(method_func, params, before_result);
|
|
637
|
+
result = await this._callAfter(method, params, result);
|
|
682
638
|
} catch (err) {
|
|
683
|
-
this.log('error',
|
|
639
|
+
this.log('error', `[${this.config.name}] 执行 ${method} 失败: ${err.message}`, err);
|
|
684
640
|
return null;
|
|
685
641
|
}
|
|
686
642
|
|
|
@@ -689,21 +645,21 @@ Mod.prototype.call = async function (name, params) {
|
|
|
689
645
|
|
|
690
646
|
/**
|
|
691
647
|
* 执行前置钩子
|
|
692
|
-
* 查找 methodBefore 方法并执行
|
|
693
648
|
* @param {string} method 方法名
|
|
694
|
-
* @param {
|
|
695
|
-
* @returns {Promise
|
|
696
|
-
* @private
|
|
649
|
+
* @param {Array} params 参数
|
|
650
|
+
* @returns {Promise<any>} 前置钩子结果
|
|
697
651
|
*/
|
|
698
652
|
Mod.prototype._callBefore = async function (method, params) {
|
|
699
|
-
|
|
700
|
-
|
|
653
|
+
let before_method = `${method}Before`;
|
|
654
|
+
let before_func = this.getMethod(before_method);
|
|
655
|
+
|
|
701
656
|
if (typeof before_func !== 'function') return undefined;
|
|
702
657
|
|
|
703
658
|
try {
|
|
704
|
-
|
|
659
|
+
let result = await before_func.call(this, ...params);
|
|
660
|
+
return result;
|
|
705
661
|
} catch (err) {
|
|
706
|
-
this.log('error',
|
|
662
|
+
this.log('error', `[${this.config.name}] 执行前置钩子 ${before_method} 失败`, err);
|
|
707
663
|
return undefined;
|
|
708
664
|
}
|
|
709
665
|
};
|
|
@@ -711,33 +667,34 @@ Mod.prototype._callBefore = async function (method, params) {
|
|
|
711
667
|
/**
|
|
712
668
|
* 执行主方法
|
|
713
669
|
* @param {Function} method_func 方法函数
|
|
714
|
-
* @param {
|
|
715
|
-
* @
|
|
716
|
-
* @
|
|
670
|
+
* @param {Array} params 参数
|
|
671
|
+
* @param {any} result 前置钩子结果
|
|
672
|
+
* @returns {Promise<any>} 主方法结果
|
|
717
673
|
*/
|
|
718
|
-
Mod.prototype._callMain = async function (method_func, params) {
|
|
719
|
-
|
|
674
|
+
Mod.prototype._callMain = async function (method_func, params, result) {
|
|
675
|
+
let ret = await method_func.call(this, ...params);
|
|
676
|
+
// 修复:如果主方法返回undefined,则使用前置钩子的结果
|
|
677
|
+
return ret !== undefined ? ret : result;
|
|
720
678
|
};
|
|
721
679
|
|
|
722
680
|
/**
|
|
723
681
|
* 执行后置钩子
|
|
724
|
-
* 查找 methodAfter 方法并执行
|
|
725
682
|
* @param {string} method 方法名
|
|
726
|
-
* @param {
|
|
727
|
-
* @param {
|
|
728
|
-
* @returns {Promise
|
|
729
|
-
* @private
|
|
683
|
+
* @param {Array} params 参数
|
|
684
|
+
* @param {any} result 主方法结果
|
|
685
|
+
* @returns {Promise<any>} 后置钩子结果
|
|
730
686
|
*/
|
|
731
687
|
Mod.prototype._callAfter = async function (method, params, result) {
|
|
732
|
-
|
|
733
|
-
|
|
688
|
+
let after_method = `${method}After`;
|
|
689
|
+
let after_func = this.getMethod(after_method);
|
|
690
|
+
|
|
734
691
|
if (typeof after_func !== 'function') return result;
|
|
735
692
|
|
|
736
693
|
try {
|
|
737
|
-
|
|
694
|
+
let after = await after_func.call(this, result, ...params);
|
|
738
695
|
return after !== undefined ? after : result;
|
|
739
696
|
} catch (err) {
|
|
740
|
-
this.log('error',
|
|
697
|
+
this.log('error', `[${this.config.name}] 执行后置钩子 ${after_method} 失败`, err);
|
|
741
698
|
return result;
|
|
742
699
|
}
|
|
743
700
|
};
|
|
@@ -748,8 +705,9 @@ Mod.prototype._callAfter = async function (method, params, result) {
|
|
|
748
705
|
*/
|
|
749
706
|
Mod.prototype.getDir = function () {
|
|
750
707
|
if (!this.getParent) return '';
|
|
751
|
-
|
|
752
|
-
return parent ? parent.getDir() || '' : '';
|
|
708
|
+
return this.getParent()?.getDir() || '';
|
|
753
709
|
};
|
|
754
710
|
|
|
755
|
-
module.exports = {
|
|
711
|
+
module.exports = {
|
|
712
|
+
Mod
|
|
713
|
+
};
|