check for duplicate events
This commit is contained in:
parent
7ac429fbd2
commit
a0e40ffd15
@ -247,13 +247,13 @@ DB.prototype.estimateFee = function(blocks, callback) {
|
|||||||
DB.prototype.getPublishEvents = function() {
|
DB.prototype.getPublishEvents = function() {
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
name: 'transaction',
|
name: 'db/transaction',
|
||||||
scope: this,
|
scope: this,
|
||||||
subscribe: this.subscribe.bind(this, 'transaction'),
|
subscribe: this.subscribe.bind(this, 'transaction'),
|
||||||
unsubscribe: this.unsubscribe.bind(this, 'transaction')
|
unsubscribe: this.unsubscribe.bind(this, 'transaction')
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'block',
|
name: 'db/block',
|
||||||
scope: this,
|
scope: this,
|
||||||
subscribe: this.subscribe.bind(this, 'block'),
|
subscribe: this.subscribe.bind(this, 'block'),
|
||||||
unsubscribe: this.unsubscribe.bind(this, 'block')
|
unsubscribe: this.unsubscribe.bind(this, 'block')
|
||||||
|
|||||||
@ -6,6 +6,8 @@ var bodyParser = require('body-parser');
|
|||||||
var socketio = require('socket.io');
|
var socketio = require('socket.io');
|
||||||
var BaseService = require('../service');
|
var BaseService = require('../service');
|
||||||
var inherits = require('util').inherits;
|
var inherits = require('util').inherits;
|
||||||
|
var index = require('../');
|
||||||
|
var log = index.log;
|
||||||
|
|
||||||
var WebService = function(options) {
|
var WebService = function(options) {
|
||||||
var self = this;
|
var self = this;
|
||||||
@ -13,6 +15,7 @@ var WebService = function(options) {
|
|||||||
this.port = options.port || this.node.port || 3456;
|
this.port = options.port || this.node.port || 3456;
|
||||||
|
|
||||||
this.node.on('ready', function() {
|
this.node.on('ready', function() {
|
||||||
|
self.eventNames = self.getEventNames();
|
||||||
self.setupAllRoutes();
|
self.setupAllRoutes();
|
||||||
self.server.listen(self.port);
|
self.server.listen(self.port);
|
||||||
self.createMethodsMap();
|
self.createMethodsMap();
|
||||||
@ -51,8 +54,14 @@ WebService.prototype.stop = function(callback) {
|
|||||||
WebService.prototype.setupAllRoutes = function() {
|
WebService.prototype.setupAllRoutes = function() {
|
||||||
for(var key in this.node.services) {
|
for(var key in this.node.services) {
|
||||||
var subApp = new express.Router();
|
var subApp = new express.Router();
|
||||||
this.app.use('/' + this.node.services[key].getRoutePrefix(), subApp);
|
var service = this.node.services[key];
|
||||||
this.node.services[key].setupRoutes(subApp, express);
|
|
||||||
|
if(service.getRoutePrefix && service.setupRoutes) {
|
||||||
|
this.app.use('/' + this.node.services[key].getRoutePrefix(), subApp);
|
||||||
|
this.node.services[key].setupRoutes(subApp, express);
|
||||||
|
} else {
|
||||||
|
log.info('Not setting up routes for ' + service.name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -73,7 +82,32 @@ WebService.prototype.createMethodsMap = function() {
|
|||||||
args: args
|
args: args
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
|
WebService.prototype.getEventNames = function() {
|
||||||
|
var events = this.node.getAllPublishEvents();
|
||||||
|
var eventNames = [];
|
||||||
|
|
||||||
|
function addEventName(name) {
|
||||||
|
if(eventNames.indexOf(name) > -1) {
|
||||||
|
throw new Error('Duplicate event ' + name);
|
||||||
|
}
|
||||||
|
|
||||||
|
eventNames.push(name);
|
||||||
|
};
|
||||||
|
|
||||||
|
events.forEach(function(event) {
|
||||||
|
addEventName(event.name);
|
||||||
|
|
||||||
|
if(event.extraEvents) {
|
||||||
|
event.extraEvents.forEach(function(name) {
|
||||||
|
addEventName(name);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return eventNames;
|
||||||
|
};
|
||||||
|
|
||||||
WebService.prototype.socketHandler = function(socket) {
|
WebService.prototype.socketHandler = function(socket) {
|
||||||
var self = this;
|
var self = this;
|
||||||
@ -90,23 +124,7 @@ WebService.prototype.socketHandler = function(socket) {
|
|||||||
bus.unsubscribe(name, params);
|
bus.unsubscribe(name, params);
|
||||||
});
|
});
|
||||||
|
|
||||||
var events = self.node.getAllPublishEvents();
|
this.eventNames.forEach(function(eventName) {
|
||||||
var eventNames = [];
|
|
||||||
|
|
||||||
events.forEach(function(event) {
|
|
||||||
eventNames.push(event.name);
|
|
||||||
|
|
||||||
if(event.extraEvents) {
|
|
||||||
eventNames = eventNames.concat(event.extraEvents);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
eventNames = eventNames.filter(function(value, index, self) {
|
|
||||||
// remove any duplicates
|
|
||||||
return self.indexOf(value) === index;
|
|
||||||
});
|
|
||||||
|
|
||||||
eventNames.forEach(function(eventName) {
|
|
||||||
bus.on(eventName, function() {
|
bus.on(eventName, function() {
|
||||||
if(socket.connected) {
|
if(socket.connected) {
|
||||||
var results = [];
|
var results = [];
|
||||||
|
|||||||
@ -102,6 +102,54 @@ describe('WebService', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('#getEventNames', function() {
|
||||||
|
it('should get event names', function() {
|
||||||
|
var Module1 = function() {};
|
||||||
|
Module1.prototype.getPublishEvents = function() {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
name: 'event1',
|
||||||
|
extraEvents: ['event2']
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
|
||||||
|
var module1 = new Module1();
|
||||||
|
var node = {
|
||||||
|
on: sinon.spy(),
|
||||||
|
getAllPublishEvents: sinon.stub().returns(module1.getPublishEvents())
|
||||||
|
};
|
||||||
|
|
||||||
|
var web = new WebService({node: node});
|
||||||
|
var events = web.getEventNames();
|
||||||
|
|
||||||
|
events.should.deep.equal(['event1', 'event2']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw an error if there is a duplicate event', function() {
|
||||||
|
var Module1 = function() {};
|
||||||
|
Module1.prototype.getPublishEvents = function() {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
name: 'event1',
|
||||||
|
extraEvents: ['event1']
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
|
||||||
|
var module1 = new Module1();
|
||||||
|
var node = {
|
||||||
|
on: sinon.spy(),
|
||||||
|
getAllPublishEvents: sinon.stub().returns(module1.getPublishEvents())
|
||||||
|
};
|
||||||
|
|
||||||
|
var web = new WebService({node: node});
|
||||||
|
(function() {
|
||||||
|
var events = web.getEventNames();
|
||||||
|
}).should.throw('Duplicate event event1');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('#socketHandler', function() {
|
describe('#socketHandler', function() {
|
||||||
var bus = new EventEmitter();
|
var bus = new EventEmitter();
|
||||||
|
|
||||||
@ -127,6 +175,7 @@ describe('WebService', function() {
|
|||||||
|
|
||||||
it('on message should call socketMessageHandler', function(done) {
|
it('on message should call socketMessageHandler', function(done) {
|
||||||
web = new WebService({node: node});
|
web = new WebService({node: node});
|
||||||
|
web.eventNames = web.getEventNames();
|
||||||
web.socketMessageHandler = function(param1) {
|
web.socketMessageHandler = function(param1) {
|
||||||
param1.should.equal('data');
|
param1.should.equal('data');
|
||||||
done();
|
done();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user