Skip to content

Commit 23a70e1

Browse files
committed
Update readme, small change to api (no longer emit on client)
1 parent fa040df commit 23a70e1

3 files changed

Lines changed: 137 additions & 8 deletions

File tree

README.md

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
* [set_default_scope](#set_default_scope)
2323
* [wait_closed](#wait_closed)
2424
* [Room](#room)
25-
25+
* [methods](#room-methods)
26+
* [properties](#room-properties)
27+
* [join](#join)
28+
* [leave](#leave)
29+
* [emit](#emit)
2630
---------------------------------------
2731

2832
## Installation
@@ -421,4 +425,119 @@ connection is actually closed.
421425

422426
## Room
423427

428+
Rooms can be implemented to listen for events from ThingsDB rooms.
429+
430+
Se the example code:
431+
432+
```python
433+
from thingsdb.room import Room, event
434+
435+
class Chat(Room):
436+
437+
@event('msg')
438+
def on_msg(self, msg):
439+
print(msg)
440+
441+
```
442+
443+
This will listen for `msg` events on a ThingsDB room. To connect out class to a
444+
room, you have to initialize the class with a `roomId` of some ThingsDB code which
445+
returns the `roomId` as integer value. For example:
446+
447+
```python
448+
# Create a chat instance. In this example we initialize our chat with some ThingsDB code
449+
chat = Chat("""//ti
450+
// Create .chat room if the room does not exist.
451+
try(.chat = room());
452+
453+
// return the roomId.
454+
.chat.id();
455+
""")
456+
457+
# Now we can join the room. (we assume that you have a ThingsDB client)
458+
await chat.join(client)
459+
```
460+
461+
## Room Methods
462+
463+
Besides implementing an `@event` handler, a room has also some methods which can be implemented to control or initialize a room.
464+
465+
### on_init(self) -> None
466+
Called when a room is joined. This method will be called only once,
467+
thus *not* after a re-connect like the `on_join(..)` method. This
468+
method is guaranteed to be called *before* the `on_join(..)` method.
469+
470+
### on_join(self) -> None:
471+
Called when a room is joined. Unlike the `on_init(..)` method,
472+
the `on_join(..)` method will be called again after a re-connect.
473+
474+
This is an async method and usually the best method to perform
475+
some ThingsDB queries (if required).
476+
477+
Unless the `wait` argument to the Room.join(..) function is explicitly
478+
set to None, the first call to this method will finish before the
479+
call the Room.join() is returned.
480+
481+
### on_leave(self) -> None:
482+
Called after a leave room request. This event is *not* triggered
483+
by ThingsDB when a client disconnects or when a node is shutting down.
484+
485+
### on_delete(self) -> None:
486+
Called when the room is removed from ThingsDB.
487+
488+
## Room Properties
489+
490+
The following properties are available on a room instance. Note that some properties
491+
might return `None` as long as a room is not joined.
492+
493+
Property | Description
494+
-------- | -----------
495+
`id` | Returns the roomId.
496+
`scope` | Returns the scope of the room.
497+
`client` | Returns the associated client of the room.
498+
499+
### join
500+
501+
```python
502+
Room().join(client: Client, wait: Optional[float] = 60) -> None
503+
```
504+
505+
Joins the room.
506+
507+
#### Args
508+
- client *(thingsdb.client.Client)*:
509+
ThingsDB client instance.
510+
- wait *(float)*:
511+
Max time (in seconds) to wait for the first `on_join` call.
512+
If wait is set to None, the join method will not wait for
513+
the first `on_join` call to happen.
514+
515+
516+
### leave
517+
518+
```python
519+
Room().leave() -> None
520+
```
521+
522+
Leave the room. If the room is not found, a `LookupError` will be raised.
523+
524+
### emit
525+
526+
```python
527+
Room().emit(event: str, *args: Optional[Any],) -> asyncio.Future
528+
```
529+
530+
Emit an event to a room.
531+
532+
#### Args
533+
534+
- *event (str)*:
535+
Name of the event to emit.
536+
- *\*args (any)*:
537+
Additional argument to send with the event.
538+
539+
#### Returns
540+
541+
Future which should be awaited. The result of the future will
542+
be set to `None` when successful.
424543

thingsdb/client/client.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -426,18 +426,15 @@ def run(
426426

427427
return self._write_pkg(Proto.REQ_RUN, data, timeout=timeout)
428428

429-
def emit(
429+
def _emit(
430430
self,
431431
room_id: int,
432432
event: str,
433-
*args,
433+
*args: Optional[Any],
434434
scope: Optional[str] = None,
435435
) -> asyncio.Future:
436436
"""Emit an event.
437437
438-
This function is most likely called from a Room instance but may be
439-
used directly.
440-
441438
Args:
442439
room_id (int):
443440
Room Id to emit the event to.

thingsdb/room/roombase.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ async def join(self, client: Client, wait: Optional[float] = 60):
107107
# wait for the first join to finish
108108
await asyncio.wait_for(self._wait_join, wait)
109109

110-
async def leave(self) -> asyncio.Future:
110+
async def leave(self):
111111
"""Leave a room.
112112
113113
Note: If the room is not found, a LookupError will be raised.
@@ -121,7 +121,20 @@ async def leave(self) -> asyncio.Future:
121121
raise LookupError(f'room Id {self._id} is not found (anymore)')
122122

123123
def emit(self, event: str, *args) -> asyncio.Future:
124-
return self._client.emit(self._id, event, *args, scope=self._scope)
124+
"""Emit an event.
125+
126+
Args:
127+
event (str):
128+
Name of the event to emit.
129+
*args:
130+
Additional argument to send with the event.
131+
132+
Returns:
133+
asyncio.Future (None):
134+
Future which should be awaited. The result of the future will
135+
be set to `None` when successful.
136+
"""
137+
return self._client._emit(self._id, event, *args, scope=self._scope)
125138

126139
def _on_event(self, pkg):
127140
self.__class__._ROOM_EVENT_MAP[pkg.tp](self, pkg.data)

0 commit comments

Comments
 (0)