Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to restart an Electron app?

In this Electron documentation page, they recommend that to restart an application, one should execute app.quit or app.exit after the call to app.relaunch:

Note that this method does not quit the app when executed, you have to call app.quit or app.exit after calling app.relaunch to make the app restart.

However after experimenting I found that the order doesn't seem to actually matter. (See my example below.)

I know that app.quit and app.exit are not quite the same. The former can be interrupted and will trigger some events while the latter will force the app to exit without triggering any events or allowing the app to cancel the action.

Question: assuming that it is always ok to force the app to exit and that we don't have any tasks to perform before the app exits, is there:

  1. A reason to prefer app.quit or app.exit?
  2. A reason why one must run app.quit or app.exit after app.relaunch?

Here's a very simple Electron app:

package.json

{
  "name": "burrito",
  "version": "1.0.0",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "devDependencies": {
    "electron": "^4"
  }
}

main.js

const {app, BrowserWindow, Menu} = require('electron');
let mainWindow;

app.on('ready', () => {
  Menu.setApplicationMenu(
    Menu.buildFromTemplate([
      {role: 'appMenu', submenu: [

        {label: 'relaunch(); exit()', click() {
          app.relaunch();
          app.exit();
        }},

        {label: 'relaunch(); quit()', click() {
          app.relaunch();
          app.quit();
        }},

        {type: 'separator'},

        {label: 'exit(); relaunch()', click() {
          app.exit();
          app.relaunch();
        }},

        {label: 'quit(); relaunch()', click() {
          app.quit();
          app.relaunch();
        }}
      ]}
    ])
  );
  mainWindow = new BrowserWindow({width: 640, height: 480});
  mainWindow.loadFile('index.html');
});

Producing the following application menu:

enter image description here

A click on any of the menu item will produce the same result: the app quits then restarts.

like image 799
customcommander Avatar asked May 04 '19 12:05

customcommander


2 Answers

The proper way to restart an electron app is:

app.relaunch()
app.exit()

See official documentation.

like image 194
Arkaprova Majumder Avatar answered Sep 22 '22 18:09

Arkaprova Majumder


app.relaunch

According to app.relaunch docs:

Relaunches the app when current instance exits.

(...)

Note that this method does not quit the app when executed, you have to call app.quit or app.exit after calling app.relaunch to make the app restart.

When app.relaunch is called for multiple times, multiple instances will be started after current instance exited.

We can assume that:

  1. Calling app.relaunch() only won't do anything (until the user closes the application, then it will restart);
  2. You can call app.quit() or app.exit() in order to close the application, then it will restart because you already called app.relaunch().

app.quit

Citing the docs again:

Try to close all windows. The before-quit event will be emitted first. If all windows are successfully closed, the will-quit event will be emitted and by default the application will terminate.

This method guarantees that all beforeunload and unload event handlers are correctly executed. It is possible that a window cancels the quitting by returning false in the beforeunload event handler.

It means that using app.quit() may or may not terminate the application.

app.exit

The docs states:

All windows will be closed immediately without asking the user, and the before-quit and will-quit events will not be emitted.

So, immediately is a dangerous word. It means that the app will be closed as soon as it is possible, and that it certainly will close, but not instantly.

Should I use app.quit or app.exit?

Both are valid, depends on your use case:

  • If your user may be on a screen where you have some validations to prevent they from closing the app because of reasons, app.quit is probably better for you. Just be careful to not call app.relaunch more than once (it causes multiple instances to be started after exiting the current instance):
app.relaunch();
app.quit(); // maybe the application will be closed; maybe not
  • If you need (or want) to close the app immediately in order to apply some setting, for example, no matter what is happening in that moment, you should call app.exit. Doing it you are safe that the app will be closed and relaunched:
app.relaunch();
app.exit(); // the application will be closed as soon as possible

Why should I call app.relaunch first?

Well, because it is logical. See the code below:

app.exit();
app.relaunch();

You're telling: "Hey, Electron, please close my app. Oh, and restart it after closing.". It'll work most of the times, but only because app.exit is slower than app.relaunch to execute. They're not running synchronously.

If, for some reason, Electron terminates your application before knowing it should be relaunched, it would be an unexpected behavior for you, but that's what you told Electron to do.

like image 45
Rafael Tavares Avatar answered Sep 26 '22 18:09

Rafael Tavares