Common: Rename EventSubscriber -> AgentEventSubscriber

This commit is contained in:
Shreya Malviya 2022-09-06 12:09:48 +05:30
parent 122e09426e
commit 86018be736
4 changed files with 16 additions and 12 deletions

View File

@ -1,3 +1,3 @@
from .types import EventSubscriber
from .types import AgentEventSubscriber
from .i_agent_event_queue import IAgentEventQueue
from .pypubsub_agent_event_queue import PyPubSubAgentEventQueue

View File

@ -3,7 +3,7 @@ from typing import Type
from common.events import AbstractAgentEvent
from . import EventSubscriber
from . import AgentEventSubscriber
class IAgentEventQueue(ABC):
@ -12,7 +12,7 @@ class IAgentEventQueue(ABC):
"""
@abstractmethod
def subscribe_all_events(self, subscriber: EventSubscriber):
def subscribe_all_events(self, subscriber: AgentEventSubscriber):
"""
Subscribes a subscriber to all events
@ -22,7 +22,9 @@ class IAgentEventQueue(ABC):
pass
@abstractmethod
def subscribe_type(self, event_type: Type[AbstractAgentEvent], subscriber: EventSubscriber):
def subscribe_type(
self, event_type: Type[AbstractAgentEvent], subscriber: AgentEventSubscriber
):
"""
Subscribes a subscriber to the specified event type
@ -33,7 +35,7 @@ class IAgentEventQueue(ABC):
pass
@abstractmethod
def subscribe_tag(self, tag: str, subscriber: EventSubscriber):
def subscribe_tag(self, tag: str, subscriber: AgentEventSubscriber):
"""
Subscribes a subscriber to the specified event tag

View File

@ -5,7 +5,7 @@ from pubsub.core import Publisher
from common.events import AbstractAgentEvent
from . import EventSubscriber, IAgentEventQueue
from . import AgentEventSubscriber, IAgentEventQueue
_ALL_EVENTS_TOPIC = "all_events_topic"
@ -17,19 +17,21 @@ class PyPubSubAgentEventQueue(IAgentEventQueue):
self._pypubsub_publisher = pypubsub_publisher
self._refs = []
def subscribe_all_events(self, subscriber: EventSubscriber):
def subscribe_all_events(self, subscriber: AgentEventSubscriber):
self._subscribe(_ALL_EVENTS_TOPIC, subscriber)
def subscribe_type(self, event_type: Type[AbstractAgentEvent], subscriber: EventSubscriber):
def subscribe_type(
self, event_type: Type[AbstractAgentEvent], subscriber: AgentEventSubscriber
):
# pypubsub.pub.subscribe needs a string as the topic/event name
event_type_topic = PyPubSubAgentEventQueue._get_type_topic(event_type)
self._subscribe(event_type_topic, subscriber)
def subscribe_tag(self, tag: str, subscriber: EventSubscriber):
def subscribe_tag(self, tag: str, subscriber: AgentEventSubscriber):
tag_topic = PyPubSubAgentEventQueue._get_tag_topic(tag)
self._subscribe(tag_topic, subscriber)
def _subscribe(self, topic: str, subscriber: EventSubscriber):
def _subscribe(self, topic: str, subscriber: AgentEventSubscriber):
try:
subscriber_name = subscriber.__name__
except AttributeError:
@ -39,7 +41,7 @@ class PyPubSubAgentEventQueue(IAgentEventQueue):
self._pypubsub_publisher.subscribe(topicName=topic, listener=subscriber)
self._keep_subscriber_strongref(subscriber)
def _keep_subscriber_strongref(self, subscriber: EventSubscriber):
def _keep_subscriber_strongref(self, subscriber: AgentEventSubscriber):
# NOTE: PyPubSub stores subscribers by weak reference. From the documentation:
# > PyPubSub holds listeners by weak reference so that the lifetime of the
# > callable is not affected by PyPubSub: once the application no longer

View File

@ -2,4 +2,4 @@ from typing import Callable
from common.events import AbstractAgentEvent
EventSubscriber = Callable[[AbstractAgentEvent], None]
AgentEventSubscriber = Callable[[AbstractAgentEvent], None]