Skip to content

Commit 81a5ff4

Browse files
authored
Explicit delegate signatures (livekit#283)
A few delegates had the same signatures (by using overloading) such as: `room(_ room: Room, didUpdate speakers: [Participant])` `room(_ room: Room, didUpdate metadata: String?)` `room(_ room: Room, didUpdate isRecording: Bool)` This pattern will break if add another method of the same type will be added in the future: `room(_ room: Room, didUpdate isSomeExampleFlag: Bool)` Additionally, since labels can be omitted in Swift, the implementation side can be ambiguous: ```Swift func room(_: Room, didUpdate _: String?) { // ... } func room(_: Room, didUpdate _: Bool) { // ... } ``` This PR makes delegate signatures explicit and future-proof by including the label in the signature: `room(_ room: Room, didUpdateSpeakingParticipants participants: [Participant])` `room(_ room: Room, didUpdateMetadata metadata: String?)` `room(_ room: Room, didUpdateIsRecording isRecording: Bool)` I can deprecate the old methods, but since this will be part of v2 release maybe it's not required to deprecate.
1 parent e901d1e commit 81a5ff4

File tree

10 files changed

+143
-122
lines changed

10 files changed

+143
-122
lines changed

Sources/LiveKit/Core/Room+EngineDelegate.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extension Room: EngineDelegate {
4040
}
4141

4242
delegates.notify(label: { "room.didUpdate connectionState: \(state.connectionState) oldValue: \(oldState.connectionState)" }) {
43-
$0.room?(self, didUpdate: state.connectionState, oldValue: oldState.connectionState)
43+
$0.room?(self, didUpdateConnectionState: state.connectionState, oldConnectionState: oldState.connectionState)
4444
}
4545

4646
// Legacy connection delegates
@@ -49,9 +49,9 @@ extension Room: EngineDelegate {
4949
delegates.notify { $0.room?(self, didConnect: didReconnect) }
5050
} else if case .disconnected = state.connectionState {
5151
if case .connecting = oldState.connectionState {
52-
delegates.notify { $0.room?(self, didFailToConnect: oldState.disconnectError) }
52+
delegates.notify { $0.room?(self, didFailToConnectWithError: oldState.disconnectError) }
5353
} else {
54-
delegates.notify { $0.room?(self, didDisconnect: state.disconnectError) }
54+
delegates.notify { $0.room?(self, didDisconnectWithError: state.disconnectError) }
5555
}
5656
}
5757
}
@@ -116,7 +116,7 @@ extension Room: EngineDelegate {
116116
guard let self else { return }
117117

118118
self.delegates.notify(label: { "room.didUpdate speakers: \(activeSpeakers)" }) {
119-
$0.room?(self, didUpdate: activeSpeakers)
119+
$0.room?(self, didUpdateSpeakingParticipants: activeSpeakers)
120120
}
121121
}
122122
}

Sources/LiveKit/Core/Room+SignalClientDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ extension Room: SignalClientDelegate {
133133
guard let self else { return }
134134

135135
self.delegates.notify(label: { "room.didUpdate speakers: \(speakers)" }) {
136-
$0.room?(self, didUpdate: activeSpeakers)
136+
$0.room?(self, didUpdateSpeakingParticipants: activeSpeakers)
137137
}
138138
}
139139
}

Sources/LiveKit/Core/Room.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public class Room: NSObject, ObservableObject, Loggable {
181181
guard let self else { return }
182182

183183
self.delegates.notify(label: { "room.didUpdate metadata: \(metadata)" }) {
184-
$0.room?(self, didUpdate: metadata)
184+
$0.room?(self, didUpdateMetadata: metadata)
185185
}
186186
}
187187
}
@@ -194,7 +194,7 @@ public class Room: NSObject, ObservableObject, Loggable {
194194
guard let self else { return }
195195

196196
self.delegates.notify(label: { "room.didUpdate isRecording: \(newState.isRecording)" }) {
197-
$0.room?(self, didUpdate: newState.isRecording)
197+
$0.room?(self, didUpdateIsRecording: newState.isRecording)
198198
}
199199
}
200200
}

Sources/LiveKit/Participant/LocalParticipant.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,10 @@ public class LocalParticipant: Participant {
207207

208208
// Notify didPublish
209209
delegates.notify(label: { "localParticipant.didPublish \(publication)" }) {
210-
$0.localParticipant?(self, didPublish: publication)
210+
$0.localParticipant?(self, didPublishPublication: publication)
211211
}
212212
room.delegates.notify(label: { "localParticipant.didPublish \(publication)" }) {
213-
$0.room?(self.room, localParticipant: self, didPublish: publication)
213+
$0.room?(self.room, localParticipant: self, didPublishPublication: publication)
214214
}
215215

216216
log("[publish] success \(publication)", .info)
@@ -259,10 +259,10 @@ public class LocalParticipant: Participant {
259259
func _notifyDidUnpublish() async {
260260
guard _notify else { return }
261261
delegates.notify(label: { "localParticipant.didUnpublish \(publication)" }) {
262-
$0.localParticipant?(self, didUnpublish: publication)
262+
$0.localParticipant?(self, didUnpublishPublication: publication)
263263
}
264264
room.delegates.notify(label: { "room.didUnpublish \(publication)" }) {
265-
$0.room?(self.room, localParticipant: self, didUnpublish: publication)
265+
$0.room?(self.room, localParticipant: self, didUnpublishPublication: publication)
266266
}
267267
}
268268

@@ -402,11 +402,11 @@ public class LocalParticipant: Participant {
402402
let didUpdate = super.set(permissions: newValue)
403403

404404
if didUpdate {
405-
delegates.notify(label: { "participant.didUpdate permissions: \(newValue)" }) {
406-
$0.participant?(self, didUpdate: newValue)
405+
delegates.notify(label: { "participant.didUpdatePermissions: \(newValue)" }) {
406+
$0.participant?(self, didUpdatePermissions: newValue)
407407
}
408-
room.delegates.notify(label: { "room.didUpdate permissions: \(newValue)" }) {
409-
$0.room?(self.room, participant: self, didUpdate: newValue)
408+
room.delegates.notify(label: { "room.didUpdatePermissions: \(newValue)" }) {
409+
$0.room?(self.room, participant: self, didUpdatePermissions: newValue)
410410
}
411411
}
412412

Sources/LiveKit/Participant/Participant.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public class Participant: NSObject, ObservableObject, Loggable {
102102

103103
if newState.isSpeaking != oldState.isSpeaking {
104104
self.delegates.notify(label: { "participant.didUpdate isSpeaking: \(self.isSpeaking)" }) {
105-
$0.participant?(self, didUpdate: self.isSpeaking)
105+
$0.participant?(self, didUpdateIsSpeaking: self.isSpeaking)
106106
}
107107
}
108108

@@ -112,10 +112,10 @@ public class Participant: NSObject, ObservableObject, Loggable {
112112
oldState.metadata == nil ? !metadata.isEmpty : true
113113
{
114114
self.delegates.notify(label: { "participant.didUpdate metadata: \(metadata)" }) {
115-
$0.participant?(self, didUpdate: metadata)
115+
$0.participant?(self, didUpdateMetadata: metadata)
116116
}
117117
self.room.delegates.notify(label: { "room.didUpdate metadata: \(metadata)" }) {
118-
$0.room?(self.room, participant: self, didUpdate: metadata)
118+
$0.room?(self.room, participant: self, didUpdateMetadata: metadata)
119119
}
120120
}
121121

@@ -133,10 +133,10 @@ public class Participant: NSObject, ObservableObject, Loggable {
133133

134134
if newState.connectionQuality != oldState.connectionQuality {
135135
self.delegates.notify(label: { "participant.didUpdate connectionQuality: \(self.connectionQuality)" }) {
136-
$0.participant?(self, didUpdate: self.connectionQuality)
136+
$0.participant?(self, didUpdateConnectionQuality: self.connectionQuality)
137137
}
138138
self.room.delegates.notify(label: { "room.didUpdate connectionQuality: \(self.connectionQuality)" }) {
139-
$0.room?(self.room, participant: self, didUpdate: self.connectionQuality)
139+
$0.room?(self.room, participant: self, didUpdateConnectionQuality: self.connectionQuality)
140140
}
141141
}
142142

Sources/LiveKit/Participant/RemoteParticipant.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ public class RemoteParticipant: Participant {
5959

6060
for publication in newTrackPublications.values {
6161
self.delegates.notify(label: { "participant.didPublish \(publication)" }) {
62-
$0.participant?(self, didPublish: publication)
62+
$0.participant?(self, didPublishPublication: publication)
6363
}
6464
self.room.delegates.notify(label: { "room.didPublish \(publication)" }) {
65-
$0.room?(self.room, participant: self, didPublish: publication)
65+
$0.room?(self.room, participant: self, didPublishPublication: publication)
6666
}
6767
}
6868
}
@@ -132,10 +132,10 @@ public class RemoteParticipant: Participant {
132132
try await track.start()
133133

134134
delegates.notify(label: { "participant.didSubscribe \(publication)" }) {
135-
$0.participant?(self, didSubscribe: publication, track: track)
135+
$0.participant?(self, didSubscribePublication: publication)
136136
}
137137
room.delegates.notify(label: { "room.didSubscribe \(publication)" }) {
138-
$0.room?(self.room, participant: self, didSubscribe: publication, track: track)
138+
$0.room?(self.room, participant: self, didSubscribePublication: publication)
139139
}
140140
}
141141

@@ -163,10 +163,10 @@ public class RemoteParticipant: Participant {
163163
func _notifyUnpublish() async {
164164
guard _notify else { return }
165165
delegates.notify(label: { "participant.didUnpublish \(publication)" }) {
166-
$0.participant?(self, didUnpublish: publication)
166+
$0.participant?(self, didUnpublishPublication: publication)
167167
}
168168
room.delegates.notify(label: { "room.didUnpublish \(publication)" }) {
169-
$0.room?(self.room, participant: self, didUnpublish: publication)
169+
$0.room?(self.room, participant: self, didUnpublishPublication: publication)
170170
}
171171
}
172172

@@ -183,10 +183,10 @@ public class RemoteParticipant: Participant {
183183
if _notify {
184184
// Notify unsubscribe
185185
delegates.notify(label: { "participant.didUnsubscribe \(publication)" }) {
186-
$0.participant?(self, didUnsubscribe: publication, track: track)
186+
$0.participant?(self, didUnsubscribePublication: publication)
187187
}
188188
room.delegates.notify(label: { "room.didUnsubscribe \(publication)" }) {
189-
$0.room?(self.room, participant: self, didUnsubscribe: publication, track: track)
189+
$0.room?(self.room, participant: self, didUnsubscribePublication: publication)
190190
}
191191
}
192192

Sources/LiveKit/Protocols/ParticipantDelegate.swift

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,35 @@ import Foundation
2323
/// All delegate methods are optional.
2424
///
2525
/// To ensure each participant's delegate is registered, you can look through ``Room/localParticipant`` and ``Room/remoteParticipants`` on connect
26-
/// and register it on new participants inside ``RoomDelegate/room(_:participantDidJoin:)-9bkm4``
26+
/// and register it on new participants inside ``RoomDelegate/room(_:participantDidJoin:)``
2727
@objc
2828
public protocol ParticipantDelegate: AnyObject {
29+
// MARK: - Participant
30+
2931
/// A ``Participant``'s metadata has updated.
3032
/// `participant` Can be a ``LocalParticipant`` or a ``RemoteParticipant``.
3133
@objc(participant:didUpdateMetadata:) optional
32-
func participant(_ participant: Participant, didUpdate metadata: String?)
34+
func participant(_ participant: Participant, didUpdateMetadata metadata: String?)
3335

3436
/// A ``Participant``'s name has updated.
3537
/// `participant` Can be a ``LocalParticipant`` or a ``RemoteParticipant``.
3638
@objc(participant:didUpdateName:) optional
37-
func participant(_ participant: Participant, didUpdateName: String?)
39+
func participant(_ participant: Participant, didUpdateName name: String?)
3840

3941
/// The isSpeaking status of a ``Participant`` has changed.
4042
/// `participant` Can be a ``LocalParticipant`` or a ``RemoteParticipant``.
4143
@objc(participant:didUpdateSpeaking:) optional
42-
func participant(_ participant: Participant, didUpdate speaking: Bool)
44+
func participant(_ participant: Participant, didUpdateIsSpeaking isSpeaking: Bool)
4345

4446
/// The connection quality of a ``Participant`` has updated.
4547
/// `participant` Can be a ``LocalParticipant`` or a ``RemoteParticipant``.
4648
@objc(participant:didUpdateConnectionQuality:) optional
47-
func participant(_ participant: Participant, didUpdate connectionQuality: ConnectionQuality)
49+
func participant(_ participant: Participant, didUpdateConnectionQuality connectionQuality: ConnectionQuality)
50+
51+
@objc(participant:didUpdatePermissions:) optional
52+
func participant(_ participant: Participant, didUpdatePermissions permissions: ParticipantPermissions)
53+
54+
// MARK: - TrackPublication
4855

4956
/// `muted` state has updated for the ``Participant``'s ``TrackPublication``.
5057
///
@@ -53,54 +60,57 @@ public protocol ParticipantDelegate: AnyObject {
5360
///
5461
/// `participant` Can be a ``LocalParticipant`` or a ``RemoteParticipant``.
5562
@objc(participant:publication:didUpdateMuted:) optional
56-
func participant(_ participant: Participant, didUpdate publication: TrackPublication, muted: Bool)
63+
func participant(_ participant: Participant, didUpdatePublication publication: TrackPublication, isMuted: Bool)
5764

58-
@objc(participant:didUpdatePermissions:) optional
59-
func participant(_ participant: Participant, didUpdate permissions: ParticipantPermissions)
65+
// MARK: - LocalTrackPublication
6066

61-
/// ``RemoteTrackPublication/streamState`` has updated for the ``RemoteParticipant``.
62-
@objc(participant:publication:didUpdateStreamState:) optional
63-
func participant(_ participant: RemoteParticipant, didUpdate publication: RemoteTrackPublication, streamState: StreamState)
67+
/// The ``LocalParticipant`` has published a ``LocalTrackPublication``.
68+
@objc(localParticipant:didPublish:) optional
69+
func localParticipant(_ participant: LocalParticipant, didPublishPublication publication: LocalTrackPublication)
6470

65-
/// ``RemoteTrackPublication/subscriptionAllowed`` has updated for the ``RemoteParticipant``.
66-
@objc(participant:publication:didUpdateCanSubscribe:) optional
67-
func participant(_ participant: RemoteParticipant, didUpdate publication: RemoteTrackPublication, permission allowed: Bool)
71+
/// The ``LocalParticipant`` has unpublished a ``LocalTrackPublication``.
72+
@objc(localParticipant:didUnpublish:) optional
73+
func localParticipant(_ participant: LocalParticipant, didUnpublishPublication publication: LocalTrackPublication)
74+
75+
// MARK: - RemoteTrackPublication
6876

6977
/// When a new ``RemoteTrackPublication`` is published to ``Room`` after the ``LocalParticipant`` has joined.
7078
///
7179
/// This delegate method will not be called for tracks that are already published.
7280
@objc(remoteParticipant:didPublish:) optional
73-
func participant(_ participant: RemoteParticipant, didPublish publication: RemoteTrackPublication)
81+
func participant(_ participant: RemoteParticipant, didPublishPublication publication: RemoteTrackPublication)
7482

7583
/// The ``RemoteParticipant`` has unpublished a ``RemoteTrackPublication``.
7684
@objc(remoteParticipant:didUnpublish:) optional
77-
func participant(_ participant: RemoteParticipant, didUnpublish publication: RemoteTrackPublication)
78-
79-
/// The ``LocalParticipant`` has published a ``LocalTrackPublication``.
80-
@objc(localParticipant:didPublish:) optional
81-
func localParticipant(_ participant: LocalParticipant, didPublish publication: LocalTrackPublication)
82-
83-
/// The ``LocalParticipant`` has unpublished a ``LocalTrackPublication``.
84-
@objc(localParticipant:didUnpublish:) optional
85-
func localParticipant(_ participant: LocalParticipant, didUnpublish publication: LocalTrackPublication)
85+
func participant(_ participant: RemoteParticipant, didUnpublishPublication publication: RemoteTrackPublication)
8686

8787
/// The ``LocalParticipant`` has subscribed to a new ``RemoteTrackPublication``.
8888
///
8989
/// This event will always fire as long as new tracks are ready for use.
90-
@objc(participant:didSubscribe:track:) optional
91-
func participant(_ participant: RemoteParticipant, didSubscribe publication: RemoteTrackPublication, track: Track)
90+
@objc(participant:didSubscribe:) optional
91+
func participant(_ participant: RemoteParticipant, didSubscribePublication publication: RemoteTrackPublication)
92+
93+
/// Unsubscribed from a ``RemoteTrackPublication`` and is no longer available.
94+
///
95+
/// Clients should listen to this event and handle cleanup.
96+
@objc(participant:didUnsubscribePublication:) optional
97+
func participant(_ participant: RemoteParticipant, didUnsubscribePublication publication: RemoteTrackPublication)
9298

9399
/// Could not subscribe to a track.
94100
///
95101
/// This is an error state, the subscription can be retried.
96102
@objc(participant:didFailToSubscribeTrackWithSid:error:) optional
97-
func participant(_ participant: RemoteParticipant, didFailToSubscribe trackSid: String, error: Error)
103+
func participant(_ participant: RemoteParticipant, didFailToSubscribe trackSid: String, error: LiveKitError)
98104

99-
/// Unsubscribed from a ``RemoteTrackPublication`` and is no longer available.
100-
///
101-
/// Clients should listen to this event and handle cleanup.
102-
@objc(participant:didUnsubscribePublication:track:) optional
103-
func participant(_ participant: RemoteParticipant, didUnsubscribe publication: RemoteTrackPublication, track: Track)
105+
/// ``TrackPublication/streamState`` has updated for the ``RemoteTrackPublication``.
106+
@objc(participant:publication:didUpdateStreamState:) optional
107+
func participant(_ participant: RemoteParticipant, didUpdatePublication publication: RemoteTrackPublication, streamState: StreamState)
108+
109+
/// ``RemoteTrackPublication/subscriptionAllowed`` has updated for the ``RemoteTrackPublication``.
110+
@objc(participant:publication:didUpdateCanSubscribe:) optional
111+
func participant(_ participant: RemoteParticipant, didUpdatePublication publication: RemoteTrackPublication, isSubscriptionAllowed: Bool)
112+
113+
// MARK: - Data
104114

105115
/// Data was received from a ``RemoteParticipant``.
106116
@objc(participant:didReceiveData:topic:) optional

0 commit comments

Comments
 (0)