From 7dbcd6767d449836f2ebeb64e744b1c7902b2de2 Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Mon, 22 Feb 2021 18:24:35 +0100 Subject: [PATCH] Be sure to not send stuff to webContent if it is destroyed... --- instance/log.ejs | 6 ++-- main.js | 81 +++++++++++++++++++++++++++--------------------- package.json | 2 +- 3 files changed, 49 insertions(+), 40 deletions(-) diff --git a/instance/log.ejs b/instance/log.ejs index 2e4d734..e3068e5 100644 --- a/instance/log.ejs +++ b/instance/log.ejs @@ -12,12 +12,10 @@ <link rel="stylesheet" href="../style/bootstrap-4.5.2-dist/css/bootstrap.min.css" /> <link rel="stylesheet" href="../style/css/instance.css" /> <link rel="stylesheet" href="../style/fontawesome-free-5.15.1-web/css/all.min.css" /> - <script src="./main.js"></script> + <script src="./log.js"></script> <script></script> </head> - <body> - <div id="mainFrame" class="container-fluid"> - </div> + <body id="body"> </body> </html> diff --git a/main.js b/main.js index 2ac27c7..0007559 100644 --- a/main.js +++ b/main.js @@ -115,6 +115,17 @@ function createInstanceWindow() { win.once('ready-to-show', () => win.show()); } +function createDevToolsWindow() { + var focused = BrowserWindow.getFocusedWindow(); + if (focused) { + if (!focused.isDevToolsOpened()) { + focused.webContents.openDevTools({ mode: 'detach' }); + } else { + focused.webContents.closeDevTools(); + } + } +} + function createLogWindow() { /* Log window */ const opt = defaultWindowOptions('Amadeus - Log window'); @@ -185,27 +196,8 @@ app.on('ready', () => { }); }); - globalShortcut.register('CommandOrControl+D', () => { - var win = BrowserWindow.getFocusedWindow(); - if (win === null) { - return; - } - logger.info('main', 'Reloading DB'); - let contents = win.webContents; - db.all().then(karas => { - contents.send('reload-db-responce', karas); - }); - }); - globalShortcut.register('F12', () => { - var focused = BrowserWindow.getFocusedWindow(); - if (focused) { - if (!focused.isDevToolsOpened()) { - focused.webContents.openDevTools({ mode: 'detach' }); - } else { - focused.webContents.closeDevTools(); - } - } - }); + globalShortcut.register('CommandOrControl+F2', () => createLogWindow()); + globalShortcut.register('CommandOrControl+F12', () => createDevToolsWindow()); }); /*************** @@ -294,7 +286,14 @@ ipcMain.on('verify-lektord', (event, arg) => { /* Install the clear function before registering all timers */ win.on('close', () => { - logger.info('main', 'Close instance window, '); + logger.info('main', 'Close instance window'); + clearTimers(); + }); + + var destroyed = false; + win.webContents.on('destroyed', () => { + logger.debug('main', 'WebContent has been destroyed'); + destroyed = true; clearTimers(); }); @@ -303,17 +302,23 @@ ipcMain.on('verify-lektord', (event, arg) => { loopTimeouts.concat([ setTimeout(() => lkt.idleActualisation(), 1000), setTimeout(() => lkt.statusActualisation(), 1500), - setTimeout(() => db.queueAll().then(karas => win.webContents.send('reload-queue-responce', karas)), 1000), + setTimeout(() => { + if (destroyed) return; + db.queueAll().then(karas => win.webContents.send('reload-queue-responce', karas)); + }, 1000), ]); loopIntervals.concat([ setInterval(() => { + if (destroyed) return; if (lkt.isQueueUpdated()) { lkt.setQueueUpdated(false); db.queueAll().then(karas => win.webContents.send('reload-queue-responce', karas)); } }, 50), + /* Position in the kara changed? */ setInterval(() => { + if (destroyed) return; var newSongTimeData = lkt.getSongTimeData(); if (newSongTimeData.elapsed != songTimeData.elapsed || newSongTimeData.song != songTimeData.song) { songTimeData = newSongTimeData; @@ -341,19 +346,25 @@ ipcMain.on('verify-lektord', (event, arg) => { }); } }, 50), - setInterval( - () => - isRunning({ win: 'klkt.exe', mac: 'klkt', linux: 'klkt' }).then(vklkt => - isRunning({ win: 'lektord.exe', mac: 'lektord', linux: 'lektord' }).then(vlektord => - win.webContents.send('send-runnings', { - klkt: vklkt, - lektord: vlektord, - }) - ) - ), - 1000 - ), + /* Get running processes */ + setInterval(() => { + if (destroyed) return; + isRunning({ win: 'klkt.exe', mac: 'klkt', linux: 'klkt' }) + .then(vklkt => + isRunning({ win: 'lektord.exe', mac: 'lektord', linux: 'lektord' }) + .then(vlektord => + win.webContents.send('send-runnings', { + klkt: vklkt, + lektord: vlektord, + }) + ) + .catch(() => logger.error('main', "Can't know if lektord is running")) + ) + .catch(() => logger.error('main', "Can't know if klkt is running")); + }, 1000), + /* Has the status of lektord changed? */ setInterval(() => { + if (destroyed) return; if (lkt.isStatusUpdated()) { win.webContents.send('send-state', lkt.getStatus().state); } diff --git a/package.json b/package.json index 8f06963..9f78d99 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ ], "license": "ISC", "scripts": { - "start": "electron --trace-uncaught ." + "start": "electron -- --trace-uncaught . --trace-uncaught" }, "dependencies": { "ejs": "^3.1.5", -- GitLab