mm_machine 3.4.6 → 3.4.8

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 (2) hide show
  1. package/lib/drive.js +80 -17
  2. package/package.json +1 -1
package/lib/drive.js CHANGED
@@ -1,3 +1,4 @@
1
+ const path = require('path');
1
2
  require('mm_tpl');
2
3
  const { conf } = require('mm_config');
3
4
  const { Mod } = require('./mod');
@@ -61,6 +62,24 @@ class Drive extends Mod {
61
62
  }
62
63
  }
63
64
 
65
+ /**
66
+ * 监听初始化事件
67
+ */
68
+ Drive.prototype._onInit = function () {
69
+ // 初始化
70
+ this.on('init:before', async (ctx) => {
71
+ this.setState('initing');
72
+ this._initOptions();
73
+ await this._initCore(...ctx.params);
74
+ });
75
+ this.on('init:success', (ctx) => {
76
+ this.isInited(true);
77
+ });
78
+ this.on('init:after', (ctx) => {
79
+ this.setState('inited');
80
+ });
81
+ };
82
+
64
83
  /**
65
84
  * 监听加载事件
66
85
  * @param {object} ctx 上下文对象
@@ -151,7 +170,6 @@ Drive.prototype.setConfig = function (config) {
151
170
  this.isLoaded(false);
152
171
  }
153
172
  this._preset();
154
- this._initOptions();
155
173
  return this.config;
156
174
  };
157
175
 
@@ -163,6 +181,46 @@ Drive.prototype.getConfig = function () {
163
181
  return this.config;
164
182
  };
165
183
 
184
+ /**
185
+ * 构建选项
186
+ * @param {object} options 选项对象
187
+ */
188
+ Drive.prototype._buildOptionsFromArr = function (options, cached) {
189
+ var values = {};
190
+ for (let i = 0; i < options.length; i++) {
191
+ let option = options[i];
192
+ let key = option.name;
193
+ let value = cached[key];
194
+ if (cached.hasOwnProperty(key) && value !== undefined) {
195
+ values[key] = value;
196
+ } else {
197
+ values[key] = option.default;
198
+ }
199
+ }
200
+ return values;
201
+ };
202
+
203
+ /**
204
+ * 构建选项
205
+ * @param {object} options 选项对象
206
+ * @param {object} cached 缓存对象
207
+ * @returns {object} 选项对象
208
+ */
209
+ Drive.prototype._buildOptionsFromDict = function (options, cached) {
210
+ var values = {};
211
+ for (let key in options) {
212
+ let option = options[key];
213
+ let value = cached[key];
214
+ if (cached.hasOwnProperty(key) && value !== undefined) {
215
+ values[key] = value;
216
+ } else {
217
+ values[key] = option.default;
218
+ }
219
+ }
220
+ return values;
221
+ };
222
+
223
+
166
224
  /**
167
225
  * 初始化选项
168
226
  * 从缓存文件加载键值对,如果没有缓存则根据config.options生成默认键值对
@@ -178,18 +236,11 @@ Drive.prototype._initOptions = function () {
178
236
  if (cache_file && cache_file.hasFile()) {
179
237
  cached = cache_file.loadJson() || {};
180
238
  }
181
-
182
- this._options = {};
183
- // 根据config.options生成键值对,缓存值优先,否则取默认值
184
- for (let i = 0; i < options.length; i++) {
185
- let option = options[i];
186
- let key = option.key;
187
- let value = cached[key];
188
- if (cached.hasOwnProperty(key) && value !== undefined) {
189
- this._options[key] = value;
190
- } else {
191
- this._options[key] = option.default;
192
- }
239
+ if (Array.isArray(options)) {
240
+ this._options = this._buildOptionsFromArr(options, cached);
241
+ }
242
+ else {
243
+ this._options = this._buildOptionsFromDict(options, cached);
193
244
  }
194
245
  };
195
246
 
@@ -210,14 +261,26 @@ Drive.prototype._getOptionsFileName = function () {
210
261
  return './' + current_name.toLowerCase() + '.json';
211
262
  };
212
263
 
264
+ /**
265
+ * 获取选项缓存目录
266
+ * @returns {string} 选项缓存目录
267
+ */
268
+ Drive.prototype._getOptionsDirName = function () {
269
+ let parent = this.getParent();
270
+ if (!parent) return '';
271
+ var current_name = parent.constructor.name + '_' + (parent.config.name || 'default');
272
+ return current_name.toLowerCase();
273
+ };
274
+
213
275
  /**
214
276
  * 获取选项缓存文件路径
215
277
  * @returns {string} 选项缓存文件路径 为空字符串表示未配置缓存目录
216
278
  */
217
279
  Drive.prototype._getOptionsFile = function () {
218
- let dir = this._getCacheDir();
219
- if (!dir) return '';
220
- return this._getOptionsFileName().fullname(dir);
280
+ let cache_dir = this._getCacheDir();
281
+ let options_dir = this._getOptionsDirName();
282
+ let file_name = this._getOptionsFileName();
283
+ return path.join(cache_dir, options_dir, file_name);
221
284
  };
222
285
 
223
286
  /**
@@ -807,7 +870,7 @@ Drive.prototype.loadBefore = function () {
807
870
  /**
808
871
  * 加载处理
809
872
  */
810
- Drive.prototype._loadCore = function () {};
873
+ Drive.prototype._loadCore = function () { };
811
874
 
812
875
  /**
813
876
  * 保存配置
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_machine",
3
- "version": "3.4.6",
3
+ "version": "3.4.8",
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": {