Skip to content

Commit

Permalink
some more try/catches around readyStates 2 and 3, now works properly …
Browse files Browse the repository at this point in the history
…in opera
  • Loading branch information
James Halliday committed Aug 20, 2011
1 parent 419cf54 commit 2a6fba6
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 38 deletions.
8 changes: 8 additions & 0 deletions example/streaming/index.html
@@ -0,0 +1,8 @@
<html>
<head>
<title>xhr</title>
<script type="text/javascript" src="/browserify.js"></script>
</head>
<body>
</body>
</html>
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 5 additions & 4 deletions request.js → lib/request.js
Expand Up @@ -24,11 +24,12 @@ var Request = module.exports = function (xhr, params) {
xhr.open(params.method || 'GET', 'http://' + uri, true);

var res = new Response;
res.on('ready', function () {
self.emit('response', res);
});

xhr.onreadystatechange = function () {
if (this.readyState === 2) {
self.emit('response', res);
}
res.response(this);
res.handle(this);
};
};

Expand Down
62 changes: 62 additions & 0 deletions lib/response.js
@@ -0,0 +1,62 @@
var EventEmitter = require('events').EventEmitter;

var Response = module.exports = function (res) {
this.offset = 0;
};

Response.prototype = new EventEmitter;

var capable = {
streaming : true,
status2 : true
};

Response.prototype.handle = function (res) {
if (res.readyState === 2 && capable.status2) {
try {
this.statusCode = res.status;
}
catch (err) {
capable.status2 = false;
}

if (capable.status2) {
this.emit('ready');
}
}
else if (capable.streaming && res.readyState === 3) {
try {
if (!this.statusCode) {
this.statusCode = res.status;
this.emit('ready');
}
}
catch (err) {}

try {
this.write(res);
}
catch (err) {
capable.streaming = false;
}
}
else if (res.readyState === 4) {
if (!this.statusCode) {
this.statusCode = res.status;
this.emit('ready');
}
this.write(res);

if (res.error) {
this.emit('error', res.responseText);
}
else this.emit('end');
}
};

Response.prototype.write = function (res) {
if (res.responseText.length > this.offset) {
this.emit('data', res.responseText.slice(this.offset));
this.offset = res.responseText.length;
}
};
34 changes: 0 additions & 34 deletions response.js

This file was deleted.

0 comments on commit 2a6fba6

Please sign in to comment.