rename funnel to eventbus
This commit is contained in:
parent
3bb9d6a4d3
commit
abf30895b9
@ -7,12 +7,15 @@ var _ = bitcore.deps._;
|
|||||||
var EventEmitter = require('events').EventEmitter;
|
var EventEmitter = require('events').EventEmitter;
|
||||||
var util = require('util');
|
var util = require('util');
|
||||||
|
|
||||||
function Funnel() {
|
function EventBus() {
|
||||||
this.handlers = {};
|
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 queue = [];
|
||||||
var done = [];
|
var done = [];
|
||||||
queue.push(e);
|
queue.push(e);
|
||||||
@ -28,13 +31,13 @@ Funnel.prototype.process = function(e) {
|
|||||||
done.push(event);
|
done.push(event);
|
||||||
}
|
}
|
||||||
done.forEach(function(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));
|
$.checkArgument(_.isFunction(handler));
|
||||||
var name = clazz.name;
|
var name = clazz.name;
|
||||||
this.handlers[name] = this.handlers[name] || [];
|
this.handlers[name] = this.handlers[name] || [];
|
||||||
@ -42,4 +45,4 @@ Funnel.prototype.register = function(clazz, handler) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
module.exports = Funnel;
|
module.exports = EventBus;
|
||||||
@ -4,13 +4,13 @@ var chai = require('chai');
|
|||||||
var should = chai.should();
|
var should = chai.should();
|
||||||
var sinon = require('sinon');
|
var sinon = require('sinon');
|
||||||
|
|
||||||
var Funnel = require('../lib/funnel');
|
var EventBus = require('../lib/eventbus');
|
||||||
|
|
||||||
describe('Funnel', function() {
|
describe('EventBus', function() {
|
||||||
|
|
||||||
it('instantiate', function() {
|
it('instantiate', function() {
|
||||||
var f = new Funnel();
|
var bus = new EventBus();
|
||||||
should.exist(f);
|
should.exist(bus);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('process', function() {
|
describe('process', function() {
|
||||||
@ -22,47 +22,47 @@ describe('Funnel', function() {
|
|||||||
bar.y = 3;
|
bar.y = 3;
|
||||||
|
|
||||||
it('no handlers registered', function() {
|
it('no handlers registered', function() {
|
||||||
var f = new Funnel();
|
var bus = new EventBus();
|
||||||
f.process.bind(f, foo).should.not.throw();
|
bus.process.bind(bus, foo).should.not.throw();
|
||||||
});
|
});
|
||||||
it('simple handler gets called', function(cb) {
|
it('simple handler gets called', function(cb) {
|
||||||
var f = new Funnel();
|
var bus = new EventBus();
|
||||||
f.register(FooEvent, function(e) {
|
bus.register(FooEvent, function(e) {
|
||||||
e.x.should.equal(foo.x);
|
e.x.should.equal(foo.x);
|
||||||
cb();
|
cb();
|
||||||
});
|
});
|
||||||
f.process(foo);
|
bus.process(foo);
|
||||||
});
|
});
|
||||||
it('other event does not get called', function() {
|
it('other event does not get called', function() {
|
||||||
var f = new Funnel();
|
var bus = new EventBus();
|
||||||
var spy = sinon.spy();
|
var spy = sinon.spy();
|
||||||
f.register(FooEvent, spy);
|
bus.register(FooEvent, spy);
|
||||||
f.process(bar);
|
bus.process(bar);
|
||||||
spy.callCount.should.equal(0);
|
spy.callCount.should.equal(0);
|
||||||
});
|
});
|
||||||
it('foo returns bar', function(cb) {
|
it('foo returns bar', function(cb) {
|
||||||
var f = new Funnel();
|
var bus = new EventBus();
|
||||||
f.register(FooEvent, function(e) {
|
bus.register(FooEvent, function(e) {
|
||||||
var b = new BarEvent();
|
var b = new BarEvent();
|
||||||
b.y = e.x;
|
b.y = e.x;
|
||||||
return [b];
|
return [b];
|
||||||
});
|
});
|
||||||
f.register(BarEvent, function(e) {
|
bus.register(BarEvent, function(e) {
|
||||||
e.y.should.equal(foo.x);
|
e.y.should.equal(foo.x);
|
||||||
cb();
|
cb();
|
||||||
});
|
});
|
||||||
f.process(foo);
|
bus.process(foo);
|
||||||
});
|
});
|
||||||
it('foo returns two bars', function() {
|
it('foo returns two bars', function() {
|
||||||
var f = new Funnel();
|
var bus = new EventBus();
|
||||||
var spy = sinon.spy();
|
var spy = sinon.spy();
|
||||||
f.register(FooEvent, function() {
|
bus.register(FooEvent, function() {
|
||||||
var b1 = new BarEvent();
|
var b1 = new BarEvent();
|
||||||
var b2 = new BarEvent();
|
var b2 = new BarEvent();
|
||||||
return [b1, b2];
|
return [b1, b2];
|
||||||
});
|
});
|
||||||
f.register(BarEvent, spy);
|
bus.register(BarEvent, spy);
|
||||||
f.process(foo);
|
bus.process(foo);
|
||||||
spy.callCount.should.equal(2);
|
spy.callCount.should.equal(2);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
Loading…
Reference in New Issue
Block a user