Skip to content

Commit 3a075c5

Browse files
authored
Add option to use the Rust sync client (#51)
1 parent 723bcbe commit 3a075c5

File tree

4 files changed

+76
-4
lines changed

4 files changed

+76
-4
lines changed

CHANGELOG.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Changelog
22

3-
## 1.1.1 (unreleased)
3+
## 1.2.0
44

55
* Improved `CrudBatch` and `CrudTransaction` `complete` function extensions. Developers no longer need to specify `nil` as an argument for `writeCheckpoint` when calling `CrudBatch.complete`. The base `complete` functions still accept an optional `writeCheckpoint` argument if developers use custom write checkpoints.
66
``` diff
@@ -12,6 +12,31 @@ guard let finalBatch = try await powersync.getCrudBatch(limit: 100) else {
1212
```
1313
* Fix reported progress around compactions / defrags on the sync service.
1414
* Use version `0.4.0` of the PowerSync core extension, which improves sync performance.
15+
* Add a new sync client implementation written in Rust instead of Kotlin. While this client is still
16+
experimental, we intend to make it the default in the future. The main benefit of this client is
17+
faster sync performance, but upcoming features will also require this client. We encourage
18+
interested users to try it out by opting in to experimental APIs and passing options when
19+
connecting:
20+
```Swift
21+
@_spi(PowerSyncExperimental) import PowerSync
22+
23+
try await db.connect(connector: connector, options: ConnectOptions(
24+
newClientImplementation: true,
25+
))
26+
```
27+
Switching between the clients can be done at any time without compatibility issues. If you run
28+
into issues with the new client, please reach out to us!
29+
* In addition to HTTP streams, the Swift SDK also supports fetching sync instructions from the
30+
PowerSync service in a binary format. This requires the new sync client, and can then be enabled
31+
on the sync options:
32+
```Swift
33+
@_spi(PowerSyncExperimental) import PowerSync
34+
35+
try await db.connect(connector: connector, options: ConnectOptions(
36+
newClientImplementation: true,
37+
connectionMethod: .webSocket,
38+
))
39+
```
1540

1641
## 1.1.0
1742

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ if let kotlinSdkPath = localKotlinSdkOverride {
2727
// Not using a local build, so download from releases
2828
conditionalTargets.append(.binaryTarget(
2929
name: "PowerSyncKotlin",
30-
url: "https://github.com/powersync-ja/powersync-kotlin/releases/download/v1.1.1/PowersyncKotlinRelease.zip",
31-
checksum: "780437e25d41e46c2c1f555adcf330436f185d3663ef442da7141381d9c0495b"
30+
url: "https://github.com/powersync-ja/powersync-kotlin/releases/download/v1.2.0/PowersyncKotlinRelease.zip",
31+
checksum: "7454481a245b46b1b63a42419ec27f88f2fcb7fba9b763e3085cb49db59dfd58"
3232
))
3333
}
3434

Sources/PowerSync/Kotlin/KotlinPowerSyncDatabaseImpl.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,21 @@ 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: createSyncOptions(
66+
newClient: resolvedOptions.newClientImplementation,
67+
webSocket: useWebsockets,
68+
userAgent: "PowerSync Swift SDK"
69+
)
6170
)
6271
}
6372

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)