mm_machine 2.5.1 → 2.5.3

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.
Files changed (4) hide show
  1. package/drive.js +26 -1
  2. package/index.js +43 -1
  3. package/mod.js +45 -0
  4. package/package.json +1 -1
package/drive.js CHANGED
@@ -63,6 +63,14 @@ class Drive extends Mod {
63
63
  }
64
64
  }
65
65
 
66
+ /**
67
+ * 获取事件触发器
68
+ * @returns {object} 事件触发器
69
+ */
70
+ Drive.prototype.getEventer = function () {
71
+ return $.eventer;
72
+ };
73
+
66
74
  /**
67
75
  * 配置驱动
68
76
  */
@@ -692,7 +700,24 @@ Drive.prototype.save = function () {
692
700
  * @param {...any} args 事件参数
693
701
  */
694
702
  Drive.prototype.emitEvent = function (event, ...args) {
695
- this._eventer?.emit(event, ...args);
703
+ this.getEventer()?.emit(event, ...args);
704
+ };
705
+
706
+ /**
707
+ * 注册事件监听
708
+ * @param {string} event 事件名
709
+ * @param {function} callback 事件回调函数
710
+ */
711
+ Drive.prototype.onEvent = function (event, callback) {
712
+ this.getEventer()?.on(event, callback);
713
+ };
714
+
715
+ /**
716
+ * 移除事件监听
717
+ * @param {string} event 事件名
718
+ */
719
+ Drive.prototype.offEvent = function (event) {
720
+ this.getEventer()?.off(event);
696
721
  };
697
722
 
698
723
  /**
package/index.js CHANGED
@@ -105,6 +105,14 @@ class Manager extends Mod {
105
105
  }
106
106
  }
107
107
 
108
+ /**
109
+ * 获取事件触发器
110
+ * @returns {object} 事件触发器
111
+ */
112
+ Manager.prototype.getEventer = function () {
113
+ return $.eventer;
114
+ };
115
+
108
116
  /**
109
117
  * 获取所有模块信息
110
118
  * @returns {object[]} 模块信息数组
@@ -286,7 +294,15 @@ Manager.prototype.newMod = function (config_file, config, script) {
286
294
  mod.mode = this.config.mode;
287
295
  mod.loadConfig(config_file);
288
296
  if (script) {
289
- mod.setMethods(script);
297
+ for (let key in script) {
298
+ let obj = script[key];
299
+ if (typeof obj == 'function') {
300
+ mod.setMethod(key, obj);
301
+ }
302
+ else {
303
+ mod.setData(key, obj);
304
+ }
305
+ }
290
306
  }
291
307
  if (config) {
292
308
  mod.setConfig(config);
@@ -844,6 +860,32 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
844
860
  return result;
845
861
  };
846
862
 
863
+ /**
864
+ * 触发事件
865
+ * @param {string} event 事件名
866
+ * @param {...any} args 事件参数
867
+ */
868
+ Manager.prototype.emitEvent = function (event, ...args) {
869
+ this.getEventer()?.emit(event, ...args);
870
+ };
871
+
872
+ /**
873
+ * 注册事件监听
874
+ * @param {string} event 事件名
875
+ * @param {function} callback 事件回调函数
876
+ */
877
+ Manager.prototype.onEvent = function (event, callback) {
878
+ this.getEventer()?.on(event, callback);
879
+ };
880
+
881
+ /**
882
+ * 移除事件监听
883
+ * @param {string} event 事件名
884
+ */
885
+ Manager.prototype.offEvent = function (event) {
886
+ this.getEventer()?.off(event);
887
+ };
888
+
847
889
  module.exports = {
848
890
  Manager,
849
891
  Drive,
package/mod.js CHANGED
@@ -331,8 +331,17 @@ Mod.prototype.unload = async function () {
331
331
  */
332
332
  Mod.prototype._unloadCore = async function () {
333
333
  if (this._methods) {
334
+ for (let key in this._methods) {
335
+ delete this[key];
336
+ }
334
337
  this._methods = {};
335
338
  }
339
+ if (this._datas) {
340
+ for (let key in this._datas) {
341
+ delete this[key];
342
+ }
343
+ this._datas = {};
344
+ }
336
345
  };
337
346
 
338
347
  /**
@@ -418,6 +427,9 @@ Mod.prototype.setMethods = function (methods) {
418
427
  this._methods = {};
419
428
  }
420
429
  Object.assign(this._methods, methods);
430
+ for (let key in this._methods) {
431
+ this[key] = this._methods[key];
432
+ }
421
433
  };
422
434
 
423
435
  /**
@@ -431,6 +443,7 @@ Mod.prototype.setMethod = function (method, func) {
431
443
  this._methods = {};
432
444
  }
433
445
  this._methods[method] = func;
446
+ this[method] = this._methods[method];
434
447
  };
435
448
 
436
449
  /**
@@ -446,6 +459,38 @@ Mod.prototype.getMethod = function (method) {
446
459
  return func;
447
460
  };
448
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
+
449
494
  /**
450
495
  * 调用函数(核心执行方法)
451
496
  * @param {string} method 函数名
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "2.5.1",
3
+ "version": "2.5.3",
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": {