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

login
register
mail settings
Submitter Alexander Plavin
Date Aug. 17, 2013, 10:28 p.m.
Message ID <4e3c3eaff5b224d6aa16.1376778537@debian-alexander.dolgopa>
Download mbox | patch
Permalink /patch/2203/
State Superseded
Commit 2228bd109706cda974a1338f5127e15d3036b559
Headers show

Comments

Alexander Plavin - Aug. 17, 2013, 10:28 p.m.
# HG changeset patch
# User Alexander Plavin <alexander@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

Patch

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;
+}