From c87be41f80a53adfe753af18362b562bdaf929e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Thu, 21 Nov 2013 16:34:59 +0000 Subject: [PATCH] Made MessageEvent more spec compliant. Attibution for #28. --- History.md | 4 ++++ lib/eventsource.js | 18 ++++++++++++------ test/eventsource_test.js | 6 +++--- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/History.md b/History.md index 9040f44..caabe95 100644 --- a/History.md +++ b/History.md @@ -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) diff --git a/lib/eventsource.js b/lib/eventsource.js index ec6e409..8fac562 100644 --- a/lib/eventsource.js +++ b/lib/eventsource.js @@ -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); @@ -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] }); + } + } } diff --git a/test/eventsource_test.js b/test/eventsource_test.js index b22b2f3..246801b 100644 --- a/test/eventsource_test.js +++ b/test/eventsource_test.js @@ -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); }; @@ -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); }