mm_expand 2.0.4 → 2.0.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/lib/base.js +18 -118
- package/lib/event.js +4 -0
- package/package.json +1 -1
package/lib/base.js
CHANGED
|
@@ -8,41 +8,24 @@ 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
12
|
* 构造函数
|
|
24
13
|
* @param {object} config 配置参数
|
|
25
|
-
* @param {object} parent 父模块
|
|
26
14
|
*/
|
|
27
|
-
constructor(config
|
|
15
|
+
constructor(config) {
|
|
28
16
|
super();
|
|
29
|
-
/**
|
|
30
|
-
* 配置参数
|
|
31
|
-
* @type {object}
|
|
32
|
-
*/
|
|
33
|
-
this.config = { ...this.constructor.config };
|
|
34
|
-
|
|
35
17
|
/**
|
|
36
18
|
* 状态
|
|
37
19
|
* @type {string} - 状态, 可选值: creating, created, initing, inited, loading, loaded, starting, running, stopping, stopped, unloading, unloaded, destroying, destroyed
|
|
38
20
|
*/
|
|
39
21
|
this.status = 'creating';
|
|
40
22
|
|
|
41
|
-
// 设置配置
|
|
42
|
-
this.setConfig(config);
|
|
43
23
|
// 初始化监听
|
|
44
24
|
this._initListen();
|
|
45
|
-
|
|
25
|
+
// 设置配置
|
|
26
|
+
this.setConfig(config);
|
|
27
|
+
|
|
28
|
+
// 状态变更为 created 已完成
|
|
46
29
|
this.status = 'created';
|
|
47
30
|
}
|
|
48
31
|
}
|
|
@@ -54,7 +37,7 @@ class Base extends Event {
|
|
|
54
37
|
* @param {...any} params 日志参数
|
|
55
38
|
*/
|
|
56
39
|
Base.prototype.log = function (level, message, ...params) {
|
|
57
|
-
this._logger[level](`[${this.constructor.name}]
|
|
40
|
+
this._logger[level](`[${this.constructor.name}] ${message}`, ...params);
|
|
58
41
|
};
|
|
59
42
|
|
|
60
43
|
/**
|
|
@@ -70,55 +53,11 @@ Base.prototype._initListen = function () {
|
|
|
70
53
|
this.on('init:after', (ctx) => {
|
|
71
54
|
this.status = 'inited';
|
|
72
55
|
});
|
|
73
|
-
// 加载
|
|
74
|
-
this.on('load:before', (ctx) => {
|
|
75
|
-
status_last = this.status;
|
|
76
|
-
this.status = 'loading';
|
|
77
|
-
});
|
|
78
|
-
this.on('load:after', (ctx) => {
|
|
79
|
-
this.status = 'loaded';
|
|
80
|
-
});
|
|
81
|
-
// 启动
|
|
82
|
-
this.on('start:before', (ctx) => {
|
|
83
|
-
status_last = this.status;
|
|
84
|
-
this.status = 'starting';
|
|
85
|
-
});
|
|
86
|
-
this.on('start:success', (ctx) => {
|
|
87
|
-
this.status = 'running';
|
|
88
|
-
});
|
|
89
|
-
this.on('start:error', (ctx) => {
|
|
90
|
-
this.status = status_last;
|
|
91
|
-
});
|
|
92
|
-
// 停止
|
|
93
|
-
this.on('stop:before', (ctx) => {
|
|
94
|
-
status_last = this.status;
|
|
95
|
-
this.status = 'stopping';
|
|
96
|
-
});
|
|
97
|
-
this.on('stop:success', (ctx) => {
|
|
98
|
-
this.status = 'stopped';
|
|
99
|
-
});
|
|
100
|
-
this.on('stop:error', (ctx) => {
|
|
101
|
-
this.status = status_last;
|
|
102
|
-
});
|
|
103
|
-
// 卸载
|
|
104
|
-
this.on('unload:before', (ctx) => {
|
|
105
|
-
status_last = this.status;
|
|
106
|
-
this.status = 'unloading';
|
|
107
|
-
});
|
|
108
|
-
this.on('unload:after', (ctx) => {
|
|
109
|
-
this.status = 'unloaded';
|
|
110
|
-
});
|
|
111
56
|
// 销毁
|
|
112
57
|
this.on('destroy:before', (ctx) => {
|
|
113
58
|
status_last = this.status;
|
|
114
59
|
this.status = 'destroying';
|
|
115
60
|
});
|
|
116
|
-
this.on('destroy:check', (ctx) => {
|
|
117
|
-
if (status_last !== 'stopped') {
|
|
118
|
-
status_last = this.status;
|
|
119
|
-
ctx.error = new Error('销毁前必须先停止');
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
61
|
this.on('destroy:success', (ctx) => {
|
|
123
62
|
this.status = 'destroyed';
|
|
124
63
|
});
|
|
@@ -134,7 +73,10 @@ Base.prototype._initListen = function () {
|
|
|
134
73
|
*/
|
|
135
74
|
Base.prototype.setConfig = function (config) {
|
|
136
75
|
if (config) {
|
|
137
|
-
|
|
76
|
+
if (typeof config !== 'object') {
|
|
77
|
+
throw new TypeError('配置参数必须是对象');
|
|
78
|
+
}
|
|
79
|
+
Object.assign(...this.constructor.config, this.config, config);
|
|
138
80
|
}
|
|
139
81
|
return this.config;
|
|
140
82
|
};
|
|
@@ -158,20 +100,6 @@ Base.prototype.init = async function (...params) {
|
|
|
158
100
|
return this;
|
|
159
101
|
};
|
|
160
102
|
|
|
161
|
-
/**
|
|
162
|
-
* 启动
|
|
163
|
-
*/
|
|
164
|
-
Base.prototype.start = async function () {
|
|
165
|
-
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* 停止
|
|
170
|
-
*/
|
|
171
|
-
Base.prototype.stop = async function () {
|
|
172
|
-
|
|
173
|
-
};
|
|
174
|
-
|
|
175
103
|
/**
|
|
176
104
|
* 销毁
|
|
177
105
|
*/
|
|
@@ -179,31 +107,6 @@ Base.prototype.destroy = async function () {
|
|
|
179
107
|
|
|
180
108
|
};
|
|
181
109
|
|
|
182
|
-
/**
|
|
183
|
-
* 重启
|
|
184
|
-
*/
|
|
185
|
-
Base.prototype.restart = async function () {
|
|
186
|
-
await this.do('stop');
|
|
187
|
-
await this.do('start');
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* 是否运行中
|
|
192
|
-
* @returns {boolean} 是否运行中
|
|
193
|
-
*/
|
|
194
|
-
Base.prototype.isRunning = function () {
|
|
195
|
-
return this.status === 'running';
|
|
196
|
-
};
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* 结束
|
|
200
|
-
*/
|
|
201
|
-
Base.prototype.end = async function () {
|
|
202
|
-
await this.do('stop');
|
|
203
|
-
await this.do('destroy');
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
|
|
207
110
|
/**
|
|
208
111
|
* 事件驱动
|
|
209
112
|
* @param {string} method 方法名称
|
|
@@ -246,22 +149,19 @@ Base.prototype._do = async function (method, ...params) {
|
|
|
246
149
|
}
|
|
247
150
|
|
|
248
151
|
await this._doExecuteMethod(method, ctx);
|
|
249
|
-
|
|
250
|
-
// 如果执行成功,进行成功处理
|
|
251
|
-
if (!ctx.error) {
|
|
252
|
-
await this._doSuccessProcess(method, ctx);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
// return ctx.result;
|
|
256
152
|
} catch (error) {
|
|
257
153
|
ctx.error = error;
|
|
258
|
-
|
|
154
|
+
}
|
|
155
|
+
if (ctx.error) {
|
|
259
156
|
// 执行错误处理
|
|
260
157
|
await this._doErrorProcess(method, ctx);
|
|
261
|
-
} finally {
|
|
262
|
-
// 无论成功失败,都要执行后处理
|
|
263
|
-
await this._doAfterProcess(method, ctx);
|
|
264
158
|
}
|
|
159
|
+
else {
|
|
160
|
+
// 如果执行成功,进行成功处理
|
|
161
|
+
await this._doSuccessProcess(method, ctx);
|
|
162
|
+
}
|
|
163
|
+
// 无论成功失败,都要执行后处理
|
|
164
|
+
await this._doAfterProcess(method, ctx);
|
|
265
165
|
return ctx;
|
|
266
166
|
};
|
|
267
167
|
|
package/lib/event.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_expand",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.6",
|
|
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": {
|