You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ja...@apache.org on 2010/06/25 16:16:39 UTC

svn commit: r957966 - /couchdb/branches/0.11.x/share/www/script/jquery.couch.js

Author: jan
Date: Fri Jun 25 14:16:39 2010
New Revision: 957966

URL: http://svn.apache.org/viewvc?rev=957966&view=rev
Log:
Merge r957380 from trunk:

make jquery.couch.js changes handling more robust

Modified:
    couchdb/branches/0.11.x/share/www/script/jquery.couch.js

Modified: couchdb/branches/0.11.x/share/www/script/jquery.couch.js
URL: http://svn.apache.org/viewvc/couchdb/branches/0.11.x/share/www/script/jquery.couch.js?rev=957966&r1=957965&r2=957966&view=diff
==============================================================================
--- couchdb/branches/0.11.x/share/www/script/jquery.couch.js [utf-8] (original)
+++ couchdb/branches/0.11.x/share/www/script/jquery.couch.js [utf-8] Fri Jun 25 14:16:39 2010
@@ -215,7 +215,9 @@
         changes: function(since, options) {
           options = options || {};
           // set up the promise object within a closure for this handler
-          var db = this, active = true, listeners = [], promise = {
+          var timeout = 100, db = this, active = true,
+            listeners = [],
+            promise = {
             onChange : function(fun) {
               listeners.push(fun);
             },
@@ -231,18 +233,24 @@
           };
           // when there is a change, call any listeners, then check for another change
           options.success = function(resp) {
+            timeout = 100;
             if (active) {
-              var seq = resp.last_seq;
+              since = resp.last_seq;
               triggerListeners(resp);
-              getChangesSince(seq);
+              getChangesSince();
             };
           };
+          options.error = function() {
+            if (active) {
+              setTimeout(getChangesSince, timeout);
+              timeout = timeout * 2;
+            }
+          };
           // actually make the changes request
-          function getChangesSince(seq) {
-            var opts = {};
-            $.extend(opts, options, {
+          function getChangesSince() {
+            var opts = $.extend({heartbeat : 10 * 1000}, options, {
               feed : "longpoll",
-              since : seq
+              since : since
             });
             ajax(
               {url: db.uri + "_changes"+encodeOptions(opts)},
@@ -252,11 +260,12 @@
           }
           // start the first request
           if (since) {
-            getChangesSince(since);
+            getChangesSince();
           } else {
             db.info({
               success : function(info) {
-                getChangesSince(info.update_seq);
+                since = info.update_seq;
+                getChangesSince();
               }
             });
           }
@@ -531,14 +540,23 @@
         }
       },
       complete: function(req) {
-        var resp = $.httpData(req, "json");
+        try {
+          var resp = $.httpData(req, "json");
+        } catch(e) {
+          if (options.error) {
+            options.error(req.status, req, e);
+          } else {
+            alert(errorMessage + ": " + e);
+          }
+          return;
+        }
         if (options.ajaxStart) {
           options.ajaxStart(resp);
         }
         if (req.status == options.successStatus) {
           if (options.success) options.success(resp);
         } else if (options.error) {
-          options.error(req.status, resp.error, resp.reason);
+          options.error(req.status, resp && resp.error || errorMessage, resp && resp.reason || "no response");
         } else {
           alert(errorMessage + ": " + resp.reason);
         }