1212import "core-js/stable" ;
1313import "regenerator-runtime/runtime" ;
1414import path from "path" ;
15- import { app , BrowserWindow , shell , dialog , Tray , Menu , clipboard } from "electron" ;
15+ import { app , BrowserWindow , shell , dialog , Tray , Menu , clipboard , ipcMain } from "electron" ;
1616import log from "electron-log" ;
1717import MenuBuilder from "./menu" ;
1818import {
@@ -24,9 +24,7 @@ import {
2424/** Storage - State */
2525import "./actions/initGlobalState" ;
2626import AutoUpdate from "../lib/autoupdate" ;
27- import { cleanupAndQuit } from "./actions/cleanup" ;
28- import { trackEventViaWebApp } from "./actions/events" ;
29- import EVENTS from "./actions/events/constants" ;
27+ import { getReadyToQuitApp } from "./actions/cleanup" ;
3028import fs from "fs" ;
3129import logger from "../utils/logger" ;
3230import { setupIPCForwardingToWebApp } from "./actions/setupIPCForwarding" ;
@@ -85,7 +83,7 @@ export default function createTrayMenu(ip?: string, port?: number) {
8583 {
8684 label : "Show Requestly" ,
8785 click : ( ) => {
88- if ( webAppWindow ) {
86+ if ( webAppWindow && ! webAppWindow . isDestroyed ( ) ) {
8987 if ( webAppWindow . isMinimized ( ) ) {
9088 webAppWindow . restore ( )
9189 }
@@ -151,7 +149,7 @@ export default function createTrayMenu(ip?: string, port?: number) {
151149 {
152150 label : "Quit" ,
153151 click : ( ) => {
154- app . quit ( ) ;
152+ webAppWindow ?. close ( ) ;
155153 } ,
156154 } ,
157155 ]
@@ -170,7 +168,7 @@ export default function createTrayMenu(ip?: string, port?: number) {
170168 tray . setContextMenu ( trayMenu ) ;
171169}
172170
173-
171+ let closingAccepted = false
174172const createWindow = async ( ) => {
175173 if ( isDevelopment ) {
176174 await installExtensions ( ) ;
@@ -258,63 +256,19 @@ const createWindow = async () => {
258256 }
259257 } ) ;
260258
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 ;
272- }
273-
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- }
259+ webAppWindow . on ( 'close' , async ( event ) => {
260+ if ( ! closingAccepted ) {
261+ event . preventDefault ( ) ;
262+ webAppWindow ?. webContents . send ( "initiate-app-close" )
315263 }
316- } ) ;
264+ } )
317265
266+ webAppWindow . on ( 'closed' , async ( ) => {
267+ saveCookies ( ) ;
268+ await getReadyToQuitApp ( ) ;
269+ webAppWindow = null ;
270+ return ;
271+ } )
318272 const enableBGWindowDebug = ( ) => {
319273 // Show bg window and toggle the devtools
320274 try {
@@ -368,7 +322,7 @@ function handleCustomProtocolURL(urlString: string) {
368322
369323// custom protocol (requestly) handler
370324app . on ( "open-url" , ( _event , rqUrl ) => {
371- if ( webAppWindow ) {
325+ if ( webAppWindow && ! webAppWindow . isDestroyed ( ) ) {
372326 handleCustomProtocolURL ( rqUrl )
373327 } else {
374328 onWebAppReadyHandlers . push ( ( ) => handleCustomProtocolURL ( rqUrl ) )
@@ -401,7 +355,7 @@ async function handleFileOpen(filePath: string, webAppWindow?: BrowserWindow) {
401355
402356app . on ( 'open-file' , async ( event , filePath ) => {
403357 event . preventDefault ( ) ;
404- if ( webAppWindow ) {
358+ if ( webAppWindow && ! webAppWindow . isDestroyed ( ) ) {
405359 handleFileOpen ( filePath , webAppWindow ) ;
406360 } else {
407361 logger . log ( "webAppWindow not ready" )
492446 . catch ( ( err ) => {
493447 console . log ( err ) ;
494448 } ) ;
449+
450+ ipcMain . handle ( "quit-app" , ( _event ) => {
451+ closingAccepted = true
452+ webAppWindow ?. close ( ) ;
453+ } )
454+
455+ app . on ( "before-quit" , ( ) => {
456+ // cleanup when quitting has been finalised
457+ ipcMain . removeAllListeners ( ) ;
458+ webAppWindow ?. removeAllListeners ( ) ;
459+ // @ts -expect-error BrowserWindow types are not being enforced for this variable
460+ backgroundWindow ?. removeAllListeners ( ) ;
461+
462+ ipcMain . removeAllListeners ( ) ;
463+ process . on ( 'uncaughtException' , ( err ) => {
464+ logger . error ( 'Unhandled Exception while quitting:' , err ) ;
465+ } ) ;
466+ process . on ( 'unhandledRejection' , ( err ) => {
467+ logger . error ( 'Unhandled Rejection while quitting:' , err ) ;
468+ } ) ;
469+ } )
0 commit comments