diff --git a/src/meshcore/events.py b/src/meshcore/events.py index 525615f..e263899 100644 --- a/src/meshcore/events.py +++ b/src/meshcore/events.py @@ -64,6 +64,15 @@ class Event: # Add any keyword arguments to the attributes dictionary if kwargs: self.attributes.update(kwargs) + def clone(self): + """ + Create a copy of the event. + + Returns: + A new Event object with the same type, payload, and attributes. + """ + copied_payload = self.payload.copy() if isinstance(self.payload, dict) else self.payload + return Event(self.type, copied_payload, self.attributes.copy()) class Subscription: @@ -79,7 +88,7 @@ class Subscription: class EventDispatcher: def __init__(self): - self.queue = asyncio.Queue() + self.queue: asyncio.Queue[Event] = asyncio.Queue() self.subscriptions: List[Subscription] = [] self.running = False self._task = None @@ -127,7 +136,7 @@ class EventDispatcher: for key, value in subscription.attribute_filters.items()): continue try: - result = subscription.callback(event) + result = subscription.callback(event.clone()) if asyncio.iscoroutine(result): await result except Exception as e: