Skip to content

Commit

Permalink
feat(model): abstract model events to a standard EventEmitter
Browse files Browse the repository at this point in the history
closes #857, #490
  • Loading branch information
kingcody committed Jul 15, 2015
1 parent 2a1f37a commit 91657d7
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 23 deletions.
41 changes: 41 additions & 0 deletions app/templates/server/api/user(auth)/user.events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* User model events
*/

'use strict';

var EventEmitter = require('events').EventEmitter;<% if (filters.mongooseModels) { %>
var User = require('./user.model');<% } if (filters.sequelizeModels) { %>
var User = require('../../sqldb').User;<% } %>
var UserEvents = new EventEmitter();

// Set max event listeners (0 == unlimited)
UserEvents.setMaxListeners(0);

// Model events<% if (filters.mongooseModels) { %>
var events = {
'save': 'save',
'remove': 'remove'
};<% } if (filters.sequelizeModels) { %>
var events = {
'afterCreate': 'save',
'afterUpdate': 'save',
'afterDestroy': 'remove'
};<% } %>

// Register the event emitter to the model events
for (var e in events) {
var event = events[e];<% if (filters.mongooseModels) { %>
User.schema.post(e, emitEvent(event));<% } if (filters.sequelizeModels) { %>
User.hook(e, emitEvent(event));<% } %>
}

function emitEvent(event) {
return function(doc<% if (filters.sequelizeModels) { %>, options, done<% } %>) {
UserEvents.emit(event + ':' + doc._id, doc);
UserEvents.emit(event, doc);<% if (filters.sequelizeModels) { %>
done(null);<% } %>
}
}

module.exports = UserEvents;
41 changes: 41 additions & 0 deletions endpoint/templates/name.events(models).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* <%= classedName %> model events
*/

'use strict';

var EventEmitter = require('events').EventEmitter;<% if (filters.mongooseModels) { %>
var <%= classedName %> = require('./<%= name %>.model');<% } if (filters.sequelizeModels) { %>
var <%= classedName %> = require('../../sqldb').<%= classedName %>;<% } %>
var <%= classedName %>Events = new EventEmitter();

// Set max event listeners (0 == unlimited)
<%= classedName %>Events.setMaxListeners(0);

// Model events<% if (filters.mongooseModels) { %>
var events = {
'save': 'save',
'remove': 'remove'
};<% } if (filters.sequelizeModels) { %>
var events = {
'afterCreate': 'save',
'afterUpdate': 'save',
'afterDestroy': 'remove'
};<% } %>

// Register the event emitter to the model events
for (var e in events) {
var event = events[e];<% if (filters.mongooseModels) { %>
<%= classedName %>.schema.post(e, emitEvent(event));<% } if (filters.sequelizeModels) { %>
<%= classedName %>.hook(e, emitEvent(event));<% } %>
}

function emitEvent(event) {
return function(doc<% if (filters.sequelizeModels) { %>, options, done<% } %>) {
<%= classedName %>Events.emit(event + ':' + doc._id, doc);
<%= classedName %>Events.emit(event, doc);<% if (filters.sequelizeModels) { %>
done(null);<% } %>
}
}

module.exports = <%= classedName %>Events;
47 changes: 24 additions & 23 deletions endpoint/templates/name.socket(socketio).js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,32 @@
*/

'use strict';
<% if (filters.mongoose) { %>
var <%= classedName %> = require('./<%= name %>.model');<% } %><% if (filters.sequelize) { %>
var <%= classedName %> = require('../../sqldb').<%= classedName %>;<% } %>

exports.register = function(socket) {<% if (filters.sequelize) { %>
<%= classedName %>.hook('afterCreate', function(doc, fields, fn) {
onSave(socket, doc);
fn(null);
});<% } %>
<% if (filters.mongoose) { %><%= classedName %>.schema.post('save', function(doc) {<% }
if (filters.sequelize) { %><%= classedName %>.hook('afterUpdate', function(doc, fields, fn) {<% } %>
onSave(socket, doc);<% if (filters.sequelize) { %>
fn(null);<% } %>
});
<% if (filters.mongoose) { %><%= classedName %>.schema.post('remove', function(doc) {<% }
if (filters.sequelize) { %><%= classedName %>.hook('afterDestroy', function(doc, fields, fn) {<% } %>
onRemove(socket, doc);<% if (filters.sequelize) { %>
fn(null);<% } %>
});

var <%= classedName %>Events = require('./<%= name %>.events');

// Model events to emit
var events = ['save', 'remove'];

exports.register = function(socket) {
// Bind model events to socket events
for (var i = 0, eventsLength = events.length; i < eventsLength; i++) {
var event = events[i];
var listener = createListener('<%= name %>:' + event, socket);

<%= classedName %>Events.on(event, listener);
socket.on('disconnect', removeListener(event, listener));
}
};

function onSave(socket, doc, cb) {
socket.emit('<%= name %>:save', doc);

function createListener(event, socket) {
return function(doc) {
socket.emit(event, doc);
};
}

function onRemove(socket, doc, cb) {
socket.emit('<%= name %>:remove', doc);
function removeListener(event, listener) {
return function() {
<%= classedName %>Events.removeListener(event, listener);
};
}
2 changes: 2 additions & 0 deletions test/test-file-creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ describe('angular-fullstack generator', function () {
if (models) {
files = files.concat([
'server/api/thing/thing.model.js',
'server/api/thing/thing.events.js',
'server/config/seed.js'
]);
}
Expand Down Expand Up @@ -270,6 +271,7 @@ describe('angular-fullstack generator', function () {
'server/api/user/user.integration.js',
'server/api/user/user.model.js',
'server/api/user/user.model.spec.js',
'server/api/user/user.events.js',
'server/auth/index.js',
'server/auth/auth.service.js',
'server/auth/local/index.js',
Expand Down

0 comments on commit 91657d7

Please sign in to comment.