Skip to content

Commit bc371e5

Browse files
committed
Migrate remaining types to StreamCore and adopt its web-socket client
1 parent f992a3b commit bc371e5

File tree

72 files changed

+246
-4151
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+246
-4151
lines changed

Sources/StreamChat/ChatClient+Environment.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Foundation
66

77
extension ChatClient {
88
/// An object containing all dependencies of `Client`
9-
struct Environment: Sendable {
9+
struct Environment: @unchecked Sendable {
1010
var apiClientBuilder: @Sendable (
1111
_ sessionConfiguration: URLSessionConfiguration,
1212
_ requestEncoder: RequestEncoder,
@@ -25,15 +25,15 @@ extension ChatClient {
2525

2626
var webSocketClientBuilder: (@Sendable (
2727
_ sessionConfiguration: URLSessionConfiguration,
28-
_ requestEncoder: RequestEncoder,
2928
_ eventDecoder: AnyEventDecoder,
3029
_ notificationCenter: EventNotificationCenter
3130
) -> WebSocketClient)? = {
32-
WebSocketClient(
31+
return WebSocketClient(
3332
sessionConfiguration: $0,
34-
requestEncoder: $1,
35-
eventDecoder: $2,
36-
eventNotificationCenter: $3
33+
eventDecoder: $1,
34+
eventNotificationCenter: $2,
35+
webSocketClientType: .coordinator,
36+
connectRequest: nil
3737
)
3838
}
3939

@@ -57,7 +57,7 @@ extension ChatClient {
5757

5858
var eventDecoderBuilder: @Sendable () -> EventDecoder = { EventDecoder() }
5959

60-
var notificationCenterBuilder: @Sendable (_ database: DatabaseContainer, _ manualEventHandler: ManualEventHandler?) -> EventNotificationCenter = { EventNotificationCenter(database: $0, manualEventHandler: $1) }
60+
var notificationCenterBuilder: @Sendable (_ database: DatabaseContainer, _ manualEventHandler: ManualEventHandler?) -> EventPersistentNotificationCenter = { EventPersistentNotificationCenter(database: $0, manualEventHandler: $1) }
6161

6262
var internetConnection: @Sendable (_ center: NotificationCenter, _ monitor: InternetConnectionMonitor) -> InternetConnection = {
6363
InternetConnection(notificationCenter: $0, monitor: $1)
@@ -76,16 +76,18 @@ extension ChatClient {
7676
var connectionRepositoryBuilder: @Sendable (
7777
_ isClientInActiveMode: Bool,
7878
_ syncRepository: SyncRepository,
79+
_ webSocketRequestEncoder: RequestEncoder?,
7980
_ webSocketClient: WebSocketClient?,
8081
_ apiClient: APIClient,
81-
_ timerType: Timer.Type
82+
_ timerType: TimerScheduling.Type
8283
) -> ConnectionRepository = {
8384
ConnectionRepository(
8485
isClientInActiveMode: $0,
8586
syncRepository: $1,
86-
webSocketClient: $2,
87-
apiClient: $3,
88-
timerType: $4
87+
webSocketRequestEncoder: $2,
88+
webSocketClient: $3,
89+
apiClient: $4,
90+
timerType: $5
8991
)
9092
}
9193

@@ -103,27 +105,25 @@ extension ChatClient {
103105
}
104106
}
105107

106-
var timerType: Timer.Type = DefaultTimer.self
108+
var timerType: TimerScheduling.Type = DefaultTimer.self
107109

108110
var tokenExpirationRetryStrategy: RetryStrategy = DefaultRetryStrategy()
109111

110112
var connectionRecoveryHandlerBuilder: @Sendable (
111113
_ webSocketClient: WebSocketClient,
112114
_ eventNotificationCenter: EventNotificationCenter,
113-
_ syncRepository: SyncRepository,
114115
_ backgroundTaskScheduler: BackgroundTaskScheduler?,
115116
_ internetConnection: InternetConnection,
116117
_ keepConnectionAliveInBackground: Bool
117118
) -> ConnectionRecoveryHandler = {
118119
DefaultConnectionRecoveryHandler(
119120
webSocketClient: $0,
120121
eventNotificationCenter: $1,
121-
syncRepository: $2,
122-
backgroundTaskScheduler: $3,
123-
internetConnection: $4,
122+
backgroundTaskScheduler: $2,
123+
internetConnection: $3,
124124
reconnectionStrategy: DefaultRetryStrategy(),
125-
reconnectionTimerType: DefaultTimer.self,
126-
keepConnectionAliveInBackground: $5
125+
reconnectionTimerType: StreamCore.DefaultTimer.self,
126+
keepConnectionAliveInBackground: $4
127127
)
128128
}
129129

@@ -132,7 +132,7 @@ extension ChatClient {
132132
_ databaseContainer: DatabaseContainer,
133133
_ connectionRepository: ConnectionRepository,
134134
_ tokenExpirationRetryStrategy: RetryStrategy,
135-
_ timerType: Timer.Type
135+
_ timerType: TimerScheduling.Type
136136
) -> AuthenticationRepository = {
137137
AuthenticationRepository(
138138
apiClient: $0,

Sources/StreamChat/ChatClient.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class ChatClient: @unchecked Sendable {
5252
private(set) var connectionRecoveryHandler: ConnectionRecoveryHandler?
5353

5454
/// The notification center used to send and receive notifications about incoming events.
55-
private(set) var eventNotificationCenter: EventNotificationCenter
55+
private(set) var eventNotificationCenter: EventPersistentNotificationCenter
5656

5757
/// The registry that contains all the attachment payloads associated with their attachment types.
5858
/// For the meantime this is a static property to avoid breaking changes. On v5, this can be changed.
@@ -99,6 +99,7 @@ public class ChatClient: @unchecked Sendable {
9999

100100
/// The `WebSocketClient` instance `Client` uses to communicate with Stream WS servers.
101101
let webSocketClient: WebSocketClient?
102+
let webSocketRequestEncoder: RequestEncoder?
102103

103104
/// The `DatabaseContainer` instance `Client` uses to store and cache data.
104105
let databaseContainer: DatabaseContainer
@@ -184,13 +185,13 @@ public class ChatClient: @unchecked Sendable {
184185
channelListUpdater
185186
)
186187
let webSocketClient = factory.makeWebSocketClient(
187-
requestEncoder: webSocketEncoder,
188188
urlSessionConfiguration: urlSessionConfiguration,
189189
eventNotificationCenter: eventNotificationCenter
190190
)
191191
let connectionRepository = environment.connectionRepositoryBuilder(
192192
config.isClientInActiveMode,
193193
syncRepository,
194+
webSocketEncoder,
194195
webSocketClient,
195196
apiClient,
196197
environment.timerType
@@ -207,6 +208,7 @@ public class ChatClient: @unchecked Sendable {
207208
self.databaseContainer = databaseContainer
208209
self.apiClient = apiClient
209210
self.webSocketClient = webSocketClient
211+
self.webSocketRequestEncoder = webSocketEncoder
210212
self.eventNotificationCenter = eventNotificationCenter
211213
self.offlineRequestsRepository = offlineRequestsRepository
212214
self.connectionRepository = connectionRepository
@@ -268,7 +270,6 @@ public class ChatClient: @unchecked Sendable {
268270
connectionRecoveryHandler = environment.connectionRecoveryHandlerBuilder(
269271
webSocketClient,
270272
eventNotificationCenter,
271-
syncRepository,
272273
environment.backgroundTaskSchedulerBuilder(),
273274
environment.internetConnection(eventNotificationCenter, environment.internetMonitor),
274275
config.staysConnectedInBackground
@@ -718,7 +719,7 @@ extension ChatClient: AuthenticationRepositoryDelegate {
718719
}
719720

720721
extension ChatClient: ConnectionStateDelegate {
721-
func webSocketClient(_ client: WebSocketClient, didUpdateConnectionState state: WebSocketConnectionState) {
722+
public func webSocketClient(_ client: WebSocketClient, didUpdateConnectionState state: WebSocketConnectionState) {
722723
connectionRepository.handleConnectionUpdate(
723724
state: state,
724725
onExpiredToken: { [weak self] in
@@ -735,6 +736,9 @@ extension ChatClient: ConnectionStateDelegate {
735736
}
736737
case .connected:
737738
reconnectionTimeoutHandler?.stop()
739+
syncRepository.syncLocalState {
740+
log.info("Local state sync completed", subsystems: .offlineSupport)
741+
}
738742
default:
739743
break
740744
}

Sources/StreamChat/ChatClientFactory.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,11 @@ class ChatClientFactory {
6565
}
6666

6767
func makeWebSocketClient(
68-
requestEncoder: RequestEncoder,
6968
urlSessionConfiguration: URLSessionConfiguration,
7069
eventNotificationCenter: EventNotificationCenter
7170
) -> WebSocketClient? {
7271
environment.webSocketClientBuilder?(
7372
urlSessionConfiguration,
74-
requestEncoder,
7573
EventDecoder(),
7674
eventNotificationCenter
7775
)
@@ -114,7 +112,7 @@ class ChatClientFactory {
114112
func makeEventNotificationCenter(
115113
databaseContainer: DatabaseContainer,
116114
currentUserId: @escaping () -> UserId?
117-
) -> EventNotificationCenter {
115+
) -> EventPersistentNotificationCenter {
118116
let center = environment.notificationCenterBuilder(databaseContainer, nil)
119117
let middlewares: [EventMiddleware] = [
120118
EventDataProcessorMiddleware(),

Sources/StreamChat/Controllers/PollController/PollVoteListController.swift

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -163,22 +163,26 @@ public class PollVoteListController: DataController, DelegateCallable, DataStore
163163
super.init()
164164
eventsObserver = client.subscribe { [weak self] event in
165165
guard let self else { return }
166-
var vote: PollVote?
167-
if let event = event as? PollVoteCastedEvent {
168-
vote = event.vote
169-
} else if let event = event as? PollVoteChangedEvent {
170-
vote = event.vote
171-
}
172-
guard let vote else { return }
173-
if vote.isAnswer == true
174-
&& query.pollId == vote.pollId
175-
&& query.optionId == nil {
176-
pollsRepository.link(pollVote: vote, to: query)
177-
} else if vote.isAnswer == false
178-
&& query.pollId == vote.pollId
179-
&& query.optionId == vote.optionId {
180-
pollsRepository.link(pollVote: vote, to: query)
181-
}
166+
self.didReceiveEvent(event)
167+
}
168+
}
169+
170+
func didReceiveEvent(_ event: Event) {
171+
var vote: PollVote?
172+
if let event = event as? PollVoteCastedEvent {
173+
vote = event.vote
174+
} else if let event = event as? PollVoteChangedEvent {
175+
vote = event.vote
176+
}
177+
guard let vote else { return }
178+
if vote.isAnswer == true
179+
&& query.pollId == vote.pollId
180+
&& query.optionId == nil {
181+
pollsRepository.link(pollVote: vote, to: query)
182+
} else if vote.isAnswer == false
183+
&& query.pollId == vote.pollId
184+
&& query.optionId == vote.optionId {
185+
pollsRepository.link(pollVote: vote, to: query)
182186
}
183187
}
184188

Sources/StreamChat/Errors/ClientError.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ extension ClientError {
1010
(underlyingError as? ErrorPayload)?.isExpiredTokenError == true
1111
}
1212

13-
/// Returns `true` if underlaying error is `ErrorPayload` with code is inside invalid token codes range.
14-
var isInvalidTokenError: Bool {
15-
(underlyingError as? ErrorPayload)?.isInvalidTokenError == true
16-
}
17-
1813
/// The error payload if the underlying error comes from a server error.
1914
public var errorPayload: ErrorPayload? { underlyingError as? ErrorPayload }
2015
}

Sources/StreamChat/Errors/ErrorPayload.swift

Lines changed: 0 additions & 77 deletions
This file was deleted.

Sources/StreamChat/Repositories/AuthenticationRepository.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ class AuthenticationRepository: @unchecked Sendable {
9797
private let apiClient: APIClient
9898
private let databaseContainer: DatabaseContainer
9999
private let connectionRepository: ConnectionRepository
100-
private let timerType: Timer.Type
100+
private let timerType: TimerScheduling.Type
101101

102102
init(
103103
apiClient: APIClient,
104104
databaseContainer: DatabaseContainer,
105105
connectionRepository: ConnectionRepository,
106106
tokenExpirationRetryStrategy: RetryStrategy,
107-
timerType: Timer.Type
107+
timerType: TimerScheduling.Type
108108
) {
109109
self.apiClient = apiClient
110110
self.databaseContainer = databaseContainer

0 commit comments

Comments
 (0)