[Pkg-javascript-commits] [sockjs-client] 288/434: Move xhr transports to new ajax infrastructure

Tonnerre Lombard tonnerre-guest at moszumanska.debian.org
Wed Jan 8 00:47:20 UTC 2014


This is an automated email from the git hooks/post-receive script.

tonnerre-guest pushed a commit to branch master
in repository sockjs-client.

commit 6148122d6574f8b501e14875371278e17aea2404
Author: Marek Majkowski <majek04 at gmail.com>
Date:   Wed Jan 11 16:27:34 2012 +0000

    Move xhr transports to new ajax infrastructure
---
 lib/trans-receiver-xhr.js | 55 +++++++++++++++++------------------------------
 lib/trans-sender.js       | 25 ++++++++++-----------
 2 files changed, 31 insertions(+), 49 deletions(-)

diff --git a/lib/trans-receiver-xhr.js b/lib/trans-receiver-xhr.js
index c6428e0..bb793ff 100644
--- a/lib/trans-receiver-xhr.js
+++ b/lib/trans-receiver-xhr.js
@@ -2,48 +2,33 @@
 var XhrReceiver = function(url, opts) {
     var that = this;
     var buf_pos = 0;
-    var orsc = function (xhr, e, abort_reason) {
-        if (xhr.readyState === 3 || xhr.readyState === 4) {
-            // IE doesn't like peeking into responseText or status on
-            // XHR and readystate=3
-            try {
-                var responseText = xhr.responseText;
-                var status = xhr.status;
-            } catch (x) {}
-            if (responseText && status === 200) {
-                var msgs = [];
-                while (1) {
-                    var buf = responseText.slice(buf_pos);
-                    var p = buf.indexOf('\n');
-                    if (p === -1) break;
-                    buf_pos += p+1;
-                    var msg = buf.slice(0, p);
-                    that.dispatchEvent(
-                        new SimpleEvent('message', {
-                                            data: msg,
-                                            readystate: xhr.readyState,
-                                            responsetext: responseText
-                                        }));
-                }
-            }
-        }
-        if (xhr.readyState === 4 || abort_reason) {
-            var reason = abort_reason ? 'user' :
-                (xhr.status === 200 ? 'network' : 'permanent');
-            that.xhr_close = null;
-            that.dispatchEvent(new SimpleEvent('close', {reason: reason}));
+
+    that.xo = utils.createXHR(true, 'POST', url);
+    that.xo.onchunk = function(status, text) {
+        if (status !== 200) return;
+        while (1) {
+            var buf = text.slice(buf_pos);
+            var p = buf.indexOf('\n');
+            if (p === -1) break;
+            buf_pos += p+1;
+            var msg = buf.slice(0, p);
+            that.dispatchEvent(new SimpleEvent('message', {data: msg}));
         }
     };
-    var createXhr = (opts.cors && _window.XDomainRequest) ?
-                                    utils.createXDR : utils.createXHR;
-    that.xhr_close = createXhr('POST', url, null, orsc);
+    that.xo.onfinish = function(status) {
+        that.xo = null;
+        var reason = status === 200 ? 'network' : 'permanent';
+        that.dispatchEvent(new SimpleEvent('close', {reason: reason}));
+    }
 };
 
 XhrReceiver.prototype = new REventTarget();
 
 XhrReceiver.prototype.abort = function() {
     var that = this;
-    if (that.xhr_close) {
-        that.xhr_close(true);
+    if (that.xo) {
+        that.xo.close();
+        that.dispatchEvent(new SimpleEvent('close', {reason: 'user'}));
+        that.xo = null;
     }
 };
diff --git a/lib/trans-sender.js b/lib/trans-sender.js
index 68de73d..fc11694 100644
--- a/lib/trans-sender.js
+++ b/lib/trans-sender.js
@@ -114,22 +114,19 @@ var jsonPGenericSender = function(url, payload, callback) {
     return completed;
 };
 
-var ajaxSender = function(url, payload, callback) {
-    var orsc = function (xhr, e, abort_reason) {
-        if(xhr.readyState === 4 || abort_reason) {
-            callback(xhr.status, abort_reason);
-        }
+var _sender = function(cors, url, payload, callback) {
+    var xo = utils.createXHR(cors, 'POST', url + '/xhr_send', payload);
+    xo.onfinish = function(status, text) {
+        callback(status);
+    };
+    return function(abort_reason) {
+        callback(0, abort_reason);
     };
-    return utils.createXHR('POST', url + '/xhr_send', payload, orsc);
 };
 
+var ajaxSender = function(url, payload, callback) {
+    return _sender(false, url, payload, callback);
+};
 var xdrSender = function(url, payload, callback) {
-    var orsc = function (xhr, e, abort_reason) {
-        if(xhr.readyState === 4 || abort_reason) {
-            callback(xhr.status, abort_reason);
-        }
-    };
-    var fun = window.XDomainRequest ? utils.createXDR : utils.createXHR;
-    return fun('POST', url + '/xhr_send', payload, orsc);
+    return _sender(true, url, payload, callback);
 };
-

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/sockjs-client.git



More information about the Pkg-javascript-commits mailing list