From 20e1f0b6ca6371183ec7e0862f24bf2d0cf6bf7c Mon Sep 17 00:00:00 2001 From: kiranniranjan Date: Thu, 18 Jan 2018 12:53:27 +0530 Subject: [PATCH] Electron-228 - Added an event to terminate screen snippet process whenever the main window is reloaded/navigated --- js/enums/api.js | 1 + js/mainApiMgr.js | 24 ++++++++++++++++++++++++ js/preload/preloadMain.js | 10 +++++----- js/screenSnippet/index.js | 8 ++++++++ 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/js/enums/api.js b/js/enums/api.js index e03bb946..1133fabd 100644 --- a/js/enums/api.js +++ b/js/enums/api.js @@ -16,6 +16,7 @@ const cmds = keyMirror({ registerProtocolHandler: null, registerActivityDetection: null, showNotificationSettings: null, + reload: null }); module.exports = { diff --git a/js/mainApiMgr.js b/js/mainApiMgr.js index 92706004..4570af86 100644 --- a/js/mainApiMgr.js +++ b/js/mainApiMgr.js @@ -13,6 +13,8 @@ const activityDetection = require('./activityDetection'); const badgeCount = require('./badgeCount.js'); const protocolHandler = require('./protocolHandler'); const configureNotification = require('./notify/settings/configure-notification-position'); +const eventEmitter = require('./eventEmitter'); +const { isMac } = require('./utils/misc'); const apiEnums = require('./enums/api.js'); const apiCmds = apiEnums.cmds; @@ -47,6 +49,24 @@ function isValidWindow(event) { return result; } +/** + * Method that is invoked when the application is reloaded/navigated + * window.addEventListener('beforeunload') + * @param windowName + */ +function reload(windowName) { + // To make sure the reload event is from the main window + if (windowMgr.getMainWindow() && windowName === windowMgr.getMainWindow().winName) { + // reset the badge count whenever an user refreshes the electron client + badgeCount.show(0); + + // Terminates the screen snippet process on reload + if (!isMac) { + eventEmitter.emit('killScreenSnippet'); + } + } +} + /** * Handle API related ipc messages from renderers. Only messages from windows * we have created are allowed. @@ -103,6 +123,10 @@ electron.ipcMain.on(apiName, (event, arg) => { if (arg.cmd === apiCmds.showNotificationSettings && typeof arg.windowName === 'string') { configureNotification.openConfigurationWindow(arg.windowName); } + + if (arg.cmd === apiCmds.reload && typeof arg.windowName === 'string') { + reload(arg.windowName); + } }); // expose these methods primarily for testing... diff --git a/js/preload/preloadMain.js b/js/preload/preloadMain.js index da43694d..033b72ec 100644 --- a/js/preload/preloadMain.js +++ b/js/preload/preloadMain.js @@ -367,17 +367,17 @@ function createAPI() { }); } - // reset the badge count whenever an user refreshes the electron client - function resetBadgeCount() { + // Invoked whenever the app is reloaded/navigated + function reload() { local.ipcRenderer.send(apiName, { - cmd: apiCmds.setBadgeCount, - count: 0 + cmd: apiCmds.reload, + windowName: window.name }); } window.addEventListener('offline', updateOnlineStatus, false); window.addEventListener('online', updateOnlineStatus, false); - window.addEventListener('beforeunload', resetBadgeCount, false); + window.addEventListener('beforeunload', reload, false); updateOnlineStatus(); } \ No newline at end of file diff --git a/js/screenSnippet/index.js b/js/screenSnippet/index.js index ed64d305..608a413f 100644 --- a/js/screenSnippet/index.js +++ b/js/screenSnippet/index.js @@ -181,6 +181,14 @@ function createWarn(msg) { } /* eslint-enable class-methods-use-this */ +// terminates the screen snippet process wherever the +// main window is reloaded/navigated +eventEmitter.on('killScreenSnippet', function () { + if (child) { + child.kill(); + } +}); + module.exports = { ScreenSnippet: ScreenSnippet, // note: readResult only exposed for testing purposes