diff lib/js/urweb.js @ 1042:a8a825861397

Explicitly abort in-flight RPCs onunload
author Adam Chlipala <adamc@hcoop.net>
date Tue, 24 Nov 2009 09:24:25 -0500
parents 0d767c8d2923
children eaba663fd6aa
line wrap: on
line diff
--- a/lib/js/urweb.js	Sun Nov 22 17:57:15 2009 -0500
+++ b/lib/js/urweb.js	Tue Nov 24 09:24:25 2009 -0500
@@ -627,7 +627,20 @@
 
 var sig = null;
 
+var unloading = false, inFlight = null;
+
+function unload() {
+  unloading = true;
+
+  for (; inFlight; inFlight = inFlight.next) {
+    inFlight.data.abort();
+  }
+}
+
 function requestUri(xhr, uri, needsSig) {
+  if (unloading)
+    return;
+
   xhr.open("POST", uri, true);
 
   if (client_id != null) {
@@ -642,9 +655,15 @@
     xhr.setRequestHeader("UrWeb-Sig", sig);
   }
 
+  inFlight = cons(xhr, inFlight);
   xhr.send(null);
 }
 
+function xhrFinished(xhr) {
+  xhr.abort();
+  inFlight = remove(xhr, inFlight);
+}
+
 function rc(prefix, uri, parse, k, needsSig) {
   uri = cat(prefix, uri);
   uri = flattenLocal(uri);
@@ -668,6 +687,8 @@
       } else {
         conn();
       }
+
+      xhrFinished(xhr);
     }
   };
 
@@ -772,7 +793,7 @@
           }
         }
 
-        xhr.abort();
+        xhrFinished(xhr);
 
         connect();
       }
@@ -786,7 +807,7 @@
   };
 
   onTimeout = function() {
-    xhr.abort();
+    xhrFinished(xhr);
     connect();
   };