Electron-228 - Changed the event name and optimized code

This commit is contained in:
kiranniranjan 2018-01-18 14:55:11 +05:30
parent 20e1f0b6ca
commit ad700f2d82
3 changed files with 54 additions and 50 deletions

View File

@ -16,7 +16,7 @@ const cmds = keyMirror({
registerProtocolHandler: null,
registerActivityDetection: null,
showNotificationSettings: null,
reload: null
sanitize: null
});
module.exports = {

View File

@ -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...

View File

@ -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();
}