Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for ystore to be started #43

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pycrdt_websocket/yroom.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,10 @@ def on_message(self, value: Callable[[bytes], Awaitable[bool] | bool] | None):
self._on_message = value

async def _broadcast_updates(self):
if self.ystore is not None and not self.ystore.started.is_set():
self._task_group.start_soon(self.ystore.start)
if self.ystore is not None:
async with self.ystore.start_lock:
if not self.ystore.started.is_set():
await self._task_group.start(self.ystore.start)

async with self._update_receive_stream:
async for update in self._update_receive_stream:
Expand Down
15 changes: 11 additions & 4 deletions pycrdt_websocket/ystore.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class BaseYStore(ABC):
_started: Event | None = None
_stopped: Event | None = None
_task_group: TaskGroup | None = None
__start_lock: Lock | None = None
_public_start_lock: Lock | None = None
_private_start_lock: Lock | None = None

@abstractmethod
def __init__(
Expand All @@ -57,11 +58,17 @@ def stopped(self) -> Event:
self._stopped = Event()
return self._stopped

@property
def start_lock(self) -> Lock:
if self._public_start_lock is None:
self._public_start_lock = Lock()
return self._public_start_lock

@property
def _start_lock(self) -> Lock:
if self.__start_lock is None:
self.__start_lock = Lock()
return self.__start_lock
if self._private_start_lock is None:
self._private_start_lock = Lock()
return self._private_start_lock

async def __aenter__(self) -> BaseYStore:
async with self._start_lock:
Expand Down
Loading