From 708ba2ddc7cfdded43451ea2508b96bd357c86ae Mon Sep 17 00:00:00 2001 From: jesopo Date: Thu, 14 Feb 2019 10:36:11 +0000 Subject: [PATCH] Support one-shot event hooks (src/EventManager.py) --- src/EventManager.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/EventManager.py b/src/EventManager.py index 01f00342..12ec524c 100644 --- a/src/EventManager.py +++ b/src/EventManager.py @@ -28,11 +28,13 @@ class Event(object): self.eaten = True class EventCallback(object): - def __init__(self, function: CALLBACK_TYPE, priority: int, kwargs: dict): + def __init__(self, function: CALLBACK_TYPE, priority: int, kwargs: dict, + one_shot: bool=False): self.function = function self.priority = priority self.kwargs = kwargs self.docstring = utils.parse.docstring(function.__doc__ or "") + self._one_shot = one_shot def call(self, event: Event) -> typing.Any: return self.function(event) @@ -206,6 +208,9 @@ class EventHook(object): else: raise + if hook._one_shot: + self.unhook(hook) + total_milliseconds = (time.monotonic() - start) * 1000 self.log.trace("event \"%s\" called in %fms", [event_path, total_milliseconds])