Skip to content

Commit f4d5654

Browse files
committed
[feature]修复缺陷
1 parent 1612603 commit f4d5654

File tree

12 files changed

+76
-37
lines changed

12 files changed

+76
-37
lines changed

README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,11 +621,37 @@ getInstance(option)
621621
/**
622622
* @param {string} eventName 通知事件名
623623
* @param {object} param 参数
624-
* @return {browserWindow}
624+
* @return {promise}
625625
*/
626626
publisher(eventName, param)
627627
```
628-
628+
**subscribe [实例方法]向当前窗口订阅通知,可参考ipc模块**
629+
```
630+
/**
631+
* @param {string} eventName 通知事件名
632+
* @param {function} callback 回调
633+
* @return {unsubscribe} 取消订阅函数
634+
*/
635+
subscribe(eventName, callback)
636+
```
637+
**unsubscribe [实例方法]向当前窗口取消订阅通知,可参考ipc模块**
638+
```
639+
/**
640+
* @param {string} eventName 通知事件名
641+
* @param {function} callback 回调
642+
*/
643+
unsubscribe(eventName, callback)
644+
```
645+
**request [实例方法]向当前窗口发起请求,可参考ipc模块**
646+
```
647+
/**
648+
* @param {string} eventName 请求事件名事件名
649+
* @param {object} param 参数
650+
* @param {number} timeout 超时时间
651+
* @return {promise}
652+
*/
653+
request(eventName, param, timeout)
654+
```
629655
**使用举例**
630656
```
631657
// -----------------------主进程-----------------------

core/BaseWindow/main.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,10 @@ class BaseWindow extends Events {
5656
// 注册到窗口中心
5757
windowCenter._register(name, this);
5858
}
59-
60-
// 发布通知
61-
publisher(eventName, params = {}) {
62-
ipc._publisher({ header: { fromId: this.name, eventName }, body: params });
63-
this.emit(eventName, params);
64-
}
65-
6659
open(option = {}) {
6760
try {
6861
if (this.instance === null) {
69-
const _option = Object.assign(defaultOption, this.option, option);
62+
const _option = Object.assign({}, defaultOption, this.option, option);
7063
this.instance = new BrowserWindow(_option);
7164
// 窗口ID,必须
7265
this.instance.windowId = this.name;
@@ -92,6 +85,24 @@ class BaseWindow extends Events {
9285
getInstance() {
9386
return this.instance;
9487
}
88+
89+
// 发布通知
90+
publisher(eventName, params = {}) {
91+
ipc._publisher({ header: { fromId: this.name, eventName }, body: params });
92+
this.emit(eventName, params);
93+
}
94+
95+
request(eventName = '', data = {}, timeout) {
96+
return ipc.request(this.name, eventName, data, timeout);
97+
}
98+
99+
subscribe(eventName = '', callback) {
100+
return ipc.subscribe(this.name, eventName, callback);
101+
}
102+
103+
unsubscribe(eventName, callback) {
104+
return ipc.unsubscribe(this.name, eventName, callback);
105+
}
95106
}
96107

97108
module.exports = BaseWindow;

core/common/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require('path');
22
const { SUGAR_OPTION } = require('../const');
3+
global[SUGAR_OPTION] = {};
34
const config = require('../config/main');
45
const store = require('../store/main');
56
const fs = require('fs');

core/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const { SUGAR_OPTION } = require('./const');
2-
global[SUGAR_OPTION] = {};
31
const common = require('./common');
42
const modules = {
53
BaseWindow: require('./BaseWindow'),
@@ -11,4 +9,4 @@ const modules = {
119
windowCenter: require('./windowCenter')
1210
};
1311

14-
module.exports = Object.assign(common, modules);
12+
module.exports = Object.assign({}, common, modules);

core/ipc/main.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ const {
99
SUBSCRIBER,
1010
UNSUBSCRIBER,
1111
IPC_NAME,
12-
RESPONSE_OVERTIME,
13-
MAIN_PROCESS_NAME
12+
RESPONSE_OVERTIME
1413
} = require('./const');
14+
const { MAIN_PROCESS_NAME } = require('../const');
1515
const processes = {}; // 进程模块合集
1616
const subscribeTasks = {}; // 缓存进程订阅任务
1717
const responseCallbacks = {};
@@ -79,7 +79,7 @@ class MainSDK {
7979
const { header = {}, body } = params;
8080
const { fromId, toId } = header;
8181
processes[fromId] && processes[fromId].webContents.send(IPC_NAME, {
82-
header: Object.assign(header, {
82+
header: Object.assign({}, header, {
8383
fromId: toId,
8484
toId: fromId
8585
}),
@@ -259,7 +259,7 @@ class MainSDK {
259259
this._publisher({ header: { fromId: MAIN_PROCESS_NAME, eventName }, body: params });
260260
}
261261

262-
subscribe(toId = '', eventName = '', callback = () => { }) {
262+
subscribe(toId = '', eventName = '', callback = () => {}) {
263263
if (!subscribeCb[`${toId}${eventName}`]) {
264264
subscribeCb[`${toId}${eventName}`] = [];
265265
}

core/store/main.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const { STORE, SET_STATE, STATE_CHANGE, MAIN_PROCESS_NAME } = require('./const');
1+
const { STORE, SET_STATE, STATE_CHANGE } = require('./const');
2+
const { MAIN_PROCESS_NAME } = require('../const');
23
const ipc = require('../ipc/main');
34
const Event = require('events');
45
const event = new Event();
@@ -17,7 +18,7 @@ function initStore(store, modules = []) {
1718
var keyState = `${STORE}${modules.join('|')}`;
1819
global[keyState] = item;
1920
if (keyState === STORE) {
20-
modules.state = global[keyState];
21+
moduleExports.state = global[keyState];
2122
}
2223
break;
2324
case 'modules':
@@ -60,7 +61,7 @@ function setState(json) {
6061
}
6162

6263
// 初始化
63-
const modules = {
64+
const moduleExports = {
6465
createStore(store = {}) {
6566
initStore(store);
6667
},
@@ -118,4 +119,4 @@ const modules = {
118119
}
119120
};
120121

121-
module.exports = modules;
122+
module.exports = moduleExports;

core/store/render.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable no-async-promise-executor */
22
const { remote } = require('electron');
3-
const { STORE, SET_STATE, STATE_CHANGE, MAIN_PROCESS_NAME } = require('./const');
3+
const { STORE, SET_STATE, STATE_CHANGE } = require('./const');
4+
const { MAIN_PROCESS_NAME } = require('../const');
45
const ipc = require('../ipc/render');
56

67
module.exports = {

core/windowCenter/render.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const remote = require('electron').remote;
22
const ipc = require('../ipc/render');
3-
const { WINDOW_CENTER_IPC_NAME, WINDOW_CENTER_GET_INFO, MAIN_PROCESS_NAME } = require('./const');
3+
const { WINDOW_CENTER_IPC_NAME, WINDOW_CENTER_GET_INFO } = require('./const');
4+
const { MAIN_PROCESS_NAME } = require('../const');
45
const modules = {};
56
const ipcKeys = ['request', 'subscribe', 'unsubscribe'];
67
async function actionWindow (windowName, action = '', args) {
@@ -10,14 +11,16 @@ async function actionWindow (windowName, action = '', args) {
1011
}
1112
// 初始化
1213
const { names = [], keys = [] } = remote.getGlobal(WINDOW_CENTER_GET_INFO);
13-
names.forEach(name => {
14+
names.concat([MAIN_PROCESS_NAME]).forEach(name => {
1415
modules[name] = {};
15-
keys.forEach(key => {
16-
modules[name][key] = function () {
17-
const args = [].slice.call(arguments);
18-
return actionWindow(name, key, args);
19-
}
20-
});
16+
if (name !== MAIN_PROCESS_NAME) {
17+
keys.forEach(key => {
18+
modules[name][key] = function () {
19+
const args = [].slice.call(arguments);
20+
return actionWindow(name, key, args);
21+
}
22+
});
23+
}
2124
ipcKeys.forEach(key => {
2225
modules[name][key] = function () {
2326
const args = [].slice.call(arguments);

main.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const { SUGAR_OPTION } = require('./core/const');
2-
global[SUGAR_OPTION] = {};
31
const common = require('./core/common/main');
42
const modules = {
53
BaseWindow: require('./core/BaseWindow/main'),

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sugar-electron",
3-
"version": "1.0.43",
3+
"version": "1.0.50",
44
"description": "",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)