rename funnel to eventbus

This commit is contained in:
Manuel Araoz 2015-02-20 16:20:00 -03:00
parent 3bb9d6a4d3
commit abf30895b9
2 changed files with 29 additions and 26 deletions

View File

@ -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;

View File

@ -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);
});
});