@@ -22,21 +22,25 @@ class ConnectionRepository: @unchecked Sendable {
2222 set { connectionQueue. async ( flags: . barrier) { self . _connectionId = newValue } }
2323 }
2424
25+ let webSocketConnectEndpoint = AllocatedUnfairLock < Endpoint < EmptyResponse > ? > ( nil )
2526 let isClientInActiveMode : Bool
2627 private let syncRepository : SyncRepository
28+ private let webSocketEncoder : RequestEncoder ?
2729 private let webSocketClient : WebSocketClient ?
2830 private let apiClient : APIClient
2931 private let timerType : TimerScheduling . Type
3032
3133 init (
3234 isClientInActiveMode: Bool ,
3335 syncRepository: SyncRepository ,
36+ webSocketEncoder: RequestEncoder ? ,
3437 webSocketClient: WebSocketClient ? ,
3538 apiClient: APIClient ,
3639 timerType: TimerScheduling . Type
3740 ) {
3841 self . isClientInActiveMode = isClientInActiveMode
3942 self . syncRepository = syncRepository
43+ self . webSocketEncoder = webSocketEncoder
4044 self . webSocketClient = webSocketClient
4145 self . apiClient = apiClient
4246 self . timerType = timerType
@@ -80,6 +84,7 @@ class ConnectionRepository: @unchecked Sendable {
8084 }
8185 }
8286 }
87+ updateWebSocketConnectURLRequest ( )
8388 webSocketClient? . connect ( )
8489 }
8590
@@ -114,29 +119,52 @@ class ConnectionRepository: @unchecked Sendable {
114119
115120 /// Updates the WebSocket endpoint to use the passed token and user information for the connection
116121 func updateWebSocketEndpoint( with token: Token , userInfo: UserInfo ? ) {
117- webSocketClient ? . connectEndpoint = . webSocketConnect( userInfo: userInfo ?? . init( id: token. userId) )
122+ webSocketConnectEndpoint . value = . webSocketConnect( userInfo: userInfo ?? . init( id: token. userId) )
118123 }
119-
124+
120125 /// Updates the WebSocket endpoint to use the passed user id
121126 func updateWebSocketEndpoint( with currentUserId: UserId ) {
122- webSocketClient? . connectEndpoint = . webSocketConnect( userInfo: UserInfo ( id: currentUserId) )
127+ webSocketConnectEndpoint. value = . webSocketConnect( userInfo: UserInfo ( id: currentUserId) )
128+ }
129+
130+ private func updateWebSocketConnectURLRequest( ) {
131+ guard let webSocketClient, let webSocketEncoder, let webSocketConnectEndpoint = webSocketConnectEndpoint. value else { return }
132+ let request : URLRequest ? = {
133+ do {
134+ return try webSocketEncoder. encodeRequest ( for: webSocketConnectEndpoint)
135+ } catch {
136+ log. error ( error. localizedDescription, error: error)
137+ return nil
138+ }
139+ } ( )
140+ guard let request else { return }
141+ webSocketClient. connectRequest = request
123142 }
124143
125144 func handleConnectionUpdate(
126145 state: WebSocketConnectionState ,
127146 onExpiredToken: ( ) -> Void
128147 ) {
148+ let event = ConnectionStatusUpdated ( webSocketConnectionState: state)
149+ if event. connectionStatus != connectionStatus {
150+ // Publish Connection event with the new state
151+ webSocketClient? . publishEvent ( event)
152+ }
153+
129154 connectionStatus = . init( webSocketConnectionState: state)
130155
131156 // We should notify waiters if connectionId was obtained (i.e. state is .connected)
132157 // or for .disconnected state except for disconnect caused by an expired token
133158 let shouldNotifyConnectionIdWaiters : Bool
134159 let connectionId : String ?
135160 switch state {
136- case let . connected( connectionId : id ) :
161+ case let . connected( healthCheckInfo : healthCheckInfo ) :
137162 shouldNotifyConnectionIdWaiters = true
138- connectionId = id
139- case let . disconnected( source) where source. serverError? . isExpiredTokenError == true :
163+ connectionId = healthCheckInfo. connectionId
164+ syncRepository. syncLocalState {
165+ log. info ( " Local state sync completed " , subsystems: . offlineSupport)
166+ }
167+ case let . disconnected( source) where source. serverError? . isTokenExpiredError == true :
140168 onExpiredToken ( )
141169 shouldNotifyConnectionIdWaiters = false
142170 connectionId = nil
@@ -146,7 +174,7 @@ class ConnectionRepository: @unchecked Sendable {
146174 case . initialized,
147175 . connecting,
148176 . disconnecting,
149- . waitingForConnectionId :
177+ . authenticating :
150178 shouldNotifyConnectionIdWaiters = false
151179 connectionId = nil
152180 }
0 commit comments