Here is a little event system I've been working on for Python. I've been trying to make it typesafe as possible, hence the generics and depending on specific classes for event info. This system has a couple neat features:
- Automatic event ID creation/decorator based event creation
- Events call parent events
- Low overhead (computations on IDs are done once and saved, simple dictionary based listener structure)
Creating and listening to events is simple:
from dataclasses import dataclass
from eventtarget import event, EventTarget
@event
@dataclass
class GreetEvent():
target: str
target = EventTarget() # I recommend inheriting this class instead, but this works too.
target.subscribe(GreetEvent, lambda event: print(f"Hello, {event.target}!"))
target.emit(GreetEvent("world")) # Prints "Hello, world!"