mm_expand 2.0.3 → 2.0.5
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/lib/base.js +75 -145
- package/lib/event.js +4 -0
- package/lib/validator.js +6 -6
- package/package.json +1 -1
package/lib/base.js
CHANGED
|
@@ -8,39 +8,25 @@ require('./global');
|
|
|
8
8
|
* 基础类
|
|
9
9
|
*/
|
|
10
10
|
class Base extends Event {
|
|
11
|
-
static config = {
|
|
12
|
-
// 模块名称
|
|
13
|
-
name: '',
|
|
14
|
-
// 模块标题
|
|
15
|
-
title: '',
|
|
16
|
-
// 模块描述
|
|
17
|
-
description: '',
|
|
18
|
-
// 状态, 可选值: 1-启用, 0-禁用
|
|
19
|
-
state: 1
|
|
20
|
-
};
|
|
21
|
-
|
|
22
11
|
/**
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
12
|
+
* 构造函数
|
|
13
|
+
* @param {object} config 配置参数
|
|
14
|
+
*/
|
|
26
15
|
constructor(config) {
|
|
27
|
-
super();
|
|
16
|
+
super(config);
|
|
28
17
|
/**
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
this.
|
|
18
|
+
* 状态
|
|
19
|
+
* @type {string} - 状态, 可选值: creating, created, initing, inited, loading, loaded, starting, running, stopping, stopped, unloading, unloaded, destroying, destroyed
|
|
20
|
+
*/
|
|
21
|
+
this.status = 'creating';
|
|
33
22
|
|
|
34
|
-
/**
|
|
35
|
-
* 状态
|
|
36
|
-
* @type {string} - 状态, 可选值: created, initing, starting, running, stopped, destroying, destroyed
|
|
37
|
-
*/
|
|
38
|
-
this.status = 'created';
|
|
39
|
-
|
|
40
|
-
// 设置配置
|
|
41
|
-
this.setConfig(config);
|
|
42
23
|
// 初始化监听
|
|
43
24
|
this._initListen();
|
|
25
|
+
// 设置配置
|
|
26
|
+
this.setConfig(config);
|
|
27
|
+
|
|
28
|
+
// 状态变更为 created 已完成
|
|
29
|
+
this.status = 'created';
|
|
44
30
|
}
|
|
45
31
|
}
|
|
46
32
|
|
|
@@ -48,10 +34,10 @@ class Base extends Event {
|
|
|
48
34
|
* 日志输出
|
|
49
35
|
* @param {string} level 日志级别
|
|
50
36
|
* @param {string} message 日志消息
|
|
51
|
-
* @param {...any}
|
|
37
|
+
* @param {...any} params 日志参数
|
|
52
38
|
*/
|
|
53
|
-
Base.prototype.log = function (level, message, ...
|
|
54
|
-
this._logger[level](`[${this.constructor.name}]
|
|
39
|
+
Base.prototype.log = function (level, message, ...params) {
|
|
40
|
+
this._logger[level](`[${this.constructor.name}] ${message}`, ...params);
|
|
55
41
|
};
|
|
56
42
|
|
|
57
43
|
/**
|
|
@@ -67,39 +53,11 @@ Base.prototype._initListen = function () {
|
|
|
67
53
|
this.on('init:after', (ctx) => {
|
|
68
54
|
this.status = 'inited';
|
|
69
55
|
});
|
|
70
|
-
// 启动
|
|
71
|
-
this.on('start:before', (ctx) => {
|
|
72
|
-
status_last = this.status;
|
|
73
|
-
this.status = 'starting';
|
|
74
|
-
});
|
|
75
|
-
this.on('start:success', (ctx) => {
|
|
76
|
-
this.status = 'running';
|
|
77
|
-
});
|
|
78
|
-
this.on('start:error', (ctx) => {
|
|
79
|
-
this.status = status_last;
|
|
80
|
-
});
|
|
81
|
-
// 停止
|
|
82
|
-
this.on('stop:before', (ctx) => {
|
|
83
|
-
status_last = this.status;
|
|
84
|
-
this.status = 'stopping';
|
|
85
|
-
});
|
|
86
|
-
this.on('stop:success', (ctx) => {
|
|
87
|
-
this.status = 'stopped';
|
|
88
|
-
});
|
|
89
|
-
this.on('stop:error', (ctx) => {
|
|
90
|
-
this.status = status_last;
|
|
91
|
-
});
|
|
92
56
|
// 销毁
|
|
93
57
|
this.on('destroy:before', (ctx) => {
|
|
94
58
|
status_last = this.status;
|
|
95
59
|
this.status = 'destroying';
|
|
96
60
|
});
|
|
97
|
-
this.on('destroy:check', (ctx) => {
|
|
98
|
-
if (status_last !== 'stopped') {
|
|
99
|
-
status_last = this.status;
|
|
100
|
-
ctx.error = new Error('销毁前必须先停止');
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
61
|
this.on('destroy:success', (ctx) => {
|
|
104
62
|
this.status = 'destroyed';
|
|
105
63
|
});
|
|
@@ -122,37 +80,23 @@ Base.prototype.setConfig = function (config) {
|
|
|
122
80
|
|
|
123
81
|
/**
|
|
124
82
|
* 初始化
|
|
125
|
-
* @param {...object}
|
|
83
|
+
* @param {...object} params 参数列表
|
|
126
84
|
* @returns {object} 返回当前对象
|
|
127
85
|
*/
|
|
128
|
-
Base.prototype._initCore = async function (...
|
|
86
|
+
Base.prototype._initCore = async function (...params) {
|
|
129
87
|
this.status = 'init';
|
|
130
88
|
return this;
|
|
131
89
|
};
|
|
132
90
|
|
|
133
91
|
/**
|
|
134
92
|
* 初始化
|
|
135
|
-
* @param {...object}
|
|
93
|
+
* @param {...object} params 参数列表
|
|
136
94
|
* @returns {object} 返回当前对象
|
|
137
95
|
*/
|
|
138
|
-
Base.prototype.init = async function (...
|
|
96
|
+
Base.prototype.init = async function (...params) {
|
|
139
97
|
return this;
|
|
140
98
|
};
|
|
141
99
|
|
|
142
|
-
/**
|
|
143
|
-
* 启动
|
|
144
|
-
*/
|
|
145
|
-
Base.prototype.start = async function () {
|
|
146
|
-
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* 停止
|
|
151
|
-
*/
|
|
152
|
-
Base.prototype.stop = async function () {
|
|
153
|
-
|
|
154
|
-
};
|
|
155
|
-
|
|
156
100
|
/**
|
|
157
101
|
* 销毁
|
|
158
102
|
*/
|
|
@@ -161,36 +105,23 @@ Base.prototype.destroy = async function () {
|
|
|
161
105
|
};
|
|
162
106
|
|
|
163
107
|
/**
|
|
164
|
-
*
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
await this.do('start');
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* 是否运行中
|
|
173
|
-
* @returns {boolean} 是否运行中
|
|
174
|
-
*/
|
|
175
|
-
Base.prototype.isRunning = function () {
|
|
176
|
-
return this.status === 'running';
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* 结束
|
|
108
|
+
* 事件驱动
|
|
109
|
+
* @param {string} method 方法名称
|
|
110
|
+
* @param {...object} params 参数列表
|
|
111
|
+
* @returns {object} 返回执行上下文对象
|
|
181
112
|
*/
|
|
182
|
-
Base.prototype.
|
|
183
|
-
await this.
|
|
184
|
-
|
|
113
|
+
Base.prototype.do = async function (method, ...params) {
|
|
114
|
+
let ctx = await this._do(method, ...params);
|
|
115
|
+
return ctx.result;
|
|
185
116
|
};
|
|
186
117
|
|
|
187
118
|
/**
|
|
188
119
|
* 事件驱动
|
|
189
120
|
* @param {string} method 方法名称
|
|
190
|
-
* @param {...object}
|
|
121
|
+
* @param {...object} params 参数列表
|
|
191
122
|
* @returns {object} 返回执行结果
|
|
192
123
|
*/
|
|
193
|
-
Base.prototype.
|
|
124
|
+
Base.prototype._do = async function (method, ...params) {
|
|
194
125
|
// 入参校验
|
|
195
126
|
if (!method || typeof method !== 'string') {
|
|
196
127
|
throw new TypeError('方法名必须是非空字符串');
|
|
@@ -202,7 +133,7 @@ Base.prototype.do = async function (method, ...args) {
|
|
|
202
133
|
|
|
203
134
|
let ctx = {
|
|
204
135
|
method,
|
|
205
|
-
params
|
|
136
|
+
params,
|
|
206
137
|
result: undefined,
|
|
207
138
|
error: undefined,
|
|
208
139
|
cancelled: false
|
|
@@ -215,26 +146,20 @@ Base.prototype.do = async function (method, ...args) {
|
|
|
215
146
|
}
|
|
216
147
|
|
|
217
148
|
await this._doExecuteMethod(method, ctx);
|
|
218
|
-
|
|
219
|
-
// 如果执行成功,进行成功处理
|
|
220
|
-
if (!ctx.error) {
|
|
221
|
-
await this._doSuccessProcess(method, ctx);
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
return ctx.result;
|
|
225
149
|
} catch (error) {
|
|
226
150
|
ctx.error = error;
|
|
227
|
-
|
|
228
|
-
|
|
151
|
+
}
|
|
152
|
+
if (ctx.error) {
|
|
229
153
|
// 执行错误处理
|
|
230
154
|
await this._doErrorProcess(method, ctx);
|
|
231
|
-
|
|
232
|
-
// 重新抛出错误,让上层能够捕获
|
|
233
|
-
throw error;
|
|
234
|
-
} finally {
|
|
235
|
-
// 无论成功失败,都要执行后处理
|
|
236
|
-
await this._doAfterProcess(method, ctx);
|
|
237
155
|
}
|
|
156
|
+
else {
|
|
157
|
+
// 如果执行成功,进行成功处理
|
|
158
|
+
await this._doSuccessProcess(method, ctx);
|
|
159
|
+
}
|
|
160
|
+
// 无论成功失败,都要执行后处理
|
|
161
|
+
await this._doAfterProcess(method, ctx);
|
|
162
|
+
return ctx;
|
|
238
163
|
};
|
|
239
164
|
|
|
240
165
|
/**
|
|
@@ -343,10 +268,10 @@ Base.prototype._doErrorProcess = async function (method, ctx) {
|
|
|
343
268
|
/**
|
|
344
269
|
* 快速执行方法
|
|
345
270
|
* @param {string} method 方法名称
|
|
346
|
-
* @param {...object}
|
|
271
|
+
* @param {...object} params 参数列表
|
|
347
272
|
* @returns {object} 返回执行结果
|
|
348
273
|
*/
|
|
349
|
-
Base.prototype.doEasy = async function (method, ...
|
|
274
|
+
Base.prototype.doEasy = async function (method, ...params) {
|
|
350
275
|
// 快速检查方法是否存在
|
|
351
276
|
if (!this[method]) {
|
|
352
277
|
throw new Error(`方法${method}不存在`);
|
|
@@ -354,7 +279,7 @@ Base.prototype.doEasy = async function (method, ...args) {
|
|
|
354
279
|
|
|
355
280
|
try {
|
|
356
281
|
// 直接执行主方法,跳过所有事件处理
|
|
357
|
-
return await this[method](...
|
|
282
|
+
return await this[method](...params);
|
|
358
283
|
} catch (error) {
|
|
359
284
|
// 简单错误处理
|
|
360
285
|
this.log('error', `快速执行方法${method}出错:`, error);
|
|
@@ -365,15 +290,15 @@ Base.prototype.doEasy = async function (method, ...args) {
|
|
|
365
290
|
/**
|
|
366
291
|
* 智能执行方法(自动选择do或doEasy)
|
|
367
292
|
* @param {string} method 方法名称
|
|
368
|
-
* @param {...object}
|
|
293
|
+
* @param {...object} params 参数列表
|
|
369
294
|
* @param {object} options 执行选项
|
|
370
295
|
* @returns {object} 返回执行结果
|
|
371
296
|
*/
|
|
372
|
-
Base.prototype.doSmart = async function (method, ...
|
|
297
|
+
Base.prototype.doSmart = async function (method, ...params) {
|
|
373
298
|
// 提取选项参数
|
|
374
299
|
let options = {};
|
|
375
|
-
if (
|
|
376
|
-
options =
|
|
300
|
+
if (params.length > 0 && typeof params[params.length - 1] === 'object') {
|
|
301
|
+
options = params.pop();
|
|
377
302
|
}
|
|
378
303
|
|
|
379
304
|
// 检查是否有监听器
|
|
@@ -382,10 +307,10 @@ Base.prototype.doSmart = async function (method, ...args) {
|
|
|
382
307
|
// 根据选项和监听器情况选择执行方式
|
|
383
308
|
if (options.fast || !has_listeners) {
|
|
384
309
|
// 快速执行:无监听器或明确要求快速
|
|
385
|
-
return await this.doEasy(method, ...
|
|
310
|
+
return await this.doEasy(method, ...params);
|
|
386
311
|
} else {
|
|
387
312
|
// 完整事件驱动执行
|
|
388
|
-
return await this.do(method, ...
|
|
313
|
+
return await this.do(method, ...params);
|
|
389
314
|
}
|
|
390
315
|
};
|
|
391
316
|
|
|
@@ -397,45 +322,50 @@ Base.prototype.doSmart = async function (method, ...args) {
|
|
|
397
322
|
*/
|
|
398
323
|
Base.prototype._hasEventListeners = function (method) {
|
|
399
324
|
return this.listenerCount(method + ':before') > 0 ||
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
325
|
+
this.listenerCount(method + ':check') > 0 ||
|
|
326
|
+
this.listenerCount(method + ':render') > 0 ||
|
|
327
|
+
this.listenerCount(method + ':after') > 0;
|
|
403
328
|
};
|
|
404
329
|
|
|
405
330
|
/**
|
|
406
331
|
* 主要执行方法
|
|
407
|
-
* @param {...any}
|
|
332
|
+
* @param {...any} params 参数列表
|
|
408
333
|
*/
|
|
409
|
-
Base.prototype.main = async function (...
|
|
410
|
-
|
|
334
|
+
Base.prototype.main = async function (...params) {
|
|
335
|
+
console.log('main', ...params);
|
|
336
|
+
return null;
|
|
411
337
|
};
|
|
412
338
|
|
|
413
339
|
/**
|
|
414
|
-
*
|
|
415
|
-
* @param {...object}
|
|
340
|
+
* 执行主方法
|
|
341
|
+
* @param {...object} params 参数列表
|
|
416
342
|
* @returns {object} 返回执行结果
|
|
417
343
|
*/
|
|
418
|
-
Base.prototype.run = async function (...
|
|
419
|
-
return await this.do('main', ...
|
|
344
|
+
Base.prototype.run = async function (...params) {
|
|
345
|
+
return await this.do('main', ...params);
|
|
420
346
|
};
|
|
421
347
|
|
|
422
348
|
/**
|
|
423
349
|
* 执行方法
|
|
424
350
|
* @param {string} method 方法名称
|
|
425
|
-
* @param {...any}
|
|
351
|
+
* @param {...any} params 参数列表
|
|
426
352
|
* @returns
|
|
427
353
|
*/
|
|
428
|
-
Base.prototype.exec = async function (method, ...
|
|
429
|
-
|
|
354
|
+
Base.prototype.exec = async function (method, ...params) {
|
|
355
|
+
const result = await this._do(method, ...params);
|
|
356
|
+
if (result.error) {
|
|
357
|
+
throw result.error;
|
|
358
|
+
}
|
|
359
|
+
return result;
|
|
430
360
|
};
|
|
431
361
|
|
|
432
362
|
/**
|
|
433
363
|
* 执行指令(主要)
|
|
434
364
|
* @param {string} command 指令
|
|
435
|
-
* @param {...object}
|
|
365
|
+
* @param {...object} params 参数列表
|
|
436
366
|
* @returns {object} 返回执行结果
|
|
437
367
|
*/
|
|
438
|
-
Base.prototype.cmd = async function (command, ...
|
|
368
|
+
Base.prototype.cmd = async function (command, ...params) {
|
|
439
369
|
// 入参校验
|
|
440
370
|
if (!command || typeof command !== 'string') {
|
|
441
371
|
this.log('error', '指令必须是非空字符串');
|
|
@@ -445,7 +375,7 @@ Base.prototype.cmd = async function (command, ...args) {
|
|
|
445
375
|
// 创建执行上下文
|
|
446
376
|
const ctx = {
|
|
447
377
|
command,
|
|
448
|
-
|
|
378
|
+
params,
|
|
449
379
|
cmds: [],
|
|
450
380
|
obj: this,
|
|
451
381
|
error: null,
|
|
@@ -564,7 +494,7 @@ Base.prototype._cmdHandleFunction = async function (ctx, is_last) {
|
|
|
564
494
|
|
|
565
495
|
if (is_last) {
|
|
566
496
|
try {
|
|
567
|
-
ctx.result = await ctx.obj(...ctx.
|
|
497
|
+
ctx.result = await ctx.obj(...ctx.params);
|
|
568
498
|
return true;
|
|
569
499
|
} catch (error) {
|
|
570
500
|
this.log('error', `执行方法${ctx.command}出错:`, error);
|
|
@@ -617,12 +547,12 @@ Base.prototype._cmdHandlePrimitive = function (ctx, is_last) {
|
|
|
617
547
|
|
|
618
548
|
/**
|
|
619
549
|
* 帮助(主要)
|
|
620
|
-
* @param {...object}
|
|
550
|
+
* @param {...object} params 参数列表
|
|
621
551
|
* @returns {object} 返回查询结果
|
|
622
552
|
*/
|
|
623
|
-
Base.prototype.help = async function (...
|
|
553
|
+
Base.prototype.help = async function (...params) {
|
|
624
554
|
const ctx = {
|
|
625
|
-
|
|
555
|
+
params,
|
|
626
556
|
param: null,
|
|
627
557
|
result: '',
|
|
628
558
|
error: null
|
|
@@ -646,8 +576,8 @@ Base.prototype.help = async function (...args) {
|
|
|
646
576
|
* @private
|
|
647
577
|
*/
|
|
648
578
|
Base.prototype._helpProcessArgs = function (ctx) {
|
|
649
|
-
if (ctx.
|
|
650
|
-
ctx.param = ctx.
|
|
579
|
+
if (ctx.params.length > 0) {
|
|
580
|
+
ctx.param = ctx.params[0];
|
|
651
581
|
|
|
652
582
|
if (ctx.param !== undefined && ctx.param !== null && typeof ctx.param !== 'string') {
|
|
653
583
|
this.log('warn', 'help参数应为字符串类型,将自动转换');
|
package/lib/event.js
CHANGED
package/lib/validator.js
CHANGED
|
@@ -389,7 +389,7 @@ Validator.prototype.notNaN = function (val) {
|
|
|
389
389
|
* @param {*} val 要验证的值
|
|
390
390
|
* @returns {string} 通过返回空字符串,没通过返回错误信息
|
|
391
391
|
*/
|
|
392
|
-
Validator.prototype.
|
|
392
|
+
Validator.prototype.notEmpty = function (val) {
|
|
393
393
|
let bl = val !== undefined && val !== null && val !== '';
|
|
394
394
|
if (!bl) {
|
|
395
395
|
return `参数必须不是空值,实际值为:${val}`;
|
|
@@ -1337,7 +1337,7 @@ Validator.prototype.checkRequired = function (val, rule) {
|
|
|
1337
1337
|
return tip;
|
|
1338
1338
|
}
|
|
1339
1339
|
if (rule.not_empty) {
|
|
1340
|
-
tip = this.
|
|
1340
|
+
tip = this.notEmpty(val);
|
|
1341
1341
|
if (tip) {
|
|
1342
1342
|
return tip;
|
|
1343
1343
|
}
|
|
@@ -1657,9 +1657,9 @@ Number.prototype.isMax = function (max) {
|
|
|
1657
1657
|
* 检查字符串是否为空
|
|
1658
1658
|
* @returns {boolean} 是否为空
|
|
1659
1659
|
*/
|
|
1660
|
-
String.prototype.
|
|
1660
|
+
String.prototype.notEmpty = function () {
|
|
1661
1661
|
let str = this + '';
|
|
1662
|
-
let tip = validator.
|
|
1662
|
+
let tip = validator.notEmpty(str);
|
|
1663
1663
|
if (tip) {
|
|
1664
1664
|
throw new TypeError(tip);
|
|
1665
1665
|
}
|
|
@@ -1776,7 +1776,7 @@ String.prototype.isName = function () {
|
|
|
1776
1776
|
* 检查数组是否为空
|
|
1777
1777
|
* @returns {Array} 原始数组对象
|
|
1778
1778
|
*/
|
|
1779
|
-
Array.prototype.
|
|
1779
|
+
Array.prototype.notEmpty = function () {
|
|
1780
1780
|
if (this.length === 0) {
|
|
1781
1781
|
throw new TypeError('数组不能为空');
|
|
1782
1782
|
}
|
|
@@ -1810,7 +1810,7 @@ Array.prototype.contains = function (element) {
|
|
|
1810
1810
|
};
|
|
1811
1811
|
|
|
1812
1812
|
// Object原型方法 - 设置为不可枚举,避免影响for...in循环
|
|
1813
|
-
Object.defineProperty(Object.prototype, '
|
|
1813
|
+
Object.defineProperty(Object.prototype, 'notEmpty', {
|
|
1814
1814
|
value: function () {
|
|
1815
1815
|
if (Object.keys(this).length === 0) {
|
|
1816
1816
|
throw new TypeError('对象不能为空');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_expand",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.5",
|
|
4
4
|
"description": "Super Meimei Prototype Function Extension Module - Enhanced operations for string, array, object, date manipulation with error prevention and simplified business logic.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|