Skip to content

Commit

Permalink
Fixes #30 Provide Event on open and error event
Browse files Browse the repository at this point in the history
  • Loading branch information
flowersinthesand committed Nov 15, 2013
1 parent dc13edc commit e4ae6b4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
22 changes: 17 additions & 5 deletions lib/eventsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function EventSource(url, eventSourceInitDict) {
if (connectPending || readyState === EventSource.CLOSED) return;
connectPending = true;
readyState = EventSource.CONNECTING;
_emit('error', err);
_emit('error', new Event('error'));

// The url may have been changed by a temporary
// redirect. If that's the case, revert it now.
Expand Down Expand Up @@ -84,7 +84,8 @@ function EventSource(url, eventSourceInitDict) {
// Handle HTTP redirects
if (res.statusCode == 301 || res.statusCode == 307) {
if (!res.headers.location) {
_emit('error', 'Server sent redirect response without Location header.');
// Server sent redirect response without Location header.
_emit('error', new Event('error'));
return;
}
if (res.statusCode == 307) reconnectUrl = url;
Expand All @@ -96,14 +97,15 @@ function EventSource(url, eventSourceInitDict) {
if (res.statusCode == 204) return self.close();

if (res.statusCode == 403) {
_emit('error', 'Access denied');
// 'Access denied'
_emit('error', new Event('error'));
return self.close();
}

readyState = EventSource.OPEN;
res.on('close', onConnectionClosed);
res.on('end', onConnectionClosed);
_emit('open');
_emit('open', new Event('open'));

var buf = '';
res.on('data', function (chunk) {
Expand All @@ -124,7 +126,7 @@ function EventSource(url, eventSourceInitDict) {
_emit(message.event || 'message', new MessageEvent(data));
});
} catch(e) {
_emit('error', e);
_emit('error', new Event('error'));
}
});
});
Expand Down Expand Up @@ -205,6 +207,16 @@ EventSource.prototype.addEventListener = function addEventListener(method, liste
}
};

/**
* W3C Event
*
* @see http://www.w3.org/TR/DOM-Level-3-Events/#interface-Event
* @api private
*/
function Event(type) {
Object.defineProperty(this, 'type', { writable: false, value: type });
}

/**
* W3C MessageEvent
*
Expand Down
6 changes: 4 additions & 2 deletions test/eventsource_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,8 @@ describe('Events', function() {
it('calls onopen when connection is established', function(done) {
createServer([], function(port, close) {
var es = new EventSource('http://localhost:' + port);
es.onopen = function() {
es.onopen = function(event) {
assert.equal(event.type, 'open');
es.close();
close(done);
}
Expand All @@ -795,7 +796,8 @@ describe('Events', function() {
it('emits open event when connection is established', function(done) {
createServer([], function(port, close) {
var es = new EventSource('http://localhost:' + port);
es.addEventListener('open', function() {
es.addEventListener('open', function(event) {
assert.equal(event.type, 'open');
es.close();
close(done);
});
Expand Down

0 comments on commit e4ae6b4

Please sign in to comment.