Prompt to save / quit before closing window
<ul>
<li>I am making a writing application. Users can open the app, write some text, save their work, etc.</li>
<li>I am trying to make it so that clicking the <code>window close</code> button will prompt the user to  (a) save their work (if necessary) or (b) just quit.</li>
<li>I am trying to use <code>window.beforeunload</code> to achieve this, but find I am getting stuck in a loop, where trying to "quit" makes the same prompt appear ad infinitum.</li>
</ul>

<p>Here's some code:</p>

<pre><code>windowCloseCheck() {
window.onbeforeunload = function(e) {
  e.returnValue = false;
// window.alert('try to close me');    
if(file.isUnsaved() || file.hasChanged()) {
  // prompt - save or just quit?
  file.fileWarning('You have unsaveeed work.', 'Save', 'Quit', function(){
    // OPTION A - save
    file.save();
  }, function() {
    // OPTION B: Quit.
    ipcRenderer.send('quitter')
  })
} 

else {
  // file is saved and no new work has been done:
  ipcRenderer.send('quitter')
}
</code></pre>

<p><code>windowCloseCheck</code> is invoked when the application is setup, initiating an event listener for closing the window. My conditional is checking if the file is unsaved or has changed from the original.</p>

<p><code>fileWarning</code> is a function that just wraps the electron dialog box, making a pop up appear with two choices and respective functions to call for each choice. </p>

<p>The rest of the code is <a href="https://github.com/teesloane/moonview" rel="noreferrer">available</a> if I'm (probably) leaving out necessary information. Would be happy to clarify if I'm not being very clear. </p>

<p><img src="https://i.stack.imgur.com/B2VhZ.gif" alt=""></p>