Skip to content

Commit c325e41

Browse files
authored
No events during on_join(..) (#36)
* No events during on_join(..) * Update README.md, 0 no longer waits
1 parent 187646b commit c325e41

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ This is an async method and usually the best method to perform
485485
some ThingsDB queries (if required).
486486

487487
Unless the `wait` argument to the Room.join(..) function is explicitly
488-
set to None, the first call to this method will finish before the
488+
set to `0` or `None`, the first call to this method will finish before the
489489
call to `Room.join()` is returned.
490490

491491
### on_leave(self) -> None:
@@ -514,7 +514,7 @@ Property | Description
514514
### join
515515

516516
```python
517-
Room().join(client: Client, wait: Optional[float] = 60) -> None
517+
Room().join(client: Client, wait: Optional[float] = 60.0) -> None
518518
```
519519

520520
Joins the room.
@@ -524,7 +524,7 @@ Joins the room.
524524
ThingsDB client instance.
525525
- wait *(float)*:
526526
Max time (in seconds) to wait for the first `on_join` call.
527-
If wait is set to None, the join method will not wait for
527+
If wait is set to `0` or `None`, the join method will not wait for
528528
the first `on_join` call to happen.
529529

530530

thingsdb/client/client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,9 @@ async def _on_room(self, room_id, pkg):
563563
f'Got an event (tp:{pkg.tp}) for room Id {room_id} but '
564564
f'the room is not known by the ThingsDB client')
565565
else:
566-
room._on_event(pkg)
566+
task = room._on_event(pkg)
567+
if isinstance(task, asyncio.Task):
568+
await task
567569

568570
def _on_event(self, pkg):
569571
if pkg.tp == Proto.ON_NODE_STATUS:

thingsdb/room/roombase.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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):

thingsdb/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.0.2'
1+
__version__ = '1.0.3'

0 commit comments

Comments
 (0)