Created
September 7, 2017 17:54
-
-
Save jykim16/cf9f751b4d0466dbda2e467da9ff4bb3 to your computer and use it in GitHub Desktop.
subscribe publish module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let messageBus = {}; | |
class Channel{ | |
constructor() { | |
this.topicSubscriptionCache = {}; | |
} | |
publish(topic, payload) { | |
if (payload) { | |
//find array of callbacks for topic in cache | |
if(this.topicSubscriptionCache[topic]) { | |
this.topicSubscriptionCache[topic].forEach((func)=>{ | |
//execute each callback in order. | |
func(payload); | |
}); | |
} | |
} else { | |
if(this.topicSubscriptionCache[topic]) { | |
this.topicSubscriptionCache[topic].forEach((func)=>{ | |
//execute each callback in order. | |
func(); | |
}); | |
} | |
} | |
} | |
subscribe(topic, callback) { | |
if(!this.topicSubscriptionCache[topic]) { | |
this.topicSubscriptionCache[topic] = []; | |
}; | |
//push callback to key value. | |
this.topicSubscriptionCache[topic].push(callback); | |
} | |
} | |
let channelCache = {general: new Channel()}; | |
messageBus.subscribe = (...args) => { | |
let channel = 'general', topic, callback; | |
args.forEach((arg, i)=>{ | |
if (typeof arg === 'object') { | |
arg.topic ? topic = arg.topic : topic; | |
arg.channel ? channel = arg.channel : channel; | |
arg.callback ? callback = arg.callback : callback; | |
} else if (i === 0 && typeof arg !== 'object') { | |
topic = arg; | |
} else if (i === 1 && typeof arg !== 'object') { | |
callback = arg; | |
} | |
}); | |
//push callback to key value. | |
if(!channelCache[channel]) { | |
channelCache[channel] = new Channel(); | |
} | |
channelCache[channel].subscribe(topic, callback); | |
}; | |
messageBus.publish = (...args) => { | |
let channel = 'general', topic, data; | |
args.forEach((arg, i)=>{ | |
if (arg.data && typeof arg === 'object') { | |
arg.topic ? topic = arg.topic : topic; | |
arg.channel ? channel = arg.channel : channel; | |
arg.data ? data = arg.data : data; | |
} else if (i === 0 && typeof arg !== 'object') { | |
topic = arg; | |
} else if (i === 1 && typeof arg.data !== 'object') { | |
data = arg; | |
} | |
}); | |
if(channelCache[channel]) { | |
channelCache[channel].publish(topic, data); | |
} | |
}; | |
//tests | |
messageBus.subscribe('new_signup', function(payload) { | |
console.log(payload) | |
}); | |
messageBus.publish('new_signup', {'foo': 'bar', 'goo': 'gar'} ); | |
messageBus.subscribe({ | |
channel: 'commerce', | |
topic: 'order_complete', | |
callback: function(payload) { | |
console.log(payload) | |
} | |
}); | |
messageBus.subscribe({ | |
channel: 'commerce', | |
topic: 'order_complete', | |
callback: function(payload) { | |
console.log('hi') | |
} | |
}); | |
messageBus.publish({ | |
channel: 'commerce', | |
topic: 'order_complete', | |
data: { | |
plan_level: 'GOLD', | |
user_id: '4273984723428' | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment