Common: Fix some logic in PypubsubEventQueue

This commit is contained in:
Shreya Malviya 2022-08-08 23:54:01 +05:30
parent b78282ef20
commit 9c5bbfe35c
1 changed files with 8 additions and 6 deletions

View File

@ -1,6 +1,6 @@
from typing import Any, Callable, Sequence from typing import Any, Callable, Sequence
from pubsub import ALL_TOPICS, pub from pubsub import pub
from common.events import AbstractEvent from common.events import AbstractEvent
@ -10,7 +10,7 @@ from .i_event_queue import IEventQueue
class PypubsubEventQueue(IEventQueue): class PypubsubEventQueue(IEventQueue):
@staticmethod @staticmethod
def subscribe_all(subscriber: Callable[..., Any]): def subscribe_all(subscriber: Callable[..., Any]):
pub.subscribe(listener=subscriber, topicName=ALL_TOPICS) pub.subscribe(listener=subscriber, topicName=pub.ALL_TOPICS)
@staticmethod @staticmethod
def subscribe_types(types: Sequence[AbstractEvent], subscriber: Callable[..., Any]): def subscribe_types(types: Sequence[AbstractEvent], subscriber: Callable[..., Any]):
@ -25,18 +25,20 @@ class PypubsubEventQueue(IEventQueue):
pub.subscribe(listener=subscriber, topicName=tag) pub.subscribe(listener=subscriber, topicName=tag)
@staticmethod @staticmethod
def publish(event: AbstractEvent, data: Any): def publish(event: AbstractEvent, data: Any = None):
data = data if data else {}
# publish to event type's topic # publish to event type's topic
event_type_name = event.__name__ event_type_name = event.__name__
pub.sendMessage(event_type_name, data) pub.sendMessage(event_type_name, **data)
# publish to tags' topics # publish to tags' topics
for tag in event.tags: for tag in event.tags:
pub.sendMessage(tag, data) pub.sendMessage(tag, **data)
@staticmethod @staticmethod
def unsubscribe_all(subscriber: Callable[..., Any]): def unsubscribe_all(subscriber: Callable[..., Any]):
pub.unsubscribe(listener=subscriber, topicName=ALL_TOPICS) pub.unsubscribe(listener=subscriber, topicName=pub.ALL_TOPICS)
@staticmethod @staticmethod
def unsubscribe_types(types: Sequence[AbstractEvent], subscriber: Callable[..., Any]): def unsubscribe_types(types: Sequence[AbstractEvent], subscriber: Callable[..., Any]):