diff --git a/lib/funnel.js b/lib/eventbus.js similarity index 72% rename from lib/funnel.js rename to lib/eventbus.js index e0592528..fc999435 100644 --- a/lib/funnel.js +++ b/lib/eventbus.js @@ -7,12 +7,15 @@ var _ = bitcore.deps._; var EventEmitter = require('events').EventEmitter; var util = require('util'); -function Funnel() { +function EventBus() { this.handlers = {}; } -util.inherits(Funnel, EventEmitter); +util.inherits(EventBus, EventEmitter); -Funnel.prototype.process = function(e) { +EventBus.prototype.process = function(e) { + $.checkArgument(_.isObject(e)); + + var self = this; var queue = []; var done = []; queue.push(e); @@ -28,13 +31,13 @@ Funnel.prototype.process = function(e) { done.push(event); } done.forEach(function(event) { - //that.emit(event.name, event); + self.emit(event.name || event.constructor.name, event); }); }; -Funnel.prototype.register = function(clazz, handler) { +EventBus.prototype.register = function(clazz, handler) { $.checkArgument(_.isFunction(handler)); var name = clazz.name; this.handlers[name] = this.handlers[name] || []; @@ -42,4 +45,4 @@ Funnel.prototype.register = function(clazz, handler) { }; -module.exports = Funnel; +module.exports = EventBus; diff --git a/test/funnel.js b/test/eventbus.js similarity index 60% rename from test/funnel.js rename to test/eventbus.js index 54672667..1e7bba65 100644 --- a/test/funnel.js +++ b/test/eventbus.js @@ -4,13 +4,13 @@ var chai = require('chai'); var should = chai.should(); var sinon = require('sinon'); -var Funnel = require('../lib/funnel'); +var EventBus = require('../lib/eventbus'); -describe('Funnel', function() { +describe('EventBus', function() { it('instantiate', function() { - var f = new Funnel(); - should.exist(f); + var bus = new EventBus(); + should.exist(bus); }); describe('process', function() { @@ -22,47 +22,47 @@ describe('Funnel', function() { bar.y = 3; it('no handlers registered', function() { - var f = new Funnel(); - f.process.bind(f, foo).should.not.throw(); + var bus = new EventBus(); + bus.process.bind(bus, foo).should.not.throw(); }); it('simple handler gets called', function(cb) { - var f = new Funnel(); - f.register(FooEvent, function(e) { + var bus = new EventBus(); + bus.register(FooEvent, function(e) { e.x.should.equal(foo.x); cb(); }); - f.process(foo); + bus.process(foo); }); it('other event does not get called', function() { - var f = new Funnel(); + var bus = new EventBus(); var spy = sinon.spy(); - f.register(FooEvent, spy); - f.process(bar); + bus.register(FooEvent, spy); + bus.process(bar); spy.callCount.should.equal(0); }); it('foo returns bar', function(cb) { - var f = new Funnel(); - f.register(FooEvent, function(e) { + var bus = new EventBus(); + bus.register(FooEvent, function(e) { var b = new BarEvent(); b.y = e.x; return [b]; }); - f.register(BarEvent, function(e) { + bus.register(BarEvent, function(e) { e.y.should.equal(foo.x); cb(); }); - f.process(foo); + bus.process(foo); }); it('foo returns two bars', function() { - var f = new Funnel(); + var bus = new EventBus(); var spy = sinon.spy(); - f.register(FooEvent, function() { + bus.register(FooEvent, function() { var b1 = new BarEvent(); var b2 = new BarEvent(); return [b1, b2]; }); - f.register(BarEvent, spy); - f.process(foo); + bus.register(BarEvent, spy); + bus.process(foo); spy.callCount.should.equal(2); }); });