Skip to content

Commit

Permalink
Small refactor : EventStream as a dataclass (All-Hands-AI#4557)
Browse files Browse the repository at this point in the history
  • Loading branch information
tofarr authored and Ethan0456 committed Oct 28, 2024
1 parent ac07dce commit 71a28eb
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions openhands/events/stream.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import threading
from dataclasses import dataclass, field
from datetime import datetime
from enum import Enum
from typing import Callable, Iterable
Expand Down Expand Up @@ -29,24 +30,17 @@ def session_exists(sid: str, file_store: FileStore) -> bool:
return False


@dataclass
class EventStream:
sid: str
file_store: FileStore
# For each subscriber ID, there is a stack of callback functions - useful
# when there are agent delegates
_subscribers: dict[str, list[Callable]]
_cur_id: int
_lock: threading.Lock

def __init__(self, sid: str, file_store: FileStore):
self.sid = sid
self.file_store = file_store
self._subscribers = {}
self._cur_id = 0
self._lock = threading.Lock()
self._reinitialize_from_file_store()
_subscribers: dict[str, list[Callable]] = field(default_factory=dict)
_cur_id: int = 0
_lock: threading.Lock = field(default_factory=threading.Lock)

def _reinitialize_from_file_store(self) -> None:
def __post_init__(self) -> None:
try:
events = self.file_store.list(f'sessions/{self.sid}/events')
except FileNotFoundError:
Expand Down Expand Up @@ -173,4 +167,4 @@ def clear(self):
self.file_store.delete(f'sessions/{self.sid}')
self._cur_id = 0
# self._subscribers = {}
self._reinitialize_from_file_store()
self.__post_init__()

0 comments on commit 71a28eb

Please sign in to comment.