Skip to content

Commit 49e21de

Browse files
Merge pull request #486 from stackify/bug/442
442: maintain references between socket.io connection events
2 parents f06b958 + d2f6308 commit 49e21de

File tree

8 files changed

+133
-142
lines changed

8 files changed

+133
-142
lines changed

ElectronNET.Host/api/browserView.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BrowserView } from 'electron';
2-
const browserViews: Electron.BrowserView[] = [];
2+
const browserViews: BrowserView[] = (global['browserViews'] = global['browserViews'] || []) as BrowserView[];
33
let browserView: BrowserView, electronSocket;
44

55
const browserViewApi = (socket: SocketIO.Socket) => {

ElectronNET.Host/api/browserWindows.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BrowserWindow, Menu, nativeImage } from 'electron';
22
import { browserViewMediateService } from './browserView';
33
const path = require('path');
4-
const windows: Electron.BrowserWindow[] = [];
4+
const windows: Electron.BrowserWindow[] = (global['browserWindows'] = global['browserWindows'] || []) as Electron.BrowserWindow[];
55
let readyToShowWindowsIds: number[] = [];
66
let window, lastOptions, electronSocket;
77
let mainWindowURL;

ElectronNET.Host/api/menu.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Menu, BrowserWindow } from 'electron';
2-
const contextMenuItems = [];
2+
const contextMenuItems = (global['contextMenuItems'] = global['contextMenuItems'] || []);
33
let electronSocket;
44

55
export = (socket: SocketIO.Socket) => {

ElectronNET.Host/api/notification.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
"use strict";
2-
const electron_1 = require("electron");
3-
const notifications = [];
4-
let electronSocket;
5-
module.exports = (socket) => {
2+
var electron_1 = require("electron");
3+
var notifications = (global['notifications'] = global['notifications'] || []);
4+
var electronSocket;
5+
module.exports = function (socket) {
66
electronSocket = socket;
7-
socket.on('createNotification', (options) => {
8-
const notification = new electron_1.Notification(options);
9-
let haveEvent = false;
7+
socket.on('createNotification', function (options) {
8+
var notification = new electron_1.Notification(options);
9+
var haveEvent = false;
1010
if (options.showID) {
1111
haveEvent = true;
12-
notification.on('show', () => {
12+
notification.on('show', function () {
1313
electronSocket.emit('NotificationEventShow', options.showID);
1414
});
1515
}
1616
if (options.clickID) {
1717
haveEvent = true;
18-
notification.on('click', () => {
18+
notification.on('click', function () {
1919
electronSocket.emit('NotificationEventClick', options.clickID);
2020
});
2121
}
2222
if (options.closeID) {
2323
haveEvent = true;
24-
notification.on('close', () => {
24+
notification.on('close', function () {
2525
electronSocket.emit('NotificationEventClose', options.closeID);
2626
});
2727
}
2828
if (options.replyID) {
2929
haveEvent = true;
30-
notification.on('reply', (event, value) => {
30+
notification.on('reply', function (event, value) {
3131
electronSocket.emit('NotificationEventReply', [options.replyID, value]);
3232
});
3333
}
3434
if (options.actionID) {
3535
haveEvent = true;
36-
notification.on('action', (event, value) => {
36+
notification.on('action', function (event, value) {
3737
electronSocket.emit('NotificationEventAction', [options.actionID, value]);
3838
});
3939
}
@@ -42,9 +42,8 @@ module.exports = (socket) => {
4242
}
4343
notification.show();
4444
});
45-
socket.on('notificationIsSupported', () => {
46-
const isSupported = electron_1.Notification.isSupported;
45+
socket.on('notificationIsSupported', function () {
46+
var isSupported = electron_1.Notification.isSupported;
4747
electronSocket.emit('notificationIsSupportedComplete', isSupported);
4848
});
4949
};
50-
//# sourceMappingURL=notification.js.map

ElectronNET.Host/api/notification.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Notification } from 'electron';
2-
const notifications: Electron.Notification[] = [];
2+
const notifications: Electron.Notification[] = (global['notifications'] = global['notifications'] || []) as Electron.Notification[];
33
let electronSocket;
44

55
export = (socket: SocketIO.Socket) => {

ElectronNET.Host/api/tray.js

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,107 @@
11
"use strict";
2-
const electron_1 = require("electron");
3-
let tray;
4-
let electronSocket;
5-
module.exports = (socket) => {
2+
var electron_1 = require("electron");
3+
var tray = (global['tray'] = global['tray'] || { value: null });
4+
var electronSocket;
5+
module.exports = function (socket) {
66
electronSocket = socket;
7-
socket.on('register-tray-click', (id) => {
8-
if (tray) {
9-
tray.on('click', (event, bounds) => {
7+
socket.on('register-tray-click', function (id) {
8+
if (tray.value) {
9+
tray.value.on('click', function (event, bounds) {
1010
electronSocket.emit('tray-click-event' + id, [event.__proto__, bounds]);
1111
});
1212
}
1313
});
14-
socket.on('register-tray-right-click', (id) => {
15-
if (tray) {
16-
tray.on('right-click', (event, bounds) => {
14+
socket.on('register-tray-right-click', function (id) {
15+
if (tray.value) {
16+
tray.value.on('right-click', function (event, bounds) {
1717
electronSocket.emit('tray-right-click-event' + id, [event.__proto__, bounds]);
1818
});
1919
}
2020
});
21-
socket.on('register-tray-double-click', (id) => {
22-
if (tray) {
23-
tray.on('double-click', (event, bounds) => {
21+
socket.on('register-tray-double-click', function (id) {
22+
if (tray.value) {
23+
tray.value.on('double-click', function (event, bounds) {
2424
electronSocket.emit('tray-double-click-event' + id, [event.__proto__, bounds]);
2525
});
2626
}
2727
});
28-
socket.on('register-tray-balloon-show', (id) => {
29-
if (tray) {
30-
tray.on('balloon-show', () => {
28+
socket.on('register-tray-balloon-show', function (id) {
29+
if (tray.value) {
30+
tray.value.on('balloon-show', function () {
3131
electronSocket.emit('tray-balloon-show-event' + id);
3232
});
3333
}
3434
});
35-
socket.on('register-tray-balloon-click', (id) => {
36-
if (tray) {
37-
tray.on('balloon-click', () => {
35+
socket.on('register-tray-balloon-click', function (id) {
36+
if (tray.value) {
37+
tray.value.on('balloon-click', function () {
3838
electronSocket.emit('tray-balloon-click-event' + id);
3939
});
4040
}
4141
});
42-
socket.on('register-tray-balloon-closed', (id) => {
43-
if (tray) {
44-
tray.on('balloon-closed', () => {
42+
socket.on('register-tray-balloon-closed', function (id) {
43+
if (tray.value) {
44+
tray.value.on('balloon-closed', function () {
4545
electronSocket.emit('tray-balloon-closed-event' + id);
4646
});
4747
}
4848
});
49-
socket.on('create-tray', (image, menuItems) => {
50-
const trayIcon = electron_1.nativeImage.createFromPath(image);
51-
tray = new electron_1.Tray(trayIcon);
49+
socket.on('create-tray', function (image, menuItems) {
50+
var trayIcon = electron_1.nativeImage.createFromPath(image);
51+
tray.value = new electron_1.Tray(trayIcon);
5252
if (menuItems) {
53-
const menu = electron_1.Menu.buildFromTemplate(menuItems);
54-
addMenuItemClickConnector(menu.items, (id) => {
53+
var menu = electron_1.Menu.buildFromTemplate(menuItems);
54+
addMenuItemClickConnector(menu.items, function (id) {
5555
electronSocket.emit('trayMenuItemClicked', id);
5656
});
57-
tray.setContextMenu(menu);
57+
tray.value.setContextMenu(menu);
5858
}
5959
});
60-
socket.on('tray-destroy', () => {
61-
if (tray) {
62-
tray.destroy();
60+
socket.on('tray-destroy', function () {
61+
if (tray.value) {
62+
tray.value.destroy();
6363
}
6464
});
65-
socket.on('tray-setImage', (image) => {
66-
if (tray) {
67-
tray.setImage(image);
65+
socket.on('tray-setImage', function (image) {
66+
if (tray.value) {
67+
tray.value.setImage(image);
6868
}
6969
});
70-
socket.on('tray-setPressedImage', (image) => {
71-
if (tray) {
72-
const img = electron_1.nativeImage.createFromPath(image);
73-
tray.setPressedImage(img);
70+
socket.on('tray-setPressedImage', function (image) {
71+
if (tray.value) {
72+
var img = electron_1.nativeImage.createFromPath(image);
73+
tray.value.setPressedImage(img);
7474
}
7575
});
76-
socket.on('tray-setToolTip', (toolTip) => {
77-
if (tray) {
78-
tray.setToolTip(toolTip);
76+
socket.on('tray-setToolTip', function (toolTip) {
77+
if (tray.value) {
78+
tray.value.setToolTip(toolTip);
7979
}
8080
});
81-
socket.on('tray-setTitle', (title) => {
82-
if (tray) {
83-
tray.setTitle(title);
81+
socket.on('tray-setTitle', function (title) {
82+
if (tray.value) {
83+
tray.value.setTitle(title);
8484
}
8585
});
86-
socket.on('tray-displayBalloon', (options) => {
87-
if (tray) {
88-
tray.displayBalloon(options);
86+
socket.on('tray-displayBalloon', function (options) {
87+
if (tray.value) {
88+
tray.value.displayBalloon(options);
8989
}
9090
});
91-
socket.on('tray-isDestroyed', () => {
92-
if (tray) {
93-
const isDestroyed = tray.isDestroyed();
91+
socket.on('tray-isDestroyed', function () {
92+
if (tray.value) {
93+
var isDestroyed = tray.value.isDestroyed();
9494
electronSocket.emit('tray-isDestroyedCompleted', isDestroyed);
9595
}
9696
});
9797
function addMenuItemClickConnector(menuItems, callback) {
98-
menuItems.forEach((item) => {
98+
menuItems.forEach(function (item) {
9999
if (item.submenu && item.submenu.items.length > 0) {
100100
addMenuItemClickConnector(item.submenu.items, callback);
101101
}
102102
if ('id' in item && item.id) {
103-
item.click = () => { callback(item.id); };
103+
item.click = function () { callback(item.id); };
104104
}
105105
});
106106
}
107107
};
108-
//# sourceMappingURL=tray.js.map

ElectronNET.Host/api/tray.ts

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
11
import { Menu, Tray, nativeImage } from 'electron';
2-
let tray: Electron.Tray;
2+
let tray: { value: Electron.Tray } = (global['$tray'] = global['tray'] || { value: null });
33
let electronSocket;
44

55
export = (socket: SocketIO.Socket) => {
66
electronSocket = socket;
77
socket.on('register-tray-click', (id) => {
8-
if (tray) {
9-
tray.on('click', (event, bounds) => {
8+
if (tray.value) {
9+
tray.value.on('click', (event, bounds) => {
1010
electronSocket.emit('tray-click-event' + id, [(<any>event).__proto__, bounds]);
1111
});
1212
}
1313
});
1414

1515
socket.on('register-tray-right-click', (id) => {
16-
if (tray) {
17-
tray.on('right-click', (event, bounds) => {
16+
if (tray.value) {
17+
tray.value.on('right-click', (event, bounds) => {
1818
electronSocket.emit('tray-right-click-event' + id, [(<any>event).__proto__, bounds]);
1919
});
2020
}
2121
});
2222

2323
socket.on('register-tray-double-click', (id) => {
24-
if (tray) {
25-
tray.on('double-click', (event, bounds) => {
24+
if (tray.value) {
25+
tray.value.on('double-click', (event, bounds) => {
2626
electronSocket.emit('tray-double-click-event' + id, [(<any>event).__proto__, bounds]);
2727
});
2828
}
2929
});
3030

3131
socket.on('register-tray-balloon-show', (id) => {
32-
if (tray) {
33-
tray.on('balloon-show', () => {
32+
if (tray.value) {
33+
tray.value.on('balloon-show', () => {
3434
electronSocket.emit('tray-balloon-show-event' + id);
3535
});
3636
}
3737
});
3838

3939
socket.on('register-tray-balloon-click', (id) => {
40-
if (tray) {
41-
tray.on('balloon-click', () => {
40+
if (tray.value) {
41+
tray.value.on('balloon-click', () => {
4242
electronSocket.emit('tray-balloon-click-event' + id);
4343
});
4444
}
4545
});
4646

4747
socket.on('register-tray-balloon-closed', (id) => {
48-
if (tray) {
49-
tray.on('balloon-closed', () => {
48+
if (tray.value) {
49+
tray.value.on('balloon-closed', () => {
5050
electronSocket.emit('tray-balloon-closed-event' + id);
5151
});
5252
}
@@ -55,58 +55,58 @@ export = (socket: SocketIO.Socket) => {
5555
socket.on('create-tray', (image, menuItems) => {
5656
const trayIcon = nativeImage.createFromPath(image);
5757

58-
tray = new Tray(trayIcon);
58+
tray.value = new Tray(trayIcon);
5959

6060
if (menuItems) {
6161
const menu = Menu.buildFromTemplate(menuItems);
6262

6363
addMenuItemClickConnector(menu.items, (id) => {
6464
electronSocket.emit('trayMenuItemClicked', id);
6565
});
66-
tray.setContextMenu(menu);
66+
tray.value.setContextMenu(menu);
6767
}
6868
});
6969

7070
socket.on('tray-destroy', () => {
71-
if (tray) {
72-
tray.destroy();
71+
if (tray.value) {
72+
tray.value.destroy();
7373
}
7474
});
7575

7676
socket.on('tray-setImage', (image) => {
77-
if (tray) {
78-
tray.setImage(image);
77+
if (tray.value) {
78+
tray.value.setImage(image);
7979
}
8080
});
8181

8282
socket.on('tray-setPressedImage', (image) => {
83-
if (tray) {
83+
if (tray.value) {
8484
const img = nativeImage.createFromPath(image);
85-
tray.setPressedImage(img);
85+
tray.value.setPressedImage(img);
8686
}
8787
});
8888

8989
socket.on('tray-setToolTip', (toolTip) => {
90-
if (tray) {
91-
tray.setToolTip(toolTip);
90+
if (tray.value) {
91+
tray.value.setToolTip(toolTip);
9292
}
9393
});
9494

9595
socket.on('tray-setTitle', (title) => {
96-
if (tray) {
97-
tray.setTitle(title);
96+
if (tray.value) {
97+
tray.value.setTitle(title);
9898
}
9999
});
100100

101101
socket.on('tray-displayBalloon', (options) => {
102-
if (tray) {
103-
tray.displayBalloon(options);
102+
if (tray.value) {
103+
tray.value.displayBalloon(options);
104104
}
105105
});
106106

107107
socket.on('tray-isDestroyed', () => {
108-
if (tray) {
109-
const isDestroyed = tray.isDestroyed();
108+
if (tray.value) {
109+
const isDestroyed = tray.value.isDestroyed();
110110
electronSocket.emit('tray-isDestroyedCompleted', isDestroyed);
111111
}
112112
});

0 commit comments

Comments
 (0)