Common: Simplify logic in PypubsubEventQueue

This commit is contained in:
Shreya Malviya 2022-08-08 13:11:07 +05:30
parent 8ae451f240
commit d773bac8c1
1 changed files with 8 additions and 18 deletions

View File

@ -15,10 +15,6 @@ class PypubsubEventQueue(IEventQueue):
@staticmethod @staticmethod
def subscribe_types(types: Sequence[AbstractEvent], subscriber: Callable[..., Any]): def subscribe_types(types: Sequence[AbstractEvent], subscriber: Callable[..., Any]):
for event_type in types: for event_type in types:
PypubsubEventQueue._subscribe_type(event_type, subscriber)
@staticmethod
def _subscribe_type(event_type: AbstractEvent, subscriber: Callable[..., Any]):
# pypubsub.pub.subscribe needs a string as the topic/event name # pypubsub.pub.subscribe needs a string as the topic/event name
event_type_name = event_type.__name__ event_type_name = event_type.__name__
pub.subscribe(listener=subscriber, topicName=event_type_name) pub.subscribe(listener=subscriber, topicName=event_type_name)
@ -26,10 +22,6 @@ class PypubsubEventQueue(IEventQueue):
@staticmethod @staticmethod
def subscribe_tags(tags: Sequence[str], subscriber: Callable[..., Any]): def subscribe_tags(tags: Sequence[str], subscriber: Callable[..., Any]):
for tag in tags: for tag in tags:
PypubsubEventQueue._subscribe_tag(tag, subscriber)
@staticmethod
def _subscribe_tag(tag: str, subscriber: Callable[..., Any]):
pub.subscribe(listener=subscriber, topicName=tag) pub.subscribe(listener=subscriber, topicName=tag)
@staticmethod @staticmethod
@ -45,10 +37,7 @@ class PypubsubEventQueue(IEventQueue):
# to the function, and publish to each class's type and tags (except the last 3 classes) # to the function, and publish to each class's type and tags (except the last 3 classes)
for event_type in event.mro()[:-3]: for event_type in event.mro()[:-3]:
PypubsubEventQueue._publish_to_type(event_type, data) PypubsubEventQueue._publish_to_type(event_type, data)
PypubsubEventQueue._publish_to_tags(event_type, data)
# one event can have multiple tags
for tag in event_type.tags:
PypubsubEventQueue._publish_to_tag(tag, data)
@staticmethod @staticmethod
def _publish_to_type(event_type: AbstractEvent, data: Any): def _publish_to_type(event_type: AbstractEvent, data: Any):
@ -56,5 +45,6 @@ class PypubsubEventQueue(IEventQueue):
pub.sendMessage(event_type_name, data) pub.sendMessage(event_type_name, data)
@staticmethod @staticmethod
def _publish_to_tag(tag: str, data: Any): def _publish_to_tags(event_type: AbstractEvent, data: Any):
for tag in event_type.tags:
pub.sendMessage(tag, data) pub.sendMessage(tag, data)