mm_machine 2.5.2 → 2.5.4

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 +42 -5
  2. package/index.js +34 -0
  3. package/mod.js +1 -1
  4. package/package.json +4 -4
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
  */
@@ -186,6 +194,18 @@ Drive.prototype._getScriptTpl = function () {
186
194
  async _init() {
187
195
  this.log('debug', \`初始化!\`);
188
196
  },
197
+ /**
198
+ * 启动
199
+ */
200
+ async _start() {
201
+ this.log('debug', \`启动!\`);
202
+ },
203
+ /**
204
+ * 停止
205
+ */
206
+ async _stop() {
207
+ this.log('debug', \`停止!\`);
208
+ },
189
209
  /**
190
210
  * 销毁
191
211
  */
@@ -459,13 +479,13 @@ Drive.prototype._getConfigTpl = function () {
459
479
  // 模块名称,必填
460
480
  "name": "\${name}",
461
481
  // 模块标题,可选,默认"示例标题"
462
- "title": "\${title || "示例标题"}",
482
+ "title": "\${title || '示例标题'}",
463
483
  // 模块描述,可选,默认"示例描述"
464
- "description": "\${description || "示例描述"}",
484
+ "description": "\${description || '示例描述'}",
465
485
  // 主脚本文件路径,可选,默认"./index.js"
466
- "main": "\${main || "./index.js"}",
486
+ "main": "\${main || './index.js'}",
467
487
  // 模块作用域,可选,默认"server"
468
- "scope": "\${scope || "server"}",
488
+ "scope": "\${scope || 'server'}",
469
489
  // 模块状态,可选,默认1
470
490
  "state": \${state || 1},
471
491
  // 模块排序,可选,默认100
@@ -692,7 +712,24 @@ Drive.prototype.save = function () {
692
712
  * @param {...any} args 事件参数
693
713
  */
694
714
  Drive.prototype.emitEvent = function (event, ...args) {
695
- this._eventer?.emit(event, ...args);
715
+ this.getEventer()?.emit(event, ...args);
716
+ };
717
+
718
+ /**
719
+ * 注册事件监听
720
+ * @param {string} event 事件名
721
+ * @param {function} callback 事件回调函数
722
+ */
723
+ Drive.prototype.onEvent = function (event, callback) {
724
+ this.getEventer()?.on(event, callback);
725
+ };
726
+
727
+ /**
728
+ * 移除事件监听
729
+ * @param {string} event 事件名
730
+ */
731
+ Drive.prototype.offEvent = function (event) {
732
+ this.getEventer()?.off(event);
696
733
  };
697
734
 
698
735
  /**
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[]} 模块信息数组
@@ -852,6 +860,32 @@ Manager.prototype.runWaterfall = async function (method, result, ...params) {
852
860
  return result;
853
861
  };
854
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
+
855
889
  module.exports = {
856
890
  Manager,
857
891
  Drive,
package/mod.js CHANGED
@@ -105,7 +105,7 @@ Mod.prototype.isLoaded = function () {
105
105
  * @param {...any} params 日志参数
106
106
  */
107
107
  Mod.prototype.log = function (level, message, ...params) {
108
- this.getLogger()[level](`[${this.constructor.name}] [${this.config.name}] ${message}`, ...params);
108
+ this.getLogger()[level](`[${this.constructor.name}.${this.config.name}] ${message}`, ...params);
109
109
  };
110
110
 
111
111
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "2.5.2",
3
+ "version": "2.5.4",
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": {
@@ -34,9 +34,9 @@
34
34
  ],
35
35
  "dependencies": {
36
36
  "@prettier/sync": "^0.6.1",
37
- "mm_config": "^2.3.0",
38
- "mm_hot_reload": "^1.3.0",
39
- "mm_tpl": "^2.4.9"
37
+ "mm_config": "^2.3.1",
38
+ "mm_hot_reload": "^1.3.1",
39
+ "mm_tpl": "^2.5.0"
40
40
  },
41
41
  "devDependencies": {
42
42
  "eslint": "^10.2.0",