Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async room id #296

Merged
merged 6 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions Sources/LiveKit/Core/Room.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,17 @@ public class Room: NSObject, ObservableObject, Loggable {

@objc
/// Server assigned id of the Room.
/// This will be empty until ``RoomDelegate/room(_:didUpdateRoomId:)`` is called.
public var sid: Sid? { _state.sid }

// Work-around error: Property with 'throws' or 'async' is not representable in Objective-C
/// Server assigned id of the Room. **async** version of ``Room/sid``.
/// Example: `let sid = try await room.asyncSid`
public var asyncSid: Sid {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how would one use this value? await room.asyncSid() ? should we state usage in the comment?

Copy link
Member Author

@hiroshihorie hiroshihorie Jan 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Xcode will show it's an await-able property. But I added doc.

get async throws {
try await _sidCompleter.wait()
}
}

@objc
public var name: String? { _state.name }

Expand Down Expand Up @@ -132,6 +140,8 @@ public class Room: NSObject, ObservableObject, Loggable {

var _state: StateSync<State>

private let _sidCompleter = AsyncCompleter<Sid>(label: "sid", timeOut: .sid)

// MARK: Objective-C Support

@objc
Expand Down Expand Up @@ -172,11 +182,10 @@ public class Room: NSObject, ObservableObject, Loggable {

guard let self else { return }

// roomId updated
if let roomId = newState.sid, roomId != oldState.sid {
self.delegates.notify(label: { "room.didUpdateRoomId:" }) {
$0.room?(self, didUpdateRoomId: roomId)
}
// sid updated
if let sid = newState.sid, sid != oldState.sid {
// Resolve sid
self._sidCompleter.resume(returning: sid)
}

// metadata updated
Expand Down Expand Up @@ -299,6 +308,9 @@ extension Room {
await cleanUpParticipants()
// Reset state
_state.mutate { $0 = State(options: $0.options) }

// Reset completers
_sidCompleter.reset()
}
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/LiveKit/Extensions/TimeInterval.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ public extension DispatchTimeInterval {
static let defaultTransportState: Self = .seconds(10)
// used for validation mode
static let defaultPublisherDataChannelOpen: Self = .seconds(7)

static let sid: Self = .seconds(7 + 5) // Join response + 5
}
4 changes: 0 additions & 4 deletions Sources/LiveKit/Protocols/RoomDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ public protocol RoomDelegate: AnyObject {

// MARK: - Room State Updates

/// ``Room/sid`` has updated.
@objc optional
func room(_ room: Room, didUpdateRoomId roomId: String)

/// ``Room/metadata`` has updated.
@objc optional
func room(_ room: Room, didUpdateMetadata metadata: String?)
Expand Down