Skip to content

Commit 15a836c

Browse files
authored
Refactor participant creation (#927)
1 parent b324c6b commit 15a836c

File tree

5 files changed

+49
-46
lines changed

5 files changed

+49
-46
lines changed

lib/src/core/room.dart

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
422422
logger.fine('[Engine] Received JoinResponse, '
423423
'serverVersion: ${event.response.serverVersion}');
424424

425-
_localParticipant ??= LocalParticipant(
425+
_localParticipant ??= await LocalParticipant.createFromInfo(
426426
room: this,
427427
info: event.response.participant,
428428
);
@@ -487,7 +487,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
487487
for (final info in event.response.otherParticipants) {
488488
logger.fine('Creating RemoteParticipant: sid = ${info.sid}(identity:${info.identity}) '
489489
'tracks:${info.tracks.map((e) => e.sid)}');
490-
await _getOrCreateRemoteParticipant(info.identity, info);
490+
await _getOrCreateRemoteParticipant(info);
491491
}
492492

493493
if (e2eeManager != null && event.response.sifTrailer.isNotEmpty) {
@@ -657,9 +657,12 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
657657
return null;
658658
}
659659

660-
Future<ParticipantCreationResult> _getOrCreateRemoteParticipant(
661-
String identity, lk_models.ParticipantInfo? info) async {
662-
RemoteParticipant? participant = _remoteParticipants[identity];
660+
Future<ParticipantCreationResult> _getOrCreateRemoteParticipant(lk_models.ParticipantInfo info) async {
661+
if (!info.hasIdentity() || !info.hasSid()) {
662+
throw Exception('ParticipantInfo must have identity and sid');
663+
}
664+
665+
final participant = _remoteParticipants[info.identity];
663666
if (participant != null) {
664667
// Return existing participant with no new publications; caller handles updates.
665668
return ParticipantCreationResult(
@@ -668,25 +671,10 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
668671
);
669672
}
670673

671-
ParticipantCreationResult result;
672-
if (info == null) {
673-
logger.warning('RemoteParticipant.info is null identity: $identity');
674-
participant = RemoteParticipant(
675-
room: this,
676-
sid: '',
677-
identity: identity,
678-
name: '',
679-
);
680-
result = ParticipantCreationResult(
681-
participant: participant,
682-
newPublications: const [],
683-
);
684-
} else {
685-
result = await RemoteParticipant.createFromInfo(
686-
room: this,
687-
info: info,
688-
);
689-
}
674+
final result = await RemoteParticipant.createFromInfo(
675+
room: this,
676+
info: info,
677+
);
690678

691679
_remoteParticipants[result.participant.identity] = result.participant;
692680
_sidToIdentity[result.participant.sid] = result.participant.identity;
@@ -717,7 +705,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
717705
continue;
718706
}
719707

720-
final result = await _getOrCreateRemoteParticipant(info.identity, info);
708+
final result = await _getOrCreateRemoteParticipant(info);
721709

722710
if (isNew) {
723711
hasChanged = true;

lib/src/participant/local.dart

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,42 @@ class LocalParticipant extends Participant<LocalTrackPublication> {
6565
// RPC Pending Responses
6666
final Map<String, Function(String? payload, RpcError? error)> _pendingResponses = {};
6767

68-
@internal
69-
LocalParticipant({
68+
LocalParticipant._({
7069
required Room room,
71-
required lk_models.ParticipantInfo info,
70+
required String sid,
71+
required String identity,
72+
required String name,
7273
}) : super(
7374
room: room,
74-
sid: info.sid,
75-
identity: info.identity,
76-
name: info.name,
77-
) {
78-
// updateFromInfo() is sync, no need to wait here.
79-
unawaited(updateFromInfo(info));
75+
sid: sid,
76+
identity: identity,
77+
name: name,
78+
);
79+
80+
@internal
81+
static Future<LocalParticipant> createFromInfo({
82+
required Room room,
83+
required lk_models.ParticipantInfo info,
84+
}) async {
85+
final participant = LocalParticipant._(
86+
room: room,
87+
sid: info.sid,
88+
identity: info.identity,
89+
name: info.name,
90+
);
91+
92+
await participant.updateFromInfo(info);
8093

8194
if (lkPlatformIs(PlatformType.iOS)) {
82-
BroadcastManager().addListener(_broadcastStateChanged);
95+
BroadcastManager().addListener(participant._broadcastStateChanged);
8396
}
8497

85-
onDispose(() async {
86-
BroadcastManager().removeListener(_broadcastStateChanged);
87-
await unpublishAllTracks();
98+
participant.onDispose(() async {
99+
BroadcastManager().removeListener(participant._broadcastStateChanged);
100+
await participant.unpublishAllTracks();
88101
});
102+
103+
return participant;
89104
}
90105

91106
/// Handle broadcast state change (iOS only)

lib/src/participant/participant.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,9 @@ abstract class Participant<T extends TrackPublication> extends DisposableChangeN
210210

211211
@internal
212212
Future<bool> updateFromInfo(lk_models.ParticipantInfo info) async {
213-
logger.fine('LocalParticipant.updateFromInfo(info: $info)');
213+
logger.fine('${runtimeType}.updateFromInfo(info: $info)');
214214
if (_participantInfo != null && _participantInfo!.sid == info.sid && _participantInfo!.version > info.version) {
215+
// Ignore out-of-order updates.
215216
return false;
216217
}
217218

lib/src/participant/remote.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ class ParticipantCreationResult {
5555

5656
/// Represents other participant in the [Room].
5757
class RemoteParticipant extends Participant<RemoteTrackPublication> {
58-
@internal
59-
RemoteParticipant({
58+
RemoteParticipant._({
6059
required Room room,
6160
required String sid,
6261
required String identity,
@@ -84,7 +83,7 @@ class RemoteParticipant extends Participant<RemoteTrackPublication> {
8483
required Room room,
8584
required lk_models.ParticipantInfo info,
8685
}) async {
87-
final participant = RemoteParticipant(
86+
final participant = RemoteParticipant._(
8887
room: room,
8988
sid: info.sid,
9089
identity: info.identity,

pubspec.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,10 @@ packages:
452452
dependency: "direct main"
453453
description:
454454
name: meta
455-
sha256: e3641ec5d63ebf0d9b41bd43201a66e3fc79a65db5f61fc181f04cd27aab950c
455+
sha256: "23f08335362185a5ea2ad3a4e597f1375e78bce8a040df5c600c8d3552ef2394"
456456
url: "https://pub.dev"
457457
source: hosted
458-
version: "1.16.0"
458+
version: "1.17.0"
459459
mime:
460460
dependency: transitive
461461
description:
@@ -721,10 +721,10 @@ packages:
721721
dependency: transitive
722722
description:
723723
name: test_api
724-
sha256: "522f00f556e73044315fa4585ec3270f1808a4b186c936e612cab0b565ff1e00"
724+
sha256: ab2726c1a94d3176a45960b6234466ec367179b87dd74f1611adb1f3b5fb9d55
725725
url: "https://pub.dev"
726726
source: hosted
727-
version: "0.7.6"
727+
version: "0.7.7"
728728
timing:
729729
dependency: transitive
730730
description:

0 commit comments

Comments
 (0)