@@ -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+ }
0 commit comments