Skip to content

Instantly share code, notes, and snippets.

@marshallswain
Last active February 1, 2017 00:09
Show Gist options
  • Save marshallswain/6eb4789f68c137d9f4f39dae70501aa0 to your computer and use it in GitHub Desktop.
Save marshallswain/6eb4789f68c137d9f4f39dae70501aa0 to your computer and use it in GitHub Desktop.
Event Management for Feathers Buzzard Release
const app = {};
class Channel {
constructor () {
this.connections = [];
}
add (connection) {
this.connections.push(connection);
}
}
app.channel = function (name) {
return app.channels[name] || (app.channels[name] = new Channel());
};
function joinChannels (entity, connection) {
entity.rooms.forEach(room =>
// Document id clashes
app.channel(room._id).add(entity._id, connection)
);
}
app.on('login', (entity, info) => {
// app.channel(entity._id).add(connection);
if (info.connection) {
joinChannels(entity, info.connection);
}
});
// Whenever the entity changes
app.service('entity').on('somechange', changedEntity => {
const entityConnection = app.connections(changedEntity._id);
// remove connections from all channels
app.channels.forEach(channel => channel.remove(entityConnection));
// rejoin channels with updated entity information
entityConnection.forEach(connection => {
joinChannels(changedEntity, connection);
});
});
app.service('messages').filter('eventname', (message, connections, hook) => {
return app.channel(`room5`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment