mirror of
https://github.com/Unleash/unleash.git
synced 2024-12-28 00:06:53 +01:00
refactored eventStore
This commit is contained in:
parent
c8021e769a
commit
d5b80c81eb
@ -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();
|
3
unleash-server/lib/eventType.js
Normal file
3
unleash-server/lib/eventType.js
Normal file
@ -0,0 +1,3 @@
|
||||
module.exports = {
|
||||
featureCreated : 'feature-created'
|
||||
};
|
@ -1,3 +0,0 @@
|
||||
module.exports = {
|
||||
featureCreated : 'featureCreated'
|
||||
};
|
@ -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();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user