Skip to content

Commit a4871f5

Browse files
authored
Enum for TextStreamOperationType (#882)
1 parent ad106c0 commit a4871f5

File tree

4 files changed

+53
-26
lines changed

4 files changed

+53
-26
lines changed

lib/src/core/room.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ extension DataStreamRoomMethods on Room {
13401340
? streamHeader.textHeader.generated
13411341
: false,
13421342
operationType: streamHeader.textHeader.hasOperationType()
1343-
? streamHeader.textHeader.operationType.name
1343+
? TextStreamOperationType.fromPBType(streamHeader.textHeader.operationType)
13441344
: null,
13451345
);
13461346

lib/src/participant/local.dart

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,22 +1152,6 @@ extension RPCMethods on LocalParticipant {
11521152
}
11531153
}
11541154

1155-
/// Helper function to convert string operation type to enum
1156-
lk_models.DataStream_OperationType _stringToOperationType(String? type) {
1157-
switch (type?.toLowerCase()) {
1158-
case 'create':
1159-
return lk_models.DataStream_OperationType.CREATE;
1160-
case 'update':
1161-
return lk_models.DataStream_OperationType.UPDATE;
1162-
case 'delete':
1163-
return lk_models.DataStream_OperationType.DELETE;
1164-
case 'reaction':
1165-
return lk_models.DataStream_OperationType.REACTION;
1166-
default:
1167-
return lk_models.DataStream_OperationType.CREATE;
1168-
}
1169-
}
1170-
11711155
extension DataStreamParticipantMethods on LocalParticipant {
11721156
Future<TextStreamInfo> sendText(String text,
11731157
{SendTextOptions? options}) async {
@@ -1259,7 +1243,7 @@ extension DataStreamParticipantMethods on LocalParticipant {
12591243
attachedStreamIds: options?.attachedStreamIds,
12601244
replyToStreamId: options?.replyToStreamId,
12611245
generated: options?.generated ?? false,
1262-
operationType: _stringToOperationType(options?.type),
1246+
operationType: options?.type?.toPBType(),
12631247
),
12641248
);
12651249

lib/src/types/data_stream.dart

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:async';
22
import 'dart:io' show File;
33

44
import '../data_stream/stream_reader.dart';
5+
import '../proto/livekit_models.pb.dart' as lk_models;
56
import '../proto/livekit_models.pb.dart' show Encryption_Type, DataStream_Chunk;
67

78
const kStreamChunkSize = 15_000;
@@ -53,7 +54,7 @@ class StreamTextOptions {
5354
int? totalSize;
5455

5556
/// 'create' | 'update' | 'delete' | 'reaction'
56-
String? type;
57+
TextStreamOperationType? type;
5758

5859
/// true if the text has been generated by an agent from a participant's audio transcription
5960
bool generated;
@@ -165,6 +166,43 @@ class ByteStreamInfo extends BaseStreamInfo {
165166
);
166167
}
167168

169+
/// Operation types for text streams
170+
enum TextStreamOperationType {
171+
create,
172+
update,
173+
delete,
174+
reaction;
175+
176+
static TextStreamOperationType? fromPBType(lk_models.DataStream_OperationType? type) {
177+
if (type == null) return TextStreamOperationType.create;
178+
switch (type) {
179+
case lk_models.DataStream_OperationType.CREATE:
180+
return TextStreamOperationType.create;
181+
case lk_models.DataStream_OperationType.UPDATE:
182+
return TextStreamOperationType.update;
183+
case lk_models.DataStream_OperationType.DELETE:
184+
return TextStreamOperationType.delete;
185+
case lk_models.DataStream_OperationType.REACTION:
186+
return TextStreamOperationType.reaction;
187+
default:
188+
return null;
189+
}
190+
}
191+
192+
lk_models.DataStream_OperationType toPBType() {
193+
switch (this) {
194+
case TextStreamOperationType.create:
195+
return lk_models.DataStream_OperationType.CREATE;
196+
case TextStreamOperationType.update:
197+
return lk_models.DataStream_OperationType.UPDATE;
198+
case TextStreamOperationType.delete:
199+
return lk_models.DataStream_OperationType.DELETE;
200+
case TextStreamOperationType.reaction:
201+
return lk_models.DataStream_OperationType.REACTION;
202+
}
203+
}
204+
}
205+
168206
class TextStreamInfo extends BaseStreamInfo {
169207
/// The stream ID this message is replying to, if any
170208
final String? replyToStreamId;
@@ -179,7 +217,7 @@ class TextStreamInfo extends BaseStreamInfo {
179217
final bool generated;
180218

181219
/// Operation type for the stream
182-
final String? operationType;
220+
final TextStreamOperationType? operationType;
183221

184222
TextStreamInfo({
185223
required String id,

test/core/data_stream_test.dart

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ void main() {
157157
});
158158

159159
test('Text Stream With Operation Types', () async {
160-
final operationTypes = ['create', 'update', 'delete', 'reaction'];
160+
final operationTypes = [
161+
TextStreamOperationType.create,
162+
TextStreamOperationType.update,
163+
TextStreamOperationType.delete,
164+
TextStreamOperationType.reaction,
165+
];
161166
final receivedMessages = <String>[];
162167

163168
for (var operationType in operationTypes) {
@@ -176,7 +181,7 @@ void main() {
176181
final stream = await room.localParticipant?.streamText(StreamTextOptions(
177182
topic: 'chat-operations',
178183
type: operationType,
179-
version: operationType == 'update' ? 2 : null,
184+
version: operationType == TextStreamOperationType.update ? 2 : null,
180185
));
181186
await stream?.write('Streamed ${operationType}');
182187
await stream?.close();
@@ -223,13 +228,13 @@ void main() {
223228
// Verify that reply metadata is accessible
224229
expect(reader.info?.replyToStreamId, originalStreamId);
225230
expect(reader.info?.version, 1);
226-
expect(reader.info?.operationType, 'CREATE');
231+
expect(reader.info?.operationType, TextStreamOperationType.create);
227232
});
228233

229234
// Send a reply to an existing stream
230235
final stream = await room.localParticipant?.streamText(StreamTextOptions(
231236
topic: 'chat-replies',
232-
type: 'create',
237+
type: TextStreamOperationType.create,
233238
streamId: replyStreamId,
234239
replyToStreamId: originalStreamId,
235240
version: 1,
@@ -388,7 +393,7 @@ void main() {
388393
final stream = await room.localParticipant?.streamText(StreamTextOptions(
389394
topic: 'concurrent-streams',
390395
streamId: 'stream-${i}',
391-
type: 'create',
396+
type: TextStreamOperationType.create,
392397
));
393398
await stream?.write('Concurrent message ${i}');
394399
await stream?.close();
@@ -454,7 +459,7 @@ void main() {
454459
// Send a message with comprehensive options
455460
final stream = await room.localParticipant?.streamText(StreamTextOptions(
456461
topic: 'header-validation',
457-
type: 'create',
462+
type: TextStreamOperationType.create,
458463
version: 1,
459464
generated: false,
460465
attributes: {

0 commit comments

Comments
 (0)