alex8088 / electron-vite

Next generation Electron build tooling based on Vite 新一代 Electron 开发构建工具,支持源代码保护
https://electron-vite.org
MIT License
3.57k stars 153 forks source link

fix: app can't be restarted successfully when the application has close-blocking interactive behavior #521

Closed mamboer closed 3 months ago

mamboer commented 6 months ago

Description

fix: app can't be restarted successfully when the application has interactive behavior that prevents closing (such as a confirmation dialog box).

Additional context

The code that may trigger the bug:

mainWindow.on('close', (e) => {
  dialog.showMessageBox({
        title: 'App Shutdown Notice',
        message: 'Are you sure to shutdown the application?',
        type: 'info',
        buttons: ['Ignore',  'Exit'],
      })
        .then((result) => {
          if (result.response === 1)
            app.quit()
        })
})

The solution:

const watchHook = (): void => {
  logger.info(colors.green(`\nrebuild the electron main process successfully`))

  if (ps) {
    logger.info(colors.cyan(`\n  waiting for electron to exit...`))

    ps.removeAllListeners()
    ps.on('exit', () => {
      ps = startElectron(inlineConfig.root)
      logger.info(colors.green(`\nrestart electron app...`))
    })
    ps.kill()
  }
}

In this code, I added an exit event listener, which starts a new Electron process after the old Electron process exits. This ensures that the new Electron process starts only after the old Electron process has completely exited.


What is the purpose of this pull request?

Before submitting the PR, please make sure you do the following