Skip to content

Commit a49e7ab

Browse files
authored
Revert "refactor: delegate quit to main renderer (#191)" (#198)
This reverts commit 9bcc991.
1 parent e3799c8 commit a49e7ab

15 files changed

Lines changed: 171 additions & 156 deletions

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"deploy": "npm run build && electron-builder build --publish always",
1616
"postinstall": "ts-node .erb/scripts/check-native-dep.js && electron-builder install-app-deps && cross-env NODE_ENV=development TS_NODE_TRANSPILE_ONLY=true webpack --config ./.erb/configs/webpack.config.renderer.dev.dll.ts",
1717
"start": "ts-node ./.erb/scripts/check-port-in-use.js && npm run start:renderer",
18-
"start:main": "cross-env NODE_ENV=development electron --inspect=9229 -r ts-node/register/transpile-only ./src/main/main.ts --trace-warnings",
18+
"start:main": "cross-env NODE_ENV=development electron -r ts-node/register/transpile-only ./src/main/main.ts --trace-warnings",
1919
"start:renderer": "cross-env NODE_ENV=development TS_NODE_TRANSPILE_ONLY=true webpack serve --config ./.erb/configs/webpack.config.renderer.dev.ts",
2020
"test": "jest",
2121
"debug-prod-build": "cross-env DEBUG_PROD=true && npm run build",

src/lib/autoupdate/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class AutoUpdate {
9393
ipcMain.handle("quit-and-install", () => {
9494
log.info("recieved quit and install")
9595
global.quitAndInstall = true;
96-
const res = autoUpdater.quitAndInstall();
96+
let res = autoUpdater.quitAndInstall();
9797
log.info("finished quit and install", res)
9898
});
9999
};

src/main/actions/cleanup.js

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
11
const { app, ipcMain } = require("electron");
22

3-
const cleanup = () => {
4-
if(global.backgroundProcessStarted) {
5-
if (global.backgroundWindow) {
6-
global.backgroundWindow.webContents.send("shutdown");
7-
} else {
8-
// No backgroundWindow. Quit directly without cleanup
3+
export const cleanupAndQuit = () => {
4+
cleanup();
5+
6+
if (global.backgroundWindow) {
7+
ipcMain.on("shutdown-success", () => {
8+
console.log("shudown sucess");
99
app.quit();
10-
}
10+
});
11+
}
12+
};
13+
14+
const cleanup = () => {
15+
if (global.backgroundWindow) {
16+
global.backgroundWindow.webContents.send("shutdown");
1117
} else {
18+
// No backgroundWindow. Quit directly without cleanup
1219
app.quit();
1320
}
1421
};
15-
16-
17-
export const getReadyToQuitApp = async () => {
18-
return new Promise((resolve) => {
19-
cleanup();
20-
21-
if (global.backgroundWindow) {
22-
ipcMain.once("shutdown-success", () => {
23-
console.log("shudown sucess");
24-
global.backgroundWindow?.close();
25-
resolve()
26-
});
27-
}
28-
})
29-
};

src/main/actions/logNetworkRequest.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
/**
2+
* @param {Array} array into which element will be pushed
3+
* @param {number} required max length of array
4+
* @param {*} element to push
5+
*/
6+
const addToLog = (array, limit, element) => {
7+
const arrayToModify = [...array];
8+
if (arrayToModify.length >= limit) {
9+
arrayToModify.shift();
10+
}
11+
arrayToModify.push(element);
12+
return arrayToModify;
13+
};
14+
115
const logNetworkRequest = (event, message, webAppWindow) => {
216
const newLog = message;
317
if (webAppWindow) {

src/main/actions/logNetworkRequestV2.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1+
/**
2+
* @param {Array} array into which element will be pushed
3+
* @param {number} required max length of array
4+
* @param {*} element to push
5+
*/
6+
const addToLog = (array, limit, element) => {
7+
const arrayToModify = [...array];
8+
if (arrayToModify.length >= limit) {
9+
arrayToModify.shift();
10+
}
11+
arrayToModify.push(element);
12+
return arrayToModify;
13+
};
14+
115
const logNetworkRequestV2 = (event, message, webAppWindow) => {
216
const newLog = message;
3-
if (webAppWindow && !webAppWindow.isDestroyed()) {
17+
if (webAppWindow) {
418
webAppWindow.send("log-network-request-v2", newLog);
519
}
620
};

src/main/actions/startBackgroundProcess.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ const startBackgroundProcess = async () => {
7979
resolve(true);
8080
});
8181

82+
backgroundWindow.on("close", (event) => {
83+
if (global.isQuitActionConfirmed) {
84+
console.log("BG quitting");
85+
return;
86+
}
87+
88+
event.preventDefault();
89+
});
8290
});
8391
};
8492

src/main/events.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ function removeFileFromAccessRecords(filePath) {
6868

6969
// These events do not require the browser window
7070
export const registerMainProcessEvents = () => {
71-
ipcMain.on("background-process-started", () => {
72-
global.backgroundProcessStarted = true;
73-
});
7471
ipcMain.handle("start-background-process", startBackgroundProcess);
7572
/** Main Process State Management */
7673
ipcMain.handle("get-state", getState);
@@ -111,32 +108,32 @@ export const registerMainProcessEventsForWebAppWindow = (webAppWindow) => {
111108

112109
// Open handle for async source detection
113110
ipcMain.handle("app-detected", async (event, payload) => {
114-
webAppWindow?.send("app-detected", payload);
111+
webAppWindow.send("app-detected", payload);
115112
});
116113

117114
ipcMain.handle("browser-connected", async (event, payload) => {
118-
webAppWindow?.send("browser-connected", payload);
115+
webAppWindow.send("browser-connected", payload);
119116
});
120117

121118
ipcMain.handle("browser-disconnected", async (event, payload) => {
122-
webAppWindow?.send("browser-disconnected", payload);
119+
webAppWindow.send("browser-disconnected", payload);
123120
});
124121

125122
// Open handle for async browser close
126123
ipcMain.handle("browser-closed", async (event, payload) => {
127-
webAppWindow?.send("browser-closed", payload);
124+
webAppWindow.send("browser-closed", payload);
128125
});
129126

130127
// Open handle for async browser close
131128
ipcMain.handle("proxy-restarted", async (event, payload) => {
132129
createOrUpdateAxiosInstance(payload);
133-
webAppWindow?.send("proxy-restarted", payload);
130+
webAppWindow.send("proxy-restarted", payload);
134131
});
135132

136133
// hacky implementation for syncing addition and deletion
137134
const resendAllNetworkLogs = async () => {
138135
const res = await getAllNetworkSessions();
139-
webAppWindow?.send("network-sessions-updated", res);
136+
webAppWindow.send("network-sessions-updated", res);
140137
};
141138

142139
ipcMain.handle("get-all-network-sessions", async () => {
@@ -259,7 +256,7 @@ export const registerMainProcessEventsForWebAppWindow = (webAppWindow) => {
259256
});
260257

261258
ipcMain.handle("helper-server-hit", () => {
262-
webAppWindow?.send("helper-server-hit");
259+
webAppWindow.send("helper-server-hit");
263260
});
264261
};
265262

src/main/main.ts

Lines changed: 64 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import "core-js/stable";
1313
import "regenerator-runtime/runtime";
1414
import path from "path";
15-
import { app, BrowserWindow, shell, dialog, Tray, Menu, clipboard, ipcMain } from "electron";
15+
import { app, BrowserWindow, shell, dialog, Tray, Menu, clipboard } from "electron";
1616
import log from "electron-log";
1717
import MenuBuilder from "./menu";
1818
import {
@@ -24,7 +24,9 @@ import {
2424
/** Storage - State */
2525
import "./actions/initGlobalState";
2626
import AutoUpdate from "../lib/autoupdate";
27-
import { getReadyToQuitApp } from "./actions/cleanup";
27+
import { cleanupAndQuit } from "./actions/cleanup";
28+
import { trackEventViaWebApp } from "./actions/events";
29+
import EVENTS from "./actions/events/constants";
2830
import fs from "fs";
2931
import logger from "../utils/logger";
3032
import { setupIPCForwardingToWebApp } from "./actions/setupIPCForwarding";
@@ -83,7 +85,7 @@ export default function createTrayMenu(ip?: string, port?: number) {
8385
{
8486
label: "Show Requestly",
8587
click: () => {
86-
if(webAppWindow && !webAppWindow.isDestroyed()) {
88+
if(webAppWindow) {
8789
if(webAppWindow.isMinimized()) {
8890
webAppWindow.restore()
8991
}
@@ -149,7 +151,7 @@ export default function createTrayMenu(ip?: string, port?: number) {
149151
{
150152
label: "Quit",
151153
click: () => {
152-
webAppWindow?.close();
154+
app.quit();
153155
},
154156
},
155157
]
@@ -168,7 +170,7 @@ export default function createTrayMenu(ip?: string, port?: number) {
168170
tray.setContextMenu(trayMenu);
169171
}
170172

171-
let closingAccepted = false
173+
172174
const createWindow = async () => {
173175
if (isDevelopment) {
174176
await installExtensions();
@@ -256,19 +258,63 @@ const createWindow = async () => {
256258
}
257259
});
258260

259-
webAppWindow.on('close', async (event) => {
260-
if(!closingAccepted) {
261-
event.preventDefault();
262-
webAppWindow?.webContents.send("initiate-app-close")
261+
// webAppWindow.on('closed', () => {
262+
// webAppWindow = null;
263+
// });
264+
265+
webAppWindow.on("close", (e) => {
266+
// Check if user has already asked to Quit app from here or somewhere else
267+
// @ts-expect-error
268+
if (global.isQuitActionConfirmed) {
269+
saveCookies();
270+
app.quit();
271+
return;
263272
}
264-
})
265273

266-
webAppWindow.on('closed', async () => {
267-
saveCookies();
268-
await getReadyToQuitApp();
269-
webAppWindow = null;
270-
return;
271-
})
274+
if (webAppWindow) {
275+
let message =
276+
"Do you really want to quit? This would also stop the proxy server.";
277+
278+
// @ts-expect-error
279+
if (global.quitAndInstall) {
280+
message = "Confirm to restart & install update";
281+
// @ts-expect-error
282+
global.quitAndInstall = false;
283+
}
284+
285+
const choice = dialog.showMessageBoxSync(webAppWindow, {
286+
type: "question",
287+
buttons: ["Yes, quit Requestly", "Minimize instead", "Cancel"],
288+
title: "Quit Requestly",
289+
message: message,
290+
});
291+
292+
switch (choice) {
293+
// If Quit is clicked
294+
case 0:
295+
// Set flag to check next iteration
296+
trackEventViaWebApp(webAppWindow, EVENTS.QUIT_APP)
297+
// @ts-expect-error
298+
global.isQuitActionConfirmed = true;
299+
// Calling app.quit() would again invoke this function
300+
e.preventDefault();
301+
cleanupAndQuit();
302+
break;
303+
// If Minimize is clicked
304+
case 1:
305+
webAppWindow.minimize();
306+
e.preventDefault();
307+
break;
308+
// If cancel is clicked
309+
case 2:
310+
e.preventDefault();
311+
break;
312+
default:
313+
break;
314+
}
315+
}
316+
});
317+
272318
const enableBGWindowDebug = () => {
273319
// Show bg window and toggle the devtools
274320
try {
@@ -322,7 +368,7 @@ function handleCustomProtocolURL(urlString: string) {
322368

323369
// custom protocol (requestly) handler
324370
app.on("open-url", (_event, rqUrl) => {
325-
if(webAppWindow && !webAppWindow.isDestroyed()) {
371+
if(webAppWindow) {
326372
handleCustomProtocolURL(rqUrl)
327373
} else {
328374
onWebAppReadyHandlers.push(() => handleCustomProtocolURL(rqUrl))
@@ -355,7 +401,7 @@ async function handleFileOpen(filePath: string, webAppWindow?: BrowserWindow) {
355401

356402
app.on('open-file', async (event, filePath) => {
357403
event.preventDefault();
358-
if(webAppWindow && !webAppWindow.isDestroyed()) {
404+
if(webAppWindow) {
359405
handleFileOpen(filePath, webAppWindow);
360406
} else {
361407
logger.log("webAppWindow not ready")
@@ -438,24 +484,3 @@ app
438484
.catch((err) => {
439485
console.log(err);
440486
});
441-
442-
ipcMain.handle("quit-app", (_event) => {
443-
closingAccepted = true
444-
webAppWindow?.close();
445-
})
446-
447-
app.on("before-quit", () => {
448-
// cleanup when quitting has been finalised
449-
ipcMain.removeAllListeners();
450-
webAppWindow?.removeAllListeners();
451-
// @ts-expect-error BrowserWindow types are not being enforced for this variable
452-
backgroundWindow?.removeAllListeners();
453-
454-
ipcMain.removeAllListeners();
455-
process.on('uncaughtException', (err) => {
456-
logger.error('Unhandled Exception while quitting:', err);
457-
});
458-
process.on('unhandledRejection', (err) => {
459-
logger.error('Unhandled Rejection while quitting:', err);
460-
});
461-
})

src/main/menu.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
app,
23
Menu,
34
shell,
45
BrowserWindow,
@@ -12,7 +13,6 @@ interface DarwinMenuItemConstructorOptions extends MenuItemConstructorOptions {
1213

1314
export default class MenuBuilder {
1415
mainWindow: BrowserWindow;
15-
1616
enableBGWindowDebug: Function;
1717

1818
constructor(mainWindow: BrowserWindow, enableBGWindowDebug: Function) {
@@ -79,7 +79,10 @@ export default class MenuBuilder {
7979
label: "Quit",
8080
accelerator: "Command+Q",
8181
click: () => {
82-
return this.mainWindow?.close();
82+
if (this.mainWindow) {
83+
return this.mainWindow.close();
84+
}
85+
app.quit();
8386
},
8487
},
8588
],
@@ -286,7 +289,10 @@ export default class MenuBuilder {
286289
label: "&Close",
287290
accelerator: "Ctrl+W",
288291
click: () => {
289-
return this.mainWindow?.close();
292+
if (this.mainWindow) {
293+
return this.mainWindow.close();
294+
}
295+
app.quit();
290296
},
291297
},
292298
],

0 commit comments

Comments
 (0)