diff --git a/lib/Dispatcher.js b/lib/Dispatcher.js index 55fdf65..d39dc8e 100644 --- a/lib/Dispatcher.js +++ b/lib/Dispatcher.js @@ -147,6 +147,9 @@ module.exports = function () { * @throws {Error} if store has handler registered that does not exist */ Dispatcher.prototype.dispatch = function dispatch(actionName, payload) { + if (this.currentAction) { + throw new Error('Cannot call dispatch while another dispatch is executing'); + } var actionHandlers = Dispatcher.handlers[actionName] || [], defaultHandlers = Dispatcher.handlers[DEFAULT] || []; if (!actionHandlers.length && !defaultHandlers.length) { diff --git a/tests/mock/Store.js b/tests/mock/Store.js index d3b6b2d..3da206a 100644 --- a/tests/mock/Store.js +++ b/tests/mock/Store.js @@ -31,6 +31,10 @@ Store.prototype.delay = function (payload) { }); }; +Store.prototype.dispatch = function (payload) { + payload.dispatcher.dispatch('DISPATCH_IN_DISPATCH'); +}; + Store.prototype.getState = function () { return this.state; }; @@ -49,7 +53,8 @@ Store.handlers = { this.state.page = 'home'; }, 'DELAY': 'delay', - 'ERROR': 'error' + 'ERROR': 'error', + 'DISPATCH': 'dispatch' }; module.exports = Store; diff --git a/tests/unit/lib/Dispatcher.js b/tests/unit/lib/Dispatcher.js index 0637501..cd7b3d1 100644 --- a/tests/unit/lib/Dispatcher.js +++ b/tests/unit/lib/Dispatcher.js @@ -154,6 +154,17 @@ describe('Dispatchr', function () { dispatcher.dispatch('ERROR', {}); }).to.throw(); }); + + it('should throw if a dispatch called within dispatch', function () { + var context = {test: 'test'}, + dispatcher = new Dispatcher(context); + + expect(function () { + dispatcher.dispatch('DISPATCH', { + dispatcher: dispatcher + }); + }).to.throw(); + }); }); describe('#dehydrate', function () {