diff --git a/index.js b/index.js index 8b2d508..867698b 100644 --- a/index.js +++ b/index.js @@ -33,10 +33,8 @@ function Dispatcher() { */ dispatcher.register = function(action, callback) { - this.callbacks.push({ - action: action, - callback: callback - }); + if (!this.callbacks[action]) this.callbacks[action] = []; + this.callbacks[action].push(callback); }; /** @@ -51,7 +49,11 @@ dispatcher.register = function(action, callback) { */ dispatcher.dispatch = function(action, data) { - this.getCallbacks(action) + if (undefined == data) throw new Error('Dispatcher.dispatch: no data provided.'); + var array = this.callbacks[action]; + if (undefined == array) throw new Error('Dispatcher.dispatch: action is not registered'); + + this.callbacks[action] .forEach(function(callback) { callback.call(callback, data); }); diff --git a/test.js b/test.js index c089f0e..58d263c 100644 --- a/test.js +++ b/test.js @@ -22,29 +22,47 @@ describe('#dispatcher()', function () { it('should save the action', function (done) { var dispatcher = Dispatcher(); dispatcher.register('test', function() {return 3}); - dispatcher.callbacks[0].action.should.equal('test'); + dispatcher.callbacks['test'].should.exist; done(); }); it('should save the callback', function (done) { var dispatcher = Dispatcher(); dispatcher.register('test', function() {return 3}); - dispatcher.callbacks[0].callback.call().should.equal(3); + dispatcher.callbacks['test'][0].call().should.equal(3); done(); }); }); describe('.dispatch()', function () { + describe('when no data argument is provided', function () { + it('should throw', function (done) { + var dispatcher = Dispatcher(); + dispatcher.dispatch.bind(dispatcher, 'something') + .should.throw('Dispatcher.dispatch: no data provided.'); + + done(); + }); + }); + describe('when the action is not found', function () { + it('should throw', function (done) { + var dispatcher = Dispatcher(); + dispatcher.dispatch.bind(dispatcher, 'something', {}) + .should.throw('Dispatcher.dispatch: action is not registered'); + + done(); + }); + }); describe('with only one item stored', function () { it('should trigger the callback when called', function (done) { var dispatcher = Dispatcher(); var count = 0; - dispatcher.callbacks = [ - {action: 'trigger', callback: function(arg) {count += arg}} - ]; + dispatcher.callbacks = { + 'trigger': [function(arg) {count += arg}] + }; - dispatcher.dispatch('nothing', 3); + dispatcher.dispatch.bind(dispatcher, 'nothing', 3).should.throw(); count.should.eql(0); dispatcher.dispatch('trigger', 3); @@ -59,13 +77,15 @@ describe('#dispatcher()', function () { var dispatcher = Dispatcher(); var count = 0; - dispatcher.callbacks = [ - {action: 'trigger', callback: function(arg) {count += arg}}, - {action: 'finger', callback: function(arg) {count += arg}}, - {action: 'finger', callback: function(arg) {count += arg}} - ]; + dispatcher.callbacks = { + 'trigger': [function(arg) {count += arg}], + 'finger': [ + function(arg) {count += arg}, + function(arg) {count += arg} + ] + }; - dispatcher.dispatch('nothing', 3); + dispatcher.dispatch.bind(dispatcher, 'nothing', 3).should.throw(); count.should.eql(0); dispatcher.dispatch('trigger', 3);