Skip to content

Commit 199f454

Browse files
authored
V5: Use concurrency and timers from StreamCore (#3858)
* V5: Use error types from StreamCore (#3859) * V5: Use JSON encoders and decoders from StreamCore (#3860) * V5: Use web-socket client from StreamCore (#3861)
1 parent 55c36d8 commit 199f454

File tree

90 files changed

+331
-5249
lines changed

Some content is hidden

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

90 files changed

+331
-5249
lines changed

DemoApp/StreamChat/Components/DemoChatThreadVC.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class DemoChatThreadVC: ChatThreadVC, CurrentChatUserControllerDelegate {
7272

7373
// MARK: - Dismissing the thread if the root message was hard deleted.
7474

75-
override func eventsController(_ controller: EventsController, didReceiveEvent event: any Event) {
75+
override func eventsController(_ controller: EventsController, didReceiveEvent event: any StreamChat.Event) {
7676
super.eventsController(controller, didReceiveEvent: event)
7777

7878
// Dismiss the thread if the root message was hard deleted.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ let package = Package(
2929
],
3030
dependencies: [
3131
.package(url: "https://github.com/apple/swift-docc-plugin", exact: "1.0.0"),
32-
.package(url: "https://github.com/GetStream/stream-core-swift.git", exact: "0.4.1")
32+
.package(url: "https://github.com/GetStream/stream-core-swift.git", exact: "0.5.0")
3333
],
3434
targets: [
3535
.target(

Sources/StreamChat/APIClient/RequestDecoder.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ struct DefaultRequestDecoder: RequestDecoder {
4646
log.debug("URL request response: \(httpResponse), data:\n\(data.debugPrettyPrintedJSON))", subsystems: .httpRequests)
4747

4848
guard httpResponse.statusCode < 300 else {
49-
let serverError: ErrorPayload
49+
let serverError: APIError
5050
do {
51-
serverError = try JSONDecoder.default.decode(ErrorPayload.self, from: data)
51+
serverError = try JSONDecoder.default.decode(APIError.self, from: data)
5252
} catch {
5353
log
5454
.error(
@@ -58,7 +58,7 @@ struct DefaultRequestDecoder: RequestDecoder {
5858
throw ClientError.Unknown("Unknown error. Server response: \(httpResponse).")
5959
}
6060

61-
if serverError.isExpiredTokenError {
61+
if serverError.isTokenExpiredError {
6262
log.info("Request failed because of an expired token.", subsystems: .httpRequests)
6363
throw ClientError.ExpiredToken()
6464
}

Sources/StreamChat/APIClient/RequestEncoder.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,6 @@ protocol ConnectionDetailsProviderDelegate: AnyObject {
304304
}
305305

306306
public extension ClientError {
307-
final class InvalidURL: ClientError, @unchecked Sendable {}
308307
final class InvalidJSON: ClientError, @unchecked Sendable {}
309308
final class MissingConnectionId: ClientError, @unchecked Sendable {}
310309
}

Sources/StreamChat/ChatClient+Environment.swift

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@ extension ChatClient {
2525

2626
var webSocketClientBuilder: (@Sendable (
2727
_ sessionConfiguration: URLSessionConfiguration,
28-
_ requestEncoder: RequestEncoder,
2928
_ eventDecoder: AnyEventDecoder,
30-
_ notificationCenter: EventNotificationCenter
29+
_ notificationCenter: PersistentEventNotificationCenter
3130
) -> WebSocketClient)? = {
32-
WebSocketClient(
31+
let wsEnvironment = WebSocketClient.Environment(eventBatchingPeriod: 0.5)
32+
return WebSocketClient(
3333
sessionConfiguration: $0,
34-
requestEncoder: $1,
35-
eventDecoder: $2,
36-
eventNotificationCenter: $3
34+
eventDecoder: $1,
35+
eventNotificationCenter: $2,
36+
webSocketClientType: .coordinator,
37+
environment: wsEnvironment,
38+
connectRequest: nil,
39+
healthCheckBeforeConnected: true
3740
)
3841
}
3942

@@ -57,7 +60,7 @@ extension ChatClient {
5760

5861
var eventDecoderBuilder: @Sendable () -> EventDecoder = { EventDecoder() }
5962

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

6265
var internetConnection: @Sendable (_ center: NotificationCenter, _ monitor: InternetConnectionMonitor) -> InternetConnection = {
6366
InternetConnection(notificationCenter: $0, monitor: $1)
@@ -76,16 +79,18 @@ extension ChatClient {
7679
var connectionRepositoryBuilder: @Sendable (
7780
_ isClientInActiveMode: Bool,
7881
_ syncRepository: SyncRepository,
82+
_ webSocketEncoder: RequestEncoder?,
7983
_ webSocketClient: WebSocketClient?,
8084
_ apiClient: APIClient,
81-
_ timerType: Timer.Type
85+
_ timerType: TimerScheduling.Type
8286
) -> ConnectionRepository = {
8387
ConnectionRepository(
8488
isClientInActiveMode: $0,
8589
syncRepository: $1,
86-
webSocketClient: $2,
87-
apiClient: $3,
88-
timerType: $4
90+
webSocketEncoder: $2,
91+
webSocketClient: $3,
92+
apiClient: $4,
93+
timerType: $5
8994
)
9095
}
9196

@@ -103,27 +108,25 @@ extension ChatClient {
103108
}
104109
}
105110

106-
var timerType: Timer.Type = DefaultTimer.self
111+
var timerType: TimerScheduling.Type = DefaultTimer.self
107112

108113
var tokenExpirationRetryStrategy: RetryStrategy = DefaultRetryStrategy()
109114

110115
var connectionRecoveryHandlerBuilder: @Sendable (
111116
_ webSocketClient: WebSocketClient,
112117
_ eventNotificationCenter: EventNotificationCenter,
113-
_ syncRepository: SyncRepository,
114118
_ backgroundTaskScheduler: BackgroundTaskScheduler?,
115119
_ internetConnection: InternetConnection,
116120
_ keepConnectionAliveInBackground: Bool
117121
) -> ConnectionRecoveryHandler = {
118122
DefaultConnectionRecoveryHandler(
119123
webSocketClient: $0,
120124
eventNotificationCenter: $1,
121-
syncRepository: $2,
122-
backgroundTaskScheduler: $3,
123-
internetConnection: $4,
125+
backgroundTaskScheduler: $2,
126+
internetConnection: $3,
124127
reconnectionStrategy: DefaultRetryStrategy(),
125128
reconnectionTimerType: DefaultTimer.self,
126-
keepConnectionAliveInBackground: $5
129+
keepConnectionAliveInBackground: $4
127130
)
128131
}
129132

@@ -132,7 +135,7 @@ extension ChatClient {
132135
_ databaseContainer: DatabaseContainer,
133136
_ connectionRepository: ConnectionRepository,
134137
_ tokenExpirationRetryStrategy: RetryStrategy,
135-
_ timerType: Timer.Type
138+
_ timerType: TimerScheduling.Type
136139
) -> AuthenticationRepository = {
137140
AuthenticationRepository(
138141
apiClient: $0,

Sources/StreamChat/ChatClient.swift

Lines changed: 5 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: PersistentEventNotificationCenter
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 webSocketEncoder: 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.webSocketEncoder = 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

Sources/StreamChat/ChatClientFactory.swift

Lines changed: 2 additions & 4 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,
70-
eventNotificationCenter: EventNotificationCenter
69+
eventNotificationCenter: PersistentEventNotificationCenter
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+
) -> PersistentEventNotificationCenter {
118116
let center = environment.notificationCenterBuilder(databaseContainer, nil)
119117
let middlewares: [EventMiddleware] = [
120118
EventDataProcessorMiddleware(),

Sources/StreamChat/Config/ChatClientConfig.swift

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -227,21 +227,3 @@ extension ChatClientConfig {
227227
public var latestMessagesLimit = 5
228228
}
229229
}
230-
231-
/// A struct representing an API key of the chat app.
232-
///
233-
/// An API key can be obtained by registering on [our website](https://getstream.io/chat/trial/\).
234-
///
235-
public struct APIKey: Equatable, Sendable {
236-
/// The string representation of the API key
237-
public let apiKeyString: String
238-
239-
/// Creates a new `APIKey` from the provided string. Fails, if the string is empty.
240-
///
241-
/// - Warning: The `apiKeyString` must be a non-empty value, otherwise an assertion failure is raised.
242-
///
243-
public init(_ apiKeyString: String) {
244-
log.assert(apiKeyString.isEmpty == false, "APIKey can't be initialize with an empty string.")
245-
self.apiKeyString = apiKeyString
246-
}
247-
}

Sources/StreamChat/Config/Token.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@ public extension Token {
7272
}
7373
}
7474

75-
extension ClientError {
76-
public final class InvalidToken: ClientError, @unchecked Sendable {}
77-
}
78-
7975
private extension String {
8076
var jwtPayload: [String: Any]? {
8177
let parts = split(separator: ".")

Sources/StreamChat/Errors/ClientError.swift

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

0 commit comments

Comments
 (0)