Skip to content

Commit 453bd58

Browse files
committed
use simple async text iterator for text streams
1 parent 1962bd5 commit 453bd58

File tree

1 file changed

+5
-20
lines changed

1 file changed

+5
-20
lines changed

livekit-rtc/livekit/rtc/data_stream.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,6 @@ class BaseStreamInfo:
4747
@dataclass
4848
class TextStreamInfo(BaseStreamInfo):
4949
attachments: List[str]
50-
pass
51-
52-
53-
@dataclass
54-
class TextStreamUpdate:
55-
current: str
56-
index: int
57-
collected: str
5850

5951

6052
class TextStreamReader:
@@ -81,31 +73,24 @@ async def _on_chunk_update(self, chunk: proto_DataStream.Chunk):
8173
async def _on_stream_close(self, trailer: proto_DataStream.Trailer):
8274
await self._queue.put(None)
8375

84-
def __aiter__(self) -> AsyncIterator[TextStreamUpdate]:
76+
def __aiter__(self) -> AsyncIterator[str]:
8577
return self
8678

87-
async def __anext__(self) -> TextStreamUpdate:
79+
async def __anext__(self) -> str:
8880
item = await self._queue.get()
8981
if item is None:
9082
raise StopAsyncIteration
9183
decodedStr = item.content.decode()
92-
93-
self._chunks[item.chunk_index] = item
94-
chunk_list = list(self._chunks.values())
95-
chunk_list.sort(key=lambda chunk: chunk.chunk_index)
96-
collected: str = "".join(map(lambda chunk: chunk.content.decode(), chunk_list))
97-
return TextStreamUpdate(
98-
current=decodedStr, index=item.chunk_index, collected=collected
99-
)
84+
return decodedStr
10085

10186
@property
10287
def info(self) -> TextStreamInfo:
10388
return self._info
10489

10590
async def read_all(self) -> str:
10691
final_string = ""
107-
async for update in self:
108-
final_string = update.collected
92+
async for chunk in self:
93+
final_string += chunk
10994
return final_string
11095

11196

0 commit comments

Comments
 (0)