summaryrefslogtreecommitdiff
path: root/ext/bg/js/settings.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-11-02 17:37:53 -0400
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-11-07 20:30:55 -0500
commitfcb922400add0b9ab5d5b5454c3d6042a2d5b256 (patch)
tree5102b3f49fb72c635cb75d49bc2f46b342a4066f /ext/bg/js/settings.js
parente5dcb418248667e3aa4627d5660ff2f951d58237 (diff)
Prevent page exit while database operations are in progress
Diffstat (limited to 'ext/bg/js/settings.js')
-rw-r--r--ext/bg/js/settings.js46
1 files changed, 46 insertions, 0 deletions
diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js
index e4446851..d4b1549e 100644
--- a/ext/bg/js/settings.js
+++ b/ext/bg/js/settings.js
@@ -212,6 +212,52 @@ $(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
*/