Permalink
Browse files

client library improved, fixed bug with missed request body

  • Loading branch information...
1 parent 9e20fbd commit ea2df8d0c445118107b13aa794de48cb6b62cefd @dimsmol committed Feb 1, 2013
Showing with 59 additions and 10 deletions.
  1. +1 −6 TODO.md
  2. +10 −1 client/src/apis/errors.js
  3. +36 −1 client/src/apis/http_request.js
  4. +12 −2 client/src/apis/jsonp_request.js
View
@@ -1,13 +1,8 @@
* client
* body parsing options
* headers translation options
- * error handling
- * httprequest - try{open(), send() ? }, listen 'error', 'timeout' events
- * abort
- * timeout
- * review frame
- * batch
* fix request id for sockets
+ * batch
## WARN
View
@@ -30,9 +30,18 @@ inherits(NetworkError, ErrorBase);
NetworkError.prototype.name = 'NetworkError';
+var TimeoutError = function () {
+ ErrorBase.call(this);
+};
+inherits(TimeoutError, ErrorBase);
+
+TimeoutError.prototype.name = 'TimeoutError';
+
+
return {
WebError: WebError,
- NetworkError: NetworkError
+ NetworkError: NetworkError,
+ TimeoutError: TimeoutError
};
});
@@ -14,6 +14,8 @@ var HttpRequest = function (http, path, method, headers, data, options, cb) {
this.url = null;
this.body = null;
this.transport = null;
+ this.isAborted = false;
+ this.timeout = null;
};
HttpRequest.prototype.methodHttpHeaderName = 'X-Method';
@@ -30,6 +32,7 @@ HttpRequest.prototype.xdomainUrlKey = 'xdomain';
HttpRequest.prototype.defaultContentType = 'application/json';
HttpRequest.prototype.send = function () {
+ this.createTimeout();
this.url = this.createUrl();
this.body = this.createBody();
this.transport = this.createTransport();
@@ -38,6 +41,36 @@ HttpRequest.prototype.send = function () {
return this;
};
+HttpRequest.prototype.abort = function () {
+ this.isAborted = true;
+ this.clearTimeout();
+ if (this.transport != null) {
+ this.transport.onreadystatechange = null;
+ this.transport.abort();
+ }
+};
+
+HttpRequest.prototype.createTimeout = function () {
+ var timeout = this.options.timeout;
+ if (timeout != null) {
+ var self = this;
+ this.timeout = setTimeout(function () {
+ self.handleTimeout();
+ }, timeout);
+ }
+};
+
+HttpRequest.prototype.handleTimeout = function () {
+ this.abort();
+ this.cb(new errors.TimeoutError());
+};
+
+HttpRequest.prototype.clearTimeout = function () {
+ if (this.timeout != null) {
+ clearTimeout(this.timeout);
+ }
+};
+
HttpRequest.prototype.createUrl = function () {
var result = this.path;
if (this.http.baseUri) {
@@ -84,6 +117,7 @@ HttpRequest.prototype.isNetworkError = function () {
};
HttpRequest.prototype.handleNetworkError = function () {
+ this.clearTimeout();
this.cb(new errors.NetworkError());
};
@@ -125,7 +159,7 @@ HttpRequest.prototype.sendPost = function () {
this.setContentTypeHttpHeader();
this.setMethodHttpHeader();
this.setHttpHeaders();
- transport.send();
+ transport.send(this.body);
};
HttpRequest.prototype.setHttpHeaders = function () {
@@ -161,6 +195,7 @@ HttpRequest.prototype.setMethodHttpHeader = function () {
};
HttpRequest.prototype.handleResponse = function () {
+ this.clearTimeout();
var result = this.createResult();
var err = this.extractError(result);
if (err != null) {
@@ -17,6 +17,11 @@ JsonpRequest.prototype.callbacksGlobalPath = 'apis.jsonp.callbacks';
JsonpRequest.prototype.xdomainValue = 'jsonp';
JsonpRequest.prototype.jsonpCallbackUrlKey = 'callback';
+JsonpRequest.prototype.abort = function () {
+ this.isAborted = true;
+ this.clearTimeout();
+};
+
JsonpRequest.prototype.getCallbacks = function () {
if (JsonpRequest.callbacks == null) {
var parts = this.callbacksGlobalPath.split('.');
@@ -45,7 +50,9 @@ JsonpRequest.prototype.createTransport = function () {
JsonpRequest.prototype.handleScriptErrorEvent = function (ev) {
this.cleanup();
- this.cb(new errors.NetworkError());
+ if (!this.isAborted) {
+ this.cb(new errors.NetworkError());
+ }
};
JsonpRequest.prototype.createCallback = function () {
@@ -105,10 +112,13 @@ JsonpRequest.prototype.setHttpHeaders = function () {
JsonpRequest.prototype.handleResponse = function () {
this.cleanup();
- JsonpRequest.super_.prototype.handleResponse.call(this);
+ if (!this.isAborted) {
+ JsonpRequest.super_.prototype.handleResponse.call(this);
+ }
};
JsonpRequest.prototype.cleanup = function () {
+ this.clearTimeout();
this.removeCallback();
this.removeScripEl();
};

0 comments on commit ea2df8d

Please sign in to comment.