Prevent duplicate hook runs.
Instead of being a list, the hooks in the hook processor should be backed by by-name and by-value maps. Don't insert a hook if its exact backing method has already been inserted. This is actually very common when hooks are defined as Python snippets imported in other scripts too.
This commit is contained in:
parent
4b41c5377a
commit
862d56a338
|
@ -1,4 +1,5 @@
|
||||||
import sys
|
import sys
|
||||||
|
from typing import Callable, List, Union
|
||||||
|
|
||||||
from ..hook import EventHook
|
from ..hook import EventHook
|
||||||
|
|
||||||
|
@ -20,10 +21,23 @@ class EventProcessor:
|
||||||
if hooks is None:
|
if hooks is None:
|
||||||
hooks = Config.get_event_hooks()
|
hooks = Config.get_event_hooks()
|
||||||
|
|
||||||
self.hooks = []
|
self._hooks_by_name = {}
|
||||||
|
self._hooks_by_value_id = {}
|
||||||
for name, hook in hooks.items():
|
for name, hook in hooks.items():
|
||||||
h = EventHook.build(name=name, hook=hook)
|
self.add_hook(name, hook)
|
||||||
self.hooks.append(h)
|
|
||||||
|
@property
|
||||||
|
def hooks(self) -> List[EventHook]:
|
||||||
|
return list(self._hooks_by_name.values())
|
||||||
|
|
||||||
|
def add_hook(self, name: str, desc: Union[dict, Callable]):
|
||||||
|
hook_id = id(desc)
|
||||||
|
if hook_id in self._hooks_by_value_id:
|
||||||
|
return # Don't add the same hook twice
|
||||||
|
|
||||||
|
hook = EventHook.build(name=name, hook=desc)
|
||||||
|
self._hooks_by_name[name] = hook
|
||||||
|
self._hooks_by_value_id[hook_id] = hook
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def notify_web_clients(event):
|
def notify_web_clients(event):
|
||||||
|
|
Loading…
Reference in a new issue