1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00

refactored eventStore

This commit is contained in:
Ivar Conradi Østhus 2014-10-22 16:53:15 +02:00
parent c8021e769a
commit d5b80c81eb
5 changed files with 28 additions and 18 deletions

View File

@ -1,6 +1,5 @@
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var events = require('./events');
function EventStore() {
EventEmitter.call(this);
@ -8,8 +7,16 @@ function EventStore() {
util.inherits(EventStore, EventEmitter);
EventStore.prototype.create = function (obj) {
this.emit(events.featureCreated, obj);
EventStore.prototype.create = function (eventType, user, eventData) {
var event = {
id: 1,
created: "2014-08-01 12:22:00",
type: eventType,
user: user,
data: eventData
};
this.emit(event.type, event);
};
module.exports = new EventStore();

View File

@ -0,0 +1,3 @@
module.exports = {
featureCreated : 'feature-created'
};

View File

@ -1,3 +0,0 @@
module.exports = {
featureCreated : 'featureCreated'
};

View File

@ -1,11 +1,10 @@
var db = require('./db');
var eventStore = require('./eventStore');
var db = require('./db')
eventStore = require('./eventStore'),
eventType = require('./eventType');
module.exports = function (app) {
app.get('/features', function (req, res) {
// TODO svelovla, fix this
eventStore.create({name: 'testing method'});
db.getFeatures().then(function (features) {
res.json({features: features});
});
@ -22,15 +21,16 @@ module.exports = function (app) {
});
app.post('/features', function (req, res) {
var newFeature = req.body;
var newFeature = req.body,
user = req.connection.remoteAddress;
db.getFeature(newFeature.name).then(function (feature) {
if (feature) {
//Todo: error-msg: feature name is already in use
res.status(403).end();
} else {
db.addFeature(newFeature).then(function () {
res.status(201).end();
});
eventStore.create(eventType.featureCreated, user, newFeature);
res.status(201).end();
}
});
});

View File

@ -1,21 +1,24 @@
var assert = require('assert');
var events = require('../lib/events');
var eventType = require('../lib/eventType');
var eventStore = require('../lib/eventStore');
describe('EventStore', function () {
describe('#create()', function () {
it('should emit event', function (done) {
eventStore.once(events.featureCreated, function (x) {
eventStore.once(eventType.featureCreated, function (x) {
assert(x);
done();
}
);
eventStore.create({
var eventData = {
'name': 'mail-server.validate-email-addresses',
'enabled': false,
'strategy': 'default',
'description': 'Feature description'
});
};
eventStore.create(eventType.featureCreated,"ole",eventData);
});
});
});