Skip to content

Commit ded94a8

Browse files
cleanup
1 parent 07d6c9f commit ded94a8

File tree

2 files changed

+58
-84
lines changed

2 files changed

+58
-84
lines changed

Sources/PowerSync/Kotlin/KotlinPowerSyncDatabaseImpl.swift

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
2525
func waitForFirstSync() async throws {
2626
try await kotlinDatabase.waitForFirstSync()
2727
}
28-
28+
2929
func waitForFirstSync(priority: Int32) async throws {
3030
try await kotlinDatabase.waitForFirstSync(priority: priority)
3131
}
@@ -173,58 +173,55 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
173173
parameters: [Any]?,
174174
mapper: @escaping (SqlCursor) throws -> RowType
175175
) throws -> AsyncThrowingStream<[RowType], Error> {
176-
try watch(options: WatchOptions(sql: sql, parameters: parameters, mapper: mapper))
177-
}
178-
179-
func watch<RowType>(
180-
options: WatchOptions<RowType>
181-
) throws -> AsyncThrowingStream<[RowType], Error> {
182-
AsyncThrowingStream { continuation in
183-
Task {
184-
do {
185-
var mapperError: Error?
186-
// HACK!
187-
// SKIEE doesn't support custom exceptions in Flows
188-
// Exceptions which occur in the Flow itself cause runtime crashes.
189-
// The most probable crash would be the internal EXPLAIN statement.
190-
// This attempts to EXPLAIN the query before passing it to Kotlin
191-
// We could introduce an onChange API in Kotlin which we use to implement watches here.
192-
// This would prevent most issues with exceptions.
193-
_ = try await self.kotlinDatabase.get(sql: "EXPLAIN \(options.sql)", parameters: options.parameters, mapper: {_ in ""})
194-
for try await values in try self.kotlinDatabase.watch(
195-
sql: options.sql,
196-
parameters: options.parameters,
197-
throttleMs: KotlinLong(value: options.throttleMs),
198-
mapper: { cursor in do {
199-
return try options.mapper(cursor)
200-
} catch {
201-
mapperError = error
202-
// The value here does not matter. We will throw the exception later
203-
// This is not ideal, this is only a workaround until we expose fine grained access to Kotlin SDK internals.
204-
return nil as RowType?
205-
} }
206-
) {
207-
if mapperError != nil {
208-
throw mapperError!
209-
}
210-
try continuation.yield(safeCast(values, to: [RowType].self))
176+
try watch(options: WatchOptions(sql: sql, parameters: parameters, mapper: mapper))
177+
}
178+
179+
func watch<RowType>(
180+
options: WatchOptions<RowType>
181+
) throws -> AsyncThrowingStream<[RowType], Error> {
182+
AsyncThrowingStream { continuation in
183+
Task {
184+
do {
185+
var mapperError: Error?
186+
// HACK!
187+
// SKIEE doesn't support custom exceptions in Flows
188+
// Exceptions which occur in the Flow itself cause runtime crashes.
189+
// The most probable crash would be the internal EXPLAIN statement.
190+
// This attempts to EXPLAIN the query before passing it to Kotlin
191+
// We could introduce an onChange API in Kotlin which we use to implement watches here.
192+
// This would prevent most issues with exceptions.
193+
_ = try await self.kotlinDatabase.getAll(sql: "EXPLAIN \(options.sql)", parameters: options.parameters, mapper: { _ in "" })
194+
for try await values in try self.kotlinDatabase.watch(
195+
sql: options.sql,
196+
parameters: options.parameters,
197+
throttleMs: KotlinLong(value: options.throttleMs),
198+
mapper: { cursor in do {
199+
return try options.mapper(cursor)
200+
} catch {
201+
mapperError = error
202+
// The value here does not matter. We will throw the exception later
203+
// This is not ideal, this is only a workaround until we expose fine grained access to Kotlin SDK internals.
204+
return nil as RowType?
205+
} }
206+
) {
207+
if mapperError != nil {
208+
throw mapperError!
211209
}
212-
continuation.finish()
213-
} catch {
214-
continuation.finish(throwing: error)
210+
try continuation.yield(safeCast(values, to: [RowType].self))
215211
}
212+
continuation.finish()
213+
} catch {
214+
continuation.finish(throwing: error)
216215
}
217216
}
218217
}
219-
220-
221-
func writeTransaction<R>(callback: @escaping (any PowerSyncTransaction) throws -> R) async throws -> R {
222-
return try safeCast(await kotlinDatabase.writeTransaction(callback: TransactionCallback(callback: callback)), to: R.self)
223-
}
224-
225-
func readTransaction<R>(callback: @escaping (any PowerSyncTransaction) throws -> R) async throws -> R {
226-
return try safeCast(await kotlinDatabase.readTransaction(callback: TransactionCallback(callback: callback)), to: R.self)
227-
}
228-
229-
}
218+
}
230219

220+
func writeTransaction<R>(callback: @escaping (any PowerSyncTransaction) throws -> R) async throws -> R {
221+
return try safeCast(await kotlinDatabase.writeTransaction(callback: TransactionCallback(callback: callback)), to: R.self)
222+
}
223+
224+
func readTransaction<R>(callback: @escaping (any PowerSyncTransaction) throws -> R) async throws -> R {
225+
return try safeCast(await kotlinDatabase.readTransaction(callback: TransactionCallback(callback: callback)), to: R.self)
226+
}
227+
}

Sources/PowerSync/QueriesProtocol.swift

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import Foundation
21
import Combine
2+
import Foundation
33
import PowerSyncKotlin
44

5-
public let DEFAULT_WATCH_THROTTLE_MS = Int64(30);
5+
public let DEFAULT_WATCH_THROTTLE_MS = Int64(30)
66

77
public struct WatchOptions<RowType> {
88
public var sql: String
99
public var parameters: [Any]
1010
public var throttleMs: Int64
1111
public var mapper: (SqlCursor) throws -> RowType
12-
12+
1313
public init(sql: String, parameters: [Any]? = [], throttleMs: Int64? = DEFAULT_WATCH_THROTTLE_MS, mapper: @escaping (SqlCursor) throws -> RowType) {
1414
self.sql = sql
1515
self.parameters = parameters ?? [] // Default to empty array if nil
1616
self.throttleMs = throttleMs ?? DEFAULT_WATCH_THROTTLE_MS // Default to the constant if nil
17-
self.mapper = mapper;
17+
self.mapper = mapper
1818
}
1919
}
2020

@@ -84,7 +84,7 @@ public protocol Queries {
8484
parameters: [Any]?,
8585
mapper: @escaping (SqlCursor) throws -> RowType
8686
) throws -> AsyncThrowingStream<[RowType], Error>
87-
87+
8888
func watch<RowType>(
8989
options: WatchOptions<RowType>
9090
) throws -> AsyncThrowingStream<[RowType], Error>
@@ -96,59 +96,36 @@ public protocol Queries {
9696
func readTransaction<R>(callback: @escaping (any PowerSyncTransaction) throws -> R) async throws -> R
9797
}
9898

99-
extension Queries {
100-
public func execute(_ sql: String) async throws -> Int64 {
99+
public extension Queries {
100+
func execute(_ sql: String) async throws -> Int64 {
101101
return try await execute(sql: sql, parameters: [])
102102
}
103103

104-
public func get<RowType>(
104+
func get<RowType>(
105105
_ sql: String,
106106
mapper: @escaping (SqlCursor) -> RowType
107107
) async throws -> RowType {
108108
return try await get(sql: sql, parameters: [], mapper: mapper)
109109
}
110110

111-
public func getAll<RowType>(
111+
func getAll<RowType>(
112112
_ sql: String,
113113
mapper: @escaping (SqlCursor) -> RowType
114114
) async throws -> [RowType] {
115115
return try await getAll(sql: sql, parameters: [], mapper: mapper)
116116
}
117117

118-
public func getOptional<RowType>(
118+
func getOptional<RowType>(
119119
_ sql: String,
120120
mapper: @escaping (SqlCursor) -> RowType
121121
) async throws -> RowType? {
122122
return try await getOptional(sql: sql, parameters: [], mapper: mapper)
123123
}
124124

125-
public func watch<RowType>(
126-
_ sql: String,
127-
mapper: @escaping (SqlCursor) -> RowType
128-
) throws -> AsyncThrowingStream<[RowType], Error> {
129-
return try watch(sql: sql, parameters: [], mapper: mapper)
130-
}
131-
132-
133-
/// Execute a read-only (SELECT) query every time the source tables are modified
134-
/// and return the results as an array in a Publisher.
135125
func watch<RowType>(
136-
sql: String,
137-
parameters: [Any]?,
126+
_ sql: String,
138127
mapper: @escaping (SqlCursor) -> RowType
139128
) throws -> AsyncThrowingStream<[RowType], Error> {
140129
return try watch(sql: sql, parameters: [], mapper: mapper)
141130
}
142-
143-
/// Execute a read-only (SELECT) query every time the source tables are modified
144-
/// and return the results as an array in a Publisher.
145-
func watch<RowType>(
146-
sql: String,
147-
parameters: [Any]?,
148-
mapper: @escaping (SqlCursor) throws -> RowType
149-
) throws -> AsyncThrowingStream<[RowType], Error> {
150-
return try watch(sql: sql, parameters: [], mapper: mapper)
151-
}
152-
153-
154131
}

0 commit comments

Comments
 (0)