Skip to content

Commit 8da17b5

Browse files
update time durations from ms to TimeInterval
1 parent 5700b23 commit 8da17b5

File tree

4 files changed

+62
-27
lines changed

4 files changed

+62
-27
lines changed

CHANGELOG.md

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

3-
# 1.0.0-Beta.14
3+
# 1.0.0
44

55
- Improved the stability of watched queries. Watched queries were previously susceptible to runtime crashes if an exception was thrown in the update stream. Errors are now gracefully handled.
66

@@ -86,6 +86,35 @@ try await database.connect(
8686
+ let time: Date? = db.currentStatus.lastSyncedAt
8787
```
8888

89+
- `crudThrottleMs` and `retryDelayMs` in the `connect` method have been updated to `crudThrottle` and `retryDelay` which are now of type `TimeInterval`. Previously the parameters were specified in milliseconds, the `TimeInterval` typing now requires values to be specified in seconds.
90+
91+
```diff
92+
try await database.connect(
93+
connector: PowerSyncBackendConnector(),
94+
- crudThrottleMs: 1000,
95+
- retryDelayMs: 5000,
96+
+ crudThrottle: 1,
97+
+ retryDelay: 5,
98+
params: [
99+
"foo": .string("bar"),
100+
]
101+
)
102+
```
103+
104+
- `throttleMs` in the watched query `WatchOptions` has been updated to `throttle` which is now of type `TimeInterval`. Previously the parameters were specified in milliseconds, the `TimeInterval` typing now requires values to be specified in seconds.
105+
106+
```diff
107+
let stream = try database.watch(
108+
options: WatchOptions(
109+
sql: "SELECT name FROM users ORDER BY id",
110+
- throttleMs: 1000,
111+
+ throttle: 1,
112+
mapper: { cursor in
113+
try cursor.getString(index: 0)
114+
}
115+
))
116+
```
117+
89118
# 1.0.0-Beta.13
90119

91120
- Update `powersync-kotlin` dependency to version `1.0.0-BETA32`, which includes:

Sources/PowerSync/Kotlin/KotlinPowerSyncDatabaseImpl.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
5555

5656
try await kotlinDatabase.connect(
5757
connector: connectorAdapter,
58-
crudThrottleMs: resolvedOptions.crudThrottleMs,
59-
retryDelayMs: resolvedOptions.retryDelayMs,
58+
crudThrottleMs: Int64(resolvedOptions.crudThrottle * 1000),
59+
retryDelayMs: Int64(resolvedOptions.retryDelay * 1000),
6060
params: resolvedOptions.params.mapValues { $0.toKotlinMap() }
6161
)
6262
}
@@ -230,7 +230,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
230230
// Watching for changes in the database
231231
for try await _ in try self.kotlinDatabase.onChange(
232232
tables: Set(watchedTables),
233-
throttleMs: options.throttleMs,
233+
throttleMs: Int64(options.throttle * 1000),
234234
triggerImmediately: true // Allows emitting the first result even if there aren't changes
235235
) {
236236
// Check if the outer task is cancelled

Sources/PowerSync/Protocol/PowerSyncDatabaseProtocol.swift

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@ import Foundation
44
///
55
/// Provides optional parameters to customize sync behavior such as throttling and retry policies.
66
public struct ConnectOptions {
7-
/// Time in milliseconds between CRUD (Create, Read, Update, Delete) operations.
7+
/// Defaults to 1 second
8+
public static let DefaultCrudThrottle: TimeInterval = 1
9+
10+
/// Defaults to 5 seconds
11+
public static let DefaultRetryDelay: TimeInterval = 5
12+
13+
/// TimeInterval (in seconds) between CRUD (Create, Read, Update, Delete) operations.
814
///
9-
/// Default is `1000` ms (1 second).
15+
/// Default is ``ConnectOptions/DefaultCrudThrottle``.
1016
/// Increase this value to reduce load on the backend server.
11-
public var crudThrottleMs: Int64
17+
public var crudThrottle: TimeInterval
1218

13-
/// Delay in milliseconds before retrying after a connection failure.
19+
/// Delay TimeInterval (in seconds) before retrying after a connection failure.
1420
///
15-
/// Default is `5000` ms (5 seconds).
21+
/// Default is ``ConnectOptions/DefaultRetryDelay``.
1622
/// Increase this value to wait longer before retrying connections in case of persistent failures.
17-
public var retryDelayMs: Int64
23+
public var retryDelay: TimeInterval
1824

1925
/// Additional sync parameters passed to the server during connection.
2026
///
@@ -32,16 +38,16 @@ public struct ConnectOptions {
3238
/// Initializes a `ConnectOptions` instance with optional values.
3339
///
3440
/// - Parameters:
35-
/// - crudThrottleMs: Time between CRUD operations in milliseconds. Defaults to `1000`.
36-
/// - retryDelayMs: Delay between retry attempts in milliseconds. Defaults to `5000`.
41+
/// - crudThrottle: TimeInterval between CRUD operations in milliseconds. Defaults to `1` second.
42+
/// - retryDelay: Delay TimeInterval between retry attempts in milliseconds. Defaults to `5` seconds.
3743
/// - params: Custom sync parameters to send to the server. Defaults to an empty dictionary.
3844
public init(
39-
crudThrottleMs: Int64 = 1000,
40-
retryDelayMs: Int64 = 5000,
45+
crudThrottle: TimeInterval = 1,
46+
retryDelay: TimeInterval = 5,
4147
params: JsonParam = [:]
4248
) {
43-
self.crudThrottleMs = crudThrottleMs
44-
self.retryDelayMs = retryDelayMs
49+
self.crudThrottle = crudThrottle
50+
self.retryDelay = retryDelay
4551
self.params = params
4652
}
4753
}
@@ -168,8 +174,8 @@ public extension PowerSyncDatabaseProtocol {
168174
///
169175
/// - Parameters:
170176
/// - connector: The PowerSyncBackendConnector to use
171-
/// - crudThrottleMs: Time between CRUD operations. Defaults to 1000ms.
172-
/// - retryDelayMs: Delay between retries after failure. Defaults to 5000ms.
177+
/// - crudThrottle: TimeInterval between CRUD operations. Defaults to ``ConnectOptions/DefaultCrudThrottle``.
178+
/// - retryDelay: Delay TimeInterval between retries after failure. Defaults to ``ConnectOptions/DefaultRetryDelay``.
173179
/// - params: Sync parameters from the client
174180
///
175181
/// Example usage:
@@ -188,15 +194,15 @@ public extension PowerSyncDatabaseProtocol {
188194
/// )
189195
func connect(
190196
connector: PowerSyncBackendConnector,
191-
crudThrottleMs: Int64 = 1000,
192-
retryDelayMs: Int64 = 5000,
197+
crudThrottle: TimeInterval = 1,
198+
retryDelay: TimeInterval = 5,
193199
params: JsonParam = [:]
194200
) async throws {
195201
try await connect(
196202
connector: connector,
197203
options: ConnectOptions(
198-
crudThrottleMs: crudThrottleMs,
199-
retryDelayMs: retryDelayMs,
204+
crudThrottle: crudThrottle,
205+
retryDelay: retryDelay,
200206
params: params
201207
)
202208
)

Sources/PowerSync/Protocol/QueriesProtocol.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import Combine
22
import Foundation
33

4-
public let DEFAULT_WATCH_THROTTLE_MS = Int64(30)
4+
public let DEFAULT_WATCH_THROTTLE: TimeInterval = 0.03 // 30ms
55

66
public struct WatchOptions<RowType> {
77
public var sql: String
88
public var parameters: [Any?]
9-
public var throttleMs: Int64
9+
public var throttle: TimeInterval
1010
public var mapper: (SqlCursor) throws -> RowType
1111

1212
public init(
1313
sql: String, parameters: [Any?]? = [],
14-
throttleMs: Int64? = DEFAULT_WATCH_THROTTLE_MS,
14+
throttle: TimeInterval? = DEFAULT_WATCH_THROTTLE,
1515
mapper: @escaping (SqlCursor) throws -> RowType
1616
) {
1717
self.sql = sql
18-
self.parameters = parameters ?? [] // Default to empty array if nil
19-
self.throttleMs = throttleMs ?? DEFAULT_WATCH_THROTTLE_MS // Default to the constant if nil
18+
self.parameters = parameters ?? []
19+
self.throttle = throttle ?? DEFAULT_WATCH_THROTTLE
2020
self.mapper = mapper
2121
}
2222
}

0 commit comments

Comments
 (0)