Skip to content

Commit 2118ec3

Browse files
committed
Add updateText API
1 parent 64902f6 commit 2118ec3

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

src/room/Room.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
DataPacket_Kind,
77
DataStream_Chunk,
88
DataStream_Header,
9+
DataStream_OperationType,
910
DataStream_Trailer,
1011
DisconnectReason,
1112
JoinResponse,
@@ -1831,6 +1832,11 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
18311832
topic: streamHeader.topic,
18321833
timestamp: Number(streamHeader.timestamp),
18331834
attributes: streamHeader.attributes,
1835+
version: streamHeader.contentHeader.value.version,
1836+
type:
1837+
streamHeader.contentHeader.value.operationType === DataStream_OperationType.UPDATE
1838+
? 'update'
1839+
: 'create',
18341840
};
18351841

18361842
const stream = new ReadableStream<DataStream_Chunk>({

src/room/StreamReader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export class TextStreamReader extends BaseStreamReader<TextStreamInfo> {
150150
}
151151
},
152152

153-
return(): IteratorResult<TextStreamChunk> {
153+
async return(): Promise<IteratorResult<TextStreamChunk>> {
154154
reader.releaseLock();
155155
return { done: true, value: undefined };
156156
},

src/room/participant/LocalParticipant.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,6 +1567,42 @@ export default class LocalParticipant extends Participant {
15671567
return writer.info;
15681568
}
15691569

1570+
/** @experimental CAUTION, might get removed or changed in a minor release */
1571+
async updateText(
1572+
streamId: string,
1573+
text: string,
1574+
options?: {
1575+
topic?: string;
1576+
destinationIdentities?: Array<string>;
1577+
},
1578+
): Promise<TextStreamInfo> {
1579+
const textInBytes = new TextEncoder().encode(text);
1580+
const totalTextLength = textInBytes.byteLength;
1581+
1582+
const writer = await this.streamText({
1583+
streamId,
1584+
totalSize: totalTextLength,
1585+
destinationIdentities: options?.destinationIdentities,
1586+
topic: options?.topic,
1587+
type: 'update',
1588+
});
1589+
1590+
const textChunkSize = Math.floor(STREAM_CHUNK_SIZE / 4); // utf8 is at most 4 bytes long, so play it safe and take a quarter of the byte size to slice the string
1591+
const totalTextChunks = Math.ceil(totalTextLength / textChunkSize);
1592+
1593+
for (let i = 0; i < totalTextChunks; i++) {
1594+
const chunkData = text.slice(
1595+
i * textChunkSize,
1596+
Math.min((i + 1) * textChunkSize, totalTextLength),
1597+
);
1598+
await this.engine.waitForBufferStatusLow(DataPacket_Kind.RELIABLE);
1599+
await writer.write(chunkData);
1600+
}
1601+
1602+
await writer.close();
1603+
return writer.info;
1604+
}
1605+
15701606
/**
15711607
* @internal
15721608
* @experimental CAUTION, might get removed in a minor release
@@ -1580,6 +1616,8 @@ export default class LocalParticipant extends Participant {
15801616
timestamp: Date.now(),
15811617
topic: options?.topic ?? '',
15821618
size: options?.totalSize,
1619+
version: options?.version,
1620+
type: options?.type ?? 'create',
15831621
};
15841622
const header = new DataStream_Header({
15851623
streamId,
@@ -1590,11 +1628,11 @@ export default class LocalParticipant extends Participant {
15901628
contentHeader: {
15911629
case: 'textHeader',
15921630
value: new DataStream_TextHeader({
1593-
version: options?.version,
1631+
version: info?.version,
15941632
attachedStreamIds: options?.attachedStreamIds,
15951633
replyToStreamId: options?.replyToStreamId,
15961634
operationType:
1597-
options?.type === 'update'
1635+
info.type === 'update'
15981636
? DataStream_OperationType.UPDATE
15991637
: DataStream_OperationType.CREATE,
16001638
}),

src/room/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ export interface ByteStreamInfo extends BaseStreamInfo {
118118
name: string;
119119
}
120120

121-
export interface TextStreamInfo extends BaseStreamInfo {}
121+
export interface TextStreamInfo extends BaseStreamInfo {
122+
version?: number;
123+
type: 'create' | 'update';
124+
}
122125

123126
export type TextStreamChunk = {
124127
index: number;

0 commit comments

Comments
 (0)