ee-core 2.1.1-beta.5 → 2.2.0-beta.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/addon/index.js CHANGED
@@ -1,30 +1,31 @@
1
- const EEAddon = Symbol('Ee#Addon');
1
+ const EE = require('../ee');
2
2
 
3
- /**
4
- * todo 插件模块
5
- */
6
3
  const Addon = {
7
4
 
8
5
  /**
9
- * 设置插件对象
6
+ * 获取 all addon instances
10
7
  */
11
- setAddonObject(name, obj) {
12
- if (!this[EEAddon]) {
13
- this[EEAddon] = new Map();
14
- }
15
-
16
- if (!this[EEAddon].has(name)) {
17
- this[EEAddon].set(name, obj);
18
- }
8
+ all() {
9
+ const { CoreApp } = EE;
10
+ const instances = CoreApp.addon || null;
11
+ if (!instances) {
12
+ throw new Error('Addons not exists or do not call directly at the top!');
13
+ };
14
+ return instances;
19
15
  },
20
16
 
21
17
  /**
22
- * 获取插件对象
18
+ * 获取 addon instance
23
19
  */
24
20
  get(name) {
25
- let addon = this[EEAddon].has(name) ? this[EEAddon].get(name): null;
26
- return addon;
27
- },
28
- }
21
+ const instances = this.all();
22
+ const instance = instances[name] || null;
23
+ if (!instance) {
24
+ throw new Error(`Addon class '${name}' not exists or do not call directly at the top!`);
25
+ };
26
+ return instance;
27
+ },
28
+
29
+ };
29
30
 
30
31
  module.exports = Addon;
@@ -18,6 +18,12 @@ class WinAddon {
18
18
  * @since 1.0.0
19
19
  */
20
20
  create(name, opt) {
21
+
22
+ // todo 判断name是否唯一
23
+ // if (this.windowContentsIdMap.hasOwnProperty(name)) {
24
+ // throw new Error(`[addon] [window] Name: ${name} already exists!`);
25
+ // }
26
+
21
27
  const options = Object.assign({
22
28
  x: 10,
23
29
  y: 10,
@@ -74,7 +80,7 @@ class WinAddon {
74
80
  * @function
75
81
  * @since 1.0.0
76
82
  */
77
- registerWCid (name, id) {
83
+ registerWCid(name, id) {
78
84
  this.windowContentsIdMap[name] = id;
79
85
  }
80
86
 
@@ -84,7 +90,7 @@ class WinAddon {
84
90
  * @function
85
91
  * @since 1.0.0
86
92
  */
87
- removeWCid (name) {
93
+ removeWCid(name) {
88
94
  delete this.windowContentsIdMap[name];
89
95
  }
90
96
  }
package/ee/application.js CHANGED
@@ -5,6 +5,7 @@ const debug = require('debug')('ee-core:Appliaction');
5
5
  const EeApp = require('./eeApp');
6
6
  const Utils = require('../utils');
7
7
  const Ps = require('../ps');
8
+ const EE = require('./index');
8
9
 
9
10
  class Appliaction extends EeApp {
10
11
  constructor() {
@@ -71,6 +72,10 @@ class Appliaction extends EeApp {
71
72
  debug('options:%j', options)
72
73
 
73
74
  super(options);
75
+
76
+ // 设置全局this
77
+ EE.CoreApp = this;
78
+
74
79
  this.initialize();
75
80
  }
76
81
 
package/ee/baseApp.js CHANGED
@@ -22,9 +22,6 @@ class BaseApp extends EeAppCore {
22
22
  this.loader.load();
23
23
 
24
24
  this.HttpClient = HttpClient;
25
-
26
- // TODO 这个不行,要么每次new对象,要么所有地方都用同一个实例,否则会出现数据无法刷新的情况
27
- //this.coreDB = this.getCoreDB();
28
25
  }
29
26
 
30
27
  get [EE_PATH]() {
package/ee/eeApp.js CHANGED
@@ -1,14 +1,15 @@
1
1
  const path = require('path');
2
2
  const fs = require('fs');
3
3
  const assert = require('assert');
4
- const { app } = require('electron');
5
4
  const is = require('is-type-of');
6
5
  const Koa = require('koa');
7
6
  const koaServe = require('koa-static');
8
7
  const https = require('https');
9
8
  const BaseApp = require('./baseApp');
10
9
  const Log = require('../log');
11
- const Electron = require('../electron');
10
+ const CoreElectron = require('../electron');
11
+ const CoreElectronApp = require('../electron/app');
12
+ const CoreElectronWindow = require('../electron/window');
12
13
  const Conf = require('../config');
13
14
  const Ps = require('../ps');
14
15
  const Socket = require('../socket');
@@ -18,14 +19,15 @@ class EeApp extends BaseApp {
18
19
  constructor(options = {}) {
19
20
  super(options);
20
21
 
21
- this.electron = Electron;
22
+ // 兼容旧的api
23
+ this.electron = CoreElectron;
22
24
  this.mainWindow;
23
25
  }
24
26
 
25
27
  /**
26
28
  * 生成端口
27
29
  */
28
- async createPorts () {
30
+ async createPorts() {
29
31
  const mainPort = await GetPort({port: this.config.mainServer.port});
30
32
  process.env.EE_MAIN_PORT = mainPort;
31
33
  this.config.mainServer.port = mainPort;
@@ -49,46 +51,17 @@ class EeApp extends BaseApp {
49
51
  /**
50
52
  * 启动通信模块
51
53
  */
52
- async startSocket () {
54
+ async startSocket() {
53
55
  Socket.startAll(this);
54
56
  }
55
57
 
56
58
  /**
57
59
  * 创建electron应用
58
60
  */
59
- async createElectronApp () {
60
- const self = this;
61
-
62
- const gotTheLock = app.requestSingleInstanceLock();
63
- if (!gotTheLock) {
64
- await this.appQuit();
65
- return;
66
- }
67
-
68
- app.on('second-instance', (event) => {
69
- self.restoreMainWindow();
70
- })
71
-
72
- app.whenReady().then(() => {
73
- self.createWindow();
74
- app.on('activate', () => {
75
- self.restoreMainWindow();
76
- })
77
- })
78
-
79
- app.on('window-all-closed', () => {
80
- if (process.platform !== 'darwin') {
81
- Log.coreLogger.info('[ee-core] [lib/eeApp] window-all-closed quit');
82
- self.appQuit();
83
- }
84
- })
85
-
86
- app.on('before-quit', () => {
87
- Electron.extra.closeWindow = true;
88
- })
89
-
90
- if (this.config.hardGpu.enable == false) {
91
- app.disableHardwareAcceleration();
61
+ async createElectronApp() {
62
+ const newApp = CoreElectronApp.create();
63
+ if (!newApp) {
64
+ return
92
65
  }
93
66
 
94
67
  await this.electronAppReady();
@@ -97,10 +70,10 @@ class EeApp extends BaseApp {
97
70
  /**
98
71
  * 创建应用主窗口
99
72
  */
100
- async createWindow () {
73
+ async createWindow() {
101
74
 
102
75
  // 初始化一个主窗口
103
- this.mainWindow = Electron.getMainWindow();
76
+ this.mainWindow = CoreElectronWindow.getMainWindow();
104
77
 
105
78
  await this.windowReady();
106
79
 
@@ -111,23 +84,10 @@ class EeApp extends BaseApp {
111
84
  this.selectAppType();
112
85
  }
113
86
 
114
- /**
115
- * 还原窗口
116
- */
117
- restoreMainWindow () {
118
- if (this.mainWindow) {
119
- if (this.mainWindow.isMinimized()) {
120
- this.mainWindow.restore();
121
- }
122
- this.mainWindow.show();
123
- this.mainWindow.focus();
124
- }
125
- }
126
-
127
87
  /**
128
88
  * 应用类型 (远程、html、单页应用)
129
89
  */
130
- selectAppType () {
90
+ selectAppType() {
131
91
  let type = '';
132
92
  let url = '';
133
93
 
@@ -167,7 +127,7 @@ class EeApp extends BaseApp {
167
127
  /**
168
128
  * 加载本地前端资源
169
129
  */
170
- loadLocalWeb (mode, staticDir, hostInfo) {
130
+ loadLocalWeb(mode, staticDir, hostInfo) {
171
131
  const self = this;
172
132
  if (!staticDir) {
173
133
  staticDir = path.join(this.config.homeDir, 'public', 'dist')
@@ -210,7 +170,7 @@ class EeApp extends BaseApp {
210
170
  /**
211
171
  * 主页面
212
172
  */
213
- loadMainUrl (type, url) {
173
+ loadMainUrl(type, url) {
214
174
  const mainServer = this.config.mainServer;
215
175
  Log.coreLogger.info('[ee-core] [main] Env: %s, Type: %s', this.config.env, type);
216
176
  Log.coreLogger.info('[ee-core] [main] App running at: %s', url);
@@ -220,15 +180,15 @@ class EeApp extends BaseApp {
220
180
  /**
221
181
  * electron app退出
222
182
  */
223
- async appQuit () {
183
+ async appQuit() {
224
184
  await this.beforeClose();
225
- app.quit();
185
+ CoreElectronApp.quit();
226
186
  }
227
187
 
228
188
  /**
229
189
  * 加载插件
230
190
  */
231
- async _loderAddons () {
191
+ async _loderAddons() {
232
192
  this.loader.loadAddons();
233
193
 
234
194
  // 注册主窗口Contents id
@@ -243,7 +203,7 @@ class EeApp extends BaseApp {
243
203
  /**
244
204
  * 预加载模块
245
205
  */
246
- async _loderPreload () {
206
+ async _loderPreload() {
247
207
  let filepath = this.loader.resolveModule(path.join(this.config.baseDir, 'preload', 'index'));
248
208
  if (!filepath) return;
249
209
  const fileObj = this.loader.loadFile(filepath);
@@ -254,46 +214,24 @@ class EeApp extends BaseApp {
254
214
  }
255
215
  }
256
216
 
257
- /**
258
- * 序列化参数
259
- */
260
- stringify(obj, ignore) {
261
- const result = {};
262
- Object.keys(obj).forEach(key => {
263
- if (!ignore.includes(key)) {
264
- result[key] = obj[key];
265
- }
266
- });
267
- return JSON.stringify(result);
268
- }
269
-
270
- /**
271
- * 捕获异常(废弃)
272
- */
273
- async catchLog () {
274
- process.on('uncaughtException', function(err) {
275
- Log.coreLogger.error(err);
276
- });
277
- }
278
-
279
217
  /**
280
218
  * electron app已经准备好,主窗口还未创建
281
219
  */
282
- async electronAppReady () {
220
+ async electronAppReady() {
283
221
  // do some things
284
222
  }
285
223
 
286
224
  /**
287
225
  * 主应用窗口已经创建
288
226
  */
289
- async windowReady () {
227
+ async windowReady() {
290
228
  // do some things
291
229
  }
292
230
 
293
231
  /**
294
232
  * app关闭之前
295
233
  */
296
- async beforeClose () {
234
+ async beforeClose() {
297
235
  // do some things
298
236
  }
299
237
  }
package/ee/index.js CHANGED
@@ -1,6 +1,10 @@
1
1
  const Utils = require('../utils');
2
2
  const EEApplication = Symbol('Ee#Application');
3
+ const BuiltInApp = Symbol('Ee#BuiltInApp');
3
4
 
5
+ /**
6
+ * EE
7
+ */
4
8
  const EE = {
5
9
 
6
10
  /**
@@ -12,7 +16,7 @@ const EE = {
12
16
  },
13
17
 
14
18
  /**
15
- * 设置app对象
19
+ * 设置实例化app对象
16
20
  */
17
21
  set app(appObject) {
18
22
  if (!this[EEApplication]) {
@@ -21,12 +25,28 @@ const EE = {
21
25
  },
22
26
 
23
27
  /**
24
- * 获取app对象
28
+ * 获取实例化app对象
25
29
  */
26
30
  get app() {
27
31
  return this[EEApplication] || null;
28
32
  },
29
33
 
34
+ /**
35
+ * 设置全局this到CoreApp (eeApp)
36
+ */
37
+ set CoreApp(obj) {
38
+ if (!this[BuiltInApp]) {
39
+ this[BuiltInApp] = obj;
40
+ }
41
+ },
42
+
43
+ /**
44
+ * 获取 CoreApp (eeApp)
45
+ */
46
+ get CoreApp() {
47
+ return this[BuiltInApp] || null;
48
+ },
49
+
30
50
  /**
31
51
  * 是否加密
32
52
  */
@@ -0,0 +1,62 @@
1
+ const { app } = require('electron');
2
+ const Window = require('../window');
3
+ const EE = require('../../ee');
4
+ const Log = require('../../log');
5
+ const Electron = require('../index');
6
+ const UtilsIs = require('../../utils/is');
7
+
8
+ /**
9
+ * CoreElectronApp (框架封装的electron app对象)
10
+ */
11
+ const CoreElectronApp = {
12
+
13
+ /**
14
+ * 创建electron应用
15
+ */
16
+ async create() {
17
+ const { CoreApp } = EE;
18
+
19
+ const gotTheLock = app.requestSingleInstanceLock();
20
+ if (!gotTheLock) {
21
+ app.quit();
22
+ return;
23
+ }
24
+
25
+ app.on('second-instance', (event) => {
26
+ Window.restoreMainWindow();
27
+ })
28
+
29
+ app.whenReady().then(() => {
30
+ CoreApp.createWindow();
31
+ app.on('activate', () => {
32
+ Window.restoreMainWindow();
33
+ })
34
+ })
35
+
36
+ app.on('window-all-closed', () => {
37
+ if (!UtilsIs.macOS) {
38
+ Log.coreLogger.info('[ee-core] [lib/eeApp] window-all-closed quit');
39
+ CoreApp.appQuit();
40
+ }
41
+ })
42
+
43
+ app.on('before-quit', () => {
44
+ Electron.extra.closeWindow = true;
45
+ })
46
+
47
+ if (CoreApp.config.hardGpu.enable == false) {
48
+ app.disableHardwareAcceleration();
49
+ }
50
+
51
+ return app;
52
+ },
53
+
54
+ /**
55
+ * 退出app
56
+ */
57
+ quit() {
58
+ app.quit();
59
+ }
60
+ }
61
+
62
+ module.exports = CoreElectronApp;
package/electron/index.js CHANGED
@@ -1,24 +1,12 @@
1
1
  const Window = require('./window');
2
- const EEMainWindow = Symbol('Ee#electron#mainWindow');
3
2
 
4
3
  const Electron = {
5
4
 
6
- /**
7
- * 获取 mainWindow
8
- */
9
- getMainWindow() {
10
- if (!this[EEMainWindow]) {
11
- this[EEMainWindow] = Window.createWindow();
12
- }
13
-
14
- return this[EEMainWindow] || null;
15
- },
16
-
17
5
  /**
18
6
  * 兼容1.x版本api
19
7
  */
20
8
  get mainWindow() {
21
- return this.getMainWindow();
9
+ return Window.getMainWindow();
22
10
  },
23
11
 
24
12
  /**
@@ -26,7 +14,7 @@ const Electron = {
26
14
  */
27
15
  extra: {
28
16
  closeWindow: false,
29
- },
17
+ },
30
18
  };
31
19
 
32
20
  module.exports = Electron;
@@ -1,13 +1,25 @@
1
- const {app, BrowserWindow, Menu} = require('electron');
2
- const Conf = require('../config');
3
- const Ps = require('../ps');
1
+ const { app, BrowserWindow, Menu } = require('electron');
2
+ const Conf = require('../../config');
3
+ const Ps = require('../../ps');
4
+ const EEMainWindow = Symbol('Ee#electron#mainWindow');
4
5
 
5
6
  const Window = {
6
7
 
8
+ /**
9
+ * 获取 mainWindow
10
+ */
11
+ getMainWindow() {
12
+ if (!this[EEMainWindow]) {
13
+ this[EEMainWindow] = this.createMainWindow();
14
+ }
15
+
16
+ return this[EEMainWindow] || null;
17
+ },
18
+
7
19
  /**
8
20
  * 创建应用主窗口
9
21
  */
10
- createWindow () {
22
+ createMainWindow() {
11
23
 
12
24
  // todo
13
25
  // const protocolName = 'eefile';
@@ -18,7 +30,8 @@ const Window = {
18
30
  // });
19
31
 
20
32
  const config = Conf.all();
21
- let win = new BrowserWindow(config.windowsOption);
33
+ const win = new BrowserWindow(config.windowsOption);
34
+ this[EEMainWindow] = win;
22
35
 
23
36
  // 菜单显示/隐藏
24
37
  if (config.openAppMenu === 'dev-show' && Ps.isProd()) {
@@ -37,9 +50,20 @@ const Window = {
37
50
  }
38
51
 
39
52
  return win;
40
- }
41
-
53
+ },
42
54
 
55
+ /**
56
+ * 还原窗口
57
+ */
58
+ restoreMainWindow() {
59
+ if (this[EEMainWindow]) {
60
+ if (this[EEMainWindow].isMinimized()) {
61
+ this[EEMainWindow].restore();
62
+ }
63
+ this[EEMainWindow].show();
64
+ this[EEMainWindow].focus();
65
+ }
66
+ }
43
67
  };
44
68
 
45
69
  module.exports = Window;
@@ -0,0 +1,186 @@
1
+ 'use strict';
2
+
3
+ const path = require('path');
4
+ const electron = require('electron');
5
+ const jsonfile = require('jsonfile');
6
+ const mkdirp = require('mkdirp');
7
+
8
+ module.exports = function (options) {
9
+ const app = electron.app || electron.remote.app;
10
+ const screen = electron.screen || electron.remote.screen;
11
+ let state;
12
+ let winRef;
13
+ let stateChangeTimer;
14
+ const eventHandlingDelay = 100;
15
+ const config = Object.assign({
16
+ file: 'window-state.json',
17
+ path: app.getPath('userData'),
18
+ maximize: true,
19
+ fullScreen: true
20
+ }, options);
21
+ const fullStoreFileName = path.join(config.path, config.file);
22
+
23
+ function isNormal(win) {
24
+ return !win.isMaximized() && !win.isMinimized() && !win.isFullScreen();
25
+ }
26
+
27
+ function hasBounds() {
28
+ return state &&
29
+ Number.isInteger(state.x) &&
30
+ Number.isInteger(state.y) &&
31
+ Number.isInteger(state.width) && state.width > 0 &&
32
+ Number.isInteger(state.height) && state.height > 0;
33
+ }
34
+
35
+ function resetStateToDefault() {
36
+ const displayBounds = screen.getPrimaryDisplay().bounds;
37
+
38
+ // Reset state to default values on the primary display
39
+ state = {
40
+ width: config.defaultWidth || 800,
41
+ height: config.defaultHeight || 600,
42
+ x: 0,
43
+ y: 0,
44
+ displayBounds
45
+ };
46
+ }
47
+
48
+ function windowWithinBounds(bounds) {
49
+ return (
50
+ state.x >= bounds.x &&
51
+ state.y >= bounds.y &&
52
+ state.x + state.width <= bounds.x + bounds.width &&
53
+ state.y + state.height <= bounds.y + bounds.height
54
+ );
55
+ }
56
+
57
+ function ensureWindowVisibleOnSomeDisplay() {
58
+ const visible = screen.getAllDisplays().some(display => {
59
+ return windowWithinBounds(display.bounds);
60
+ });
61
+
62
+ if (!visible) {
63
+ // Window is partially or fully not visible now.
64
+ // Reset it to safe defaults.
65
+ return resetStateToDefault();
66
+ }
67
+ }
68
+
69
+ function validateState() {
70
+ const isValid = state && (hasBounds() || state.isMaximized || state.isFullScreen);
71
+ if (!isValid) {
72
+ state = null;
73
+ return;
74
+ }
75
+
76
+ if (hasBounds() && state.displayBounds) {
77
+ ensureWindowVisibleOnSomeDisplay();
78
+ }
79
+ }
80
+
81
+ function updateState(win) {
82
+ win = win || winRef;
83
+ if (!win) {
84
+ return;
85
+ }
86
+ // Don't throw an error when window was closed
87
+ try {
88
+ const winBounds = win.getBounds();
89
+ if (isNormal(win)) {
90
+ state.x = winBounds.x;
91
+ state.y = winBounds.y;
92
+ state.width = winBounds.width;
93
+ state.height = winBounds.height;
94
+ }
95
+ state.isMaximized = win.isMaximized();
96
+ state.isFullScreen = win.isFullScreen();
97
+ state.displayBounds = screen.getDisplayMatching(winBounds).bounds;
98
+ } catch (err) {}
99
+ }
100
+
101
+ function saveState(win) {
102
+ // Update window state only if it was provided
103
+ if (win) {
104
+ updateState(win);
105
+ }
106
+
107
+ // Save state
108
+ try {
109
+ mkdirp.sync(path.dirname(fullStoreFileName));
110
+ jsonfile.writeFileSync(fullStoreFileName, state);
111
+ } catch (err) {
112
+ // Don't care
113
+ }
114
+ }
115
+
116
+ function stateChangeHandler() {
117
+ // Handles both 'resize' and 'move'
118
+ clearTimeout(stateChangeTimer);
119
+ stateChangeTimer = setTimeout(updateState, eventHandlingDelay);
120
+ }
121
+
122
+ function closeHandler() {
123
+ updateState();
124
+ }
125
+
126
+ function closedHandler() {
127
+ // Unregister listeners and save state
128
+ unmanage();
129
+ saveState();
130
+ }
131
+
132
+ function manage(win) {
133
+ if (config.maximize && state.isMaximized) {
134
+ win.maximize();
135
+ }
136
+ if (config.fullScreen && state.isFullScreen) {
137
+ win.setFullScreen(true);
138
+ }
139
+ win.on('resize', stateChangeHandler);
140
+ win.on('move', stateChangeHandler);
141
+ win.on('close', closeHandler);
142
+ win.on('closed', closedHandler);
143
+ winRef = win;
144
+ }
145
+
146
+ function unmanage() {
147
+ if (winRef) {
148
+ winRef.removeListener('resize', stateChangeHandler);
149
+ winRef.removeListener('move', stateChangeHandler);
150
+ clearTimeout(stateChangeTimer);
151
+ winRef.removeListener('close', closeHandler);
152
+ winRef.removeListener('closed', closedHandler);
153
+ winRef = null;
154
+ }
155
+ }
156
+
157
+ // Load previous state
158
+ try {
159
+ state = jsonfile.readFileSync(fullStoreFileName);
160
+ } catch (err) {
161
+ // Don't care
162
+ }
163
+
164
+ // Check state validity
165
+ validateState();
166
+
167
+ // Set state fallback values
168
+ state = Object.assign({
169
+ width: config.defaultWidth || 800,
170
+ height: config.defaultHeight || 600
171
+ }, state);
172
+
173
+ return {
174
+ get x() { return state.x; },
175
+ get y() { return state.y; },
176
+ get width() { return state.width; },
177
+ get height() { return state.height; },
178
+ get displayBounds() { return state.displayBounds; },
179
+ get isMaximized() { return state.isMaximized; },
180
+ get isFullScreen() { return state.isFullScreen; },
181
+ saveState,
182
+ unmanage,
183
+ manage,
184
+ resetStateToDefault
185
+ };
186
+ };
package/index.js CHANGED
@@ -12,13 +12,13 @@ const EE = require('./ee');
12
12
  * @member {Controller} EeCore#Controller
13
13
  * @since 1.0.0
14
14
  */
15
- const Controller = require('./controller');
15
+ const Controller = require('./controller/baseContextClass');
16
16
 
17
17
  /**
18
18
  * @member {Service} EeCore#Service
19
19
  * @since 1.0.0
20
20
  */
21
- const Service = require('./service');
21
+ const Service = require('./services/baseContextClass');
22
22
 
23
23
  /**
24
24
  * @member {Storage}
package/package.json CHANGED
@@ -1,11 +1,9 @@
1
1
  {
2
2
  "name": "ee-core",
3
- "version": "2.1.1-beta.5",
3
+ "version": "2.2.0-beta.1",
4
4
  "description": "ee core",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "publish-beta": "npm publish --tag beta",
8
- "publish": "npm publish",
9
7
  "test": "echo \"Error: no test specified\" && exit 1"
10
8
  },
11
9
  "author": "",
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * BaseContextClass is a base class that can be extended,
5
+ * it's instantiated in context level,
6
+ * {@link Helper}, {@link Service} is extending it.
7
+ */
8
+ class BaseContextClass {
9
+
10
+ /**
11
+ * @class
12
+ * @param {Context} ctx - context instance
13
+ * @since 1.0.0
14
+ */
15
+ constructor(ctx) {
16
+ /**
17
+ * @member {Application} BaseContextClass#app
18
+ * @since 1.0.0
19
+ */
20
+ this.app = ctx;
21
+ /**
22
+ * @member {Config} BaseContextClass#config
23
+ * @since 1.0.0
24
+ */
25
+ this.config = ctx.config;
26
+ /**
27
+ * @member {Service} BaseContextClass#service
28
+ * @since 1.0.0
29
+ */
30
+ this.service = ctx.service;
31
+ }
32
+ }
33
+
34
+ module.exports = BaseContextClass;
@@ -0,0 +1,31 @@
1
+ const EE = require('../ee');
2
+
3
+ const Services = {
4
+
5
+ /**
6
+ * 获取 all addon instances
7
+ */
8
+ all() {
9
+ const { CoreApp } = EE;
10
+ const instances = CoreApp.service || null;
11
+ if (!instances) {
12
+ throw new Error('Services not exists or do not call directly at the top!');
13
+ };
14
+ return instances;
15
+ },
16
+
17
+ /**
18
+ * 获取 addon instance
19
+ */
20
+ get(name) {
21
+ const instances = this.all();
22
+ const instance = instances[name] || null;
23
+ if (!instance) {
24
+ throw new Error(`Service class '${name}' not exists or do not call directly at the top!`);
25
+ };
26
+ return instance;
27
+ },
28
+
29
+ };
30
+
31
+ module.exports = Services;