Skip to content

Commit 5777315

Browse files
committed
Hook up the WebSocket status API to the Swift layer
1 parent 6c9f763 commit 5777315

File tree

10 files changed

+1249
-867
lines changed

10 files changed

+1249
-867
lines changed

Sources/ConvexMobile/ConvexMobile.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ import Foundation
1515
/// Convex backend.
1616
public class ConvexClient {
1717
let ffiClient: UniFFI.MobileConvexClientProtocol
18+
fileprivate let webSocketStateAdapter = WebSocketStateAdapter()
1819

1920
/// Creates a new instance of ``ConvexClient``.
2021
///
2122
/// - Parameters:
2223
/// - deploymentUrl: The Convex backend URL to connect to; find it in the [dashboard](https://dashboard.convex.dev) Settings for your project
2324
public init(deploymentUrl: String) {
2425
self.ffiClient = UniFFI.MobileConvexClient(
25-
deploymentUrl: deploymentUrl, clientId: "swift-\(convexMobileVersion)")
26+
deploymentUrl: deploymentUrl, clientId: "swift-\(convexMobileVersion)", webSocketStateSubscriber: webSocketStateAdapter)
2627
}
2728

2829
init(ffiClient: UniFFI.MobileConvexClientProtocol) {
@@ -48,7 +49,7 @@ public class ConvexClient {
4849
// 2. Feed the subscription handle into the Convex data Publisher so it can cancel the upstream
4950
// subscription when downstream subscribers are done consuming data
5051

51-
// This Publisher will ultimated publish the data received from Convex.
52+
// This Publisher will ultimately publish the data received from Convex.
5253
let convexPublisher = PassthroughSubject<T, ClientError>()
5354
let adapter = SubscriptionAdapter<T>(publisher: convexPublisher)
5455

@@ -151,6 +152,10 @@ public class ConvexClient {
151152
}
152153

153154
typealias RemoteCall = (String, [String: String]) async throws -> String
155+
156+
public func watchWebSocketState() -> AnyPublisher<WebSocketState, Never> {
157+
return webSocketStateAdapter.newPublisher()
158+
}
154159
}
155160

156161
/// Authentication states that can be experienced when using an ``AuthProvider`` with
@@ -287,5 +292,18 @@ private class SubscriptionAdapter<T: Decodable>: QuerySubscriber {
287292
func onUpdate(value: String) {
288293
publisher.send(try! JSONDecoder().decode(Publisher.Output.self, from: Data(value.utf8)))
289294
}
295+
}
296+
297+
private class WebSocketStateAdapter: WebSocketStateSubscriber {
298+
private let subject = PassthroughSubject<WebSocketState, Never>()
299+
300+
init() { }
290301

302+
func onStateChange(state: UniFFI.WebSocketState) {
303+
subject.send(state)
304+
}
305+
306+
func newPublisher() -> AnyPublisher<WebSocketState, Never> {
307+
return subject.eraseToAnyPublisher()
308+
}
291309
}

0 commit comments

Comments
 (0)