I am checking for updates within a certain time interval. Whenever the updates fail, I am displaying a window with an error notification. Problem is that every time a new window is created.
I already tried
let window = null;
if(window === null) {
window = new BrowserWindow();
window.loadURL('notification.html');
}
else {
window.restore(); // But that is only for minimizing
// WHAT DO I PUT HERE?
}
I am checking for updates within a certain time interval. Whenever the updates fail, I am displaying a window with an error notification. Problem is that every time a new window is created.
I already tried
let window = null;
if(window === null) {
window = new BrowserWindow();
window.loadURL('notification.html');
}
else {
window.restore(); // But that is only for minimizing
// WHAT DO I PUT HERE?
}
Share
Improve this question
asked Oct 19, 2019 at 16:32
neolithneolith
8192 gold badges13 silver badges24 bronze badges
6
- could you just kill a window after a button click on it? – Pranav Commented Oct 19, 2019 at 16:35
- Yeah but if no one uses the device for a few hours and it checks for updates every 10 Min, there will be like 50 windows to close... – neolith Commented Oct 19, 2019 at 16:42
-
I don't understand the
else
block - why do you need to restore? Can't you just have a global variable for the window that's set in the case the update fails. and then when the timer fires again, if the global is set, don't do anything. clicking out of the error message can close and null out the global variable – pushkin Commented Oct 19, 2019 at 17:25 - @pushkin That was just my first thought. I tried your solution before, but when I close the window manually it doesn't reappear. The window should always reappear as long as the issue has not been solved – neolith Commented Oct 19, 2019 at 17:46
- you want it to reappear the next time the timer runs? then you should listen to the windows "closed" event and null out the variable when the window closes, so the next time the timer runs, it recreates the window – pushkin Commented Oct 19, 2019 at 17:49
2 Answers
Reset to default 6Since Electron 5.0.x, the single instance API is changed. You can use it this way.
/** Check if single instance, if not, simply quit new instance */
let isSingleInstance = app.requestSingleInstanceLock()
if (!isSingleInstance) {
app.quit()
}
// Behaviour on second instance for parent process- Pretty much optional
app.on('second-instance', (event, argv, cwd) => {
if (window) {
if (window.isMinimized()) window.restore()
window.focus()
}
})
I had another window for settings, and a button to open it from the menu. So, if the user clicked on that button in the menu it was opening multiple instances of the settings window.
10 clicks = 10 instances of the settings window.
So I wrote the following to solve this by checking if the window already exists before creating it. I don't know if this is the best way to acplish this... I am also learning.
let settingsWindow;
const createSettingsWindow = () => {
if (!settingsWindow) { // If not already opened
settingsWindow = new BrowserWindow({
width: 500,
height: 400,
})
settingsWindow.loadURL(url.format({
pathname: path.join(__dirname, "./src/setting.html"),
protocol: 'file',
slashes: true,
resizable: false,
}))
settingsWindow.on("closed", () => {
settingsWindow = null;
})
} else {
// Handle behaviour when opening again from the menu.
console.log("Don't open another instance of About window.")
}
}
So now when a user clicks on the settings button in the menu, it prints Don't open another instance of About window
in the console.