Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Commit

Permalink
Add error for dispatching within a dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ridgway committed Nov 25, 2014
1 parent 2f3e8f7 commit 5c102b5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/Dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion tests/mock/Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -49,7 +53,8 @@ Store.handlers = {
this.state.page = 'home';
},
'DELAY': 'delay',
'ERROR': 'error'
'ERROR': 'error',
'DISPATCH': 'dispatch'
};

module.exports = Store;
11 changes: 11 additions & 0 deletions tests/unit/lib/Dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 5c102b5

Please sign in to comment.