Merge branch 'main' into feature/refactor

This commit is contained in:
fdlamotte
2025-08-06 10:56:24 +02:00
committed by GitHub
6 changed files with 79 additions and 24 deletions

View File

@@ -1,4 +1,5 @@
from enum import Enum
import inspect
import logging
from typing import Any, Dict, Optional, Callable, List, Union
import asyncio
@@ -148,6 +149,7 @@ class EventDispatcher:
logger.debug(
f"Dispatching event: {event.type}, {event.payload}, {event.attributes}"
)
for subscription in self.subscriptions.copy():
# Check if event type matches
if (
@@ -165,15 +167,24 @@ class EventDispatcher:
for key, value in subscription.attribute_filters.items()
):
continue
try:
result = subscription.callback(event.clone())
if asyncio.iscoroutine(result):
await result
except Exception as e:
print(f"Error in event handler: {e}")
# Fire the call back asychronously
asyncio.create_task(self._execute_callback(subscription.callback, event.clone()))
self.queue.task_done()
async def _execute_callback(self, callback, event):
"""Execute a callback with proper error handling"""
try:
if asyncio.iscoroutinefunction(callback):
await callback(event)
else:
result = callback(event)
if inspect.iscoroutine(result):
await result
except Exception as e:
logger.error(f"Error in event handler for {event.type}: {e}", exc_info=True)
async def start(self):
if not self.running:
self.running = True