diff --git a/public/electron.js b/public/electron.js index d67c0f2d4..1a293579a 100644 --- a/public/electron.js +++ b/public/electron.js @@ -38,7 +38,6 @@ const lndArgs = process.argv.filter(a => // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. let win; -let lndProcess; let btcdProcess; log.transports.console.level = 'info'; @@ -111,6 +110,13 @@ function createWindow() { ); } + win.on('close', e => { + if (win) { + e.preventDefault(); + win.webContents.send('app-close'); + } + }); + // Emitted when the window is closed. win.on('closed', () => { // Dereference the window object, usually you would store windows @@ -135,7 +141,7 @@ const startLnd = async () => { btcdSettingsDir, miningAddress: BTCD_MINING_ADDRESS, }); - lndProcess = await startLndProcess({ + startLndProcess({ isDev, lndSettingsDir, lndPort: LND_PORT, @@ -179,13 +185,6 @@ app.on('ready', () => { startLnd(); }); -// Quit when all windows are closed. -app.on('window-all-closed', () => { - // On macOS it is common for applications and their menu bar - // to stay active until the user quits explicitly with Cmd + Q - app.quit(); -}); - app.on('activate', () => { // On macOS it's common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. @@ -194,8 +193,12 @@ app.on('activate', () => { } }); +ipcMain.on('app-close', () => { + win = null; + app.quit(); +}); + app.on('quit', () => { - lndProcess && lndProcess.kill(); btcdProcess && btcdProcess.kill(); }); diff --git a/public/grpc-client.js b/public/grpc-client.js index 4f22db305..151a70343 100644 --- a/public/grpc-client.js +++ b/public/grpc-client.js @@ -85,8 +85,10 @@ module.exports.init = async function({ }); ipcMain.on('lndClose', event => { + lnd['stopDaemon']({}, (err, response) => { + event.sender.send('lndClosed', { err, response }); + }); lnd.close(); - event.sender.send('lndClosed', {}); }); ipcMain.on('unlockRequest', (event, { method, body }) => { @@ -122,4 +124,18 @@ module.exports.init = async function({ if (!stream) return; stream.write(data); }); + + ipcMain.on('app-close', () => { + if (lnd) { + const errorCheck = err => { + if (err) { + console.log(`Errored stopping lnd daemon: ${JSON.stringify(err)}`); + } + }; + lnd['stopDaemon']({}, errorCheck); + lnd.close(); + } else if (unlocker) { + unlocker.close(); + } + }); }; diff --git a/public/preload.js b/public/preload.js index 21470fcee..346a55421 100644 --- a/public/preload.js +++ b/public/preload.js @@ -24,3 +24,5 @@ window.ipcRenderer = { _ipcRenderer.once(filter(event), callback); }, }; + +_ipcRenderer.on('app-close', () => _ipcRenderer.send('app-close')); diff --git a/test/integration/action/action-integration.spec.js b/test/integration/action/action-integration.spec.js index fd71c277d..5078e6ba0 100644 --- a/test/integration/action/action-integration.spec.js +++ b/test/integration/action/action-integration.spec.js @@ -20,7 +20,7 @@ const { startBtcdProcess, mineBlocks, } = require('../../../public/lnd-child-process'); -const grcpClient = require('../../../public/grpc-client'); +const grpcClient = require('../../../public/grpc-client'); /* eslint-disable no-unused-vars */ @@ -129,13 +129,13 @@ describe('Action Integration Tests', function() { lndProcess1 = await lndProcess1Promise; lndProcess2 = await lndProcess2Promise; - await grcpClient.init({ + await grpcClient.init({ ipcMain: ipcMainStub1, lndPort: LND_PORT_1, lndSettingsDir: LND_SETTINGS_DIR_1, network: NETWORK, }); - await grcpClient.init({ + await grpcClient.init({ ipcMain: ipcMainStub2, lndPort: LND_PORT_2, lndSettingsDir: LND_SETTINGS_DIR_2,