Skip to content

Commit

Permalink
Made MessageEvent more spec compliant. Attibution for #28.
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Nov 21, 2013
1 parent e93f42f commit c87be41
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
4 changes: 4 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# [Git master](https://github.com/aslakhellesoy/eventsource-node/compare/v0.0.9...master)

* Expose `lastEventId` on messages. ([#28](https://github.com/aslakhellesoy/eventsource-node/pull/28) mbieser)

# [0.0.9](https://github.com/aslakhellesoy/eventsource-node/compare/v0.0.8...v0.0.9)

* Bugfix: old "last-event-id" used on reconnect ([#27](https://github.com/aslakhellesoy/eventsource-node/pull/27) Aslak Hellesøy)
Expand Down
18 changes: 12 additions & 6 deletions lib/eventsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ function EventSource(url, eventSourceInitDict) {
return;
}
if (message.id) lastEventId = message.id;
_emit(message.event || 'message', new MessageEvent(data, lastEventId));
var type = message.event || 'message';
_emit(type, new MessageEvent(type, {
data: data,
lastEventId: lastEventId
}));
});
} catch(e) {
_emit('error', e);
Expand Down Expand Up @@ -208,12 +212,14 @@ EventSource.prototype.addEventListener = function addEventListener(method, liste
/**
* W3C MessageEvent
*
* @see http://www.w3.org/TR/html5/comms.html
* @see http://www.w3.org/TR/webmessaging/#event-definitions
* @api private
*/
function MessageEvent(dataArg, idArg) {
// Currently only the data and id attributes are implemented. More can be added later if needed.
Object.defineProperty(this, 'data', { writable: false, value: dataArg });
Object.defineProperty(this, 'lastEventId', { writable: false, value: idArg });
function MessageEvent(type, eventInitDict) {
for(var f in eventInitDict) {
if(eventInitDict.hasOwnProperty(f)) {
Object.defineProperty(this, f, { writable: false, value: eventInitDict[f] });
}
}
}

6 changes: 3 additions & 3 deletions test/eventsource_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ describe('Events', function() {
createServer(["id: 123\ndata: sample_data\n\n"], function(port, close) {
var es = new EventSource('http://localhost:' + port);
es.onmessage = function(m) {
assert("123" == m.lastEventId);
assert.equal(m.lastEventId, "123");
es.close();
close(done);
};
Expand All @@ -838,8 +838,8 @@ describe('Events', function() {
}

function second(m) {
assert.equal("World", m.data);
assert.equal("123", m.lastEventId); //expect to get back the previous event id
assert.equal(m.data, "World");
assert.equal(m.lastEventId, "123"); //expect to get back the previous event id
es.close();
close(done);
}
Expand Down

0 comments on commit c87be41

Please sign in to comment.