diff options
author | gaye <gaye@mozilla.com> | 2013-04-16 15:50:24 -0700 |
---|---|---|
committer | gaye <gaye@mozilla.com> | 2013-04-18 10:46:25 -0700 |
commit | 5e788a0315b937ebfaea47ee15103b093bd4edb3 (patch) | |
tree | 9024ea21340ac246de43d7c467a4886dcd5d8382 /lib | |
parent | c439df4388cc4bb5c1777c4dbdc5b164d5192d7f (diff) | |
download | jsCalDAV-5e788a0315b937ebfaea47ee15103b093bd4edb3.tar.gz |
Add an abort routine to Caldav.Xhr
Diffstat (limited to 'lib')
-rw-r--r-- | lib/caldav/request/abstract.js | 4 | ||||
-rw-r--r-- | lib/caldav/request/asset.js | 12 | ||||
-rw-r--r-- | lib/caldav/request/calendar_home.js | 13 | ||||
-rw-r--r-- | lib/caldav/xhr.js | 61 |
4 files changed, 60 insertions, 30 deletions
diff --git a/lib/caldav/request/abstract.js b/lib/caldav/request/abstract.js index 7116217..9edf6fd 100644 --- a/lib/caldav/request/abstract.js +++ b/lib/caldav/request/abstract.js @@ -54,6 +54,8 @@ * @param {Function} callback node style callback. * Receives three arguments * error, parsedData, xhr. + * @return {Caldav.Xhr} The xhr request so that the caller + * has a chance to abort the request. */ send: function(callback) { var self = this; @@ -75,6 +77,8 @@ callback(new Errors.CaldavHttpError(xhr.status)); } }); + + return req; } }; diff --git a/lib/caldav/request/asset.js b/lib/caldav/request/asset.js index b26e4f7..98c943a 100644 --- a/lib/caldav/request/asset.js +++ b/lib/caldav/request/asset.js @@ -71,6 +71,8 @@ * * @param {Object} [options] calendar options. * @param {Function} callback node style [err, data, xhr]. + * @return {Caldav.Xhr} The underlying xhr request so that the caller + * has a chance to abort the request. */ get: function(options, callback) { if (typeof(options) === 'function') { @@ -80,7 +82,7 @@ var req = this._buildRequest('GET', options); - req.send(function(err, xhr) { + return req.send(function(err, xhr) { callback(err, xhr.responseText, xhr); }); }, @@ -91,6 +93,8 @@ * @param {Object} [options] see get. * @param {String} data post content. * @param {Function} callback node style [err, data, xhr]. + * @return {Caldav.Xhr} The underlying xhr request so that the caller + * has a chance to abort the request. */ put: function(options, data, callback) { if (typeof(options) === 'string') { @@ -106,7 +110,7 @@ var req = this._buildRequest('PUT', options); req.data = data; - req.send(function(err, xhr) { + return req.send(function(err, xhr) { callback(err, xhr.responseText, xhr); }); }, @@ -116,6 +120,8 @@ * * @param {Object} [options] see get. * @param {Function} callback node style [err, data, xhr]. + * @return {Caldav.Xhr} The underlying xhr request so that the caller + * has a chance to abort the request. */ delete: function(options, callback) { if (typeof(options) === 'function') { @@ -125,7 +131,7 @@ var req = this._buildRequest('DELETE', options); - req.send(function(err, xhr) { + return req.send(function(err, xhr) { callback(err, xhr.responseText, xhr); }); } diff --git a/lib/caldav/request/calendar_home.js b/lib/caldav/request/calendar_home.js index 24b27bc..a1f8ca6 100644 --- a/lib/caldav/request/calendar_home.js +++ b/lib/caldav/request/calendar_home.js @@ -52,6 +52,10 @@ Propfind: ns.require('request/propfind'), + /** + * @return {Caldav.Xhr} The underlying xhr request so that the caller + * has a chance to abort the request. + */ _findPrincipal: function(url, callback) { var find = new this.Propfind(this.connection, { url: url @@ -60,7 +64,7 @@ find.prop('current-user-principal'); find.prop('principal-URL'); - find.send(function(err, data) { + return find.send(function(err, data) { var principal; if (err) { @@ -81,7 +85,6 @@ } else { callback(new Errors.CaldavHttpError(404)); } - }); }, @@ -93,7 +96,7 @@ find.prop(['caldav', 'calendar-home-set']); - find.send(function(err, data) { + return find.send(function(err, data) { if (err) { callback(err); return; @@ -112,10 +115,12 @@ * * @param {Function} callback node style where second argument * are the details of the home calendar. + * @return {Caldav.Xhr} The underlying xhr request so that the caller + * has a chance to abort the request. */ send: function(callback) { var self = this; - self._findPrincipal(self.url, function(err, url) { + return self._findPrincipal(self.url, function(err, url) { if (!url) { callback(err); diff --git a/lib/caldav/xhr.js b/lib/caldav/xhr.js index 77ecd5c..e88e789 100644 --- a/lib/caldav/xhr.js +++ b/lib/caldav/xhr.js @@ -43,6 +43,7 @@ Xhr.prototype = { globalXhrOptions: null, xhrClass: Native, + xhr: null, method: 'GET', async: true, waiting: false, @@ -53,7 +54,7 @@ headers: {}, data: null, - _seralize: function _seralize() { + _serialize: function _serialize() { return this.data; }, @@ -70,31 +71,45 @@ }, /** + * Aborts the request if it has already been sent. + * @param {Function=} cb An optional callback function. + */ + abort: function(cb) { + if (this.waiting) { + this.xhr.abort(); + this.waiting = false; + } + + if (cb !== undefined) { + cb(); + } + }, + + /** * Sends request to server. * * @param {Function} callback success/failure handler. */ send: function send(callback) { - var header, xhr; + var header; if (typeof(callback) === 'undefined') { callback = this.callback; } - if (this.globalXhrOptions) { - xhr = new this.xhrClass(this.globalXhrOptions); - } else { - xhr = new this.xhrClass(); - } + this.xhr = new this.xhrClass( + this.globalXhrOptions ? this.globalXhrOptions : undefined); + // This hack is in place due to some platform // bug in gecko when using mozSystem xhr // the credentials only seem to work as expected // when constructing them manually. if (!this.globalXhrOptions || !this.globalXhrOptions.mozSystem) { - xhr.open(this.method, this.url, this.async, this.user, this.password); + this.xhr.open( + this.method, this.url, this.async, this.user, this.password); } else { - xhr.open(this.method, this.url, this.async); - xhr.setRequestHeader('Authorization', this._credentials( + this.xhr.open(this.method, this.url, this.async); + this.xhr.setRequestHeader('Authorization', this._credentials( this.user, this.password )); @@ -103,12 +118,12 @@ var useMozChunkedText = false; if (this.globalXhrOptions && this.globalXhrOptions.useMozChunkedText) { useMozChunkedText = true; - xhr.responseType = 'moz-chunked-text'; + this.xhr.responseType = 'moz-chunked-text'; } for (header in this.headers) { if (Object.hasOwnProperty.call(this.headers, header)) { - xhr.setRequestHeader(header, this.headers[header]); + this.xhr.setRequestHeader(header, this.headers[header]); } } @@ -116,19 +131,19 @@ var hasProgressEvents = false; // check for progress event support. - if ('onprogress' in xhr) { + if ('onprogress' in this.xhr) { hasProgressEvents = true; var last = 0; if (useMozChunkedText) { - xhr.onprogress = (function onChunkedProgress(event) { + this.xhr.onprogress = (function onChunkedProgress(event) { if (this.ondata) { - this.ondata(xhr.responseText); + this.ondata(this.xhr.responseText); } }.bind(this)); } else { - xhr.onprogress = (function onProgress(event) { - var chunk = xhr.responseText.substr(last, event.loaded); + this.xhr.onprogress = (function onProgress(event) { + var chunk = this.xhr.responseText.substr(last, event.loaded); last = event.loaded; if (this.ondata) { this.ondata(chunk); @@ -137,10 +152,10 @@ } } - xhr.onreadystatechange = (function onReadyStateChange() { + this.xhr.onreadystatechange = (function onReadyStateChange() { var data; - if (xhr.readyState === 4) { - data = xhr.responseText; + if (this.xhr.readyState === 4) { + data = this.xhr.responseText; // emulate progress events for node... // this really lame we should probably just @@ -151,14 +166,14 @@ } this.waiting = false; - callback(null, xhr); + callback(null, this.xhr); } }.bind(this)); this.waiting = true; - xhr.send(this._seralize()); + this.xhr.send(this._serialize()); - return xhr; + return this.xhr; } }; |