[英]Using the 'before-quit' event in electron (atom-shell)
我有一个应用程序,它需要在退出之前进行API调用(类似于注销)。 由于我仍需要访问某些应用程序数据(redux存储)以进行API调用,因此我决定在应用程序上侦听“退出前”事件。
我尝试了以下代码:
import {remote} from 'electron';
let loggedout = false;
remote.app.on('before-quit', (event) => {
if (loggedout) return; // if we are logged out just quit.
console.warn('users tries to quit');
// prevent the default which should cancel the quit
event.preventDefault();
// in the place of the setTimout will be an API call
setTimeout(() => {
// if api call was a success
if (true) {
loggedout = true;
remote.app.quit();
} else {
// tell the user log-out was not successfull. retry and quit after second try.
}
}, 1000);
});
该事件似乎永远不会触发或阻止关机无法正常工作。 当我用browser-window-blur
替换before-quit
事件时,该事件确实触发,并且代码似乎可以正常工作。
作为参考,我使用Electron 1.2.8(由于某些依赖关系,我无法升级)。 我已经仔细检查before-quit
事件已在该版本中实现 。
任何想法为何此事件似乎没有被解雇?
在此先感谢您,节日快乐!
我有同样的问题,这是我的解决方案:
在渲染器中:
const { ipcRenderer } = require('electron')
window._saved = false
window.onbeforeunload = (e) => {
if (!window.saved) {
callSaveAPI(() => {
// success? quit the app
window._saved = true
ipcRenderer.send('app_quit')
window.onbeforeunload = null
})
}
e.returnValue = false
}
在主要方面:
const { ipcMain } = require('electron')
// listen the 'app_quit' event
ipcMain.on('app_quit', (event, info) => {
app.quit()
})
有两个导致代码无法正常工作的问题:
window.onbeforeunload
函数来完成。 就像在这个线程中建议的那样。 一个警告是onbeforeunload
的return语句不会被更新。 就我而言,我首先返回false(以防止关闭窗口)。 第二次它没有返回false,但是一直阻止关闭窗口。
我通过使用不返回false的新函数覆盖window.onbeforeunload
解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.