Skip to content

Commit 48bca4f

Browse files
committed
Simplify connection methods
1 parent 723bcbe commit 48bca4f

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

Sources/PowerSync/Kotlin/KotlinPowerSyncDatabaseImpl.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,20 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
5252
)
5353

5454
let resolvedOptions = options ?? ConnectOptions()
55+
let useWebsockets = switch (resolvedOptions.connectionMethod) {
56+
case .http: false
57+
case .webSocket: true
58+
}
5559

5660
try await kotlinDatabase.connect(
5761
connector: connectorAdapter,
5862
crudThrottleMs: Int64(resolvedOptions.crudThrottle * 1000),
5963
retryDelayMs: Int64(resolvedOptions.retryDelay * 1000),
60-
params: resolvedOptions.params.mapValues { $0.toKotlinMap() }
64+
params: resolvedOptions.params.mapValues { $0.toKotlinMap() },
65+
options: SyncOptions(
66+
newClientImplementation: resolvedOptions.newClientImplementation,
67+
method: createConnectionMethod(webSocket: useWebsockets),
68+
)
6169
)
6270
}
6371

Sources/PowerSync/Protocol/PowerSyncDatabaseProtocol.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,22 @@ public struct ConnectOptions {
3434
/// ]
3535
/// ```
3636
public var params: JsonParam
37+
38+
/// Uses a new sync client implemented in Rust instead of the one implemented in Kotlin.
39+
///
40+
/// The new client is more efficient and will become the default in the future, but is still marked as experimental for now.
41+
/// We encourage interested users to try the new client.
42+
@_spi(PowerSyncExperimental)
43+
public var newClientImplementation: Bool
3744

45+
/// The connection method used to connect to the Powersync service.
46+
///
47+
/// The default method is ``ConnectionMethod/http``. Using ``ConnectionMethod/webSocket(_:)`` can
48+
/// improve performance as a more efficient binary protocol is used. However, using the websocket connection method
49+
/// requires enabling ``ConnectOptions/newClientImplementation``.
50+
@_spi(PowerSyncExperimental)
51+
public var connectionMethod: ConnectionMethod
52+
3853
/// Initializes a `ConnectOptions` instance with optional values.
3954
///
4055
/// - Parameters:
@@ -49,9 +64,32 @@ public struct ConnectOptions {
4964
self.crudThrottle = crudThrottle
5065
self.retryDelay = retryDelay
5166
self.params = params
67+
self.newClientImplementation = false
68+
self.connectionMethod = .http
69+
}
70+
71+
/// Initializes a ``ConnectOptions`` instance with optional values, including experimental options.
72+
@_spi(PowerSyncExperimental)
73+
public init(
74+
crudThrottle: TimeInterval = 1,
75+
retryDelay: TimeInterval = 5,
76+
params: JsonParam = [:],
77+
newClientImplementation: Bool = false,
78+
connectionMethod: ConnectionMethod = .http,
79+
) {
80+
self.crudThrottle = crudThrottle
81+
self.retryDelay = retryDelay
82+
self.params = params
83+
self.newClientImplementation = newClientImplementation
84+
self.connectionMethod = connectionMethod
5285
}
5386
}
5487

88+
@_spi(PowerSyncExperimental)
89+
public enum ConnectionMethod {
90+
case http
91+
case webSocket
92+
}
5593

5694
/// A PowerSync managed database.
5795
///

0 commit comments

Comments
 (0)