Common: Rename EventSubscriber -> AgentEventSubscriber
This commit is contained in:
parent
122e09426e
commit
86018be736
|
@ -1,3 +1,3 @@
|
||||||
from .types import EventSubscriber
|
from .types import AgentEventSubscriber
|
||||||
from .i_agent_event_queue import IAgentEventQueue
|
from .i_agent_event_queue import IAgentEventQueue
|
||||||
from .pypubsub_agent_event_queue import PyPubSubAgentEventQueue
|
from .pypubsub_agent_event_queue import PyPubSubAgentEventQueue
|
||||||
|
|
|
@ -3,7 +3,7 @@ from typing import Type
|
||||||
|
|
||||||
from common.events import AbstractAgentEvent
|
from common.events import AbstractAgentEvent
|
||||||
|
|
||||||
from . import EventSubscriber
|
from . import AgentEventSubscriber
|
||||||
|
|
||||||
|
|
||||||
class IAgentEventQueue(ABC):
|
class IAgentEventQueue(ABC):
|
||||||
|
@ -12,7 +12,7 @@ class IAgentEventQueue(ABC):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def subscribe_all_events(self, subscriber: EventSubscriber):
|
def subscribe_all_events(self, subscriber: AgentEventSubscriber):
|
||||||
"""
|
"""
|
||||||
Subscribes a subscriber to all events
|
Subscribes a subscriber to all events
|
||||||
|
|
||||||
|
@ -22,7 +22,9 @@ class IAgentEventQueue(ABC):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@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
|
Subscribes a subscriber to the specified event type
|
||||||
|
|
||||||
|
@ -33,7 +35,7 @@ class IAgentEventQueue(ABC):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@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
|
Subscribes a subscriber to the specified event tag
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ from pubsub.core import Publisher
|
||||||
|
|
||||||
from common.events import AbstractAgentEvent
|
from common.events import AbstractAgentEvent
|
||||||
|
|
||||||
from . import EventSubscriber, IAgentEventQueue
|
from . import AgentEventSubscriber, IAgentEventQueue
|
||||||
|
|
||||||
_ALL_EVENTS_TOPIC = "all_events_topic"
|
_ALL_EVENTS_TOPIC = "all_events_topic"
|
||||||
|
|
||||||
|
@ -17,19 +17,21 @@ class PyPubSubAgentEventQueue(IAgentEventQueue):
|
||||||
self._pypubsub_publisher = pypubsub_publisher
|
self._pypubsub_publisher = pypubsub_publisher
|
||||||
self._refs = []
|
self._refs = []
|
||||||
|
|
||||||
def subscribe_all_events(self, subscriber: EventSubscriber):
|
def subscribe_all_events(self, subscriber: AgentEventSubscriber):
|
||||||
self._subscribe(_ALL_EVENTS_TOPIC, subscriber)
|
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
|
# pypubsub.pub.subscribe needs a string as the topic/event name
|
||||||
event_type_topic = PyPubSubAgentEventQueue._get_type_topic(event_type)
|
event_type_topic = PyPubSubAgentEventQueue._get_type_topic(event_type)
|
||||||
self._subscribe(event_type_topic, subscriber)
|
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)
|
tag_topic = PyPubSubAgentEventQueue._get_tag_topic(tag)
|
||||||
self._subscribe(tag_topic, subscriber)
|
self._subscribe(tag_topic, subscriber)
|
||||||
|
|
||||||
def _subscribe(self, topic: str, subscriber: EventSubscriber):
|
def _subscribe(self, topic: str, subscriber: AgentEventSubscriber):
|
||||||
try:
|
try:
|
||||||
subscriber_name = subscriber.__name__
|
subscriber_name = subscriber.__name__
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
@ -39,7 +41,7 @@ class PyPubSubAgentEventQueue(IAgentEventQueue):
|
||||||
self._pypubsub_publisher.subscribe(topicName=topic, listener=subscriber)
|
self._pypubsub_publisher.subscribe(topicName=topic, listener=subscriber)
|
||||||
self._keep_subscriber_strongref(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:
|
# NOTE: PyPubSub stores subscribers by weak reference. From the documentation:
|
||||||
# > PyPubSub holds listeners by weak reference so that the lifetime of the
|
# > PyPubSub holds listeners by weak reference so that the lifetime of the
|
||||||
# > callable is not affected by PyPubSub: once the application no longer
|
# > callable is not affected by PyPubSub: once the application no longer
|
||||||
|
|
|
@ -2,4 +2,4 @@ from typing import Callable
|
||||||
|
|
||||||
from common.events import AbstractAgentEvent
|
from common.events import AbstractAgentEvent
|
||||||
|
|
||||||
EventSubscriber = Callable[[AbstractAgentEvent], None]
|
AgentEventSubscriber = Callable[[AbstractAgentEvent], None]
|
||||||
|
|
Loading…
Reference in New Issue