mm_config 2.3.3 → 2.3.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 +14 -44
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -21,6 +21,7 @@ class Config extends Base {
|
|
|
21
21
|
format: 'json',
|
|
22
22
|
file: ''
|
|
23
23
|
};
|
|
24
|
+
|
|
24
25
|
/**
|
|
25
26
|
* 预设选项
|
|
26
27
|
* @type {object}
|
|
@@ -41,8 +42,6 @@ class Config extends Base {
|
|
|
41
42
|
constructor(config, options = {}) {
|
|
42
43
|
super(config);
|
|
43
44
|
this.options = { ...Config.options, ...options };
|
|
44
|
-
this._conn = null;
|
|
45
|
-
this._timer = null;
|
|
46
45
|
this._promise = Promise.resolve();
|
|
47
46
|
}
|
|
48
47
|
}
|
|
@@ -63,16 +62,6 @@ Config.prototype.getOptions = function () {
|
|
|
63
62
|
return this.options;
|
|
64
63
|
};
|
|
65
64
|
|
|
66
|
-
/**
|
|
67
|
-
* 初始化配置(连接文件)
|
|
68
|
-
* @param {object} conn - 已连接的文件对象
|
|
69
|
-
* @returns {object} 连接的文件对象
|
|
70
|
-
*/
|
|
71
|
-
Config.prototype.setup = function (conn) {
|
|
72
|
-
if (conn) this._conn = conn;
|
|
73
|
-
return this._conn;
|
|
74
|
-
};
|
|
75
|
-
|
|
76
65
|
/**
|
|
77
66
|
* 保存配置到文件(同步)
|
|
78
67
|
* @returns {boolean} 保存是否成功
|
|
@@ -202,14 +191,7 @@ Config.prototype.getAsyncProxy = function () {
|
|
|
202
191
|
* @returns {*} 配置值
|
|
203
192
|
*/
|
|
204
193
|
Config.prototype.get = async function (key) {
|
|
205
|
-
|
|
206
|
-
if (this._conn) {
|
|
207
|
-
value = await this._conn.get(key);
|
|
208
|
-
}
|
|
209
|
-
if (!value) {
|
|
210
|
-
value = this.options[key];
|
|
211
|
-
}
|
|
212
|
-
return value;
|
|
194
|
+
return this.options[key];
|
|
213
195
|
};
|
|
214
196
|
|
|
215
197
|
/**
|
|
@@ -217,13 +199,8 @@ Config.prototype.get = async function (key) {
|
|
|
217
199
|
* @param {string} key - 配置键
|
|
218
200
|
* @param {*} value - 配置值
|
|
219
201
|
*/
|
|
220
|
-
Config.prototype.set =
|
|
221
|
-
|
|
222
|
-
await this._conn.set(key, value);
|
|
223
|
-
}
|
|
224
|
-
else {
|
|
225
|
-
this.options[key] = value;
|
|
226
|
-
}
|
|
202
|
+
Config.prototype.set = function (key, value) {
|
|
203
|
+
this.options[key] = value;
|
|
227
204
|
};
|
|
228
205
|
|
|
229
206
|
/**
|
|
@@ -232,14 +209,7 @@ Config.prototype.set = async function (key, value) {
|
|
|
232
209
|
* @returns {boolean} 配置项是否存在
|
|
233
210
|
*/
|
|
234
211
|
Config.prototype.has = async function (key) {
|
|
235
|
-
|
|
236
|
-
if (this._conn) {
|
|
237
|
-
bl = await this._conn.has(key);
|
|
238
|
-
};
|
|
239
|
-
if (!bl) {
|
|
240
|
-
bl = this.options.hasOwnProperty(key);
|
|
241
|
-
}
|
|
242
|
-
return bl;
|
|
212
|
+
return this.options.hasOwnProperty(key);
|
|
243
213
|
};
|
|
244
214
|
|
|
245
215
|
/**
|
|
@@ -247,9 +217,6 @@ Config.prototype.has = async function (key) {
|
|
|
247
217
|
* @param {string} key - 配置键
|
|
248
218
|
*/
|
|
249
219
|
Config.prototype.del = async function (key) {
|
|
250
|
-
if (this._conn) {
|
|
251
|
-
await this._conn.del(key);
|
|
252
|
-
};
|
|
253
220
|
delete this.options[key];
|
|
254
221
|
};
|
|
255
222
|
|
|
@@ -259,9 +226,6 @@ Config.prototype.del = async function (key) {
|
|
|
259
226
|
*/
|
|
260
227
|
Config.prototype.keys = async function () {
|
|
261
228
|
let keys = [];
|
|
262
|
-
if (this._conn) {
|
|
263
|
-
keys = await this._conn.keys();
|
|
264
|
-
}
|
|
265
229
|
let local_keys = Object.keys(this.options);
|
|
266
230
|
keys = keys.concat(local_keys);
|
|
267
231
|
return keys;
|
|
@@ -271,12 +235,18 @@ Config.prototype.keys = async function () {
|
|
|
271
235
|
* 销毁
|
|
272
236
|
*/
|
|
273
237
|
Config.prototype.destroy = function () {
|
|
274
|
-
if (this._conn) {
|
|
275
|
-
this._conn.close();
|
|
276
|
-
}
|
|
277
238
|
delete dict[this.config.file];
|
|
278
239
|
};
|
|
279
240
|
|
|
241
|
+
/**
|
|
242
|
+
* 从配置文件加载配置
|
|
243
|
+
* @returns {boolean} 加载是否成功
|
|
244
|
+
*/
|
|
245
|
+
Config.prototype.load = function () {
|
|
246
|
+
if (!this.config.file) return false;
|
|
247
|
+
this.options = this.config.file.loadJson() || {};
|
|
248
|
+
};
|
|
249
|
+
|
|
280
250
|
/**
|
|
281
251
|
* 创建配置(同步)
|
|
282
252
|
* @param {string} file - 配置文件路径
|