Merge pull request #486 from stackify/bug/442

442: maintain references between socket.io connection events
This commit is contained in:
Gregor Biswanger
2021-01-11 23:27:51 +01:00
committed by GitHub
8 changed files with 133 additions and 142 deletions

View File

@@ -1,5 +1,5 @@
import { BrowserView } from 'electron';
const browserViews: Electron.BrowserView[] = [];
const browserViews: BrowserView[] = (global['browserViews'] = global['browserViews'] || []) as BrowserView[];
let browserView: BrowserView, electronSocket;
const browserViewApi = (socket: SocketIO.Socket) => {

View File

@@ -1,7 +1,7 @@
import { BrowserWindow, Menu, nativeImage } from 'electron';
import { browserViewMediateService } from './browserView';
const path = require('path');
const windows: Electron.BrowserWindow[] = [];
const windows: Electron.BrowserWindow[] = (global['browserWindows'] = global['browserWindows'] || []) as Electron.BrowserWindow[];
let readyToShowWindowsIds: number[] = [];
let window, lastOptions, electronSocket;
let mainWindowURL;

View File

@@ -1,5 +1,5 @@
import { Menu, BrowserWindow } from 'electron';
const contextMenuItems = [];
const contextMenuItems = (global['contextMenuItems'] = global['contextMenuItems'] || []);
let electronSocket;
export = (socket: SocketIO.Socket) => {

View File

@@ -1,39 +1,39 @@
"use strict";
const electron_1 = require("electron");
const notifications = [];
let electronSocket;
module.exports = (socket) => {
var electron_1 = require("electron");
var notifications = (global['notifications'] = global['notifications'] || []);
var electronSocket;
module.exports = function (socket) {
electronSocket = socket;
socket.on('createNotification', (options) => {
const notification = new electron_1.Notification(options);
let haveEvent = false;
socket.on('createNotification', function (options) {
var notification = new electron_1.Notification(options);
var haveEvent = false;
if (options.showID) {
haveEvent = true;
notification.on('show', () => {
notification.on('show', function () {
electronSocket.emit('NotificationEventShow', options.showID);
});
}
if (options.clickID) {
haveEvent = true;
notification.on('click', () => {
notification.on('click', function () {
electronSocket.emit('NotificationEventClick', options.clickID);
});
}
if (options.closeID) {
haveEvent = true;
notification.on('close', () => {
notification.on('close', function () {
electronSocket.emit('NotificationEventClose', options.closeID);
});
}
if (options.replyID) {
haveEvent = true;
notification.on('reply', (event, value) => {
notification.on('reply', function (event, value) {
electronSocket.emit('NotificationEventReply', [options.replyID, value]);
});
}
if (options.actionID) {
haveEvent = true;
notification.on('action', (event, value) => {
notification.on('action', function (event, value) {
electronSocket.emit('NotificationEventAction', [options.actionID, value]);
});
}
@@ -42,9 +42,8 @@ module.exports = (socket) => {
}
notification.show();
});
socket.on('notificationIsSupported', () => {
const isSupported = electron_1.Notification.isSupported;
socket.on('notificationIsSupported', function () {
var isSupported = electron_1.Notification.isSupported;
electronSocket.emit('notificationIsSupportedComplete', isSupported);
});
};
//# sourceMappingURL=notification.js.map

View File

@@ -1,5 +1,5 @@
import { Notification } from 'electron';
const notifications: Electron.Notification[] = [];
const notifications: Electron.Notification[] = (global['notifications'] = global['notifications'] || []) as Electron.Notification[];
let electronSocket;
export = (socket: SocketIO.Socket) => {

View File

@@ -1,108 +1,107 @@
"use strict";
const electron_1 = require("electron");
let tray;
let electronSocket;
module.exports = (socket) => {
var electron_1 = require("electron");
var tray = (global['tray'] = global['tray'] || { value: null });
var electronSocket;
module.exports = function (socket) {
electronSocket = socket;
socket.on('register-tray-click', (id) => {
if (tray) {
tray.on('click', (event, bounds) => {
socket.on('register-tray-click', function (id) {
if (tray.value) {
tray.value.on('click', function (event, bounds) {
electronSocket.emit('tray-click-event' + id, [event.__proto__, bounds]);
});
}
});
socket.on('register-tray-right-click', (id) => {
if (tray) {
tray.on('right-click', (event, bounds) => {
socket.on('register-tray-right-click', function (id) {
if (tray.value) {
tray.value.on('right-click', function (event, bounds) {
electronSocket.emit('tray-right-click-event' + id, [event.__proto__, bounds]);
});
}
});
socket.on('register-tray-double-click', (id) => {
if (tray) {
tray.on('double-click', (event, bounds) => {
socket.on('register-tray-double-click', function (id) {
if (tray.value) {
tray.value.on('double-click', function (event, bounds) {
electronSocket.emit('tray-double-click-event' + id, [event.__proto__, bounds]);
});
}
});
socket.on('register-tray-balloon-show', (id) => {
if (tray) {
tray.on('balloon-show', () => {
socket.on('register-tray-balloon-show', function (id) {
if (tray.value) {
tray.value.on('balloon-show', function () {
electronSocket.emit('tray-balloon-show-event' + id);
});
}
});
socket.on('register-tray-balloon-click', (id) => {
if (tray) {
tray.on('balloon-click', () => {
socket.on('register-tray-balloon-click', function (id) {
if (tray.value) {
tray.value.on('balloon-click', function () {
electronSocket.emit('tray-balloon-click-event' + id);
});
}
});
socket.on('register-tray-balloon-closed', (id) => {
if (tray) {
tray.on('balloon-closed', () => {
socket.on('register-tray-balloon-closed', function (id) {
if (tray.value) {
tray.value.on('balloon-closed', function () {
electronSocket.emit('tray-balloon-closed-event' + id);
});
}
});
socket.on('create-tray', (image, menuItems) => {
const trayIcon = electron_1.nativeImage.createFromPath(image);
tray = new electron_1.Tray(trayIcon);
socket.on('create-tray', function (image, menuItems) {
var trayIcon = electron_1.nativeImage.createFromPath(image);
tray.value = new electron_1.Tray(trayIcon);
if (menuItems) {
const menu = electron_1.Menu.buildFromTemplate(menuItems);
addMenuItemClickConnector(menu.items, (id) => {
var menu = electron_1.Menu.buildFromTemplate(menuItems);
addMenuItemClickConnector(menu.items, function (id) {
electronSocket.emit('trayMenuItemClicked', id);
});
tray.setContextMenu(menu);
tray.value.setContextMenu(menu);
}
});
socket.on('tray-destroy', () => {
if (tray) {
tray.destroy();
socket.on('tray-destroy', function () {
if (tray.value) {
tray.value.destroy();
}
});
socket.on('tray-setImage', (image) => {
if (tray) {
tray.setImage(image);
socket.on('tray-setImage', function (image) {
if (tray.value) {
tray.value.setImage(image);
}
});
socket.on('tray-setPressedImage', (image) => {
if (tray) {
const img = electron_1.nativeImage.createFromPath(image);
tray.setPressedImage(img);
socket.on('tray-setPressedImage', function (image) {
if (tray.value) {
var img = electron_1.nativeImage.createFromPath(image);
tray.value.setPressedImage(img);
}
});
socket.on('tray-setToolTip', (toolTip) => {
if (tray) {
tray.setToolTip(toolTip);
socket.on('tray-setToolTip', function (toolTip) {
if (tray.value) {
tray.value.setToolTip(toolTip);
}
});
socket.on('tray-setTitle', (title) => {
if (tray) {
tray.setTitle(title);
socket.on('tray-setTitle', function (title) {
if (tray.value) {
tray.value.setTitle(title);
}
});
socket.on('tray-displayBalloon', (options) => {
if (tray) {
tray.displayBalloon(options);
socket.on('tray-displayBalloon', function (options) {
if (tray.value) {
tray.value.displayBalloon(options);
}
});
socket.on('tray-isDestroyed', () => {
if (tray) {
const isDestroyed = tray.isDestroyed();
socket.on('tray-isDestroyed', function () {
if (tray.value) {
var isDestroyed = tray.value.isDestroyed();
electronSocket.emit('tray-isDestroyedCompleted', isDestroyed);
}
});
function addMenuItemClickConnector(menuItems, callback) {
menuItems.forEach((item) => {
menuItems.forEach(function (item) {
if (item.submenu && item.submenu.items.length > 0) {
addMenuItemClickConnector(item.submenu.items, callback);
}
if ('id' in item && item.id) {
item.click = () => { callback(item.id); };
item.click = function () { callback(item.id); };
}
});
}
};
//# sourceMappingURL=tray.js.map

View File

@@ -1,52 +1,52 @@
import { Menu, Tray, nativeImage } from 'electron';
let tray: Electron.Tray;
let tray: { value: Electron.Tray } = (global['$tray'] = global['tray'] || { value: null });
let electronSocket;
export = (socket: SocketIO.Socket) => {
electronSocket = socket;
socket.on('register-tray-click', (id) => {
if (tray) {
tray.on('click', (event, bounds) => {
if (tray.value) {
tray.value.on('click', (event, bounds) => {
electronSocket.emit('tray-click-event' + id, [(<any>event).__proto__, bounds]);
});
}
});
socket.on('register-tray-right-click', (id) => {
if (tray) {
tray.on('right-click', (event, bounds) => {
if (tray.value) {
tray.value.on('right-click', (event, bounds) => {
electronSocket.emit('tray-right-click-event' + id, [(<any>event).__proto__, bounds]);
});
}
});
socket.on('register-tray-double-click', (id) => {
if (tray) {
tray.on('double-click', (event, bounds) => {
if (tray.value) {
tray.value.on('double-click', (event, bounds) => {
electronSocket.emit('tray-double-click-event' + id, [(<any>event).__proto__, bounds]);
});
}
});
socket.on('register-tray-balloon-show', (id) => {
if (tray) {
tray.on('balloon-show', () => {
if (tray.value) {
tray.value.on('balloon-show', () => {
electronSocket.emit('tray-balloon-show-event' + id);
});
}
});
socket.on('register-tray-balloon-click', (id) => {
if (tray) {
tray.on('balloon-click', () => {
if (tray.value) {
tray.value.on('balloon-click', () => {
electronSocket.emit('tray-balloon-click-event' + id);
});
}
});
socket.on('register-tray-balloon-closed', (id) => {
if (tray) {
tray.on('balloon-closed', () => {
if (tray.value) {
tray.value.on('balloon-closed', () => {
electronSocket.emit('tray-balloon-closed-event' + id);
});
}
@@ -55,7 +55,7 @@ export = (socket: SocketIO.Socket) => {
socket.on('create-tray', (image, menuItems) => {
const trayIcon = nativeImage.createFromPath(image);
tray = new Tray(trayIcon);
tray.value = new Tray(trayIcon);
if (menuItems) {
const menu = Menu.buildFromTemplate(menuItems);
@@ -63,50 +63,50 @@ export = (socket: SocketIO.Socket) => {
addMenuItemClickConnector(menu.items, (id) => {
electronSocket.emit('trayMenuItemClicked', id);
});
tray.setContextMenu(menu);
tray.value.setContextMenu(menu);
}
});
socket.on('tray-destroy', () => {
if (tray) {
tray.destroy();
if (tray.value) {
tray.value.destroy();
}
});
socket.on('tray-setImage', (image) => {
if (tray) {
tray.setImage(image);
if (tray.value) {
tray.value.setImage(image);
}
});
socket.on('tray-setPressedImage', (image) => {
if (tray) {
if (tray.value) {
const img = nativeImage.createFromPath(image);
tray.setPressedImage(img);
tray.value.setPressedImage(img);
}
});
socket.on('tray-setToolTip', (toolTip) => {
if (tray) {
tray.setToolTip(toolTip);
if (tray.value) {
tray.value.setToolTip(toolTip);
}
});
socket.on('tray-setTitle', (title) => {
if (tray) {
tray.setTitle(title);
if (tray.value) {
tray.value.setTitle(title);
}
});
socket.on('tray-displayBalloon', (options) => {
if (tray) {
tray.displayBalloon(options);
if (tray.value) {
tray.value.displayBalloon(options);
}
});
socket.on('tray-isDestroyed', () => {
if (tray) {
const isDestroyed = tray.isDestroyed();
if (tray.value) {
const isDestroyed = tray.value.isDestroyed();
electronSocket.emit('tray-isDestroyedCompleted', isDestroyed);
}
});

View File

@@ -165,53 +165,46 @@ function startSocketApiBridge(port) {
io.on('connection', (socket) => {
// we need to remove previously cache instances
// otherwise it will fire the same event multiple depends how many time
// live reload watch happen.
socket.on('disconnect', function (reason) {
console.log('Got disconnect! Reason: ' + reason);
delete require.cache[require.resolve('./api/app')];
delete require.cache[require.resolve('./api/browserWindows')];
delete require.cache[require.resolve('./api/commandLine')];
delete require.cache[require.resolve('./api/autoUpdater')];
delete require.cache[require.resolve('./api/ipc')];
delete require.cache[require.resolve('./api/menu')];
delete require.cache[require.resolve('./api/dialog')];
delete require.cache[require.resolve('./api/notification')];
delete require.cache[require.resolve('./api/tray')];
delete require.cache[require.resolve('./api/webContents')];
delete require.cache[require.resolve('./api/globalShortcut')];
delete require.cache[require.resolve('./api/shell')];
delete require.cache[require.resolve('./api/screen')];
delete require.cache[require.resolve('./api/clipboard')];
delete require.cache[require.resolve('./api/browserView')];
delete require.cache[require.resolve('./api/powerMonitor')];
delete require.cache[require.resolve('./api/nativeTheme')];
delete require.cache[require.resolve('./api/dock')];
try {
if (hostHook) {
const hostHookScriptFilePath = path.join(__dirname, 'ElectronHostHook', 'index.js');
delete require.cache[require.resolve(hostHookScriptFilePath)];
hostHook = undefined;
}
} catch (error) {
console.error(error.message);
}
});
global['electronsocket'] = socket;
global['electronsocket'].setMaxListeners(0);
if (global['electronsocket'] === undefined) {
global['electronsocket'] = socket;
global['electronsocket'].setMaxListeners(0);
}
console.log('ASP.NET Core Application connected...', 'global.electronsocket', global['electronsocket'].id, new Date());
appApi = require('./api/app')(socket, app);
browserWindows = require('./api/browserWindows')(socket, app);
commandLine = require('./api/commandLine')(socket, app);
autoUpdater = require('./api/autoUpdater')(socket);
ipc = require('./api/ipc')(socket);
menu = require('./api/menu')(socket);
dialogApi = require('./api/dialog')(socket);
notification = require('./api/notification')(socket);
tray = require('./api/tray')(socket);
webContents = require('./api/webContents')(socket);
globalShortcut = require('./api/globalShortcut')(socket);
shellApi = require('./api/shell')(socket);
screen = require('./api/screen')(socket);
clipboard = require('./api/clipboard')(socket);
browserView = require('./api/browserView').browserViewApi(socket);
powerMonitor = require('./api/powerMonitor')(socket);
nativeTheme = require('./api/nativeTheme')(socket);
dock = require('./api/dock')(socket);
if (appApi === undefined) appApi = require('./api/app')(socket, app);
if (browserWindows === undefined) browserWindows = require('./api/browserWindows')(socket, app);
if (commandLine === undefined) commandLine = require('./api/commandLine')(socket, app);
if (autoUpdater === undefined) autoUpdater = require('./api/autoUpdater')(socket);
if (ipc === undefined) ipc = require('./api/ipc')(socket);
if (menu === undefined) menu = require('./api/menu')(socket);
if (dialogApi === undefined) dialogApi = require('./api/dialog')(socket);
if (notification === undefined) notification = require('./api/notification')(socket);
if (tray === undefined) tray = require('./api/tray')(socket);
if (webContents === undefined) webContents = require('./api/webContents')(socket);
if (globalShortcut === undefined) globalShortcut = require('./api/globalShortcut')(socket);
if (shellApi === undefined) shellApi = require('./api/shell')(socket);
if (screen === undefined) screen = require('./api/screen')(socket);
if (clipboard === undefined) clipboard = require('./api/clipboard')(socket);
if (browserView === undefined) browserView = require('./api/browserView')(socket);
if (powerMonitor === undefined) powerMonitor = require('./api/powerMonitor')(socket);
if (nativeTheme === undefined) nativeTheme = require('./api/nativeTheme')(socket);
if (dock === undefined) dock = require('./api/dock')(socket);
socket.on('register-app-open-file-event', (id) => {
electronSocket = socket;