ee-core 2.12.0 → 4.0.0

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 (159) hide show
  1. package/app/application.d.ts +5 -0
  2. package/app/application.js +31 -0
  3. package/app/boot.d.ts +5 -0
  4. package/app/boot.js +74 -0
  5. package/app/dir.d.ts +1 -0
  6. package/app/dir.js +27 -0
  7. package/app/events.d.ts +14 -0
  8. package/app/events.js +56 -0
  9. package/app/index.d.ts +2 -0
  10. package/app/index.js +7 -0
  11. package/config/config_loader.d.ts +13 -0
  12. package/config/config_loader.js +19 -23
  13. package/config/default_config.d.ts +95 -0
  14. package/config/default_config.js +111 -0
  15. package/config/index.d.ts +28 -0
  16. package/config/index.js +20 -37
  17. package/const/channel.d.ts +16 -0
  18. package/const/channel.js +26 -16
  19. package/controller/controller_loader.d.ts +9 -0
  20. package/controller/controller_loader.js +78 -0
  21. package/controller/index.d.ts +2 -0
  22. package/controller/index.js +18 -19
  23. package/core/index.d.ts +2 -0
  24. package/core/index.js +6 -5
  25. package/core/loader/file_loader.d.ts +66 -0
  26. package/core/{lib/loader → loader}/file_loader.js +39 -156
  27. package/core/utils/index.d.ts +12 -0
  28. package/core/utils/index.js +83 -0
  29. package/core/utils/timing.d.ts +22 -0
  30. package/core/{lib/utils → utils}/timing.js +3 -2
  31. package/cross/cross.d.ts +19 -0
  32. package/cross/cross.js +152 -0
  33. package/cross/crossProcess.d.ts +29 -0
  34. package/cross/{spawnProcess.js → crossProcess.js} +36 -53
  35. package/cross/index.d.ts +2 -0
  36. package/cross/index.js +6 -181
  37. package/electron/app/index.d.ts +5 -0
  38. package/electron/app/index.js +38 -54
  39. package/electron/index.d.ts +3 -0
  40. package/electron/index.js +13 -17
  41. package/electron/window/index.d.ts +6 -0
  42. package/electron/window/index.js +260 -65
  43. package/exception/index.d.ts +9 -0
  44. package/exception/index.js +42 -54
  45. package/html/index.d.ts +1 -0
  46. package/html/index.js +7 -10
  47. package/index.d.ts +2 -0
  48. package/index.js +4 -52
  49. package/jobs/child/app.d.ts +1 -0
  50. package/jobs/child/app.js +10 -10
  51. package/jobs/child/index.d.ts +13 -0
  52. package/jobs/child/index.js +14 -11
  53. package/jobs/child/jobProcess.d.ts +16 -0
  54. package/jobs/child/{forkProcess.js → jobProcess.js} +31 -62
  55. package/jobs/child-pool/index.d.ts +26 -0
  56. package/jobs/child-pool/index.js +25 -48
  57. package/jobs/index.d.ts +3 -0
  58. package/jobs/index.js +2 -4
  59. package/jobs/load-balancer/algorithm/index.d.ts +4 -0
  60. package/jobs/load-balancer/algorithm/minimumConnection.d.ts +2 -0
  61. package/jobs/load-balancer/algorithm/polling.d.ts +2 -0
  62. package/jobs/load-balancer/algorithm/random.d.ts +2 -0
  63. package/jobs/load-balancer/algorithm/specify.d.ts +2 -0
  64. package/jobs/load-balancer/algorithm/weights.d.ts +2 -0
  65. package/jobs/load-balancer/algorithm/weightsMinimumConnection.d.ts +2 -0
  66. package/jobs/load-balancer/algorithm/weightsPolling.d.ts +2 -0
  67. package/jobs/load-balancer/algorithm/weightsRandom.d.ts +2 -0
  68. package/jobs/load-balancer/consts.d.ts +8 -0
  69. package/jobs/load-balancer/index.d.ts +96 -0
  70. package/jobs/load-balancer/scheduler.d.ts +16 -0
  71. package/loader/index.d.ts +9 -0
  72. package/loader/index.js +73 -143
  73. package/log/index.d.ts +5 -0
  74. package/log/index.js +74 -57
  75. package/log/logger.d.ts +2 -0
  76. package/log/logger.js +75 -80
  77. package/message/childMessage.d.ts +10 -0
  78. package/message/childMessage.js +15 -25
  79. package/message/index.d.ts +3 -0
  80. package/message/index.js +7 -16
  81. package/package.json +8 -2
  82. package/ps/index.d.ts +37 -0
  83. package/ps/index.js +135 -238
  84. package/socket/httpServer.d.ts +8 -0
  85. package/socket/httpServer.js +55 -45
  86. package/socket/index.d.ts +10 -0
  87. package/socket/index.js +52 -69
  88. package/socket/ipcServer.d.ts +9 -0
  89. package/socket/ipcServer.js +87 -94
  90. package/socket/socketServer.d.ts +10 -0
  91. package/socket/socketServer.js +37 -25
  92. package/storage/index.d.ts +5 -0
  93. package/storage/index.js +5 -37
  94. package/storage/sqliteStorage.d.ts +16 -0
  95. package/storage/sqliteStorage.js +20 -20
  96. package/tsconfig.json +14 -0
  97. package/utils/extend.d.ts +2 -0
  98. package/utils/extend.js +10 -5
  99. package/utils/helper.d.ts +14 -0
  100. package/utils/helper.js +39 -53
  101. package/utils/index.d.ts +10 -0
  102. package/utils/index.js +47 -40
  103. package/utils/ip.d.ts +2 -0
  104. package/utils/ip.js +5 -45
  105. package/utils/is.d.ts +14 -0
  106. package/utils/is.js +107 -141
  107. package/utils/json.d.ts +5 -0
  108. package/utils/json.js +15 -7
  109. package/utils/pargv.d.ts +3 -0
  110. package/utils/pargv.js +5 -1
  111. package/utils/port/index.d.ts +1 -0
  112. package/utils/{get-port → port}/index.js +4 -26
  113. package/utils/wrap.d.ts +4 -0
  114. package/utils/wrap.js +8 -3
  115. package/addon/index.js +0 -35
  116. package/addon/window/index.js +0 -99
  117. package/bin/tools.js +0 -8
  118. package/config/cache.js +0 -62
  119. package/config/config.default.js +0 -331
  120. package/const/index.js +0 -9
  121. package/controller/baseContextClass.js +0 -25
  122. package/core/lib/ee.js +0 -216
  123. package/core/lib/loader/context_loader.js +0 -106
  124. package/core/lib/loader/ee_loader.js +0 -435
  125. package/core/lib/loader/mixin/addon.js +0 -32
  126. package/core/lib/loader/mixin/config.js +0 -130
  127. package/core/lib/loader/mixin/controller.js +0 -125
  128. package/core/lib/loader/mixin/service.js +0 -28
  129. package/core/lib/utils/base_context_class.js +0 -34
  130. package/core/lib/utils/function.js +0 -30
  131. package/core/lib/utils/index.js +0 -133
  132. package/core/lib/utils/sequencify.js +0 -59
  133. package/ee/appLoader.js +0 -48
  134. package/ee/application.js +0 -101
  135. package/ee/baseApp.js +0 -99
  136. package/ee/eeApp.js +0 -406
  137. package/ee/index.js +0 -58
  138. package/electron/window/winState.js +0 -186
  139. package/httpclient/index.js +0 -161
  140. package/jobs/baseJobClass.js +0 -16
  141. package/jobs/renderer/index.js +0 -141
  142. package/jobs/renderer/loadView.js +0 -41
  143. package/jobs/unification.js +0 -64
  144. package/main/index.js +0 -57
  145. package/old-utils/index.js +0 -91
  146. package/services/baseContextClass.js +0 -24
  147. package/services/index.js +0 -41
  148. package/socket/io.js +0 -28
  149. package/storage/jsondb/adapters/Base.js +0 -23
  150. package/storage/jsondb/adapters/FileSync.js +0 -64
  151. package/storage/jsondb/main.js +0 -55
  152. package/storage/jsondbStorage.js +0 -196
  153. package/utils/co.js +0 -237
  154. package/utils/copyto.js +0 -161
  155. package/utils/depd/index.js +0 -538
  156. package/utils/depd/lib/browser/index.js +0 -77
  157. package/utils/get-port/index.d.ts +0 -64
  158. package/utils/time/index.js +0 -20
  159. package/utils/time/ms.js +0 -162
@@ -4,7 +4,6 @@ const assert = require('assert');
4
4
  const MAP = Symbol('Timing#map');
5
5
  const LIST = Symbol('Timing#list');
6
6
 
7
-
8
7
  class Timing {
9
8
 
10
9
  constructor() {
@@ -74,4 +73,6 @@ class Timing {
74
73
  }
75
74
  }
76
75
 
77
- module.exports = Timing;
76
+ module.exports = {
77
+ Timing
78
+ };
@@ -0,0 +1,19 @@
1
+ import { EventEmitter } from 'node:events';
2
+ import { CrossProcess } from "./crossProcess";
3
+ import internal = require("stream");
4
+ export declare class Cross {
5
+ emitter: EventEmitter<[never]>;
6
+ children: {};
7
+ childrenMap: {};
8
+ create(): Promise<void>;
9
+ run(service: string, opt?: {}): Promise<CrossProcess>;
10
+ killAll(): void;
11
+ kill(pid: string|number): void;
12
+ killByName(name: string): void;
13
+ getUrl(name: string): string;
14
+ getProcByName(name: string): CrossProcess;
15
+ getProc(pid: string|number): CrossProcess;
16
+ getPids(): string[];
17
+ _initEventEmitter(): void;
18
+ }
19
+ export declare let cross: Cross;
package/cross/cross.js ADDED
@@ -0,0 +1,152 @@
1
+ 'use strict';
2
+
3
+ const EventEmitter = require('events');
4
+ const { getConfig } = require('../config');
5
+ const { sleep, getValueFromArgv, replaceArgsValue } = require('../utils/helper');
6
+ const { CrossProcess } = require('./crossProcess');
7
+ const { Events } = require('../const/channel');
8
+ const { extend } = require('../utils/extend');
9
+ const { getPort } = require('../utils/port');
10
+
11
+ class Cross {
12
+ constructor() {
13
+ this.emitter = undefined;
14
+
15
+ // pid唯一
16
+ // {pid:{name,entity}, pid:{name,entity}, ...}
17
+ this.children = {};
18
+
19
+ // name唯一
20
+ // {name:pid, name:pid, ...}
21
+ this.childrenMap = {};
22
+
23
+ // eventEmitter
24
+ this._initEventEmitter();
25
+ }
26
+
27
+ async create() {
28
+
29
+ // boot services
30
+ const crossCfg = getConfig().cross;
31
+ //await sleep(5 * 1000);
32
+
33
+ for (let key of Object.keys(crossCfg)) {
34
+ let val = crossCfg[key];
35
+ if (val.enable == true) {
36
+ this.run(key)
37
+ }
38
+ }
39
+ }
40
+
41
+ // run
42
+ async run(service, opt = {}) {
43
+ const crossConf = getConfig().cross;
44
+ const defaultOpt = crossConf[service] || {};
45
+ const targetConf = extend(true, {}, defaultOpt, opt);
46
+ if (Object.keys(targetConf).length == 0) {
47
+ throw new Error(`[ee-core] [cross] The service [${service}] config does not exit`);
48
+ }
49
+
50
+ // format params
51
+ let tmpArgs = targetConf.args;
52
+ let confPort = parseInt(getValueFromArgv(tmpArgs, 'port'));
53
+ // 某些程序给它传入不存在的参数时会报错
54
+ if (isNaN(confPort) && targetConf.port > 0) {
55
+ confPort = targetConf.port;
56
+ }
57
+ if (confPort > 0) {
58
+ // 动态生成port,传入的端口必须为int
59
+ confPort = await getPort({ port: confPort });
60
+ // 替换port
61
+ targetConf.args = replaceArgsValue(tmpArgs, "port", String(confPort));
62
+ }
63
+
64
+ // 创建进程
65
+ const subProcess = new CrossProcess(this, { targetConf, port: confPort });
66
+ let uniqueName = targetConf.name;
67
+ if (this.childrenMap.hasOwnProperty(uniqueName)) {
68
+ uniqueName = uniqueName + "-" + String(subProcess.pid);
69
+ }
70
+ this.childrenMap[uniqueName] = subProcess.pid;
71
+ subProcess.name = uniqueName;
72
+ this.children[subProcess.pid] = {
73
+ name: uniqueName,
74
+ entity: subProcess
75
+ };
76
+
77
+ return subProcess;
78
+ }
79
+
80
+ killAll() {
81
+ Object.keys(this.children).forEach(pid => {
82
+ this.kill(pid)
83
+ });
84
+ }
85
+
86
+ kill(pid) {
87
+ const entity = this.getProc(pid);
88
+ if (entity) {
89
+ entity.kill();
90
+ }
91
+ }
92
+
93
+ killByName(name) {
94
+ const entity = this.getProcByName(name);
95
+ if (entity) {
96
+ entity.kill();
97
+ }
98
+ }
99
+
100
+ getUrl(name) {
101
+ const entity = this.getProcByName(name);
102
+ const url = entity.getUrl();
103
+
104
+ return url;
105
+ }
106
+
107
+ // 获取 proc
108
+ getProcByName(name) {
109
+ const pid = this.childrenMap[name];
110
+ if (!pid) {
111
+ throw new Error(`[ee-core] [cross] The process named [${name}] does not exit`);
112
+ }
113
+ const entity = this.getProc(pid);
114
+
115
+ return entity;
116
+ }
117
+
118
+ // 获取 proc
119
+ getProc(pid) {
120
+ const child = this.children[pid];
121
+ if (!pid) {
122
+ throw new Error(`[ee-core] [cross] The process pid [${pid}] does not exit`);
123
+ }
124
+
125
+ return child.entity;
126
+ }
127
+
128
+ // 获取pids
129
+ getPids() {
130
+ let pids = Object.keys(this.children);
131
+ return pids;
132
+ }
133
+
134
+ _initEventEmitter() {
135
+ this.emitter = new EventEmitter();
136
+ this.emitter.on(Events.childProcessExit, (data) => {
137
+ const child = this.children[data.pid];
138
+ delete this.childrenMap[child.name];
139
+ delete this.children[data.pid];
140
+ });
141
+ this.emitter.on(Events.childProcessError, (data) => {
142
+ const child = this.children[data.pid];
143
+ delete this.childrenMap[child.name];
144
+ delete this.children[data.pid];
145
+ });
146
+ }
147
+ }
148
+
149
+ module.exports = {
150
+ Cross,
151
+ cross: new Cross()
152
+ };
@@ -0,0 +1,29 @@
1
+ import EventEmitter = require("events");
2
+ export declare class CrossProcess {
3
+ constructor(host: any, opt?: {});
4
+ emitter: EventEmitter<[never]>;
5
+ host: any;
6
+ child: any;
7
+ pid: number;
8
+ port: number;
9
+ name: string;
10
+ config: {};
11
+ /**
12
+ * 初始化子进程
13
+ */
14
+ _init(options?: {}): void;
15
+ /**
16
+ * kill
17
+ */
18
+ kill(timeout?: number): void;
19
+ getUrl(): string;
20
+ getArgsObj(): {
21
+ _: any[];
22
+ };
23
+ setPort(port: string|number): void;
24
+ _generateId(): string;
25
+ /**
26
+ * exit electron
27
+ */
28
+ _exitElectron(timeout?: number): void;
29
+ }
@@ -1,15 +1,17 @@
1
+ 'use strict';
2
+
1
3
  const EventEmitter = require('events');
2
4
  const path = require('path');
3
5
  const crossSpawn = require('cross-spawn');
4
- const Log = require('../log');
5
- const Ps = require('../ps');
6
- const Channel = require('../const/channel');
7
- const EE = require('../ee');
8
- const Helper = require('../utils/helper');
9
- const UtilsIs = require('../utils/is');
10
- const UtilsPargv = require('../utils/pargv');
11
-
12
- class SpawnProcess {
6
+ const { coreLogger } = require('../log');
7
+ const { getExtraResourcesDir, isPackaged, isDev, getBaseDir } = require('../ps');
8
+ const { Events } = require('../const/channel');
9
+ const { getRandomString, getValueFromArgv } = require('../utils/helper');
10
+ const { is } = require('../utils');
11
+ const { parseArgv } = require('../utils/pargv');
12
+ const { app: electronApp } = require('electron');
13
+
14
+ class CrossProcess {
13
15
  constructor(host, opt = {}) {
14
16
  this.emitter = new EventEmitter();
15
17
  this.host = host;
@@ -35,9 +37,9 @@ class SpawnProcess {
35
37
  // Launch executable program
36
38
  let cmdPath = '';
37
39
  let cmdArgs = targetConf.args;
38
- let execDir = Ps.getExtraResourcesDir();
40
+ let execDir = getExtraResourcesDir();
39
41
  let standardOutput = ['inherit', 'inherit', 'inherit', 'ipc'];
40
- if (Ps.isPackaged()) {
42
+ if (isPackaged()) {
41
43
  standardOutput = ['ignore', 'ignore', 'ignore', 'ipc'];
42
44
  }
43
45
  if (targetConf.stdio) {
@@ -51,18 +53,18 @@ class SpawnProcess {
51
53
  throw new Error(`[ee-core] [cross] The config [directory] attribute does not exist`);
52
54
  }
53
55
  cmdPath = cmd;
54
- if (!path.isAbsolute(cmd) && !Ps.isDev()) {
55
- cmdPath = path.join(Ps.getExtraResourcesDir(), cmd);
56
+ if (!path.isAbsolute(cmd) && !isDev()) {
57
+ cmdPath = path.join(getExtraResourcesDir(), cmd);
56
58
  }
57
59
  } else {
58
- cmdPath = path.join(Ps.getExtraResourcesDir(), targetConf.name);
60
+ cmdPath = path.join(getExtraResourcesDir(), targetConf.name);
59
61
  }
60
62
 
61
63
  // windows
62
- if (UtilsIs.windows() && path.extname(cmdPath) != '.exe') {
64
+ if (is.windows() && path.extname(cmdPath) != '.exe') {
63
65
  // Complete the executable program extension
64
66
  // notice: python.exe may bring up the App Store
65
- if (targetConf.windowsExtname === true || !Ps.isDev()) {
67
+ if (targetConf.windowsExtname === true || !isDev()) {
66
68
  cmdPath += ".exe";
67
69
  }
68
70
  }
@@ -71,16 +73,16 @@ class SpawnProcess {
71
73
  if (directory && path.isAbsolute(directory)) {
72
74
  execDir = directory;
73
75
  } else if (directory && !path.isAbsolute(directory)) {
74
- if (Ps.isDev()) {
75
- execDir = path.join(Ps.getHomeDir(), directory);
76
+ if (isDev()) {
77
+ execDir = path.join(getBaseDir(), directory);
76
78
  } else {
77
- execDir = path.join(Ps.getExtraResourcesDir(), directory);
79
+ execDir = path.join(getExtraResourcesDir(), directory);
78
80
  }
79
81
  } else {
80
- execDir = Ps.getExtraResourcesDir();
82
+ execDir = getExtraResourcesDir();
81
83
  }
82
84
 
83
- Log.coreLogger.info(`[ee-core] [cross/run] cmd: ${cmdPath}, args: ${cmdArgs}`);
85
+ coreLogger.info(`[ee-core] [cross/run] cmd: ${cmdPath}, args: ${cmdArgs}`);
84
86
  const coreProcess = crossSpawn(cmdPath, cmdArgs, {
85
87
  stdio: standardOutput,
86
88
  detached: false,
@@ -94,9 +96,9 @@ class SpawnProcess {
94
96
  let data = {
95
97
  pid: this.pid
96
98
  }
97
- this.host.emitter.emit(Channel.events.childProcessExit, data);
99
+ this.host.emitter.emit(Events.childProcessExit, data);
98
100
  // Child process closed: The child process was killed externally or an internal error caused the application to stop, resulting in the application exiting
99
- Log.coreLogger.info(`[ee-core] [corss/process] received a exit from child-process, code:${code}, signal:${signal}, pid:${this.pid}, cmd:${cmdPath}, args: ${cmdArgs}`);
101
+ coreLogger.info(`[ee-core] [corss/process] received a exit from child-process, code:${code}, signal:${signal}, pid:${this.pid}, cmd:${cmdPath}, args: ${cmdArgs}`);
100
102
  this._exitElectron();
101
103
  });
102
104
 
@@ -104,8 +106,8 @@ class SpawnProcess {
104
106
  let data = {
105
107
  pid: this.pid
106
108
  }
107
- this.host.emitter.emit(Channel.events.childProcessError, data);
108
- Log.coreLogger.error(`[ee-core] [corss/process] received a error from child-process, error: ${err}, pid:${this.pid}`);
109
+ this.host.emitter.emit(Events.childProcessError, data);
110
+ coreLogger.error(`[ee-core] [corss/process] received a error from child-process, error: ${err}, pid:${this.pid}`);
109
111
  this._exitElectron();
110
112
  });
111
113
  }
@@ -121,29 +123,9 @@ class SpawnProcess {
121
123
  }, timeout)
122
124
  }
123
125
 
124
- // send(message) {
125
- // return this.sendByType(message, 'message');
126
- // }
127
-
128
- // close() {
129
- // return this.sendByType('close', 'close');
130
- // }
131
-
132
- // async sendByType(message, type) {
133
- // const msg = typeof message === 'string' ? message : JSON.stringify(message);
134
- // const id = this._generateId();
135
-
136
- // this.child.send({
137
- // id,
138
- // type,
139
- // data: msg,
140
- // });
141
- // return;
142
- // }
143
-
144
126
  getUrl() {
145
- const ssl = Helper.getValueFromArgv(this.config.args, 'ssl');
146
- let hostname = Helper.getValueFromArgv(this.config.args, 'hostname')
127
+ const ssl = getValueFromArgv(this.config.args, 'ssl');
128
+ let hostname = getValueFromArgv(this.config.args, 'hostname')
147
129
  let protocol = 'http://';
148
130
  if (ssl && (ssl == 'true' || ssl == '1')) {
149
131
  protocol = 'https://';
@@ -155,7 +137,7 @@ class SpawnProcess {
155
137
  }
156
138
 
157
139
  getArgsObj() {
158
- const obj = UtilsPargv(this.config.args);
140
+ const obj = parseArgv(this.config.args);
159
141
  return obj;
160
142
  }
161
143
 
@@ -164,7 +146,7 @@ class SpawnProcess {
164
146
  }
165
147
 
166
148
  _generateId() {
167
- const rid = Helper.getRandomString();
149
+ const rid = getRandomString();
168
150
  return `node:${this.pid}:${rid}`;
169
151
  }
170
152
 
@@ -172,14 +154,15 @@ class SpawnProcess {
172
154
  * exit electron
173
155
  */
174
156
  _exitElectron(timeout = 1000) {
175
- const { CoreApp } = EE;
176
157
  if (this.config.appExit) {
177
158
  setTimeout(() => {
178
- // 进程退出前的一些清理工作
179
- CoreApp.appQuit();
159
+ // 主进程退出
160
+ electronApp.quit();
180
161
  }, timeout)
181
162
  }
182
163
  }
183
164
  }
184
165
 
185
- module.exports = SpawnProcess;
166
+ module.exports = {
167
+ CrossProcess
168
+ };
@@ -0,0 +1,2 @@
1
+ import { Cross, cross } from "./cross";
2
+ export { Cross, cross };
package/cross/index.js CHANGED
@@ -1,183 +1,8 @@
1
- const fs = require('fs');
2
- const EventEmitter = require('events');
3
- const Conf = require('../config/cache');
4
- const Helper = require('../utils/helper');
5
- const Ps = require('../ps');
6
- const SpawnProcess = require('./spawnProcess');
7
- const Channel = require('../const/channel');
8
- const extend = require('../utils/extend');
9
- const GetPort = require('../utils/get-port');
1
+ 'use strict';
10
2
 
11
- /**
12
- * Cross-language service
13
- * 跨语言服务
14
- */
15
- const CrossLanguageService = {
3
+ const { Cross, cross} = require('./cross');
16
4
 
17
- emitter: undefined,
18
-
19
- /**
20
- * pid唯一
21
- * {pid:{name,entity}, pid:{name,entity}, ...}
22
- */
23
- children: {},
24
-
25
- /**
26
- * name唯一
27
- * {name:pid, name:pid, ...}
28
- */
29
- childrenMap: {},
30
-
31
- /**
32
- * create
33
- */
34
- async create() {
35
-
36
- // boot services
37
- const servicesCfg = Conf.getValue('cross');
38
- //await Helper.sleep(5 * 1000);
39
-
40
- for (let key of Object.keys(servicesCfg)) {
41
- let cfg = servicesCfg[key];
42
- if (cfg.enable == true) {
43
- this.run(key)
44
- }
45
- }
46
- },
47
-
48
- /**
49
- * _initEventEmitter
50
- */
51
- _initEventEmitter() {
52
- if (this.emitter) {
53
- return
54
- }
55
- this.emitter = new EventEmitter();
56
- this.emitter.on(Channel.events.childProcessExit, (data) => {
57
- const child = this.children[data.pid];
58
- delete this.childrenMap[child.name];
59
- delete this.children[data.pid];
60
- });
61
- this.emitter.on(Channel.events.childProcessError, (data) => {
62
- const child = this.children[data.pid];
63
- delete this.childrenMap[child.name];
64
- delete this.children[data.pid];
65
- });
66
- },
67
-
68
- /**
69
- * run
70
- */
71
- async run(service, opt = {}) {
72
- // init dir
73
- this._initPath();
74
-
75
- const allConfig = Conf.all();
76
- const defaultOpt = allConfig.cross[service] || {};
77
- const targetConf = extend(true, {}, defaultOpt, opt);
78
- if (Object.keys(targetConf).length == 0) {
79
- throw new Error(`[ee-core] [cross] The service [${service}] config does not exit`);
80
- }
81
-
82
- // eventEmitter
83
- this._initEventEmitter();
84
-
85
- // format params
86
- let tmpArgs = targetConf.args;
87
- let confPort = parseInt(Helper.getValueFromArgv(tmpArgs, 'port'));
88
- // 某些程序给它传入不存在的参数时会报错
89
- if (isNaN(confPort) && targetConf.port > 0) {
90
- confPort = targetConf.port;
91
- }
92
- if (confPort > 0) {
93
- // 动态生成port,传入的端口必须为int
94
- confPort = await GetPort({ port: confPort });
95
- // 替换port
96
- targetConf.args = Helper.replaceArgsValue(tmpArgs, "port", String(confPort));
97
- }
98
-
99
- // 创建进程
100
- const subProcess = new SpawnProcess(this, { targetConf, port: confPort });
101
- let uniqueName = targetConf.name;
102
- if (this.childrenMap.hasOwnProperty(uniqueName)) {
103
- uniqueName = uniqueName + "-" + String(subProcess.pid);
104
- }
105
- this.childrenMap[uniqueName] = subProcess.pid;
106
- subProcess.name = uniqueName;
107
- this.children[subProcess.pid] = {
108
- name: uniqueName,
109
- entity: subProcess
110
- };
111
-
112
- return subProcess;
113
- },
114
-
115
- killAll() {
116
- Object.keys(this.children).forEach(pid => {
117
- this.kill(pid)
118
- });
119
- },
120
-
121
- kill(pid) {
122
- const entity = this.getProc(pid);
123
- if (entity) {
124
- entity.kill();
125
- }
126
- },
127
-
128
- killByName(name) {
129
- const entity = this.getProcByName(name);
130
- if (entity) {
131
- entity.kill();
132
- }
133
- },
134
-
135
- getUrl(name) {
136
- const entity = this.getProcByName(name);
137
- const url = entity.getUrl();
138
-
139
- return url;
140
- },
141
-
142
- // 获取 proc
143
- getProcByName(name) {
144
- const pid = this.childrenMap[name];
145
- if (!pid) {
146
- throw new Error(`[ee-core] [cross] The process named [${name}] does not exit`);
147
- }
148
- const entity = this.getProc(pid);
149
-
150
- return entity;
151
- },
152
-
153
- // 获取 proc
154
- getProc(pid) {
155
- const child = this.children[pid];
156
- if (!pid) {
157
- throw new Error(`[ee-core] [cross] The process pid [${pid}] does not exit`);
158
- }
159
-
160
- return child.entity;
161
- },
162
-
163
- /**
164
- * 获取pids
165
- */
166
- getPids() {
167
- let pids = Object.keys(this.children);
168
- return pids;
169
- },
170
-
171
- /**
172
- * init path
173
- */
174
- _initPath() {
175
- const pathname = Ps.getUserHomeConfigDir();
176
- if (!fs.existsSync(pathname)) {
177
- Helper.mkdir(pathname, {mode: 0o755});
178
- }
179
- },
180
-
181
- }
182
-
183
- module.exports = CrossLanguageService;
5
+ module.exports = {
6
+ Cross,
7
+ cross
8
+ };
@@ -0,0 +1,5 @@
1
+ /**
2
+ * 创建electron应用
3
+ */
4
+ export declare function createElectron(): void;
5
+ export { electronApp };
@@ -1,64 +1,48 @@
1
- const { app } = require('electron');
2
- const EE = require('../../ee');
3
- const Log = require('../../log');
4
- const Electron = require('../index');
5
- const UtilsIs = require('../../utils/is');
6
- const Cross = require('../../cross');
7
- const Window = require('../window');
1
+ 'use strict';
2
+
3
+ const debug = require('debug')('ee-core:electron:app');
4
+ const { app: electronApp } = require('electron');
5
+ const { coreLogger } = require('../../log');
6
+ const { is } = require('../../utils');
7
+ const { cross } = require('../../cross');
8
+ const { createMainWindow, setCloseAndQuit, loadServer } = require('../window');
9
+ const { eventBus, ElectronAppReady, BeforeClose, Preload } = require('../../app/events');
10
+ const { getConfig } = require('../../config');
8
11
 
9
12
  /**
10
- * CoreElectronApp (框架封装的electron app对象)
13
+ * 创建electron应用
11
14
  */
12
- const CoreElectronApp = {
13
-
14
- /**
15
- * 创建electron应用
16
- */
17
- async create() {
18
- const { CoreApp } = EE;
19
-
20
- const gotTheLock = app.requestSingleInstanceLock();
21
- if (!gotTheLock) {
22
- app.quit();
23
- }
24
-
25
- app.whenReady().then(() => {
26
- CoreApp.createWindow();
27
- })
28
-
29
- // 显示首次打开的窗口
30
- app.on('second-instance', () => {
31
- Log.coreLogger.info('[ee-core] [lib/eeApp] second-instance');
32
- Window.restoreMainWindow();
33
- });
34
-
35
- app.on('window-all-closed', () => {
36
- if (!UtilsIs.macOS()) {
37
- Log.coreLogger.info('[ee-core] [lib/eeApp] window-all-closed quit');
38
- CoreApp.appQuit();
39
- }
40
- })
41
-
42
- app.on('before-quit', () => {
43
- Electron.extra.closeWindow = true;
15
+ function createElectron() {
16
+ const { singleLock } = getConfig();
17
+ // 允许多个实例
18
+ const gotTheLock = electronApp.requestSingleInstanceLock();
19
+ if (singleLock && !gotTheLock) {
20
+ electronApp.quit();
21
+ }
44
22
 
45
- // kill cross services
46
- Cross.killAll();
47
- })
23
+ electronApp.whenReady().then(() => {
24
+ createMainWindow();
25
+ eventBus.emitLifecycle(Preload);
26
+ loadServer();
27
+ })
48
28
 
49
- if (CoreApp.config.hardGpu.enable == false) {
50
- app.disableHardwareAcceleration();
29
+ electronApp.on('window-all-closed', () => {
30
+ if (!is.macOS()) {
31
+ coreLogger.info('[ee-core] [lib/eeApp] window-all-closed quit');
32
+ electronApp.quit();
51
33
  }
34
+ })
52
35
 
53
- return app;
54
- },
36
+ electronApp.on('before-quit', () => {
37
+ setCloseAndQuit(true);
38
+ eventBus.emitLifecycle(BeforeClose);
39
+ cross.killAll();
40
+ })
55
41
 
56
- /**
57
- * 退出app
58
- */
59
- quit() {
60
- app.quit();
61
- }
42
+ eventBus.emitLifecycle(ElectronAppReady);
62
43
  }
63
44
 
64
- module.exports = CoreElectronApp;
45
+ module.exports = {
46
+ electronApp,
47
+ createElectron,
48
+ };