Skip to content

Instantly share code, notes, and snippets.

@twalk4821
Created April 13, 2017 16:58
Show Gist options
  • Save twalk4821/cd3e80804cdd9835ed37131cc68630ac to your computer and use it in GitHub Desktop.
Save twalk4821/cd3e80804cdd9835ed37131cc68630ac to your computer and use it in GitHub Desktop.
//labels
//subscribe to labels, needs to create a new label with users as properties, or an array of users
//with references to users, or their components
//multiple users need to access the same message bus
//have a handler for updating the data associated with each user
var messageBus = function() {
this.topics = {}
this.channels = {}
this.subscribe = function(payload, user) {
var topic = payload.topic;
//handle channels
if (payload.channel) {
var channel = payload.channel
if (channel in channels) {
if (topic in this.channel.topics) {
this.channels[channel].topics[topic].users.push(user)
} else {
this.channels[channel] = {
topics: []
}
this.channels[channel].topics[topic] = {
users: [],
messages: []
}
this.channels[channel].topics[topic].users.push(user)
}
}
} else { //regular case (no channel)
if (topic in this.topics) {
this.topics[topic].users.push(user)
} else {
this.topics[topic] = {
users: [],
messages: []
}
this.topics[topic].users.push(user)
}
}
}.bind(this);
this.publish = function(topic, message) {
//todo implement channels
if (!(topic in this.topics)) {
throw err;
} else {
this.topics[topic].messages.push(message);
}
for (var i = 0; i<this.topics[topic].users.length; i++) {
this.topics[topic].users.notify(); //notify defined client side
}
}.bind(this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment