[PATCH 2 of 6 V2] hgweb: add makeRequest javascript function

Alexander Plavin alexander at plav.in
Sat Aug 17 17:28:57 CDT 2013


# HG changeset patch
# User Alexander Plavin <alexander at plav.in>
# Date 1376739693 -14400
#      Sat Aug 17 15:41:33 2013 +0400
# Node ID 4e3c3eaff5b224d6aa16a41f112c330341fedf97
# Parent  9a094911541a650e3b3e84eef5d3ed46a24cb74e
hgweb: add makeRequest javascript function

This function performs an asynchronous HTTP request and calls provided
callbacks:
- onstart: request is sent
- onsuccess: response is received
- onerror: some error occured
- oncomplete: response is fully processed and all other callbacks finished

diff -r 9a094911541a -r 4e3c3eaff5b2 mercurial/templates/static/mercurial.js
--- a/mercurial/templates/static/mercurial.js	Fri Aug 09 15:01:33 2013 +0400
+++ b/mercurial/templates/static/mercurial.js	Sat Aug 17 15:41:33 2013 +0400
@@ -304,3 +304,27 @@
         return String(replacements[p1]);
     });
 }
+
+function makeRequest(url, method, onstart, onsuccess, onerror, oncomplete) {
+    xfr = new XMLHttpRequest();
+    xfr.onreadystatechange = function() {
+        if (xfr.readyState === 4) {
+            try {
+                if (xfr.status === 200) {
+                    onsuccess(xfr.responseText);
+                } else {
+                    throw 'server error';
+                }
+            } catch (e) {
+                onerror(e);
+            } finally {
+                oncomplete();
+            }
+        }
+    };
+
+    xfr.open(method, url);
+    xfr.send();
+    onstart();
+    return xfr;
+}


More information about the Mercurial-devel mailing list