mm_machine 2.9.7 → 2.9.9
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 +47 -1497
- package/mod.js +452 -498
- package/package.json +3 -4
- package/registry.js +293 -515
- package/scanner.js +62 -0
- package/dir_scanner.js +0 -262
- package/id_gen.js +0 -138
- package/word.js +0 -209
package/mod.js
CHANGED
|
@@ -1,337 +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
|
-
// 如果是 created 状态,自动加载
|
|
128
|
-
if (this.lifecycle_state === 'created') {
|
|
129
|
-
await this.load();
|
|
130
|
-
}
|
|
131
|
-
if (this.lifecycle_state !== 'loaded') {
|
|
132
|
-
throw new Error('模块未加载,无法初始化');
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
try {
|
|
136
|
-
var init_hook = this.getMethod('_init');
|
|
137
|
-
if (init_hook) {
|
|
138
|
-
var result = init_hook(this);
|
|
139
|
-
if (result instanceof Promise) {
|
|
140
|
-
var timeout = new Promise((resolve, reject) => {
|
|
141
|
-
setTimeout(() => {
|
|
142
|
-
reject(new Error('初始化超时'));
|
|
143
|
-
}, 30000);
|
|
144
|
-
});
|
|
145
|
-
await Promise.race([result, timeout]);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// Manager 类型模块自动调用 boot()(在设置状态前执行)
|
|
150
|
-
if (typeof this.boot === 'function') {
|
|
151
|
-
await this.boot();
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
this.lifecycle_state = 'inited';
|
|
155
|
-
this.emit('init:success');
|
|
156
|
-
} catch (err) {
|
|
157
|
-
this.emit('init:error', err);
|
|
158
|
-
throw err;
|
|
159
|
-
}
|
|
300
|
+
Mod.prototype._stop = async function () {
|
|
301
|
+
return this;
|
|
160
302
|
};
|
|
161
303
|
|
|
162
304
|
/**
|
|
163
|
-
*
|
|
164
|
-
*
|
|
165
|
-
* @returns {Promise<void>}
|
|
166
|
-
* @private
|
|
305
|
+
* 停止
|
|
306
|
+
* @returns {object} 模块对象
|
|
167
307
|
*/
|
|
168
|
-
Mod.prototype.
|
|
169
|
-
|
|
170
|
-
if (this.lifecycle_state === 'started') {
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
throw new Error('模块未初始化,无法启动');
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
this.emit('start:before');
|
|
177
|
-
|
|
178
|
-
try {
|
|
179
|
-
var start_hook = this.getMethod('_start');
|
|
180
|
-
if (start_hook) {
|
|
181
|
-
var result = start_hook(this);
|
|
182
|
-
if (result instanceof Promise) {
|
|
183
|
-
var timeout = new Promise((resolve, reject) => {
|
|
184
|
-
setTimeout(() => {
|
|
185
|
-
reject(new Error('启动超时'));
|
|
186
|
-
}, 30000);
|
|
187
|
-
});
|
|
188
|
-
await Promise.race([result, timeout]);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
this.lifecycle_state = 'started';
|
|
193
|
-
this.emit('start:success');
|
|
194
|
-
} catch (err) {
|
|
195
|
-
this.emit('start:error', err);
|
|
196
|
-
throw err;
|
|
197
|
-
}
|
|
308
|
+
Mod.prototype.stop = async function () {
|
|
309
|
+
return await this._stop();
|
|
198
310
|
};
|
|
199
311
|
|
|
200
312
|
/**
|
|
201
|
-
*
|
|
202
|
-
* 调用 _pause 钩子,传播到子模块
|
|
203
|
-
* @returns {Promise<void>}
|
|
313
|
+
* 停止核心
|
|
204
314
|
* @private
|
|
205
315
|
*/
|
|
206
|
-
Mod.prototype.
|
|
207
|
-
if (this.lifecycle_state !== 'started') {
|
|
208
|
-
throw new Error('模块未启动,无法暂停');
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
this.emit('pause:before');
|
|
212
|
-
|
|
213
|
-
try {
|
|
214
|
-
var pause_hook = this.getMethod('_pause');
|
|
215
|
-
if (pause_hook) {
|
|
216
|
-
pause_hook(this);
|
|
217
|
-
}
|
|
316
|
+
Mod.prototype._stopCore = async function () {};
|
|
218
317
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
318
|
+
/**
|
|
319
|
+
* 销毁核心
|
|
320
|
+
* @returns {object} 返回当前对象
|
|
321
|
+
*/
|
|
322
|
+
Mod.prototype._destroyCore = async function () {
|
|
323
|
+
if (this.config.need_cache) {
|
|
324
|
+
this.saveDatas();
|
|
224
325
|
}
|
|
326
|
+
this._datas = {};
|
|
327
|
+
this._methods = {};
|
|
225
328
|
};
|
|
226
329
|
|
|
227
330
|
/**
|
|
228
|
-
*
|
|
229
|
-
* 调用 _resume 钩子,传播到子模块
|
|
230
|
-
* @returns {Promise<void>}
|
|
331
|
+
* 卸载 - 内部方法
|
|
231
332
|
* @private
|
|
333
|
+
* @returns {object} 模块对象
|
|
232
334
|
*/
|
|
233
|
-
Mod.prototype.
|
|
234
|
-
|
|
235
|
-
throw new Error('模块未暂停,无法恢复');
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
this.emit('resume:before');
|
|
239
|
-
|
|
240
|
-
try {
|
|
241
|
-
var resume_hook = this.getMethod('_resume');
|
|
242
|
-
if (resume_hook) {
|
|
243
|
-
resume_hook(this);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
this.lifecycle_state = 'started';
|
|
247
|
-
this.emit('resume:success');
|
|
248
|
-
} catch (err) {
|
|
249
|
-
this.emit('resume:error', err);
|
|
250
|
-
throw err;
|
|
251
|
-
}
|
|
335
|
+
Mod.prototype._unload = async function () {
|
|
336
|
+
return this;
|
|
252
337
|
};
|
|
253
338
|
|
|
254
339
|
/**
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
* @returns {Promise<void>}
|
|
258
|
-
* @private
|
|
340
|
+
* 卸载
|
|
341
|
+
* @returns {object} 模块对象
|
|
259
342
|
*/
|
|
260
|
-
Mod.prototype.
|
|
261
|
-
|
|
262
|
-
throw new Error('模块未启动或暂停,无法结束');
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
this.emit('end:before');
|
|
266
|
-
|
|
267
|
-
try {
|
|
268
|
-
var end_hook = this.getMethod('_end');
|
|
269
|
-
if (end_hook) {
|
|
270
|
-
end_hook(this);
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
this.lifecycle_state = 'ended';
|
|
274
|
-
this.emit('end:success');
|
|
275
|
-
} catch (err) {
|
|
276
|
-
this.emit('end:error', err);
|
|
277
|
-
throw err;
|
|
278
|
-
}
|
|
343
|
+
Mod.prototype.unload = async function () {
|
|
344
|
+
return await this._unload();
|
|
279
345
|
};
|
|
280
346
|
|
|
281
347
|
/**
|
|
282
|
-
*
|
|
283
|
-
* 调用 _onDestroy 钩子,释放引用
|
|
284
|
-
* @returns {Promise<void>}
|
|
285
|
-
* @private
|
|
348
|
+
* 卸载核心
|
|
286
349
|
*/
|
|
287
|
-
Mod.prototype.
|
|
288
|
-
if (this.
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
this.emit('destroy:before');
|
|
293
|
-
|
|
294
|
-
try {
|
|
295
|
-
var destroy_hook = this.getMethod('_onDestroy');
|
|
296
|
-
if (destroy_hook) {
|
|
297
|
-
destroy_hook(this);
|
|
350
|
+
Mod.prototype._unloadCore = async function () {
|
|
351
|
+
if (this._methods) {
|
|
352
|
+
for (let key in this._methods) {
|
|
353
|
+
delete this[key];
|
|
298
354
|
}
|
|
299
|
-
|
|
300
355
|
this._methods = {};
|
|
356
|
+
}
|
|
357
|
+
if (this._datas) {
|
|
358
|
+
for (let key in this._datas) {
|
|
359
|
+
delete this[key];
|
|
360
|
+
}
|
|
301
361
|
this._datas = {};
|
|
302
|
-
this.lifecycle_state = 'destroyed';
|
|
303
|
-
this.emit('destroy:success');
|
|
304
|
-
} catch (err) {
|
|
305
|
-
this.emit('destroy:error', err);
|
|
306
|
-
throw err;
|
|
307
362
|
}
|
|
308
363
|
};
|
|
309
364
|
|
|
310
|
-
|
|
365
|
+
/**
|
|
366
|
+
* 重载
|
|
367
|
+
*/
|
|
368
|
+
Mod.prototype.reload = async function () {
|
|
369
|
+
await this.do('unload');
|
|
370
|
+
await this.do('load');
|
|
371
|
+
};
|
|
311
372
|
|
|
312
373
|
/**
|
|
313
|
-
*
|
|
314
|
-
* @returns {boolean} 是否已加载
|
|
374
|
+
* 重启
|
|
315
375
|
*/
|
|
316
|
-
Mod.prototype.
|
|
317
|
-
|
|
376
|
+
Mod.prototype.restart = async function () {
|
|
377
|
+
await this.do('stop');
|
|
378
|
+
await this.do('start');
|
|
318
379
|
};
|
|
319
380
|
|
|
320
381
|
/**
|
|
321
|
-
*
|
|
322
|
-
* @
|
|
323
|
-
* @param {string} message 日志消息
|
|
324
|
-
* @param {...any} params 日志参数
|
|
382
|
+
* 是否运行中
|
|
383
|
+
* @returns {boolean} 是否运行中
|
|
325
384
|
*/
|
|
326
|
-
Mod.prototype.
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
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');
|
|
335
396
|
};
|
|
336
397
|
|
|
337
398
|
/**
|
|
@@ -339,7 +400,7 @@ Mod.prototype.log = function (level, message) {
|
|
|
339
400
|
* @returns {object} 事件管理器
|
|
340
401
|
*/
|
|
341
402
|
Mod.prototype.getEventer = function () {
|
|
342
|
-
return this._eventer ||
|
|
403
|
+
return this._eventer || $.eventer;
|
|
343
404
|
};
|
|
344
405
|
|
|
345
406
|
/**
|
|
@@ -355,15 +416,8 @@ Mod.prototype.setEventer = function (eventer) {
|
|
|
355
416
|
* @param {string} event 事件名
|
|
356
417
|
* @param {...any} args 事件参数
|
|
357
418
|
*/
|
|
358
|
-
Mod.prototype.emitEvent =
|
|
359
|
-
|
|
360
|
-
if (eventer && eventer.emitWait) {
|
|
361
|
-
await eventer.emitWait(event);
|
|
362
|
-
} else if (eventer && eventer.emit) {
|
|
363
|
-
await eventer.emit(event);
|
|
364
|
-
} else if (this.emit) {
|
|
365
|
-
this.emit(event);
|
|
366
|
-
}
|
|
419
|
+
Mod.prototype.emitEvent = function (event, ...args) {
|
|
420
|
+
this.getEventer()?.emit(event, ...args);
|
|
367
421
|
};
|
|
368
422
|
|
|
369
423
|
/**
|
|
@@ -372,12 +426,8 @@ Mod.prototype.emitEvent = async function (event) {
|
|
|
372
426
|
* @param {...any} args 事件参数
|
|
373
427
|
* @returns {Promise<any>} 事件触发结果
|
|
374
428
|
*/
|
|
375
|
-
Mod.prototype.emitEventRace = async function (event) {
|
|
376
|
-
|
|
377
|
-
if (eventer && eventer.emitRace) {
|
|
378
|
-
return await eventer.emitRace(event);
|
|
379
|
-
}
|
|
380
|
-
return null;
|
|
429
|
+
Mod.prototype.emitEventRace = async function (event, ...args) {
|
|
430
|
+
return await this.getEventer()?.emitRace(event, ...args);
|
|
381
431
|
};
|
|
382
432
|
|
|
383
433
|
/**
|
|
@@ -386,12 +436,8 @@ Mod.prototype.emitEventRace = async function (event) {
|
|
|
386
436
|
* @param {...any} args 事件参数
|
|
387
437
|
* @returns {Promise<any>} 事件触发结果
|
|
388
438
|
*/
|
|
389
|
-
Mod.prototype.emitEventFirst = async function (event) {
|
|
390
|
-
|
|
391
|
-
if (eventer && eventer.emitFirst) {
|
|
392
|
-
return await eventer.emitFirst(event);
|
|
393
|
-
}
|
|
394
|
-
return null;
|
|
439
|
+
Mod.prototype.emitEventFirst = async function (event, ...args) {
|
|
440
|
+
return await this.getEventer()?.emitFirst(event, ...args);
|
|
395
441
|
};
|
|
396
442
|
|
|
397
443
|
/**
|
|
@@ -400,10 +446,7 @@ Mod.prototype.emitEventFirst = async function (event) {
|
|
|
400
446
|
* @param {Function} callback 事件回调函数
|
|
401
447
|
*/
|
|
402
448
|
Mod.prototype.onEvent = function (event, callback) {
|
|
403
|
-
|
|
404
|
-
if (eventer && eventer.on) {
|
|
405
|
-
eventer.on(event, callback);
|
|
406
|
-
}
|
|
449
|
+
this.getEventer()?.on(event, callback);
|
|
407
450
|
};
|
|
408
451
|
|
|
409
452
|
/**
|
|
@@ -411,70 +454,7 @@ Mod.prototype.onEvent = function (event, callback) {
|
|
|
411
454
|
* @param {string} event 事件名
|
|
412
455
|
*/
|
|
413
456
|
Mod.prototype.offEvent = function (event) {
|
|
414
|
-
|
|
415
|
-
if (eventer && eventer.off) {
|
|
416
|
-
eventer.off(event);
|
|
417
|
-
}
|
|
418
|
-
};
|
|
419
|
-
|
|
420
|
-
/**
|
|
421
|
-
* 是否运行中
|
|
422
|
-
* @returns {boolean} 是否运行中
|
|
423
|
-
*/
|
|
424
|
-
Mod.prototype.isRunning = function () {
|
|
425
|
-
return this.lifecycle_state === 'started';
|
|
426
|
-
};
|
|
427
|
-
|
|
428
|
-
/**
|
|
429
|
-
* 重载
|
|
430
|
-
* 记录当前状态,清除缓存,重新执行生命周期到原状态
|
|
431
|
-
* @returns {Promise<void>}
|
|
432
|
-
*/
|
|
433
|
-
Mod.prototype.reload = async function () {
|
|
434
|
-
var state = this.lifecycle_state;
|
|
435
|
-
var priority = 0;
|
|
436
|
-
if (state === 'loaded') {
|
|
437
|
-
priority = 1;
|
|
438
|
-
} else if (state === 'inited') {
|
|
439
|
-
priority = 2;
|
|
440
|
-
} else if (state === 'started' || state === 'paused') {
|
|
441
|
-
priority = 3;
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
// 结束当前状态
|
|
445
|
-
if (this.lifecycle_state === 'started' || this.lifecycle_state === 'paused') {
|
|
446
|
-
await this._endCore();
|
|
447
|
-
}
|
|
448
|
-
if (this.lifecycle_state === 'ended') {
|
|
449
|
-
await this._destroyCore();
|
|
450
|
-
}
|
|
451
|
-
// 恢复为 created 以重新加载
|
|
452
|
-
this.lifecycle_state = 'created';
|
|
453
|
-
|
|
454
|
-
// 重新加载
|
|
455
|
-
if (priority >= 1) {
|
|
456
|
-
await this.load();
|
|
457
|
-
}
|
|
458
|
-
if (priority >= 2) {
|
|
459
|
-
await this.init();
|
|
460
|
-
}
|
|
461
|
-
if (priority >= 3) {
|
|
462
|
-
await this.start();
|
|
463
|
-
}
|
|
464
|
-
};
|
|
465
|
-
|
|
466
|
-
/**
|
|
467
|
-
* 重启
|
|
468
|
-
* 从 started 重新执行 start
|
|
469
|
-
* @returns {Promise<void>}
|
|
470
|
-
*/
|
|
471
|
-
Mod.prototype.restart = async function () {
|
|
472
|
-
if (this.lifecycle_state !== 'started') {
|
|
473
|
-
throw new Error('模块未启动,无法重启');
|
|
474
|
-
}
|
|
475
|
-
await this._endCore();
|
|
476
|
-
this.lifecycle_state = 'inited';
|
|
477
|
-
await this._startCore();
|
|
457
|
+
this.getEventer()?.off(event);
|
|
478
458
|
};
|
|
479
459
|
|
|
480
460
|
/**
|
|
@@ -482,209 +462,181 @@ Mod.prototype.restart = async function () {
|
|
|
482
462
|
* @param {...any} args 方法参数
|
|
483
463
|
* @returns {Promise<any>} 方法执行结果
|
|
484
464
|
*/
|
|
485
|
-
Mod.prototype.main = async function () {
|
|
486
|
-
|
|
487
|
-
|
|
465
|
+
Mod.prototype.main = async function (...args) {
|
|
466
|
+
let method = args[0];
|
|
467
|
+
let func = this.getMethod(method);
|
|
488
468
|
if (!func) {
|
|
489
|
-
throw new Error(
|
|
469
|
+
throw new Error(`方法${method}不存在`);
|
|
490
470
|
}
|
|
491
|
-
|
|
492
|
-
return await func
|
|
471
|
+
let params = args.slice(1);
|
|
472
|
+
return await func(...params);
|
|
493
473
|
};
|
|
494
474
|
|
|
495
|
-
// ==================== 数据管理 ====================
|
|
496
|
-
|
|
497
475
|
/**
|
|
498
|
-
*
|
|
476
|
+
* 设置脚本方法
|
|
499
477
|
* @param {object} methods 脚本方法对象
|
|
500
478
|
*/
|
|
501
479
|
Mod.prototype.setMethods = function (methods) {
|
|
502
480
|
if (typeof methods !== 'object') {
|
|
503
|
-
throw new Error(
|
|
481
|
+
throw new Error(`参数(methods)必须是对象`);
|
|
504
482
|
}
|
|
505
483
|
if (!methods) return;
|
|
506
|
-
|
|
484
|
+
if (!this._methods) {
|
|
485
|
+
this._methods = {};
|
|
486
|
+
}
|
|
487
|
+
for (let key in methods) {
|
|
507
488
|
this.setMethod(key, methods[key]);
|
|
508
489
|
}
|
|
509
490
|
};
|
|
510
491
|
|
|
511
492
|
/**
|
|
512
|
-
*
|
|
513
|
-
* @param {string}
|
|
514
|
-
* @
|
|
493
|
+
* 设置脚本方法
|
|
494
|
+
* @param {string} method 方法名
|
|
495
|
+
* @param {Function} func 方法函数
|
|
515
496
|
*/
|
|
516
|
-
Mod.prototype.
|
|
517
|
-
|
|
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;
|
|
518
516
|
};
|
|
519
517
|
|
|
520
518
|
/**
|
|
521
|
-
*
|
|
522
|
-
* @param {string}
|
|
523
|
-
* @
|
|
519
|
+
* 获取方法函数
|
|
520
|
+
* @param {string} method 方法名
|
|
521
|
+
* @returns {Function} 方法函数
|
|
524
522
|
*/
|
|
525
|
-
Mod.prototype.
|
|
526
|
-
if (typeof
|
|
527
|
-
throw new Error(
|
|
523
|
+
Mod.prototype.getMethod = function (method) {
|
|
524
|
+
if (typeof method !== 'string') {
|
|
525
|
+
throw new Error(`第一个参数(method)必须是字符串`);
|
|
528
526
|
}
|
|
529
|
-
|
|
530
|
-
throw new Error('key 不能为保留键');
|
|
531
|
-
}
|
|
532
|
-
if (value === undefined || value === null) {
|
|
533
|
-
throw new Error('第二个参数(value)不能为undefined或null');
|
|
534
|
-
}
|
|
535
|
-
if (typeof value === 'function') {
|
|
536
|
-
throw new Error('第二个参数(value)不能为函数');
|
|
537
|
-
}
|
|
538
|
-
this._datas[key] = typeof value;
|
|
539
|
-
this[key] = value;
|
|
527
|
+
return this[method];
|
|
540
528
|
};
|
|
541
529
|
|
|
542
530
|
/**
|
|
543
|
-
*
|
|
531
|
+
* 获取数据
|
|
544
532
|
* @returns {object} 数据对象
|
|
545
533
|
*/
|
|
546
534
|
Mod.prototype.getDatas = function () {
|
|
547
|
-
|
|
548
|
-
|
|
535
|
+
let datas = this._datas;
|
|
536
|
+
let result = {};
|
|
537
|
+
for (let key in datas) {
|
|
549
538
|
result[key] = this[key];
|
|
550
539
|
}
|
|
551
540
|
return result;
|
|
552
541
|
};
|
|
553
542
|
|
|
554
543
|
/**
|
|
555
|
-
*
|
|
556
|
-
* @param {
|
|
544
|
+
* 获取数据
|
|
545
|
+
* @param {string} key 数据键
|
|
546
|
+
* @returns {object} 数据对象
|
|
557
547
|
*/
|
|
558
|
-
Mod.prototype.
|
|
559
|
-
|
|
560
|
-
throw new Error('参数(datas)必须是对象');
|
|
561
|
-
}
|
|
562
|
-
if (!datas) return;
|
|
563
|
-
for (var key in datas) {
|
|
564
|
-
// eslint-disable-next-line mm_eslint/function-name
|
|
565
|
-
this.setData(key, datas[key]);
|
|
566
|
-
}
|
|
548
|
+
Mod.prototype.getData = function (key) {
|
|
549
|
+
return this[key];
|
|
567
550
|
};
|
|
568
551
|
|
|
569
552
|
/**
|
|
570
|
-
*
|
|
553
|
+
* 设置数据
|
|
554
|
+
* @param {object} datas 数据对象
|
|
571
555
|
*/
|
|
572
|
-
Mod.prototype.
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
if (!
|
|
577
|
-
|
|
578
|
-
process.cwd(),
|
|
579
|
-
'cache',
|
|
580
|
-
(this.config && this.config.name) || 'mod',
|
|
581
|
-
'data.json'
|
|
582
|
-
);
|
|
556
|
+
Mod.prototype.setDatas = function (datas) {
|
|
557
|
+
if (typeof datas !== 'object') {
|
|
558
|
+
throw new Error(`参数(datas)必须是对象`);
|
|
559
|
+
}
|
|
560
|
+
if (!this._datas) {
|
|
561
|
+
this._datas = {};
|
|
583
562
|
}
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
563
|
+
for (let key in datas) {
|
|
564
|
+
this.setData(key, datas[key]);
|
|
587
565
|
}
|
|
588
|
-
var data = this.getDatas();
|
|
589
|
-
fs.writeFileSync(file, JSON.stringify(data, null, 2), 'utf8');
|
|
590
566
|
};
|
|
591
567
|
|
|
592
568
|
/**
|
|
593
|
-
*
|
|
569
|
+
* 设置数据
|
|
570
|
+
* @param {string} key 数据键
|
|
571
|
+
* @param {*} value 数据值
|
|
594
572
|
*/
|
|
595
|
-
Mod.prototype.
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
var file = (this.config && this.config.cache_file) || '';
|
|
599
|
-
if (!file) {
|
|
600
|
-
file = path.resolve(
|
|
601
|
-
process.cwd(),
|
|
602
|
-
'cache',
|
|
603
|
-
(this.config && this.config.name) || 'mod',
|
|
604
|
-
'data.json'
|
|
605
|
-
);
|
|
573
|
+
Mod.prototype.setData = function (key, value) {
|
|
574
|
+
if (typeof key !== 'string') {
|
|
575
|
+
throw new Error(`第一个参数(key)必须是字符串`);
|
|
606
576
|
}
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
var content = fs.readFileSync(file, 'utf8');
|
|
610
|
-
var data = JSON.parse(content);
|
|
611
|
-
this.setDatas(data);
|
|
612
|
-
}
|
|
613
|
-
} catch {
|
|
614
|
-
// 文件不存在或解析失败
|
|
577
|
+
if (key === '_datas') {
|
|
578
|
+
throw new Error(`第一个参数(key)不能为_datas键`);
|
|
615
579
|
}
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
// ==================== 方法管理 ====================
|
|
619
|
-
|
|
620
|
-
/**
|
|
621
|
-
* 注册方法
|
|
622
|
-
* @param {string} name 方法名
|
|
623
|
-
* @param {Function} func 函数
|
|
624
|
-
*/
|
|
625
|
-
Mod.prototype.setMethod = function (name, func) {
|
|
626
|
-
if (typeof name !== 'string') {
|
|
627
|
-
throw new TypeError('方法名必须是字符串');
|
|
580
|
+
if (key === '_methods') {
|
|
581
|
+
throw new Error(`第一个参数(key)不能为_methods键`);
|
|
628
582
|
}
|
|
629
|
-
if (
|
|
630
|
-
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)不能为函数`);
|
|
631
587
|
}
|
|
632
|
-
this.
|
|
633
|
-
this[
|
|
588
|
+
this._datas[key] = true;
|
|
589
|
+
this[key] = value;
|
|
634
590
|
};
|
|
635
591
|
|
|
636
592
|
/**
|
|
637
|
-
*
|
|
638
|
-
* @
|
|
639
|
-
* @returns {Function|null} 方法函数,不存在返回 null
|
|
593
|
+
* 获取缓存文件路径
|
|
594
|
+
* @returns {string} 缓存文件路径
|
|
640
595
|
*/
|
|
641
|
-
Mod.prototype.
|
|
642
|
-
return this.
|
|
596
|
+
Mod.prototype.getCacheFile = function () {
|
|
597
|
+
return this.config.cache_file || `/cache/${this.constructor.name}_${this.config.name}.json`;
|
|
643
598
|
};
|
|
644
599
|
|
|
645
600
|
/**
|
|
646
|
-
*
|
|
647
|
-
* @param {string} name 方法名
|
|
601
|
+
* 保存数据
|
|
648
602
|
*/
|
|
649
|
-
Mod.prototype.
|
|
650
|
-
|
|
603
|
+
Mod.prototype.saveDatas = function () {
|
|
604
|
+
let datas = this.getDatas();
|
|
605
|
+
let file = this.getCacheFile();
|
|
606
|
+
file.saveJson(datas);
|
|
651
607
|
};
|
|
652
608
|
|
|
653
609
|
/**
|
|
654
|
-
*
|
|
655
|
-
* @param {string} name 方法名
|
|
656
|
-
* @returns {boolean} 是否存在
|
|
610
|
+
* 加载数据
|
|
657
611
|
*/
|
|
658
|
-
Mod.prototype.
|
|
659
|
-
|
|
612
|
+
Mod.prototype.loadDatas = function () {
|
|
613
|
+
let file = this.getCacheFile();
|
|
614
|
+
let datas = file.loadJson();
|
|
615
|
+
this.setDatas(datas);
|
|
660
616
|
};
|
|
661
617
|
|
|
662
618
|
/**
|
|
663
|
-
*
|
|
664
|
-
*
|
|
665
|
-
* @param {
|
|
666
|
-
* @
|
|
667
|
-
* @returns {Promise<*>} 方法执行结果
|
|
619
|
+
* 调用函数(核心执行方法)
|
|
620
|
+
* @param {string} method 函数名
|
|
621
|
+
* @param {*} params 参数集合
|
|
622
|
+
* @returns {Promise<any>} 执行结果
|
|
668
623
|
*/
|
|
669
|
-
Mod.prototype.call = async function (
|
|
670
|
-
|
|
671
|
-
if (
|
|
672
|
-
|
|
673
|
-
}
|
|
674
|
-
|
|
675
|
-
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} 未注册,跳过执行`);
|
|
676
628
|
return null;
|
|
677
629
|
}
|
|
678
630
|
|
|
679
|
-
|
|
631
|
+
let result;
|
|
680
632
|
|
|
681
633
|
try {
|
|
682
|
-
|
|
683
|
-
if (
|
|
684
|
-
result = await this._callMain(
|
|
685
|
-
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);
|
|
686
638
|
} catch (err) {
|
|
687
|
-
this.log('error',
|
|
639
|
+
this.log('error', `[${this.config.name}] 执行 ${method} 失败: ${err.message}`, err);
|
|
688
640
|
return null;
|
|
689
641
|
}
|
|
690
642
|
|
|
@@ -693,21 +645,21 @@ Mod.prototype.call = async function (name, params) {
|
|
|
693
645
|
|
|
694
646
|
/**
|
|
695
647
|
* 执行前置钩子
|
|
696
|
-
* 查找 methodBefore 方法并执行
|
|
697
648
|
* @param {string} method 方法名
|
|
698
|
-
* @param {
|
|
699
|
-
* @returns {Promise
|
|
700
|
-
* @private
|
|
649
|
+
* @param {Array} params 参数
|
|
650
|
+
* @returns {Promise<any>} 前置钩子结果
|
|
701
651
|
*/
|
|
702
652
|
Mod.prototype._callBefore = async function (method, params) {
|
|
703
|
-
|
|
704
|
-
|
|
653
|
+
let before_method = `${method}Before`;
|
|
654
|
+
let before_func = this.getMethod(before_method);
|
|
655
|
+
|
|
705
656
|
if (typeof before_func !== 'function') return undefined;
|
|
706
657
|
|
|
707
658
|
try {
|
|
708
|
-
|
|
659
|
+
let result = await before_func.call(this, ...params);
|
|
660
|
+
return result;
|
|
709
661
|
} catch (err) {
|
|
710
|
-
this.log('error',
|
|
662
|
+
this.log('error', `[${this.config.name}] 执行前置钩子 ${before_method} 失败`, err);
|
|
711
663
|
return undefined;
|
|
712
664
|
}
|
|
713
665
|
};
|
|
@@ -715,33 +667,34 @@ Mod.prototype._callBefore = async function (method, params) {
|
|
|
715
667
|
/**
|
|
716
668
|
* 执行主方法
|
|
717
669
|
* @param {Function} method_func 方法函数
|
|
718
|
-
* @param {
|
|
719
|
-
* @
|
|
720
|
-
* @
|
|
670
|
+
* @param {Array} params 参数
|
|
671
|
+
* @param {any} result 前置钩子结果
|
|
672
|
+
* @returns {Promise<any>} 主方法结果
|
|
721
673
|
*/
|
|
722
|
-
Mod.prototype._callMain = async function (method_func, params) {
|
|
723
|
-
|
|
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;
|
|
724
678
|
};
|
|
725
679
|
|
|
726
680
|
/**
|
|
727
681
|
* 执行后置钩子
|
|
728
|
-
* 查找 methodAfter 方法并执行
|
|
729
682
|
* @param {string} method 方法名
|
|
730
|
-
* @param {
|
|
731
|
-
* @param {
|
|
732
|
-
* @returns {Promise
|
|
733
|
-
* @private
|
|
683
|
+
* @param {Array} params 参数
|
|
684
|
+
* @param {any} result 主方法结果
|
|
685
|
+
* @returns {Promise<any>} 后置钩子结果
|
|
734
686
|
*/
|
|
735
687
|
Mod.prototype._callAfter = async function (method, params, result) {
|
|
736
|
-
|
|
737
|
-
|
|
688
|
+
let after_method = `${method}After`;
|
|
689
|
+
let after_func = this.getMethod(after_method);
|
|
690
|
+
|
|
738
691
|
if (typeof after_func !== 'function') return result;
|
|
739
692
|
|
|
740
693
|
try {
|
|
741
|
-
|
|
694
|
+
let after = await after_func.call(this, result, ...params);
|
|
742
695
|
return after !== undefined ? after : result;
|
|
743
696
|
} catch (err) {
|
|
744
|
-
this.log('error',
|
|
697
|
+
this.log('error', `[${this.config.name}] 执行后置钩子 ${after_method} 失败`, err);
|
|
745
698
|
return result;
|
|
746
699
|
}
|
|
747
700
|
};
|
|
@@ -752,8 +705,9 @@ Mod.prototype._callAfter = async function (method, params, result) {
|
|
|
752
705
|
*/
|
|
753
706
|
Mod.prototype.getDir = function () {
|
|
754
707
|
if (!this.getParent) return '';
|
|
755
|
-
|
|
756
|
-
return parent ? parent.getDir() || '' : '';
|
|
708
|
+
return this.getParent()?.getDir() || '';
|
|
757
709
|
};
|
|
758
710
|
|
|
759
|
-
module.exports = {
|
|
711
|
+
module.exports = {
|
|
712
|
+
Mod
|
|
713
|
+
};
|