@@ -25,7 +25,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
25
25
func waitForFirstSync( ) async throws {
26
26
try await kotlinDatabase. waitForFirstSync ( )
27
27
}
28
-
28
+
29
29
func waitForFirstSync( priority: Int32 ) async throws {
30
30
try await kotlinDatabase. waitForFirstSync ( priority: priority)
31
31
}
@@ -165,38 +165,42 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
165
165
parameters: [ Any ] ? ,
166
166
mapper: @escaping ( SqlCursor ) -> RowType
167
167
) throws -> AsyncThrowingStream < [ RowType ] , Error > {
168
- AsyncThrowingStream { continuation in
169
- Task {
170
- do {
171
- for await values in try self . kotlinDatabase. watch (
172
- sql: sql,
173
- parameters: parameters,
174
- mapper: mapper
175
- ) {
176
- try continuation. yield ( safeCast ( values, to: [ RowType ] . self) )
177
- }
178
- continuation. finish ( )
179
- } catch {
180
- continuation. finish ( throwing: error)
181
- }
182
- }
183
- }
168
+ try watch ( options: WatchOptions ( sql: sql, parameters: parameters, mapper: mapper) )
184
169
}
185
170
186
171
func watch< RowType> (
187
172
sql: String ,
188
173
parameters: [ Any ] ? ,
189
174
mapper: @escaping ( SqlCursor ) throws -> RowType
175
+ ) 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 >
190
181
) throws -> AsyncThrowingStream < [ RowType ] , Error > {
191
182
AsyncThrowingStream { continuation in
192
183
Task {
193
184
do {
194
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 (
194
+ sql: " EXPLAIN \( options. sql) " ,
195
+ parameters: options. parameters,
196
+ mapper: { _ in " " }
197
+ )
195
198
for try await values in try self . kotlinDatabase. watch (
196
- sql: sql,
197
- parameters: parameters,
199
+ sql: options. sql,
200
+ parameters: options. parameters,
201
+ throttleMs: KotlinLong ( value: options. throttleMs) ,
198
202
mapper: { cursor in do {
199
- return try mapper ( cursor)
203
+ return try options . mapper ( cursor)
200
204
} catch {
201
205
mapperError = error
202
206
// The value here does not matter. We will throw the exception later
@@ -217,12 +221,11 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
217
221
}
218
222
}
219
223
220
- public func writeTransaction< R> ( callback: @escaping ( any PowerSyncTransaction ) throws -> R ) async throws -> R {
224
+ func writeTransaction< R> ( callback: @escaping ( any PowerSyncTransaction ) throws -> R ) async throws -> R {
221
225
return try safeCast ( await kotlinDatabase. writeTransaction ( callback: TransactionCallback ( callback: callback) ) , to: R . self)
222
226
}
223
227
224
- public func readTransaction< R> ( callback: @escaping ( any PowerSyncTransaction ) throws -> R ) async throws -> R {
228
+ func readTransaction< R> ( callback: @escaping ( any PowerSyncTransaction ) throws -> R ) async throws -> R {
225
229
return try safeCast ( await kotlinDatabase. readTransaction ( callback: TransactionCallback ( callback: callback) ) , to: R . self)
226
230
}
227
231
}
228
-
0 commit comments