From 18ac9b323d4fad692e8d5b355f9f8ca42b03aa5b Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Thu, 19 Jun 2025 00:00:04 -0500 Subject: [PATCH] Fixed SIGINT and SIGTERM exit calls to save settings --- newton/main.js | 82 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 54 insertions(+), 28 deletions(-) diff --git a/newton/main.js b/newton/main.js index 7efbbce..0c37d63 100644 --- a/newton/main.js +++ b/newton/main.js @@ -8,10 +8,62 @@ const { newton } = require('./app'); -let window = null; +let window = null; +let hasExitSaved = false; +const createWindow = () => { + window = newton.createWindow( + newton.args.getStartType(), + newton.args.debugMode(), + newton.args.getArgs(), + ); +} + +const handleExitSignal = (code) => { + if (!hasExitSaved) { + newton.settings.saveConfig(); + hasExitSaved = true; + } + + process.exit(code); +} + + +const loadProcessSignalHandlers = () => { + process.on('SIGINT', () => { + // https://nodejs.org/docs/v14.16.0/api/process.html#process_signal_events + handleExitSignal(128 + 2); + }); + + process.on('SIGTERM', () => { + // https://nodejs.org/docs/v14.16.0/api/process.html#process_signal_events + handleExitSignal(128 + 15); + }); + + process.on('uncaughtException', (error) => { + if (error.message != null) { + console.log(error.message); + } + + if (error.stack != null) { + console.log(error.stack); + } + }); + + process.on('unhandledRejection', function(error = {}) { + if (error.message != null) { + console.log(error.message); + } + + if (error.stack != null) { + console.log(error.stack); + } + }); + +} + const loadHandlers = () => { ipcMain.handle('getLspConfigData', (eve) => newton.fs.getLspConfigData()); ipcMain.handle('getFileContents', (eve, path) => newton.fs.getFileContents(path)); @@ -21,16 +73,8 @@ const loadHandlers = () => { ipcMain.handle('saveFileAs', (eve, content) => newton.fs.saveFileAs(content)); } -const createWindow = () => { - window = newton.createWindow( - newton.args.getStartType(), - newton.args.debugMode(), - newton.args.getArgs(), - ); -} - - app.whenReady().then(async () => { + loadProcessSignalHandlers(); newton.args.loadArgs(); if ( !await newton.ipc.isIPCServerUp() ) { @@ -68,22 +112,4 @@ app.on('window-all-closed', () => { newton.settings.saveConfig(); }); -process.on('uncaughtException', (error) => { - if (error.message != null) { - console.log(error.message); - } - if (error.stack != null) { - console.log(error.stack); - } -}); - -process.on('unhandledRejection', function(error = {}) { - if (error.message != null) { - console.log(error.message); - } - - if (error.stack != null) { - console.log(error.stack); - } -}); \ No newline at end of file