Permalink
Browse files

destroy() and "close" events

  • Loading branch information...
1 parent 44702c3 commit 7071d4b4fb7aa668eaa3219682ade175f1a14e72 @substack committed Nov 5, 2012
Showing with 17 additions and 6 deletions.
  1. +9 −0 lib/request.js
  2. +7 −5 lib/response.js
  3. +1 −1 package.json
View
@@ -30,6 +30,10 @@ var Request = module.exports = function (xhr, params) {
}
var res = new Response;
+ res.on('close', function () {
+ self.emit('close');
+ });
+
res.on('ready', function () {
self.emit('response', res);
});
@@ -57,6 +61,11 @@ Request.prototype.write = function (s) {
this.body.write(s);
};
+Request.prototype.destroy = function (s) {
+ this.xhr.abort();
+ this.emit('close');
+};
+
Request.prototype.end = function (s) {
if (s !== undefined) this.body.write(s);
this.body.end()
View
@@ -45,8 +45,8 @@ function parseHeaders (res) {
Response.prototype.getResponse = function (xhr) {
var respType = xhr.responseType.toLowerCase();
- if (respType === "blob") return xhr.responseBlob;
- if (respType === "arraybuffer") return xhr.response;
+ if (respType === 'blob') return xhr.responseBlob;
+ if (respType === 'arraybuffer') return xhr.response;
return xhr.responseText;
}
@@ -79,7 +79,7 @@ Response.prototype.handle = function (res) {
catch (err) {}
try {
- this.write(res);
+ this._emitData(res);
}
catch (err) {
capable.streaming = false;
@@ -90,16 +90,18 @@ Response.prototype.handle = function (res) {
this.statusCode = res.status;
this.emit('ready');
}
- this.write(res);
+ this._emitData(res);
if (res.error) {
this.emit('error', this.getResponse(res));
}
else this.emit('end');
+
+ this.emit('close');
}
};
-Response.prototype.write = function (res) {
+Response.prototype._emitData = function (res) {
var respBody = this.getResponse(res);
if (respBody.toString().match(/ArrayBuffer/)) {
this.emit('data', new Uint8Array(respBody, this.offset));
View
@@ -1,6 +1,6 @@
{
"name" : "http-browserify",
- "version" : "0.1.3",
+ "version" : "0.1.4",
"description" : "http module compatability for browserify",
"main" : "index.js",
"browserify" : "index.js",

0 comments on commit 7071d4b

Please sign in to comment.