Prevent page exit while database operations are in progress

This commit is contained in:
toasted-nutbread 2019-11-02 17:37:53 -04:00
parent e5dcb41824
commit fcb922400a
2 changed files with 59 additions and 0 deletions

View File

@ -222,7 +222,10 @@ class SettingsDictionaryEntryUI {
const progressBar = this.content.querySelector('.progress-bar'); const progressBar = this.content.querySelector('.progress-bar');
this.isDeleting = true; this.isDeleting = true;
const prevention = new PageExitPrevention();
try { try {
prevention.start();
const onProgress = ({processed, count, storeCount, storesProcesed}) => { const onProgress = ({processed, count, storeCount, storesProcesed}) => {
let percent = 0.0; let percent = 0.0;
if (count > 0 && storesProcesed > 0) { if (count > 0 && storesProcesed > 0) {
@ -235,6 +238,7 @@ class SettingsDictionaryEntryUI {
} catch (e) { } catch (e) {
dictionaryErrorsShow([e]); dictionaryErrorsShow([e]);
} finally { } finally {
prevention.end();
this.isDeleting = false; this.isDeleting = false;
progress.hidden = true; progress.hidden = true;
@ -479,7 +483,10 @@ async function onDictionaryPurge(e) {
const dictProgress = document.querySelector('#dict-purge'); const dictProgress = document.querySelector('#dict-purge');
dictProgress.hidden = false; dictProgress.hidden = false;
const prevention = new PageExitPrevention();
try { try {
prevention.start();
dictionaryErrorsShow(null); dictionaryErrorsShow(null);
dictionarySpinnerShow(true); dictionarySpinnerShow(true);
@ -496,6 +503,8 @@ async function onDictionaryPurge(e) {
} catch (err) { } catch (err) {
dictionaryErrorsShow([err]); dictionaryErrorsShow([err]);
} finally { } finally {
prevention.end();
dictionarySpinnerShow(false); dictionarySpinnerShow(false);
dictControls.show(); dictControls.show();
@ -513,7 +522,10 @@ async function onDictionaryImport(e) {
const dictProgress = $('#dict-import-progress').show(); const dictProgress = $('#dict-import-progress').show();
const dictImportInfo = document.querySelector('#dict-import-info'); const dictImportInfo = document.querySelector('#dict-import-info');
const prevention = new PageExitPrevention();
try { try {
prevention.start();
dictionaryErrorsShow(null); dictionaryErrorsShow(null);
dictionarySpinnerShow(true); dictionarySpinnerShow(true);
@ -559,6 +571,7 @@ async function onDictionaryImport(e) {
} catch (err) { } catch (err) {
dictionaryErrorsShow([err]); dictionaryErrorsShow([err]);
} finally { } finally {
prevention.end();
dictionarySpinnerShow(false); dictionarySpinnerShow(false);
dictImportInfo.hidden = false; dictImportInfo.hidden = false;

View File

@ -211,6 +211,52 @@ async function onReady() {
$(document).ready(utilAsync(onReady)); $(document).ready(utilAsync(onReady));
/*
* Page exit prevention
*/
class PageExitPrevention {
constructor() {
}
start() {
PageExitPrevention._addInstance(this);
}
end() {
PageExitPrevention._removeInstance(this);
}
static _addInstance(instance) {
const size = PageExitPrevention._instances.size;
PageExitPrevention._instances.set(instance, true);
if (size === 0) {
window.addEventListener('beforeunload', PageExitPrevention._onBeforeUnload);
}
}
static _removeInstance(instance) {
if (
PageExitPrevention._instances.delete(instance) &&
PageExitPrevention._instances.size === 0
) {
window.removeEventListener('beforeunload', PageExitPrevention._onBeforeUnload);
}
}
static _onBeforeUnload(e) {
if (PageExitPrevention._instances.size === 0) {
return;
}
e.preventDefault();
e.returnValue = '';
return '';
}
}
PageExitPrevention._instances = new Map();
/* /*
* Appearance * Appearance
*/ */