Skip to content

Commit

Permalink
Async room id (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshihorie authored Jan 9, 2024
1 parent ffcec8f commit aed9406
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
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 {
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

0 comments on commit aed9406

Please sign in to comment.