mm_logic 1.0.0 → 1.0.1
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/README.md +1 -1
- package/index.js +3 -21
- package/lib/admin.js +22 -0
- package/lib/logic.js +12 -17
- package/lib/manager.js +4 -4
- package/package.json +1 -1
package/README.md
CHANGED
package/index.js
CHANGED
|
@@ -1,29 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const Manager = require('./lib/manager');
|
|
4
3
|
const Logic = require('./lib/logic');
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 逻辑管理器实例池
|
|
9
|
-
* @type {object}
|
|
10
|
-
*/
|
|
11
|
-
const admin = reg('mm_logic');
|
|
12
|
-
admin.setClass(Manager);
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* 获取指定作用域的逻辑管理器
|
|
16
|
-
* @param {string} [scope='os'] 作用域名称,同一作用域共享同一实例
|
|
17
|
-
* @param {object} [config] 管理器配置,仅在首次创建实例时生效
|
|
18
|
-
* @returns {Manager} 逻辑管理器实例
|
|
19
|
-
*/
|
|
20
|
-
function logicAdmin(scope, config) {
|
|
21
|
-
return admin.get(scope || 'os', config);
|
|
22
|
-
}
|
|
4
|
+
const Manager = require('./lib/manager');
|
|
5
|
+
const admin_module = require('./lib/admin');
|
|
23
6
|
|
|
24
7
|
module.exports = {
|
|
25
8
|
Logic,
|
|
26
9
|
Manager,
|
|
27
|
-
|
|
28
|
-
logicAdmin
|
|
10
|
+
...admin_module
|
|
29
11
|
};
|
package/lib/admin.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { reg } = require('mm_admin');
|
|
4
|
+
const Manager = require('./manager');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 逻辑管理器实例池(Admin 工厂)
|
|
8
|
+
*/
|
|
9
|
+
let admin = reg('mm_logic');
|
|
10
|
+
admin.setClass(Manager);
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 获取指定作用域的逻辑管理器
|
|
14
|
+
* @param {string} [scope='os'] 作用域名称,同一作用域共享同一实例
|
|
15
|
+
* @param {object} [config] 管理器配置,仅在首次创建实例时生效
|
|
16
|
+
* @returns {Manager} 逻辑管理器实例
|
|
17
|
+
*/
|
|
18
|
+
function logicAdmin(scope, config) {
|
|
19
|
+
return admin.get(scope || 'os', config);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = { admin, logicAdmin };
|
package/lib/logic.js
CHANGED
|
@@ -74,7 +74,7 @@ Logic.prototype.getDir = function () {
|
|
|
74
74
|
* @private
|
|
75
75
|
*/
|
|
76
76
|
Logic.prototype._getScriptFile = function () {
|
|
77
|
-
|
|
77
|
+
let main = this.config.main;
|
|
78
78
|
if (!main) {
|
|
79
79
|
return null;
|
|
80
80
|
}
|
|
@@ -94,25 +94,20 @@ Logic.prototype.isLoaded = function () {
|
|
|
94
94
|
* @returns {object|null} 加载的模块对象,文件不存在或加载失败返回 null
|
|
95
95
|
*/
|
|
96
96
|
Logic.prototype.load = function () {
|
|
97
|
-
|
|
97
|
+
let file = this._getScriptFile();
|
|
98
98
|
if (!file || !hasFile(file)) {
|
|
99
99
|
return null;
|
|
100
100
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
this._handlers.set(key, mod[key]);
|
|
107
|
-
}
|
|
101
|
+
let mod = require(file);
|
|
102
|
+
if (mod) {
|
|
103
|
+
for (let key in mod) {
|
|
104
|
+
if (typeof mod[key] === 'function') {
|
|
105
|
+
this._handlers.set(key, mod[key]);
|
|
108
106
|
}
|
|
109
107
|
}
|
|
110
|
-
this._loaded = true;
|
|
111
|
-
return mod;
|
|
112
|
-
} catch (err) {
|
|
113
|
-
this.log('error', '加载逻辑脚本失败', file, err);
|
|
114
|
-
return null;
|
|
115
108
|
}
|
|
109
|
+
this._loaded = true;
|
|
110
|
+
return mod;
|
|
116
111
|
};
|
|
117
112
|
|
|
118
113
|
/**
|
|
@@ -166,8 +161,8 @@ Logic.prototype.list = function () {
|
|
|
166
161
|
* @returns {*} 处理结果,方法不存在时返回 null
|
|
167
162
|
*/
|
|
168
163
|
Logic.prototype.run = async function (action, params, options) {
|
|
169
|
-
|
|
170
|
-
|
|
164
|
+
let action_name = action;
|
|
165
|
+
let params_obj = params;
|
|
171
166
|
if (typeof action !== 'string') {
|
|
172
167
|
params_obj = action;
|
|
173
168
|
options = params;
|
|
@@ -176,7 +171,7 @@ Logic.prototype.run = async function (action, params, options) {
|
|
|
176
171
|
if (!this._loaded && this.config.main) {
|
|
177
172
|
this.load();
|
|
178
173
|
}
|
|
179
|
-
|
|
174
|
+
let handler = this._handlers.get(action_name);
|
|
180
175
|
if (typeof handler !== 'function') {
|
|
181
176
|
return null;
|
|
182
177
|
}
|
package/lib/manager.js
CHANGED
|
@@ -80,7 +80,7 @@ LogicManager.prototype._stop = async function () {
|
|
|
80
80
|
* @returns {object} 逻辑规则实例
|
|
81
81
|
*/
|
|
82
82
|
LogicManager.prototype.add = function (config) {
|
|
83
|
-
|
|
83
|
+
let drive = new this.Drive({ scope: this.config.name, ...config }, this);
|
|
84
84
|
this.set(drive.config.name, drive);
|
|
85
85
|
return drive;
|
|
86
86
|
};
|
|
@@ -91,7 +91,7 @@ LogicManager.prototype.add = function (config) {
|
|
|
91
91
|
* @returns {Promise<boolean>} 是否删除成功
|
|
92
92
|
*/
|
|
93
93
|
LogicManager.prototype.del = async function (name) {
|
|
94
|
-
|
|
94
|
+
let drive = this.get(name);
|
|
95
95
|
if (drive) {
|
|
96
96
|
await drive.stop();
|
|
97
97
|
}
|
|
@@ -104,7 +104,7 @@ LogicManager.prototype.del = async function (name) {
|
|
|
104
104
|
* @returns {Promise<boolean>} 是否执行成功
|
|
105
105
|
*/
|
|
106
106
|
LogicManager.prototype.reload = async function (name) {
|
|
107
|
-
|
|
107
|
+
let drive = this.get(name);
|
|
108
108
|
if (!drive) {
|
|
109
109
|
this.log('warn', '逻辑规则不存在,无法重启', name);
|
|
110
110
|
return false;
|
|
@@ -139,7 +139,7 @@ LogicManager.prototype._initDrive = async function (drive) {
|
|
|
139
139
|
* @returns {*} 逻辑处理结果,规则不存在或未启用时返回 null
|
|
140
140
|
*/
|
|
141
141
|
LogicManager.prototype.run = async function (name, action, params, options) {
|
|
142
|
-
|
|
142
|
+
let drive = this.get(name);
|
|
143
143
|
if (!drive || drive.config.state !== 1) {
|
|
144
144
|
return null;
|
|
145
145
|
}
|