mm_machine 2.5.0 → 2.5.2
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 +10 -5
- package/index.js +9 -1
- package/mod.js +55 -5
- package/package.json +1 -1
package/drive.js
CHANGED
|
@@ -361,12 +361,17 @@ Drive.prototype._setMainMethod = function (mod) {
|
|
|
361
361
|
if (name && mod[name]) {
|
|
362
362
|
this.main = mod[name];
|
|
363
363
|
}
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
364
|
+
|
|
365
|
+
for (let key in mod) {
|
|
366
|
+
let obj = mod[key];
|
|
367
|
+
if (typeof obj === 'function') {
|
|
368
|
+
this._methods[key] = obj;
|
|
369
|
+
}
|
|
370
|
+
else {
|
|
371
|
+
this._datas[key] = obj;
|
|
372
|
+
}
|
|
369
373
|
}
|
|
374
|
+
Object.assign(this, mod);
|
|
370
375
|
this.is_loaded = true;
|
|
371
376
|
};
|
|
372
377
|
|
package/index.js
CHANGED
|
@@ -286,7 +286,15 @@ Manager.prototype.newMod = function (config_file, config, script) {
|
|
|
286
286
|
mod.mode = this.config.mode;
|
|
287
287
|
mod.loadConfig(config_file);
|
|
288
288
|
if (script) {
|
|
289
|
-
|
|
289
|
+
for (let key in script) {
|
|
290
|
+
let obj = script[key];
|
|
291
|
+
if (typeof obj == 'function') {
|
|
292
|
+
mod.setMethod(key, obj);
|
|
293
|
+
}
|
|
294
|
+
else {
|
|
295
|
+
mod.setData(key, obj);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
290
298
|
}
|
|
291
299
|
if (config) {
|
|
292
300
|
mod.setConfig(config);
|
package/mod.js
CHANGED
|
@@ -75,6 +75,10 @@ class Mod extends Base {
|
|
|
75
75
|
*/
|
|
76
76
|
constructor(config, parent = null) {
|
|
77
77
|
super(config);
|
|
78
|
+
// 脚本方法
|
|
79
|
+
this._methods = {};
|
|
80
|
+
// 脚本数据
|
|
81
|
+
this._datas = {};
|
|
78
82
|
// 是否已加载
|
|
79
83
|
this.is_loaded = false;
|
|
80
84
|
// 追加查询到上级的方法
|
|
@@ -326,8 +330,17 @@ Mod.prototype.unload = async function () {
|
|
|
326
330
|
* 卸载核心
|
|
327
331
|
*/
|
|
328
332
|
Mod.prototype._unloadCore = async function () {
|
|
329
|
-
if (this.
|
|
330
|
-
this.
|
|
333
|
+
if (this._methods) {
|
|
334
|
+
for (let key in this._methods) {
|
|
335
|
+
delete this[key];
|
|
336
|
+
}
|
|
337
|
+
this._methods = {};
|
|
338
|
+
}
|
|
339
|
+
if (this._datas) {
|
|
340
|
+
for (let key in this._datas) {
|
|
341
|
+
delete this[key];
|
|
342
|
+
}
|
|
343
|
+
this._datas = {};
|
|
331
344
|
}
|
|
332
345
|
};
|
|
333
346
|
|
|
@@ -396,11 +409,12 @@ Mod.prototype.emitEvent = function (event, ...args) {
|
|
|
396
409
|
*/
|
|
397
410
|
Mod.prototype.main = async function (...args) {
|
|
398
411
|
let method = args[0];
|
|
399
|
-
|
|
412
|
+
let func = this.getMethod(method);
|
|
413
|
+
if (!func) {
|
|
400
414
|
throw new Error(`方法${method}不存在`);
|
|
401
415
|
}
|
|
402
416
|
let params = args.slice(1);
|
|
403
|
-
return await
|
|
417
|
+
return await func(...params);
|
|
404
418
|
};
|
|
405
419
|
|
|
406
420
|
/**
|
|
@@ -413,6 +427,9 @@ Mod.prototype.setMethods = function (methods) {
|
|
|
413
427
|
this._methods = {};
|
|
414
428
|
}
|
|
415
429
|
Object.assign(this._methods, methods);
|
|
430
|
+
for (let key in this._methods) {
|
|
431
|
+
this[key] = this._methods[key];
|
|
432
|
+
}
|
|
416
433
|
};
|
|
417
434
|
|
|
418
435
|
/**
|
|
@@ -426,6 +443,7 @@ Mod.prototype.setMethod = function (method, func) {
|
|
|
426
443
|
this._methods = {};
|
|
427
444
|
}
|
|
428
445
|
this._methods[method] = func;
|
|
446
|
+
this[method] = this._methods[method];
|
|
429
447
|
};
|
|
430
448
|
|
|
431
449
|
/**
|
|
@@ -434,13 +452,45 @@ Mod.prototype.setMethod = function (method, func) {
|
|
|
434
452
|
* @returns {Function|null} 方法函数
|
|
435
453
|
*/
|
|
436
454
|
Mod.prototype.getMethod = function (method) {
|
|
437
|
-
let func = this._methods
|
|
455
|
+
let func = this._methods[method];
|
|
438
456
|
if (!func) {
|
|
439
457
|
func = this[method];
|
|
440
458
|
}
|
|
441
459
|
return func;
|
|
442
460
|
};
|
|
443
461
|
|
|
462
|
+
/**
|
|
463
|
+
* 获取数据
|
|
464
|
+
* @returns {object} 数据对象
|
|
465
|
+
*/
|
|
466
|
+
Mod.prototype.getDatas = function () {
|
|
467
|
+
return this._datas;
|
|
468
|
+
};
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* 设置数据
|
|
472
|
+
* @param {object} datas 数据对象
|
|
473
|
+
*/
|
|
474
|
+
Mod.prototype.setDatas = function (datas) {
|
|
475
|
+
if (!this._datas) {
|
|
476
|
+
this._datas = {};
|
|
477
|
+
}
|
|
478
|
+
Object.assign(this._datas, datas);
|
|
479
|
+
for (let key in this._datas) {
|
|
480
|
+
this[key] = this._datas[key];
|
|
481
|
+
}
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* 设置数据
|
|
486
|
+
* @param {string} key 数据键
|
|
487
|
+
* @param {*} value 数据值
|
|
488
|
+
*/
|
|
489
|
+
Mod.prototype.setData = function (key, value) {
|
|
490
|
+
this._datas[key] = value;
|
|
491
|
+
this[key] = value;
|
|
492
|
+
};
|
|
493
|
+
|
|
444
494
|
/**
|
|
445
495
|
* 调用函数(核心执行方法)
|
|
446
496
|
* @param {string} method 函数名
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_machine",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.2",
|
|
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": {
|