Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/eventDispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,13 @@ EventDispatcher.prototype = {
}

if (events) {

for (var i = 0, len = events.length; i < len; i++) {
self.undispatchedEventsQueue.push(events[i]);
// If there are a lot of events then we can hit issues with the call stack size when processing in one go
if(i % 1000 === 0){
trigger(self);
}
}
}

Expand Down
40 changes: 40 additions & 0 deletions test/eventDispatcherTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,46 @@ describe('EventDispatcher', function () {

});

it('should not crash when there are lots of pending events', function (done) {

var eventsInStore = [];

for(var i = 0; i < 10000; i++){
eventsInStore.push({
payload: {
index: i
}
});
}

function getUndispatchedEvents (callback) {
callback(null, eventsInStore);
}

var publishedEvents = [];

function publisher (evt) {
publishedEvents.push(evt);
check();
}

function check () {
if (publishedEvents.length === eventsInStore.length) {
done();
}
}

var eventDispatcher = new EventDispatcher(publisher, {
getUndispatchedEvents: getUndispatchedEvents,
setEventToDispatched: function (evt, callback) { callback(null); }});
expect(eventDispatcher.undispatchedEventsQueue.length).to.eql(0);

eventDispatcher.start(function(err) {
expect(err).not.to.be.ok();
});

});

});

describe('and calling addUndispatchedEvents', function () {
Expand Down