From fda20e623e9e85b273a6022379e530b7a9e1fb8d Mon Sep 17 00:00:00 2001 From: Alex Wolden Date: Thu, 15 May 2025 11:52:15 -0700 Subject: [PATCH] Add copy to event handling to avoid cross mutations --- src/meshcore/events.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) 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: