event_listener.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import uuid
  2. from dataclasses import dataclass, field
  3. from typing import Any, Callable, Dict, List
  4. from fastapi import Request
  5. from .helpers import KWONLY_SLOTS
  6. @dataclass(**KWONLY_SLOTS)
  7. class EventListener:
  8. id: str = field(init=False)
  9. element_id: int
  10. type: str
  11. args: List[str]
  12. handler: Callable
  13. throttle: float
  14. leading_events: bool
  15. trailing_events: bool
  16. request: Request
  17. def __post_init__(self) -> None:
  18. self.id = str(uuid.uuid4())
  19. def to_dict(self) -> Dict[str, Any]:
  20. words = self.type.split('.')
  21. type = words.pop(0)
  22. specials = [w for w in words if w in {'capture', 'once', 'passive'}]
  23. modifiers = [w for w in words if w in {'stop', 'prevent', 'self', 'ctrl', 'shift', 'alt', 'meta'}]
  24. keys = [w for w in words if w not in specials + modifiers]
  25. return {
  26. 'listener_id': self.id,
  27. 'type': type,
  28. 'specials': specials,
  29. 'modifiers': modifiers,
  30. 'keys': keys,
  31. 'args': self.args,
  32. 'throttle': self.throttle,
  33. 'leading_events': self.leading_events,
  34. 'trailing_events': self.trailing_events,
  35. }