Skip to content

Commit a2654a0

Browse files
committed
State machine improvements
1 parent 095ffe7 commit a2654a0

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

Sources/LiveKit/Agent/Agent.swift

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,40 @@ public struct Agent: Loggable {
4343

4444
// MARK: - Transitions
4545

46+
mutating func disconnected() {
47+
log("Agent disconnected from \(state)", .debug)
48+
// From any state
49+
state = .disconnected
50+
}
51+
52+
mutating func failed(_ error: Error) {
53+
log("Agent failed with error \(error) from \(state)")
54+
// From any state
55+
state = .failed(error)
56+
}
57+
4658
mutating func connecting() {
59+
log("Agent connecting from \(state)")
4760
switch state {
48-
case .disconnected:
61+
case .disconnected, .connecting, .connected: // pre-connect is listening (connected)
4962
state = .connecting
5063
default:
5164
log("Invalid transition from \(state) to connecting", .warning)
5265
}
5366
}
5467

5568
mutating func listening() {
69+
log("Agent listening from \(state)")
5670
switch state {
57-
case .disconnected, .connecting:
71+
case .disconnected:
5872
state = .connected(agentState: .listening, audioTrack: nil, avatarVideoTrack: nil)
5973
default:
6074
log("Invalid transition from \(state) to listening", .warning)
6175
}
6276
}
6377

6478
mutating func connected(participant: Participant) {
79+
log("Agent connected to \(participant) from \(state)")
6580
switch state {
6681
case .connecting, .connected:
6782
state = .connected(agentState: participant.agentState,
@@ -72,15 +87,6 @@ public struct Agent: Loggable {
7287
}
7388
}
7489

75-
mutating func failed(_ error: Error) {
76-
switch state {
77-
case .disconnected, .connecting, .connected:
78-
state = .failed(error)
79-
default:
80-
log("Invalid transition from \(state) to failed", .warning)
81-
}
82-
}
83-
8490
// MARK: - Public
8591

8692
public var isConnected: Bool {

Sources/LiveKit/Agent/Session.swift

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,25 +143,21 @@ open class Session: ObservableObject {
143143
Task { [weak self] in
144144
for try await _ in room.changes {
145145
guard let self else { return }
146-
147-
connectionState = room.connectionState
148-
agent = updatedAgent(in: room)
146+
updateAgent(in: room)
149147
}
150148
}
151149
}
152150

153-
private func updatedAgent(in room: Room) -> Agent {
154-
var agent = Agent()
155-
156-
if connectionState != .disconnected {
157-
agent.connecting()
158-
}
151+
private func updateAgent(in room: Room) {
152+
connectionState = room.connectionState
159153

160-
if let firstAgent = room.agentParticipants.values.first {
154+
if connectionState == .disconnected {
155+
agent.disconnected()
156+
} else if let firstAgent = room.agentParticipants.values.first {
161157
agent.connected(participant: firstAgent)
158+
} else {
159+
agent.connecting()
162160
}
163-
164-
return agent
165161
}
166162

167163
private func observe(receivers: [any MessageReceiver]) {

Sources/LiveKit/Support/ObservableObject+.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
@preconcurrency import Combine
1818

1919
extension ObservableObject {
20-
/// An async sequence that emits the `objectWillChange` events.
20+
/// An async stream that emits the `objectWillChange` events.
2121
var changes: AsyncStream<Void> {
2222
AsyncStream { continuation in
2323
let cancellable = objectWillChange.sink { _ in

0 commit comments

Comments
 (0)