Skip to content

Commit d0bb054

Browse files
committed
Suppor for peers only emits
1 parent 30b06d9 commit d0bb054

File tree

5 files changed

+16
-6
lines changed

5 files changed

+16
-6
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ Leave the room. If the room is not found, a `LookupError` will be raised.
572572
### emit
573573

574574
```python
575-
Room().emit(event: str, *args: Any) -> asyncio.Future
575+
Room().emit(event: str, *args: Any, peers_only: bool = False) -> asyncio.Future
576576
```
577577

578578
Emit an event to a room.
@@ -583,6 +583,11 @@ Emit an event to a room.
583583
Name of the event to emit.
584584
- *\*args (any)*:
585585
Additional argument to send with the event.
586+
- *peers_only (bool)*:
587+
If `True`, the event will only be sent to other
588+
listeners (peers) and will not be echoed back to the
589+
sender. Defaults to `False`, which sends to all
590+
_(requires ThingsDB v1.7.7 or higher)_.
586591

587592
#### Returns
588593

thingsdb/client/baseprotocol.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class Proto(enum.IntEnum):
5252
REQ_JOIN = 0x26
5353
REQ_LEAVE = 0x27
5454
REQ_EMIT = 0x28
55+
REQ_EMIT_PEER = 0x29
5556

5657

5758
class Err(enum.IntEnum):

thingsdb/client/client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,8 @@ async def _emit(
473473
room_id: int | str,
474474
event: str,
475475
*args: Any,
476-
scope: str | None = None):
476+
scope: str | None = None,
477+
peers_only: bool = False):
477478
"""Emit an event.
478479
479480
Use Room(room_id, scope=scope).emit(..) instead of this function to
@@ -500,7 +501,8 @@ async def _emit(
500501
"""
501502
if scope is None:
502503
scope = self._scope
503-
await self._write_pkg(Proto.REQ_EMIT, [scope, room_id, event, *args])
504+
proto = Proto.REQ_EMIT_PEER if peers_only else Proto.REQ_EMIT
505+
await self._write_pkg(proto, [scope, room_id, event, *args])
504506

505507
def _join(self, *ids: int | str,
506508
scope: str | None = None

thingsdb/room/roombase.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ async def leave(self):
176176
if res[0] is None:
177177
raise LookupError(f'room Id {self._id} is not found (anymore)')
178178

179-
async def emit(self, event: str, *args):
179+
async def emit(self, event: str, *args, peers_only: bool = False):
180180
"""Emit an event.
181181
182182
Args:
@@ -193,7 +193,9 @@ async def emit(self, event: str, *args):
193193
if self._client is None:
194194
raise RuntimeError(
195195
'must call join(..) or no_join(..) before using emit')
196-
await self._client._emit(self._id, event, *args, scope=self._scope)
196+
await self._client._emit(self._id, event, *args,
197+
scope=self._scope,
198+
peers_only=peers_only)
197199

198200
def _on_event(self, pkg) -> asyncio.Task | None:
199201
return self.__class__._ROOM_EVENT_MAP[pkg.tp](self, pkg.data)

thingsdb/version.py

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

0 commit comments

Comments
 (0)