Skip to content

Commit

Permalink
Merge pull request #380 from NeurodataWithoutBorders/avoid-extra-proc…
Browse files Browse the repository at this point in the history
…esses

Ensure processes are closed
  • Loading branch information
CodyCBakerPhD authored Sep 20, 2023
2 parents 48e0a24 + 7b6b129 commit 66a9824
Showing 1 changed file with 39 additions and 31 deletions.
70 changes: 39 additions & 31 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ const exitPyProc = async () => {
"/t",
]) // Windows does not properly shut off the python server process. This ensures it is killed.

else pyflaskProcess.kill()
pyflaskProcess.kill() // Try killing twice on Windows

pyflaskProcess = null;
};
Expand Down Expand Up @@ -223,6 +223,8 @@ let hasBeenOpened = false;

function initialize() {

if (globals.mainWindow) return // Do not re-initialize if the main window is already declared

makeSingleInstance();

function createWindow() {
Expand Down Expand Up @@ -254,42 +256,39 @@ function initialize() {
})
.then((responseObject) => {
let { response } = responseObject;
if (response === 0) quit_app()
if (response === 0) app.quit();
else globals.mainWindowReady = true
});
}
} else {
await exitPyProc();
app.exit();
app.quit();
}
});
}

const quit_app = () => {
globals.mainWindow.close();
if (!globals.mainWindow.closed) globals.mainWindow.destroy()
};

function onAppReady () {

const promise = createPyProc();

// Listen after first load
promise.then(() => {
const chokidar = require('chokidar');
chokidar.watch(path.join(__dirname, "../../pyflask"), {
ignored: ['**/__pycache__/**']
}).on('all', async (event: string) => {
if (event === 'change' && !globals.python.restart) {
globals.python.restart = true
await exitPyProc();
setTimeout(async () => {
await createPyProc();
globals.python.restart = false
}, 1000)
}
});
})
// Only create one python process
if (!pyflaskProcess) {
const promise = createPyProc();

// Listen after first load
promise.then(() => {
const chokidar = require('chokidar');
chokidar.watch(path.join(__dirname, "../../pyflask"), {
ignored: ['**/__pycache__/**']
}).on('all', async (event: string) => {
if (event === 'change' && !globals.python.restart) {
globals.python.restart = true
await exitPyProc();
setTimeout(async () => {
await createPyProc();
globals.python.restart = false
}, 1000)
}
});
})
}

const windowOptions = {
minWidth: 1121,
Expand Down Expand Up @@ -394,13 +393,22 @@ app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) initialize()
})

app.on("window-all-closed", async () => {
if (process.platform != 'darwin') {
await exitPyProc();
app.quit();
app.on('will-quit', async () => {
try {
await exitPyProc()
if (globals.mainWindow) {
globals.mainWindow.close();
if (!globals.mainWindow.closed) globals.mainWindow.destroy()
}
} catch (err) {
console.error(err);
}
});

app.on("window-all-closed", async () => {
if (process.platform != 'darwin') app.quit();
});

// app.on("will-quit", () => app.quit());

app.on("open-file", onFileOpened)
Expand Down

0 comments on commit 66a9824

Please sign in to comment.