Created
July 15, 2020 13:08
-
-
Save tonyroberts/7caba8aa24e1c57abe59e3f88ad113c1 to your computer and use it in GitHub Desktop.
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
class MessageBroker: | |
def __init__(self): | |
self.__subscribers = {} | |
def publish(self, topic, message): | |
for callback in self.__subscribers.get(topic, []): | |
callback(message) | |
def subscribe(self, topic, callback): | |
subscribers = self.__subscribers.setdefault(topic, []) | |
subscribers.append(callback) | |
def unsubscribe(self, topic, callback): | |
subscribers = self.__subscribers[topic] | |
subscribers.remove(callback) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment