-
Notifications
You must be signed in to change notification settings - Fork 139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add 'handle_once' property for unregistering an EventHandler after one event #141
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,10 @@ | |
"""Module for EventHandler class.""" | ||
|
||
from typing import Callable | ||
from typing import List | ||
from typing import Optional | ||
from typing import Text | ||
from typing import Tuple | ||
|
||
from .event import Event | ||
from .some_actions_type import SomeActionsType | ||
|
@@ -39,7 +42,8 @@ def __init__( | |
self, | ||
*, | ||
matcher: Callable[[Event], bool], | ||
entities: Optional[SomeActionsType] = None | ||
entities: Optional[SomeActionsType] = None, | ||
handle_once: bool = False | ||
) -> None: | ||
""" | ||
Constructor. | ||
|
@@ -48,16 +52,63 @@ def __init__( | |
the event should be handled by this event handler, False otherwise. | ||
:param: entities is an LaunchDescriptionEntity or list of them, and is | ||
returned by handle() unconditionally if matcher returns True. | ||
:param: handle_once is a flag that, if True, unregisters this EventHandler | ||
after being handled once. | ||
""" | ||
self.__matcher = matcher | ||
self.__entities = entities | ||
self.handle_once = handle_once | ||
|
||
@property | ||
def entities(self): | ||
"""Getter for entities.""" | ||
# if self.__entities is None: | ||
# return [] | ||
return self.__entities | ||
|
||
# TODO(wjwwood): setup standard interface for describing event handlers | ||
@property | ||
def handle_once(self): | ||
"""Getter for handle_once flag.""" | ||
return self.__handle_once | ||
|
||
@handle_once.setter | ||
def handle_once(self, value): | ||
"""Setter for handle_once flag.""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might be wise to remove the public setter so that users can't get into strange situations by modifying the flag. FWICT it'd be sufficient to just set it in the constructor now. If a need comes up later, we can consider re-adding it to the API (it's harder though to remove API). |
||
if not isinstance(value, bool): | ||
raise TypeError( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as a general-purpose review comment, we try to avoid nesting where possible by returning early. if not isinstance(value, bool):
raise TypeError(...)
self.__handle_once = value |
||
'handle_once expects type "bool", but received {} instead.'.format(type(value)) | ||
) | ||
self.__handle_once = value | ||
|
||
@property | ||
def handler_description(self): | ||
""" | ||
Return the string description of the handler. | ||
|
||
This should be overridden. | ||
""" | ||
return None | ||
|
||
@property | ||
def matcher_description(self): | ||
""" | ||
Return the string description of the matcher. | ||
|
||
This should be overridden. | ||
""" | ||
return None | ||
|
||
def describe(self) -> Tuple[Text, List[SomeActionsType]]: | ||
"""Return the description list with 0 as a string, and then LaunchDescriptionEntity's.""" | ||
return ( | ||
"{}(matcher='{}', handler='{}', handle_once={})".format( | ||
type(self).__name__, | ||
self.matcher_description, | ||
self.handler_description, | ||
self.handle_once | ||
), | ||
self.entities if self.entities is not None else [] | ||
) | ||
|
||
def matches(self, event: Event) -> bool: | ||
"""Return True if the given event should be handled by this event handler.""" | ||
|
@@ -66,4 +117,6 @@ def matches(self, event: Event) -> bool: | |
def handle(self, event: Event, context: 'LaunchContext') -> Optional[SomeActionsType]: | ||
"""Handle the given event.""" | ||
context.extend_locals({'event': event}) | ||
if self.handle_once: | ||
context.unregister_event_handler(self) | ||
return self.__entities |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll remove this.