avoid memory leak when re-adding the eventlisteners

This commit is contained in:
Fre
2020-08-21 10:32:02 +02:00
parent e136ed4127
commit d0c92cac85
4 changed files with 31 additions and 9 deletions

View File

@@ -41,6 +41,7 @@ namespace ElectronNET.API
if (_socket == null && !HybridSupport.IsElectronActive)
{
_socket = IO.Socket(new Uri("http://localhost"), new IO.Options { AutoConnect = false });
Console.WriteLine("Electron not active. Socket created.");
}
}
}

View File

@@ -72,21 +72,36 @@ module.exports = (socket) => {
socket.on('autoUpdater-updateConfigPath-set', (value) => {
electron_updater_1.autoUpdater.updateConfigPath = value;
});
socket.on('autoUpdater-currentVersion-get', () => {
electronSocket.emit('autoUpdater-currentVersion-get-reply', electron_updater_1.autoUpdater.currentVersion);
});
socket.on('autoUpdater-channel-get', () => {
electronSocket.emit('autoUpdater-channel-get-reply', electron_updater_1.autoUpdater.channel || '');
});
socket.on('autoUpdater-channel-set', (value) => {
electron_updater_1.autoUpdater.channel = value;
});
socket.on('autoUpdater-requestHeaders-get', () => {
electronSocket.emit('autoUpdater-requestHeaders-get-reply', electron_updater_1.autoUpdater.requestHeaders);
});
socket.on('autoUpdater-requestHeaders-set', (value) => {
electron_updater_1.autoUpdater.requestHeaders = value;
});
// Methods ********
socket.on('autoUpdaterCheckForUpdatesAndNotify', async (guid) => {
const updateCheckResult = await electron_updater_1.autoUpdater.checkForUpdatesAndNotify();
electronSocket.emit('autoUpdaterCheckForUpdatesAndNotifyComplete' + guid, updateCheckResult);
electron_updater_1.autoUpdater.checkForUpdatesAndNotify().then((updateCheckResult) => {
electronSocket.emit('autoUpdaterCheckForUpdatesAndNotifyComplete' + guid, updateCheckResult);
}).catch((error) => {
electronSocket.emit('autoUpdaterCheckForUpdatesAndNotifyError' + guid, error);
});
});
socket.on('autoUpdaterCheckForUpdates', async (guid) => {
// autoUpdater.updateConfigPath = path.join(__dirname, 'dev-app-update.yml');
const updateCheckResult = await electron_updater_1.autoUpdater.checkForUpdates();
electronSocket.emit('autoUpdaterCheckForUpdatesComplete' + guid, updateCheckResult);
electron_updater_1.autoUpdater.checkForUpdates().then((updateCheckResult) => {
electronSocket.emit('autoUpdaterCheckForUpdatesComplete' + guid, updateCheckResult);
}).catch((error) => {
electronSocket.emit('autoUpdaterCheckForUpdatesError' + guid, error);
});
});
socket.on('autoUpdaterQuitAndInstall', async (isSilent, isForceRunAfter) => {
electron_updater_1.autoUpdater.quitAndInstall(isSilent, isForceRunAfter);

File diff suppressed because one or more lines are too long

View File

@@ -1,5 +1,6 @@
const { app } = require('electron');
const { BrowserWindow } = require('electron');
const { ipcMain } = require('electron');
const path = require('path');
const cProcess = require('child_process').spawn;
const portscanner = require('portscanner');
@@ -139,13 +140,18 @@ function startSocketApiBridge(port) {
app['mainWindow'] = null;
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);
socket.removeAllListeners();
//todo fre: added to avoid memory leak when re-adding the eventlisteners
ipcMain.eventNames().forEach(n =>
{
ipcMain.removeAllListeners(n)
});
delete require.cache[require.resolve('./api/app')];
delete require.cache[require.resolve('./api/browserWindows')];
delete require.cache[require.resolve('./api/commandLine')];
@@ -167,8 +173,8 @@ function startSocketApiBridge(port) {
});
global['electronsocket'] = socket;
global['electronsocket'].setMaxListeners(0);
console.log('ASP.NET Core Application connected...', 'global.electronsocket', global['electronsocket'].id, new Date());
//global['electronsocket'].setMaxListeners(0);
console.log('ASP.NET Core Application connected...', 'global.electronsocket', global['electronsocket'].id, socket.listenerCount());
appApi = require('./api/app')(socket, app);
browserWindows = require('./api/browserWindows')(socket, app);