Created
April 13, 2017 16:28
-
-
Save mikeliao97/99955048f76764ebdd0a85006c533e4d to your computer and use it in GitHub Desktop.
This file contains 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
var messageBus = { | |
//What is the realtionship between subscrib and publish? | |
subscriberCallbacks: {}, | |
subscribe: function(title, callback) { | |
if (!this.subscriberCallbacks[title]) { | |
this.subscriberCallbacks[title] = [callback] | |
} else { | |
this.subscriberCallbacks[title].push(callback); | |
} | |
}, | |
//When you publish, you expect everyone subscribing to that title to recieve the payload | |
//How do you keep trck of who is subscribing to the message Bus? | |
publish: function(title, payload) { | |
var subscribers = this.subscriberCallbacks[title]; | |
for (var sIndex = 0; sIndex < subscribers.length; sIndex++) { | |
var callback = subscribers[sIndex]; | |
callback(payload); | |
} | |
} | |
} | |
messageBus.subscribe('new_signup', function(payload) { | |
console.log('a', payload); | |
}) | |
messageBus.subscribe('new_signup', function(payload) { | |
console.log('b', payload); | |
}) | |
messageBus.subscribe('new_signup', function(payload) { | |
console.log('c', payload); | |
}) | |
messageBus.subscribe('new_signup', function(payload) { | |
console.log('d', payload); | |
}) | |
messageBus.subscribe('new_signup', function(payload) { | |
console.log('e', payload); | |
}) | |
messageBus.publish('new_signup', {'foo': 'bar', 'goo': 'gar'}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment