Skip to content

Commit

Permalink
Replace array lookup with object lookup, speeding things up significa…
Browse files Browse the repository at this point in the history
…ntly
  • Loading branch information
yoshuawuyts committed May 28, 2014
1 parent f788366 commit 6329407
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

/**
Expand All @@ -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);
});
Expand Down
44 changes: 32 additions & 12 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 6329407

Please sign in to comment.