@@ -36,7 +36,7 @@ def __init__(
3636 self ._client = None
3737 self ._id = room
3838 self ._scope = scope
39- self ._wait_join = None
39+ self ._wait_join = False
4040
4141 @property
4242 def id (self ):
@@ -50,16 +50,16 @@ def scope(self):
5050 def client (self ):
5151 return self ._client
5252
53- async def join (self , client : Client , wait : Optional [float ] = 60 ):
53+ async def join (self , client : Client , wait : Optional [float ] = 60.0 ):
5454 """Join a room.
5555
5656 Args:
5757 client (thingsdb.client.Client):
5858 ThingsDB client instance.
5959 wait (float):
6060 Max time (in seconds) to wait for the first `on_join` call.
61- If wait is set to None, the join method will not wait for
62- the first `on_join` call to happen.
61+ If wait is set to `0` or ` None` , the join method will not
62+ wait for the first `on_join` call to happen.
6363 """
6464 # Although ThingsDB guarantees to return the response on the join
6565 # request before the "on_join" event is being transmitted, the asyncio
@@ -100,10 +100,10 @@ async def join(self, client: Client, wait: Optional[float] = 60):
100100
101101 client ._rooms [self ._id ] = self
102102 self .on_init ()
103- if wait is not None :
103+ if wait :
104104 self ._wait_join = asyncio .Future ()
105105
106- if wait is not None :
106+ if wait :
107107 # wait for the first join to finish
108108 await asyncio .wait_for (self ._wait_join , wait )
109109
@@ -136,8 +136,8 @@ def emit(self, event: str, *args) -> asyncio.Future:
136136 """
137137 return self ._client ._emit (self ._id , event , * args , scope = self ._scope )
138138
139- def _on_event (self , pkg ):
140- self .__class__ ._ROOM_EVENT_MAP [pkg .tp ](self , pkg .data )
139+ def _on_event (self , pkg ) -> Optional [ asyncio . Task ] :
140+ return self .__class__ ._ROOM_EVENT_MAP [pkg .tp ](self , pkg .data )
141141
142142 @abc .abstractmethod
143143 def on_init (self ) -> None :
@@ -169,10 +169,19 @@ async def _on_first_join(self):
169169 fut .set_result (None )
170170
171171 def _on_join (self , _data ):
172- loop = self .client .get_event_loop ()
173172 if self ._wait_join :
174- asyncio .ensure_future (self ._on_first_join (), loop = loop )
173+ # Future, the first join. Return a task so the room lock is kept
174+ # until the on_first_join is finished
175+ return asyncio .create_task (self ._on_first_join ())
176+ elif self ._wait_join is None :
177+ # Initially a wait was set, do not handle (new) events until the
178+ # join is (again) finished
179+ return asyncio .create_task (self .on_join ())
175180 else :
181+ # User has decided not to wait for the join. Thus we can asume that
182+ # event handlers do not depend on the on_join to be finished
183+ assert self ._wait_join is False
184+ loop = self .client .get_event_loop ()
176185 asyncio .ensure_future (self .on_join (), loop = loop )
177186
178187 def _on_stop (self , func ):
0 commit comments