Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't check twice for single instance (WEBAPP-6663) #3856

Merged
merged 2 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion electron/src/lib/LocalAccountDeletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const LOG_DIR = path.join(USER_DATA_DIR, 'logs');
const logger = getLogger(path.basename(__filename));

const clearStorage = async (session: Session): Promise<void> => {
await session.clearStorageData();
session.clearStorageData();
await session.clearCache();
session.flushStorageData();
};
Expand Down
10 changes: 6 additions & 4 deletions electron/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,9 @@ const showMainWindow = async (mainWindowState: WindowStateKeeper.State) => {

// App Events
const handleAppEvents = () => {
app.on('window-all-closed', () => {
app.on('window-all-closed', async () => {
if (!EnvironmentUtil.platform.IS_MAC_OS) {
lifecycle.quit();
await lifecycle.quit();
}
});

Expand Down Expand Up @@ -599,8 +599,10 @@ class ElectronWrapperInit {
customProtocolHandler.registerCoreProtocol();
Raygun.initClient();
handlePortableFlags();
lifecycle.checkSingleInstance();
lifecycle.checkForUpdate().catch(error => logger.error(error));
lifecycle
.checkSingleInstance()
.then(() => lifecycle.initSquirrelListener())
.catch(error => logger.error(error));

// Stop further execution on update to prevent second tray icon
if (lifecycle.isFirstInstance) {
Expand Down
4 changes: 2 additions & 2 deletions electron/src/menu/TrayHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import * as path from 'path';

import * as locale from '../locale/locale';
import {linuxDesktop, platform} from '../runtime/EnvironmentUtil';
import {quit as lifecycleQuit} from '../runtime/lifecycle';
import * as lifecycle from '../runtime/lifecycle';
import {config} from '../settings/config';
import {WindowManager} from '../window/WindowManager';

Expand Down Expand Up @@ -81,7 +81,7 @@ export class TrayHandler {
label: locale.getText('trayOpen'),
},
{
click: () => lifecycleQuit(),
click: () => lifecycle.quit(),
label: locale.getText('trayQuit'),
},
]);
Expand Down
25 changes: 15 additions & 10 deletions electron/src/runtime/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,24 @@ const logger = getLogger(path.basename(__filename));

export let isFirstInstance: boolean | undefined = undefined;

export async function checkForUpdate(): Promise<void> {
export async function initSquirrelListener(): Promise<void> {
if (EnvironmentUtil.platform.IS_WINDOWS) {
logger.info('Checking for Windows update ...');
await Squirrel.handleSquirrelEvent(isFirstInstance);
await Squirrel.handleSquirrelArgs();

ipcMain.on(EVENT_TYPE.WRAPPER.UPDATE, () => Squirrel.installUpdate());
}
}

export const checkSingleInstance = () => {
export const checkSingleInstance = async () => {
if (process.mas) {
isFirstInstance = true;
} else {
isFirstInstance = app.requestSingleInstanceLock();
logger.info('Checking if we are the first instance ...', isFirstInstance);

if (!EnvironmentUtil.platform.IS_WINDOWS && !isFirstInstance) {
quit();
await quit(false);
} else {
app.on('second-instance', () => WindowManager.showPrimaryWindow());
}
Expand All @@ -68,20 +68,25 @@ export const getWebViewId = (contents: WebContents): string | undefined => {

// Using exit instead of quit for the time being
// see: https://github.com/electron/electron/issues/8862#issuecomment-294303518
export const quit = (): void => {
export const quit = async (clearCache = true): Promise<void> => {
logger.info('Initiating app quit ...');
settings.persistToFile();

logger.info('Clear cache ...');
if (session.defaultSession) {
session.defaultSession.clearCache().catch(error => logger.error(error));
if (clearCache) {
logger.info('Clearing cache ...');

try {
await session.defaultSession?.clearCache();
} catch (error) {
logger.error(error);
}
}

logger.info('Exiting ...');
app.exit();
};

export const relaunch = () => {
export const relaunch = async () => {
logger.info('Relaunching the app ...');
if (EnvironmentUtil.platform.IS_MAC_OS) {
/*
Expand All @@ -92,6 +97,6 @@ export const relaunch = () => {
WindowManager.sendActionToPrimaryWindow(EVENT_TYPE.WRAPPER.RELOAD);
} else {
app.relaunch();
quit();
await quit();
}
};
22 changes: 9 additions & 13 deletions electron/src/update/squirrel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,37 +139,33 @@ async function scheduleUpdate(): Promise<void> {
setInterval(installUpdate, everyCheck);
}

export async function handleSquirrelEvent(isFirstInstance?: boolean): Promise<boolean | void> {
export async function handleSquirrelArgs(): Promise<void> {
const squirrelEvent = process.argv[1];

switch (squirrelEvent) {
case SQUIRREL_EVENT.INSTALL: {
await createStartShortcut();
await createDesktopShortcut();
lifecycle.quit();
return true;
await lifecycle.quit();
return;
}

case SQUIRREL_EVENT.UPDATED: {
lifecycle.quit();
return true;
await lifecycle.quit();
return;
}

case SQUIRREL_EVENT.UNINSTALL: {
await removeShortcuts();
lifecycle.quit();
return true;
await lifecycle.quit();
return;
}

case SQUIRREL_EVENT.OBSOLETE: {
lifecycle.quit();
return true;
await lifecycle.quit();
return;
}
}

if (!isFirstInstance) {
return lifecycle.quit();
}

await scheduleUpdate();
}