|
|
@@ -21,9 +21,10 @@ class EventSource(str, Enum):
|
|
|
|
|
|
|
|
|
class EventStream:
|
|
|
- _subscribers: dict[str, Callable] = {}
|
|
|
- _events: list[Event] = []
|
|
|
- _lock = asyncio.Lock()
|
|
|
+ def __init__(self):
|
|
|
+ self._subscribers: dict[str, Callable] = {}
|
|
|
+ self._events: list[Event] = []
|
|
|
+ self._lock = asyncio.Lock()
|
|
|
|
|
|
def subscribe(self, id: EventStreamSubscriber, callback: Callable):
|
|
|
if id in self._subscribers:
|
|
|
@@ -40,9 +41,9 @@ class EventStream:
|
|
|
# TODO: make this not async
|
|
|
async def add_event(self, event: Event, source: EventSource):
|
|
|
async with self._lock:
|
|
|
- event._id = len(self._events) # type: ignore [attr-defined]
|
|
|
- event._timestamp = datetime.now() # type: ignore [attr-defined]
|
|
|
- event._source = source # type: ignore [attr-defined]
|
|
|
+ event._id = len(self._events) # type: ignore[attr-defined]
|
|
|
+ event._timestamp = datetime.now() # type: ignore[attr-defined]
|
|
|
+ event._source = source # type: ignore[attr-defined]
|
|
|
self._events.append(event)
|
|
|
for key, fn in self._subscribers.items():
|
|
|
await fn(event)
|