forked from p34709852/monkey
Common: Use EventSubscriber for IEventQueue type hints
This commit is contained in:
parent
97a612be2d
commit
35155c345f
|
@ -1,8 +1,10 @@
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from typing import Callable, Type
|
from typing import Type
|
||||||
|
|
||||||
from common.events import AbstractEvent
|
from common.events import AbstractEvent
|
||||||
|
|
||||||
|
from . import EventSubscriber
|
||||||
|
|
||||||
|
|
||||||
class IEventQueue(ABC):
|
class IEventQueue(ABC):
|
||||||
"""
|
"""
|
||||||
|
@ -10,35 +12,33 @@ class IEventQueue(ABC):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def subscribe_all_events(self, subscriber: Callable[[AbstractEvent], None]):
|
def subscribe_all_events(self, subscriber: EventSubscriber):
|
||||||
"""
|
"""
|
||||||
Subscribes a subscriber to all events
|
Subscribes a subscriber to all events
|
||||||
|
|
||||||
:param subscriber: Callable that should subscribe to events
|
:param subscriber: A subscriber that well receive events
|
||||||
"""
|
"""
|
||||||
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def subscribe_type(
|
def subscribe_type(self, event_type: Type[AbstractEvent], subscriber: EventSubscriber):
|
||||||
self, event_type: Type[AbstractEvent], subscriber: Callable[[AbstractEvent], None]
|
|
||||||
):
|
|
||||||
"""
|
"""
|
||||||
Subscribes a subscriber to the specifed event type
|
Subscribes a subscriber to the specifed event type
|
||||||
|
|
||||||
:param event_type: Event type to which the subscriber should subscribe
|
:param event_type: Event type to which the subscriber should subscribe
|
||||||
:param subscriber: Callable that should subscribe to events
|
:param subscriber: A subscriber that well receive events
|
||||||
"""
|
"""
|
||||||
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def subscribe_tag(self, tag: str, subscriber: Callable[[AbstractEvent], None]):
|
def subscribe_tag(self, tag: str, subscriber: EventSubscriber):
|
||||||
"""
|
"""
|
||||||
Subscribes a subscriber to the specified event tag
|
Subscribes a subscriber to the specified event tag
|
||||||
|
|
||||||
:param tag: Event tag to which the subscriber should subscribe
|
:param tag: Event tag to which the subscriber should subscribe
|
||||||
:param subscriber: Callable that should subscribe to events
|
:param subscriber: A subscriber that well receive events
|
||||||
"""
|
"""
|
||||||
|
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
from typing import Callable, Type
|
from typing import Type
|
||||||
|
|
||||||
from pubsub.core import Publisher
|
from pubsub.core import Publisher
|
||||||
|
|
||||||
from common.events import AbstractEvent
|
from common.events import AbstractEvent
|
||||||
|
|
||||||
|
from . import EventSubscriber
|
||||||
from .i_event_queue import IEventQueue
|
from .i_event_queue import IEventQueue
|
||||||
|
|
||||||
_ALL_EVENTS_TOPIC = "all_events_topic"
|
_ALL_EVENTS_TOPIC = "all_events_topic"
|
||||||
|
@ -13,17 +14,15 @@ class PyPubSubEventQueue(IEventQueue):
|
||||||
def __init__(self, pypubsub_publisher: Publisher):
|
def __init__(self, pypubsub_publisher: Publisher):
|
||||||
self._pypubsub_publisher = pypubsub_publisher
|
self._pypubsub_publisher = pypubsub_publisher
|
||||||
|
|
||||||
def subscribe_all_events(self, subscriber: Callable[[AbstractEvent], None]):
|
def subscribe_all_events(self, subscriber: EventSubscriber):
|
||||||
self._pypubsub_publisher.subscribe(listener=subscriber, topicName=_ALL_EVENTS_TOPIC)
|
self._pypubsub_publisher.subscribe(listener=subscriber, topicName=_ALL_EVENTS_TOPIC)
|
||||||
|
|
||||||
def subscribe_type(
|
def subscribe_type(self, event_type: Type[AbstractEvent], subscriber: EventSubscriber):
|
||||||
self, event_type: Type[AbstractEvent], subscriber: Callable[[AbstractEvent], None]
|
|
||||||
):
|
|
||||||
# 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 = PyPubSubEventQueue._get_type_topic(event_type)
|
event_type_topic = PyPubSubEventQueue._get_type_topic(event_type)
|
||||||
self._pypubsub_publisher.subscribe(listener=subscriber, topicName=event_type_topic)
|
self._pypubsub_publisher.subscribe(listener=subscriber, topicName=event_type_topic)
|
||||||
|
|
||||||
def subscribe_tag(self, tag: str, subscriber: Callable[[AbstractEvent], None]):
|
def subscribe_tag(self, tag: str, subscriber: EventSubscriber):
|
||||||
tag_topic = PyPubSubEventQueue._get_tag_topic(tag)
|
tag_topic = PyPubSubEventQueue._get_tag_topic(tag)
|
||||||
self._pypubsub_publisher.subscribe(listener=subscriber, topicName=tag_topic)
|
self._pypubsub_publisher.subscribe(listener=subscriber, topicName=tag_topic)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue