mm_machine 3.1.2 → 3.1.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/index.js +1 -19
- package/manager.js +7 -6
- package/package.json +1 -1
- package/registry.js +10 -177
package/index.js
CHANGED
|
@@ -26,7 +26,7 @@ class Index {
|
|
|
26
26
|
};
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
//
|
|
29
|
+
// 注册表:管理模块配置持久化
|
|
30
30
|
if (!$.registry) {
|
|
31
31
|
$.registry = new Registry({
|
|
32
32
|
name: this.constructor.name + '_' + this.config.name
|
|
@@ -46,24 +46,6 @@ Index.prototype.load = function () {
|
|
|
46
46
|
$.registry.load();
|
|
47
47
|
};
|
|
48
48
|
|
|
49
|
-
/**
|
|
50
|
-
* 获取模块实例(懒加载)
|
|
51
|
-
* @param {string} name 模块名
|
|
52
|
-
* @returns {object|null} 模块实例
|
|
53
|
-
*/
|
|
54
|
-
Index.prototype.getMod = function (name) {
|
|
55
|
-
return $.registry.getMod(name);
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* 按类名获取模块实例列表
|
|
60
|
-
* @param {string} class_name 模块类名
|
|
61
|
-
* @returns {Array} 模块实例数组
|
|
62
|
-
*/
|
|
63
|
-
Index.prototype.getMods = function (class_name) {
|
|
64
|
-
return $.registry.getMods(class_name);
|
|
65
|
-
};
|
|
66
|
-
|
|
67
49
|
module.exports = {
|
|
68
50
|
Index,
|
|
69
51
|
Mod,
|
package/manager.js
CHANGED
|
@@ -127,8 +127,12 @@ Manager.prototype.getEventer = function () {
|
|
|
127
127
|
return $.eventer;
|
|
128
128
|
};
|
|
129
129
|
|
|
130
|
+
/**
|
|
131
|
+
* 获取父项键名
|
|
132
|
+
* @returns {string} 父项键名
|
|
133
|
+
*/
|
|
130
134
|
Manager.prototype.getParentKey = function () {
|
|
131
|
-
return this.constructor.name + '_' + this.name;
|
|
135
|
+
return this.constructor.name + '_' + this.config.name;
|
|
132
136
|
};
|
|
133
137
|
|
|
134
138
|
/**
|
|
@@ -357,8 +361,7 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
|
|
|
357
361
|
sort: mod.config.sort,
|
|
358
362
|
state: mod.config.state,
|
|
359
363
|
config_file: config_file,
|
|
360
|
-
main: mod.config.main || ''
|
|
361
|
-
class_name: this.config.class_name || 'Drive'
|
|
364
|
+
main: mod.config.main || ''
|
|
362
365
|
};
|
|
363
366
|
// 获取触发键
|
|
364
367
|
let trigger = this.config.trigger;
|
|
@@ -368,7 +371,6 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
|
|
|
368
371
|
}
|
|
369
372
|
}
|
|
370
373
|
// 用管理器自身的 name 作为 parent 标签,实现多 Manager 隔离
|
|
371
|
-
// 同步 registry 的内部缓存,确保 Index.getMod() 可查
|
|
372
374
|
$.registry.setMod(this.getParentKey(), mod.config.name, entry);
|
|
373
375
|
}
|
|
374
376
|
} else {
|
|
@@ -380,8 +382,7 @@ Manager.prototype.regMod = function (config_file, config = {}, script = {}) {
|
|
|
380
382
|
sort: mod.config.sort,
|
|
381
383
|
state: mod.config.state,
|
|
382
384
|
config_file: config_file,
|
|
383
|
-
main: mod.config.main || ''
|
|
384
|
-
class_name: this.config.class_name || 'Drive'
|
|
385
|
+
main: mod.config.main || ''
|
|
385
386
|
};
|
|
386
387
|
this.infos.push(entry);
|
|
387
388
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_machine",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.5",
|
|
4
4
|
"description": "A flexible Node.js plugin mechanism system for dynamic loading, management and execution of modules. Supports hot reload, lifecycle management, and modern JavaScript features.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
package/registry.js
CHANGED
|
@@ -2,7 +2,7 @@ require('mm_expand');
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* 注册类 - 注册表模式
|
|
5
|
-
*
|
|
5
|
+
* 管理模块配置的持久化、模块配置的 CRUD
|
|
6
6
|
*/
|
|
7
7
|
class Registry {
|
|
8
8
|
/**
|
|
@@ -15,167 +15,11 @@ class Registry {
|
|
|
15
15
|
dir: '/data/registry'.fullname(__dirname),
|
|
16
16
|
...config
|
|
17
17
|
};
|
|
18
|
-
this.class_dict = this.getDefaultClassDict();
|
|
19
18
|
this.mod_dict = {};
|
|
20
|
-
|
|
21
|
-
// 注册表缓存:类名 -> 类构造函数
|
|
22
|
-
this.class_cache = {};
|
|
23
|
-
this._preset();
|
|
19
|
+
this.config.dir.addDir();
|
|
24
20
|
}
|
|
25
21
|
}
|
|
26
22
|
|
|
27
|
-
/**
|
|
28
|
-
* 预设默认模块类
|
|
29
|
-
*/
|
|
30
|
-
Registry.prototype._preset = function () {
|
|
31
|
-
this.config.dir.addDir();
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* 获取模块类字典
|
|
36
|
-
* @returns {object} 模块类字典
|
|
37
|
-
*/
|
|
38
|
-
Registry.prototype.getClassDict = function () {
|
|
39
|
-
return this.class_dict;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* 获取默认模块类
|
|
44
|
-
* @returns {object} 默认模块类
|
|
45
|
-
*/
|
|
46
|
-
Registry.prototype.getDefaultClassDict = function () {
|
|
47
|
-
return {
|
|
48
|
-
Mod: './mod.js'.fullname(__dirname),
|
|
49
|
-
Drive: './drive.js'.fullname(__dirname),
|
|
50
|
-
Manager: './manager.js'.fullname(__dirname)
|
|
51
|
-
};
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* 设置模块类
|
|
56
|
-
* @param {string} class_name 模块类名
|
|
57
|
-
* @param {string} file 模块类文件路径
|
|
58
|
-
*/
|
|
59
|
-
Registry.prototype.setClass = function (class_name, file) {
|
|
60
|
-
this.class_dict[class_name] = file;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* 删除模块类
|
|
65
|
-
* @param {string} class_name 模块类名
|
|
66
|
-
*/
|
|
67
|
-
Registry.prototype.delClass = function (class_name) {
|
|
68
|
-
delete this.class_dict[class_name];
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
// ==== 注册表缓存方法 ====
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* 注册模块类到缓存
|
|
75
|
-
* @param {string} class_name 模块类名
|
|
76
|
-
* @param {Function} class_ref 模块类构造函数
|
|
77
|
-
*/
|
|
78
|
-
Registry.prototype.registerClass = function (class_name, class_ref) {
|
|
79
|
-
if (!class_name || !class_ref) {
|
|
80
|
-
throw new TypeError('模块类名和类引用不能为空');
|
|
81
|
-
}
|
|
82
|
-
this.class_cache[class_name] = class_ref;
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* 获取模块类构造函数
|
|
87
|
-
* @param {string} class_name 模块类名
|
|
88
|
-
* @returns {Function} 模块类构造函数
|
|
89
|
-
*/
|
|
90
|
-
Registry.prototype.getClass = function (class_name) {
|
|
91
|
-
return this.class_cache[class_name];
|
|
92
|
-
};
|
|
93
|
-
/**
|
|
94
|
-
* 获取模块实例(懒加载)
|
|
95
|
-
* 首次调用时根据 config 和已注册的类构造函数实例化
|
|
96
|
-
* @param {string} name 模块名
|
|
97
|
-
* @returns {object|null} 模块实例
|
|
98
|
-
*/
|
|
99
|
-
Registry.prototype.getMod = function (name) {
|
|
100
|
-
// 从 mod_dict 中查找配置
|
|
101
|
-
var config = null;
|
|
102
|
-
for (var parent in this.mod_dict) {
|
|
103
|
-
var lt = this.mod_dict[parent];
|
|
104
|
-
for (var i = 0; i < lt.length; i++) {
|
|
105
|
-
if (lt[i].name === name) {
|
|
106
|
-
config = lt[i];
|
|
107
|
-
break;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
if (config) break;
|
|
111
|
-
}
|
|
112
|
-
if (!config) return null;
|
|
113
|
-
|
|
114
|
-
var class_name = config.class_name || 'Drive';
|
|
115
|
-
var CS = this.class_cache[class_name];
|
|
116
|
-
if (!CS) {
|
|
117
|
-
console.error('模块类 ' + class_name + ' 未注册');
|
|
118
|
-
return null;
|
|
119
|
-
}
|
|
120
|
-
try {
|
|
121
|
-
return new CS(config);
|
|
122
|
-
} catch (err) {
|
|
123
|
-
console.error('实例化模块 ' + name + ' 失败: ' + err.message);
|
|
124
|
-
return null;
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* 按类名获取模块实例列表
|
|
130
|
-
* @param {string} class_name 模块类名
|
|
131
|
-
* @returns {Array} 模块实例数组
|
|
132
|
-
*/
|
|
133
|
-
Registry.prototype.getModsFromClass = function (class_name) {
|
|
134
|
-
var result = [];
|
|
135
|
-
for (var parent in this.mod_dict) {
|
|
136
|
-
var lt = this.mod_dict[parent];
|
|
137
|
-
for (var i = 0; i < lt.length; i++) {
|
|
138
|
-
var cfg = lt[i];
|
|
139
|
-
if ((cfg.class_name || 'Drive') === class_name) {
|
|
140
|
-
var instance = this.getMod(cfg.name);
|
|
141
|
-
if (instance) {
|
|
142
|
-
result.push(instance);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
return result;
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* 加载注册数据
|
|
152
|
-
* 从 JSON 文件读取持久化数据,注册所有类和模块到缓存
|
|
153
|
-
*/
|
|
154
|
-
Registry.prototype.load = function () {
|
|
155
|
-
this.read();
|
|
156
|
-
|
|
157
|
-
// 注册模块类(幂等-safe,Node require 返回同一引用)
|
|
158
|
-
for (var key in this.class_dict) {
|
|
159
|
-
var file = this.class_dict[key];
|
|
160
|
-
try {
|
|
161
|
-
var exports = require(file);
|
|
162
|
-
var class_ref = exports[key] || exports;
|
|
163
|
-
this.registerClass(key, class_ref);
|
|
164
|
-
} catch (err) {
|
|
165
|
-
console.error('注册模块类 ' + key + ' 失败: ' + err.message);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// 注册模块配置
|
|
170
|
-
for (var parent in this.mod_dict) {
|
|
171
|
-
var lt = this.mod_dict[parent];
|
|
172
|
-
for (var i = 0; i < lt.length; i++) {
|
|
173
|
-
var mod_cfg = lt[i];
|
|
174
|
-
this.setMod(parent, mod_cfg.name, mod_cfg);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
};
|
|
178
|
-
|
|
179
23
|
// ==== 配置项 CRUD ====
|
|
180
24
|
|
|
181
25
|
/**
|
|
@@ -193,7 +37,6 @@ Registry.prototype.newModel = function () {
|
|
|
193
37
|
sort: 100,
|
|
194
38
|
state: 1,
|
|
195
39
|
auto_start: 'auto',
|
|
196
|
-
class_name: 'Drive',
|
|
197
40
|
config_file: '',
|
|
198
41
|
main: ''
|
|
199
42
|
};
|
|
@@ -315,7 +158,6 @@ Registry.prototype.checkConfig = function (config) {
|
|
|
315
158
|
version,
|
|
316
159
|
state,
|
|
317
160
|
auto_start,
|
|
318
|
-
class_name,
|
|
319
161
|
config_file,
|
|
320
162
|
main,
|
|
321
163
|
dependencies
|
|
@@ -336,9 +178,6 @@ Registry.prototype.checkConfig = function (config) {
|
|
|
336
178
|
if (valid_auto.indexOf(auto_start) === -1 && ![true, false].includes(auto_start)) {
|
|
337
179
|
throw new TypeError('模块自动启动配置错误,只能为true、false、auto');
|
|
338
180
|
}
|
|
339
|
-
if (!this.class_cache[class_name]) {
|
|
340
|
-
throw new TypeError('模块类不存在');
|
|
341
|
-
}
|
|
342
181
|
if (!version) {
|
|
343
182
|
cg.version = '1.0.0';
|
|
344
183
|
}
|
|
@@ -365,17 +204,6 @@ Registry.prototype.getDir = function () {
|
|
|
365
204
|
return this.config.dir.fullname();
|
|
366
205
|
};
|
|
367
206
|
|
|
368
|
-
/**
|
|
369
|
-
* 获取模块类文件
|
|
370
|
-
* @returns {string} 模块类文件路径
|
|
371
|
-
*/
|
|
372
|
-
Registry.prototype.getClassFile = function () {
|
|
373
|
-
let file_name = this.config.name + '_class.json';
|
|
374
|
-
let file = file_name.fullname(this.getDir());
|
|
375
|
-
// file.addDir();
|
|
376
|
-
return file;
|
|
377
|
-
};
|
|
378
|
-
|
|
379
207
|
/**
|
|
380
208
|
* 获取模块文件
|
|
381
209
|
* @returns {string} 模块文件路径
|
|
@@ -383,16 +211,22 @@ Registry.prototype.getClassFile = function () {
|
|
|
383
211
|
Registry.prototype.getModFile = function () {
|
|
384
212
|
let file_name = this.config.name + '_module.json';
|
|
385
213
|
let file = file_name.fullname(this.getDir());
|
|
386
|
-
// file.addDir();
|
|
387
214
|
return file;
|
|
388
215
|
};
|
|
389
216
|
|
|
217
|
+
/**
|
|
218
|
+
* 加载注册数据
|
|
219
|
+
* 从 JSON 文件读取持久化数据
|
|
220
|
+
*/
|
|
221
|
+
Registry.prototype.load = function () {
|
|
222
|
+
this.read();
|
|
223
|
+
};
|
|
224
|
+
|
|
390
225
|
/**
|
|
391
226
|
* 读取模块
|
|
392
227
|
* @returns {Array} 模块配置项数组
|
|
393
228
|
*/
|
|
394
229
|
Registry.prototype.read = function () {
|
|
395
|
-
this.class_dict = this.getClassFile().loadJson() || this.getDefaultClassDict();
|
|
396
230
|
this.mod_dict = this.getModFile().loadJson() || {};
|
|
397
231
|
};
|
|
398
232
|
|
|
@@ -403,7 +237,6 @@ Registry.prototype.read = function () {
|
|
|
403
237
|
*/
|
|
404
238
|
Registry.prototype.save = function () {
|
|
405
239
|
try {
|
|
406
|
-
this.getClassFile().saveJson(this.class_dict);
|
|
407
240
|
this.getModFile().saveJson(this.mod_dict);
|
|
408
241
|
} catch (err) {
|
|
409
242
|
console.error('保存注册数据失败: ' + err.message);
|