@@ -61,36 +61,45 @@ public final class Conversation: Sendable {
61
61
self . init ( client: RealtimeAPI ( connectingTo: request) )
62
62
}
63
63
64
- @MainActor public func whenConnected< E> ( _ callback: @Sendable ( ) async throws ( E) -> Void ) async throws ( E) {
64
+ /// Wait for the connection to be established
65
+ @MainActor public func waitForConnection( ) async {
65
66
while true {
66
67
if connected {
67
- return try await callback ( )
68
+ return
68
69
}
69
70
70
71
try ? await Task . sleep ( for: . milliseconds( 500 ) )
71
72
}
72
73
}
73
74
75
+ /// Execute a block of code when the connection is established
76
+ @MainActor public func whenConnected< E> ( _ callback: @Sendable ( ) async throws ( E) -> Void ) async throws ( E) {
77
+ await waitForConnection ( )
78
+ try await callback ( )
79
+ }
80
+
74
81
/// Make changes to the current session
75
- /// Note that this will fail if the session hasn't started yet.
82
+ /// Note that this will fail if the session hasn't started yet. Use `whenConnected` to ensure the session is ready.
76
83
public func updateSession( withChanges callback: ( inout Session ) -> Void ) async throws {
77
84
guard var session = await session else {
78
85
throw ConversationError . sessionNotFound
79
86
}
80
87
81
88
callback ( & session)
82
89
83
- try await updateSession ( session)
90
+ try await setSession ( session)
84
91
}
85
92
86
- public func updateSession( _ session: Session ) async throws {
93
+ /// Set the configuration of the current session
94
+ public func setSession( _ session: Session ) async throws {
87
95
// update endpoint errors if we include the session id
88
96
var session = session
89
97
session. id = nil
90
98
91
99
try await client. send ( event: . updateSession( session) )
92
100
}
93
101
102
+ /// Send a client event to the server
94
103
public func send( event: ClientEvent ) async throws {
95
104
try await client. send ( event: event)
96
105
}
0 commit comments