Browse Source

Show confirmation when there are unsaved changes

Eric Zhang 4 years ago
parent
commit
06445802da
2 changed files with 18 additions and 8 deletions
  1. 1 1
      rustpad-server/src/lib.rs
  2. 17 7
      src/rustpad.ts

+ 1 - 1
rustpad-server/src/lib.rs

@@ -62,7 +62,7 @@ fn backend() -> BoxedFilter<(impl Reply,)> {
                 let value = entry.value_mut();
                 value.last_accessed = Instant::now();
                 let rustpad = Arc::clone(&value.rustpad);
-                ws.on_upgrade(move |socket| async move { rustpad.on_connection(socket).await })
+                ws.on_upgrade(|socket| async move { rustpad.on_connection(socket).await })
             },
         );
 

+ 17 - 7
src/rustpad.ts

@@ -18,6 +18,7 @@ class Rustpad {
   private recentFailures: number = 0;
   private readonly model: editor.ITextModel;
   private readonly onChangeHandle: any;
+  private readonly beforeUnload: (event: BeforeUnloadEvent) => void;
   private readonly tryConnectId: number;
   private readonly resetFailuresId: number;
 
@@ -36,15 +37,23 @@ class Rustpad {
     this.onChangeHandle = options.editor.onDidChangeModelContent((e) =>
       this.onChange(e)
     );
+    this.beforeUnload = (event: BeforeUnloadEvent) => {
+      if (this.outstanding) {
+        event.preventDefault();
+        event.returnValue = "";
+      } else {
+        delete event.returnValue;
+      }
+    };
+    window.addEventListener("beforeunload", this.beforeUnload);
+
+    const interval = options.reconnectInterval ?? 1000;
     this.tryConnect();
-    const interval = options.reconnectInterval ?? 1000
-    this.tryConnectId = window.setInterval(
-      () => this.tryConnect(),
-      interval
+    this.tryConnectId = window.setInterval(() => this.tryConnect(), interval);
+    this.resetFailuresId = window.setInterval(
+      () => (this.recentFailures = 0),
+      15 * interval
     );
-    this.resetFailuresId = window.setInterval(() =>
-      this.recentFailures = 0
-    , 15 * interval);
   }
 
   /** Destroy this Rustpad instance and close any sockets. */
@@ -52,6 +61,7 @@ class Rustpad {
     window.clearInterval(this.tryConnectId);
     window.clearInterval(this.resetFailuresId);
     this.onChangeHandle.dispose();
+    window.removeEventListener("beforeunload", this.beforeUnload);
     this.ws?.close();
   }