Skip to content

Commit a787606

Browse files
committed
Simplify pre-connect state
1 parent 160f8fb commit a787606

File tree

2 files changed

+10
-20
lines changed

2 files changed

+10
-20
lines changed

Sources/LiveKit/Agent/Agent.swift

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public struct Agent: Loggable {
4848

4949
private enum State {
5050
case disconnected
51-
case connecting
51+
case connecting(buffering: Bool)
5252
case connected(agentState: AgentState, audioTrack: (any AudioTrack)?, avatarVideoTrack: (any VideoTrack)?)
53-
case failed(Error)
53+
case failed(error: Error)
5454
}
5555

5656
private var state: State = .disconnected
@@ -63,32 +63,22 @@ public struct Agent: Loggable {
6363
state = .disconnected
6464
}
6565

66-
mutating func failed(_ error: Error) {
66+
mutating func failed(error: Error) {
6767
log("Agent failed with error \(error) from \(state)")
6868
// From any state
69-
state = .failed(error)
69+
state = .failed(error: error)
7070
}
7171

72-
mutating func connecting() {
72+
mutating func connecting(buffering: Bool) {
7373
log("Agent connecting from \(state)")
7474
switch state {
75-
case .disconnected, .connecting, .connected: // pre-connect is listening (connected)
76-
state = .connecting
75+
case .disconnected, .connecting:
76+
state = .connecting(buffering: buffering)
7777
default:
7878
log("Invalid transition from \(state) to connecting", .warning)
7979
}
8080
}
8181

82-
mutating func listening() {
83-
log("Agent listening from \(state)")
84-
switch state {
85-
case .disconnected:
86-
state = .connected(agentState: .listening, audioTrack: nil, avatarVideoTrack: nil)
87-
default:
88-
log("Invalid transition from \(state) to listening", .warning)
89-
}
90-
}
91-
9282
mutating func connected(participant: Participant) {
9383
log("Agent connected to \(participant) from \(state)")
9484
switch state {

Sources/LiveKit/Agent/Session.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ open class Session: ObservableObject {
202202
} else if let firstAgent = room.agentParticipants.values.first {
203203
agent.connected(participant: firstAgent)
204204
} else {
205-
agent.connecting()
205+
agent.connecting(buffering: options.preConnectAudio)
206206
}
207207
}
208208

@@ -234,7 +234,7 @@ open class Session: ObservableObject {
234234
try Task.checkCancellation()
235235
guard let self else { return }
236236
if isConnected, !agent.isConnected {
237-
self.agent.failed(.timeout)
237+
self.agent.failed(error: .timeout)
238238
}
239239
}
240240
}
@@ -250,7 +250,7 @@ open class Session: ObservableObject {
250250
try await room.withPreConnectAudio(timeout: timeout) {
251251
await MainActor.run {
252252
self.connectionState = .connecting
253-
self.agent.listening()
253+
self.agent.connecting(buffering: true)
254254
}
255255
try await connect()
256256
}

0 commit comments

Comments
 (0)