Skip to content

Commit

Permalink
individual connection state delegates
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshihorie committed Jan 3, 2024
1 parent 0a3e95f commit 3959ff6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
16 changes: 12 additions & 4 deletions Sources/LiveKit/Core/Room+EngineDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 LiveKit
* Copyright 2024 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -43,11 +43,19 @@ extension Room: EngineDelegate {
$0.room?(self, didUpdateConnectionState: state.connectionState, oldConnectionState: oldState.connectionState)
}

// Legacy connection delegates
// Individual connectionState delegates
if case .connected = state.connectionState {
let didReconnect = oldState.connectionState == .reconnecting
delegates.notify { $0.room?(self, didConnect: didReconnect) }
// Connected
if case .reconnecting = oldState.connectionState {
delegates.notify { $0.roomDidReconnect?(self) }
} else {
delegates.notify { $0.roomDidConnect?(self) }
}
} else if case .reconnecting = state.connectionState {
// Re-connecting
delegates.notify { $0.roomIsReconnecting?(self) }
} else if case .disconnected = state.connectionState {
// Disconnected
if case .connecting = oldState.connectionState {
delegates.notify { $0.room?(self, didFailToConnectWithError: oldState.disconnectError) }
} else {
Expand Down
18 changes: 13 additions & 5 deletions Sources/LiveKit/Protocols/RoomDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 LiveKit
* Copyright 2024 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,14 +39,22 @@ public protocol RoomDelegate: AnyObject {
func room(_ room: Room, didUpdateConnectionState connectionState: ConnectionState, oldConnectionState: ConnectionState)

/// Successfully connected to the room.
@objc(room:didConnectIsReconnect:) optional
func room(_ room: Room, didConnect isReconnect: Bool)
@objc(roomDidConnect:) optional
func roomDidConnect(_ room: Room)

/// Could not connect to the room.
/// Successfully re-connected to the room.
@objc(roomIsReconnecting:) optional
func roomIsReconnecting(_ room: Room)

/// Successfully re-connected to the room.
@objc(roomDidReconnect:) optional
func roomDidReconnect(_ room: Room)

/// Could not connect to the room. Only triggered when the initial connect attempt fails.
@objc(room:didFailToConnectWithError:) optional
func room(_ room: Room, didFailToConnectWithError error: LiveKitError?)

/// Client disconnected from the room unexpectedly.
/// Client disconnected from the room unexpectedly after a successful connection.
@objc(room:didDisconnectWithError:) optional
func room(_ room: Room, didDisconnectWithError error: LiveKitError?)

Expand Down

0 comments on commit 3959ff6

Please sign in to comment.