mirror of
https://github.com/meshcore-dev/meshcore_py.git
synced 2026-06-11 11:56:18 +00:00
Add copy to event handling to avoid cross mutations
This commit is contained in:
@@ -64,6 +64,15 @@ class Event:
|
|||||||
# Add any keyword arguments to the attributes dictionary
|
# Add any keyword arguments to the attributes dictionary
|
||||||
if kwargs:
|
if kwargs:
|
||||||
self.attributes.update(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:
|
class Subscription:
|
||||||
@@ -79,7 +88,7 @@ class Subscription:
|
|||||||
|
|
||||||
class EventDispatcher:
|
class EventDispatcher:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.queue = asyncio.Queue()
|
self.queue: asyncio.Queue[Event] = asyncio.Queue()
|
||||||
self.subscriptions: List[Subscription] = []
|
self.subscriptions: List[Subscription] = []
|
||||||
self.running = False
|
self.running = False
|
||||||
self._task = None
|
self._task = None
|
||||||
@@ -127,7 +136,7 @@ class EventDispatcher:
|
|||||||
for key, value in subscription.attribute_filters.items()):
|
for key, value in subscription.attribute_filters.items()):
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
result = subscription.callback(event)
|
result = subscription.callback(event.clone())
|
||||||
if asyncio.iscoroutine(result):
|
if asyncio.iscoroutine(result):
|
||||||
await result
|
await result
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
Reference in New Issue
Block a user