From ad700f2d8287b9fb1b8fff302fda33b56e14cea3 Mon Sep 17 00:00:00 2001 From: kiranniranjan Date: Thu, 18 Jan 2018 14:55:11 +0530 Subject: [PATCH] Electron-228 - Changed the event name and optimized code --- js/enums/api.js | 2 +- js/mainApiMgr.js | 96 ++++++++++++++++++++------------------- js/preload/preloadMain.js | 6 +-- 3 files changed, 54 insertions(+), 50 deletions(-) diff --git a/js/enums/api.js b/js/enums/api.js index 1133fabd..7d139129 100644 --- a/js/enums/api.js +++ b/js/enums/api.js @@ -16,7 +16,7 @@ const cmds = keyMirror({ registerProtocolHandler: null, registerActivityDetection: null, showNotificationSettings: null, - reload: null + sanitize: null }); module.exports = { diff --git a/js/mainApiMgr.js b/js/mainApiMgr.js index 4570af86..4473165f 100644 --- a/js/mainApiMgr.js +++ b/js/mainApiMgr.js @@ -54,7 +54,7 @@ function isValidWindow(event) { * window.addEventListener('beforeunload') * @param windowName */ -function reload(windowName) { +function sanitize(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 @@ -80,53 +80,57 @@ electron.ipcMain.on(apiName, (event, arg) => { return; } - if (arg.cmd === apiCmds.isOnline && typeof arg.isOnline === 'boolean') { - windowMgr.setIsOnline(arg.isOnline); - return; + switch(arg.cmd) { + case apiCmds.isOnline: + if (typeof arg.isOnline === 'boolean') { + windowMgr.setIsOnline(arg.isOnline); + } + break; + case apiCmds.setBadgeCount: + if (typeof arg.count === 'number') { + badgeCount.show(arg.count); + } + break; + case apiCmds.registerProtocolHandler: + protocolHandler.setProtocolWindow(event.sender); + protocolHandler.checkProtocolAction(); + break; + case apiCmds.badgeDataUrl: + if (typeof arg.dataUrl === 'string' && typeof arg.count === 'number') { + badgeCount.setDataUrl(arg.dataUrl, arg.count); + } + break; + case apiCmds.activate: + if (typeof arg.windowName === 'string') { + windowMgr.activate(arg.windowName); + } + break; + case apiCmds.registerBoundsChange: + windowMgr.setBoundsChangeWindow(event.sender); + break; + case apiCmds.registerLogger: + // renderer window that has a registered logger from JS. + log.setLogWindow(event.sender); + break; + case apiCmds.registerActivityDetection: + if (typeof arg.period === 'number') { + // renderer window that has a registered activity detection from JS. + activityDetection.setActivityWindow(arg.period, event.sender); + } + break; + case apiCmds.showNotificationSettings: + if (typeof arg.windowName === 'string') { + configureNotification.openConfigurationWindow(arg.windowName); + } + break; + case apiCmds.sanitize: + if (typeof arg.windowName === 'string') { + sanitize(arg.windowName); + } + break; + default: } - if (arg.cmd === apiCmds.setBadgeCount && typeof arg.count === 'number') { - badgeCount.show(arg.count); - return; - } - - if (arg.cmd === apiCmds.registerProtocolHandler) { - protocolHandler.setProtocolWindow(event.sender); - protocolHandler.checkProtocolAction(); - } - - if (arg.cmd === apiCmds.badgeDataUrl && typeof arg.dataUrl === 'string' && - typeof arg.count === 'number') { - badgeCount.setDataUrl(arg.dataUrl, arg.count); - return; - } - - if (arg.cmd === apiCmds.activate && typeof arg.windowName === 'string') { - windowMgr.activate(arg.windowName); - return; - } - - if (arg.cmd === apiCmds.registerBoundsChange) { - windowMgr.setBoundsChangeWindow(event.sender); - } - - if (arg.cmd === apiCmds.registerLogger) { - // renderer window that has a registered logger from JS. - log.setLogWindow(event.sender); - } - - if (arg.cmd === apiCmds.registerActivityDetection) { - // renderer window that has a registered activity detection from JS. - activityDetection.setActivityWindow(arg.period, event.sender); - } - - 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 033b72ec..0c4a42a7 100644 --- a/js/preload/preloadMain.js +++ b/js/preload/preloadMain.js @@ -368,16 +368,16 @@ function createAPI() { } // Invoked whenever the app is reloaded/navigated - function reload() { + function sanitize() { local.ipcRenderer.send(apiName, { - cmd: apiCmds.reload, + cmd: apiCmds.sanitize, windowName: window.name }); } window.addEventListener('offline', updateOnlineStatus, false); window.addEventListener('online', updateOnlineStatus, false); - window.addEventListener('beforeunload', reload, false); + window.addEventListener('beforeunload', sanitize, false); updateOnlineStatus(); } \ No newline at end of file