mirror of
https://github.com/meshcore-dev/meshcore_py.git
synced 2026-04-20 22:13:49 +00:00
Add copy to event handling to avoid cross mutations
This commit is contained in:
parent
68dda3aba2
commit
fda20e623e
1 changed files with 11 additions and 2 deletions
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue