Skip to content

Instantly share code, notes, and snippets.

@johnschimmel
Last active September 17, 2018 09:06
Show Gist options
  • Save johnschimmel/8f0efcbdd90c4f17366404ca7b37ceda to your computer and use it in GitHub Desktop.
Save johnschimmel/8f0efcbdd90c4f17366404ca7b37ceda to your computer and use it in GitHub Desktop.
Pub Sub for Handling BG Messages
/******************************/
/* Bluegrass Event Bus PubSub */
/* from https://davidwalsh.name/pubsub-javascript */
/******************************/
var BGEventBus = (function(){
var topics = {};
var hOP = topics.hasOwnProperty;
return {
subscribe: function(topic, listener) {
// Create the topic's object if not yet created
if(!hOP.call(topics, topic)) topics[topic] = [];
// Add the listener to queue
var index = topics[topic].push(listener) -1;
// Provide handle back for removal of topic
return {
remove: function() {
delete topics[topic][index];
}
};
},
publish: function(topic, info) {
// If the topic doesn't exist, or there's no listeners in queue, just leave
if(!hOP.call(topics, topic)) return;
// Cycle through topics queue, fire!
topics[topic].forEach(function(item) {
item(info != undefined ? info : {});
});
}
};
})();
/******************************/
/* Subscribe to BGEventBus */
/******************************/
var bgEventsSubscriptions = {};
bgEventsSubscriptions['stack_added'] = BGEventBus.subscribe('stack_added', function(obj) {
console.log('stack_added', obj);
});
bgEventsSubscriptions['stack_changed'] = BGEventBus.subscribe('stack_changed', function(obj) {
console.log('stack_changed', obj);
});
bgEventsSubscriptions['card_added'] = BGEventBus.subscribe('card_added', function(obj) {
console.log('card_added', obj);
});
bgEventsSubscriptions['card_changed'] = BGEventBus.subscribe('card_changed', function(obj) {
console.log('card_changed', obj);
});
bgEventsSubscriptions['card_removed'] = BGEventBus.subscribe('card_removed', function(obj) {
console.log('card_removed', obj);
});
var liveCoverageBGMsgHandler = function (message) {
var msgObj = JSON.parse(message.b.d);
var cudMap = {
'c': 'added',
'u': 'changed',
'd': 'removed'
};
if (msgObj.hasOwnProperty('type') && msgObj.hasOwnProperty('cud') && msgObj.hasOwnProperty('data')) {
var eventType = msgObj.type + "_" + cudMap[msgObj.cud]; //TYPE_CUDMAPPROPERTY
BGEventBus.publish(eventType, dmsgObj.data);
}
}
/************************************************************/
/* Connect to Bluegrass and use liveCoverageBGMsgHandler */
/************************************************************/
var bgURL = 'http://sbkwbgxwebs01.production.bigcharts.com/bg/api/connect.ashx'; // dev
// var bgURL = 'http://wsjstream.wsj.net/bg/api/connect.ashx'; //prod
bluegrass.Runtime.openConnection({to:bgURL, via:''}, function(connection, type) {
var channel = null;
var lcChannel = '/dj/livecoverage/livecoverage_dev.schimmel-test-1';
if (type == "connect") {
console.log('connected');
channel = connection.subscribe(lcChannel, liveCoverageBGMsgHandler);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment