From abf30895b90e40d7e12989c0865409af56436709 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Fri, 20 Feb 2015 16:20:00 -0300 Subject: [PATCH 1/6] rename funnel to eventbus --- lib/{funnel.js => eventbus.js} | 15 ++++++++----- test/{funnel.js => eventbus.js} | 40 ++++++++++++++++----------------- 2 files changed, 29 insertions(+), 26 deletions(-) rename lib/{funnel.js => eventbus.js} (72%) rename test/{funnel.js => eventbus.js} (60%) 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); }); }); From cdf8dab86c3e25e50ecc8673762faaf6852e0d13 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Fri, 20 Feb 2015 17:52:49 -0300 Subject: [PATCH 2/6] promisify event bus --- lib/eventbus.js | 42 ++++++++++++++++++++++++++---------------- test/eventbus.js | 17 +++++++++++++++++ 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/lib/eventbus.js b/lib/eventbus.js index fc999435..20da9316 100644 --- a/lib/eventbus.js +++ b/lib/eventbus.js @@ -2,6 +2,7 @@ var bitcore = require('bitcore'); +var Promise = require('bluebird'); var $ = bitcore.util.preconditions; var _ = bitcore.deps._; var EventEmitter = require('events').EventEmitter; @@ -16,24 +17,33 @@ EventBus.prototype.process = function(e) { $.checkArgument(_.isObject(e)); var self = this; - var queue = []; var done = []; - queue.push(e); - while (queue.length !== 0) { - var event = queue.shift(); - var handlers = this.handlers[event.constructor.name] || []; - handlers.forEach(function(handler) { - var responses = handler(event); - if (responses && responses.length > 0) { - queue = queue.concat(responses); - } - }); - done.push(event); - } - done.forEach(function(event) { - self.emit(event.name || event.constructor.name, event); - }); + var processEvent = function(event) { + done = done.concat(event); + var handlers = self.handlers[event.constructor.name] || []; + var whenHandlersResolve = Promise.all(handlers.map(function(handler) { + return handler(event); + })); + return whenHandlersResolve.each(function(events) { + if (_.isUndefined(events)) { + events = []; + } + if (!_.isArray(events)) { + events = [events]; + } + return Promise.all( + events.map(processEvent) + ); + }); + }; + var eventsEmitted = processEvent(e) + .then(function() { + done.forEach(function(event) { + self.emit(event.name || event.constructor.name, event); + }); + }); + return eventsEmitted; }; diff --git a/test/eventbus.js b/test/eventbus.js index 1e7bba65..ccd74df5 100644 --- a/test/eventbus.js +++ b/test/eventbus.js @@ -5,6 +5,7 @@ var should = chai.should(); var sinon = require('sinon'); var EventBus = require('../lib/eventbus'); +require('bluebird').longStackTraces(); describe('EventBus', function() { @@ -15,6 +16,7 @@ describe('EventBus', function() { describe('process', function() { function FooEvent() {} + function BarEvent() {} var foo = new FooEvent(); var bar = new BarEvent(); @@ -65,6 +67,21 @@ describe('EventBus', function() { bus.process(foo); spy.callCount.should.equal(2); }); + it('foo returns two bars and emits external events', function(cb) { + var bus = new EventBus(); + var b1 = new BarEvent(); + var b2 = new BarEvent(); + var spy = sinon.spy(bus, 'emit'); + bus.register(FooEvent, function() { + return [b1, b2]; + }); + bus.process(foo) + .then(function() { + spy.calledWith('BarEvent', b1).should.equal(true); + spy.calledWith('BarEvent', b2).should.equal(true); + }) + .then(cb); + }); }); }); From 868c4ef9c31fa403ce7ffc7552b88d5410cc690c Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Fri, 20 Feb 2015 18:06:07 -0300 Subject: [PATCH 3/6] add async test --- .jshintrc | 5 ++++- test/eventbus.js | 22 +++++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/.jshintrc b/.jshintrc index 40fd8c6f..73413260 100644 --- a/.jshintrc +++ b/.jshintrc @@ -41,5 +41,8 @@ "it", "module", "require" - ] + ], + "globals" : { + "Promise" : true + } } diff --git a/test/eventbus.js b/test/eventbus.js index ccd74df5..7e2a5846 100644 --- a/test/eventbus.js +++ b/test/eventbus.js @@ -4,8 +4,9 @@ var chai = require('chai'); var should = chai.should(); var sinon = require('sinon'); +var Promise = require('bluebird'); var EventBus = require('../lib/eventbus'); -require('bluebird').longStackTraces(); +Promise.longStackTraces(); describe('EventBus', function() { @@ -55,12 +56,12 @@ describe('EventBus', function() { }); bus.process(foo); }); + var b1 = new BarEvent(); + var b2 = new BarEvent(); it('foo returns two bars', function() { var bus = new EventBus(); var spy = sinon.spy(); bus.register(FooEvent, function() { - var b1 = new BarEvent(); - var b2 = new BarEvent(); return [b1, b2]; }); bus.register(BarEvent, spy); @@ -69,8 +70,6 @@ describe('EventBus', function() { }); it('foo returns two bars and emits external events', function(cb) { var bus = new EventBus(); - var b1 = new BarEvent(); - var b2 = new BarEvent(); var spy = sinon.spy(bus, 'emit'); bus.register(FooEvent, function() { return [b1, b2]; @@ -82,6 +81,19 @@ describe('EventBus', function() { }) .then(cb); }); + it('foo returns two async bars', function(cb) { + var bus = new EventBus(); + var spy = sinon.spy(); + bus.register(FooEvent, function() { + return Promise.resolve([b1, b2]).delay(1); + }); + bus.register(BarEvent, spy); + bus.process(foo) + .then(function() { + spy.callCount.should.equal(2); + }) + .then(cb); + }); }); }); From 7377b34cec4ab645c59f8011d05b5d8a7eb08566 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Fri, 20 Feb 2015 18:25:50 -0300 Subject: [PATCH 4/6] add test --- test/eventbus.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/eventbus.js b/test/eventbus.js index 7e2a5846..350a1f97 100644 --- a/test/eventbus.js +++ b/test/eventbus.js @@ -57,7 +57,9 @@ describe('EventBus', function() { bus.process(foo); }); var b1 = new BarEvent(); + b1.x = 42; var b2 = new BarEvent(); + b2.x = 69; it('foo returns two bars', function() { var bus = new EventBus(); var spy = sinon.spy(); @@ -94,6 +96,25 @@ describe('EventBus', function() { }) .then(cb); }); + it('events are not externalized when async processing fails', function(cb) { + var bus = new EventBus(); + var spy = sinon.spy(bus, 'emit'); + var err = new Error(); + bus.register(FooEvent, function() { + return Promise.resolve([b1, b2]).delay(1); + }); + bus.register(BarEvent, function(e) { + if (e.x === b1.x) { + throw err; + } + }); + bus.process(foo) + .catch(function(reason) { + reason.should.equal(err); + spy.callCount.should.equal(0); + cb(); + }); + }); }); }); From e077488a79a5182b697ed1dad5781bc005286624 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Fri, 20 Feb 2015 18:33:12 -0300 Subject: [PATCH 5/6] 100% test coverage --- test/eventbus.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/eventbus.js b/test/eventbus.js index 350a1f97..b890f290 100644 --- a/test/eventbus.js +++ b/test/eventbus.js @@ -48,7 +48,7 @@ describe('EventBus', function() { bus.register(FooEvent, function(e) { var b = new BarEvent(); b.y = e.x; - return [b]; + return b; }); bus.register(BarEvent, function(e) { e.y.should.equal(foo.x); From 5d798ab5e3ed87ba1d8591e7a3c68a12345abeb3 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Fri, 20 Feb 2015 18:51:16 -0300 Subject: [PATCH 6/6] make coveralls werk --- lib/databases/transaction.js | 15 +-------------- package.json | 21 +++++---------------- 2 files changed, 6 insertions(+), 30 deletions(-) diff --git a/lib/databases/transaction.js b/lib/databases/transaction.js index 37e50733..370d3ea6 100644 --- a/lib/databases/transaction.js +++ b/lib/databases/transaction.js @@ -45,20 +45,7 @@ var db = imports.db || levelup(config.leveldb + '/txs', { maxOpenFiles: MAX_OPEN_FILES }); var PoolMatch = imports.poolMatch || require('soop').load('./PoolMatch', config); -// This is 0.1.2 = > c++ version of base58-native -var base58 = require('base58-native').base58Check; -var encodedData = require('soop').load('bitcore/util/EncodedData', { - base58: base58 -}); -var versionedData = require('soop').load('bitcore/util/VersionedData', { - parent: encodedData -}); - -var Address = require('soop').load('bitcore/lib/Address', { - parent: versionedData -}); - - +var Address = require('bitcore').Address; var TransactionDb = function() { TransactionDb.super(this, arguments); diff --git a/package.json b/package.json index 17c7fef6..34b9d5b2 100644 --- a/package.json +++ b/package.json @@ -43,15 +43,13 @@ "start": "node node_modules/grunt-cli/bin/grunt" }, "dependencies": { - "async": "*", - "base58-native": "0.1.2", + "async": "0.9.0", "bignum": "*", - "bitauth": "^0.1.1", - "bitcore": "git://github.com/bitpay/bitcore.git#aa41c70cff2583d810664c073a324376c39c8b36", + "bitcore": "0.10.4", + "bluebird": "^2.9.12", "bufferput": "git://github.com/bitpay/node-bufferput.git", "buffertools": "*", "commander": "^2.3.0", - "connect-ratelimit": "git://github.com/dharmafly/connect-ratelimit.git#0550eff209c54f35078f46445000797fa942ab97", "cron": "^1.0.4", "express": "~3.4.7", "glob": "*", @@ -61,7 +59,6 @@ "microtime": "^0.6.0", "mkdirp": "^0.5.0", "moment": "~2.5.0", - "nodemailer": "^1.3.0", "preconditions": "^1.0.7", "request": "^2.48.0", "socket.io": "1.0.6", @@ -71,17 +68,9 @@ "xmlhttprequest": "~1.6.0" }, "devDependencies": { - "gulp": "^3.8.10", - "gulp-bump": "^0.1.11", - "gulp-coveralls": "^0.1.3", - "gulp-jshint": "^1.9.0", - "gulp-mocha": "^2.0.0", - "gulp-shell": "^0.2.10", - "istanbul": "^0.3.5", - "mocha": "^2.0.1", - "plato": "^1.3.0", + "bitcore-build": "bitpay/bitcore-build", "chai": "*", - "memdown": "^0.10.2", + "gulp": "^3.8.10", "should": "^2.1.1", "sinon": "^1.10.3" }