@@ -277,22 +277,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol, @unchecked S
277277 return try await wrapPowerSyncException {
278278 try safeCast (
279279 await kotlinDatabase. writeLock (
280- callback: PowerSyncKotlin . wrapContextHandler { kotlinContext in
281- do {
282- return try PowerSyncKotlin . LockCallbackResult. Success (
283- value: callback (
284- KotlinConnectionContext (
285- ctx: kotlinContext
286- )
287- ) )
288- } catch {
289- return PowerSyncKotlin . LockCallbackResult. Failure ( exception:
290- PowerSyncKotlin . PowerSyncException (
291- message: error. localizedDescription,
292- cause: nil
293- ) )
294- }
295- }
280+ callback: wrapLockContext ( callback: callback)
296281 ) ,
297282 to: R . self
298283 )
@@ -305,22 +290,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol, @unchecked S
305290 return try await wrapPowerSyncException {
306291 try safeCast (
307292 await kotlinDatabase. writeTransaction (
308- callback: PowerSyncKotlin . wrapTransactionContextHandler { kotlinContext in
309- do {
310- return try PowerSyncKotlin . LockCallbackResult. Success (
311- value: callback (
312- KotlinTransactionContext (
313- ctx: kotlinContext
314- )
315- ) )
316- } catch {
317- return PowerSyncKotlin . LockCallbackResult. Failure ( exception:
318- PowerSyncKotlin . PowerSyncException (
319- message: error. localizedDescription,
320- cause: nil
321- ) )
322- }
323- }
293+ callback: wrapTransactionContext ( callback: callback)
324294 ) ,
325295 to: R . self
326296 )
@@ -335,22 +305,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol, @unchecked S
335305 return try await wrapPowerSyncException {
336306 try safeCast (
337307 await kotlinDatabase. readLock (
338- callback: PowerSyncKotlin . wrapContextHandler { kotlinContext in
339- do {
340- return try PowerSyncKotlin . LockCallbackResult. Success (
341- value: callback (
342- KotlinConnectionContext (
343- ctx: kotlinContext
344- )
345- ) )
346- } catch {
347- return PowerSyncKotlin . LockCallbackResult. Failure ( exception:
348- PowerSyncKotlin . PowerSyncException (
349- message: error. localizedDescription,
350- cause: nil
351- ) )
352- }
353- }
308+ callback: wrapLockContext ( callback: callback)
354309 ) ,
355310 to: R . self
356311 )
@@ -363,22 +318,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol, @unchecked S
363318 return try await wrapPowerSyncException {
364319 try safeCast (
365320 await kotlinDatabase. readTransaction (
366- callback: PowerSyncKotlin . wrapTransactionContextHandler { kotlinContext in
367- do {
368- return try PowerSyncKotlin . LockCallbackResult. Success (
369- value: callback (
370- KotlinTransactionContext (
371- ctx: kotlinContext
372- )
373- ) )
374- } catch {
375- return PowerSyncKotlin . LockCallbackResult. Failure ( exception:
376- PowerSyncKotlin . PowerSyncException (
377- message: error. localizedDescription,
378- cause: nil
379- ) )
380- }
381- }
321+ callback: wrapTransactionContext ( callback: callback)
382322 ) ,
383323 to: R . self
384324 )
@@ -426,11 +366,11 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol, @unchecked S
426366 }
427367 )
428368
429- let rootPages = rows. compactMap { r in
430- if ( r . opcode == " OpenRead " || r . opcode == " OpenWrite " ) &&
431- r . p3 == 0 && r . p2 != 0
369+ let rootPages = rows. compactMap { row in
370+ if ( row . opcode == " OpenRead " || row . opcode == " OpenWrite " ) &&
371+ row . p3 == 0 && row . p2 != 0
432372 {
433- return r . p2
373+ return row . p2
434374 }
435375 return nil
436376 }
@@ -468,3 +408,50 @@ private struct ExplainQueryResult {
468408 let p2 : Int64
469409 let p3 : Int64
470410}
411+
412+ extension Error {
413+ func toPowerSyncError( ) -> PowerSyncKotlin . PowerSyncException {
414+ return PowerSyncKotlin . PowerSyncException (
415+ message: localizedDescription,
416+ cause: nil
417+ )
418+ }
419+ }
420+
421+ func wrapLockContext(
422+ callback: @escaping ( any ConnectionContext ) throws -> Any
423+ ) throws -> PowerSyncKotlin . ThrowableLockCallback {
424+ PowerSyncKotlin . wrapContextHandler { kotlinContext in
425+ do {
426+ return try PowerSyncKotlin . PowerSyncResult. Success (
427+ value: callback (
428+ KotlinConnectionContext (
429+ ctx: kotlinContext
430+ )
431+ ) )
432+ } catch {
433+ return PowerSyncKotlin . PowerSyncResult. Failure (
434+ exception: error. toPowerSyncError ( )
435+ )
436+ }
437+ }
438+ }
439+
440+ func wrapTransactionContext(
441+ callback: @escaping ( any Transaction ) throws -> Any
442+ ) throws -> PowerSyncKotlin . ThrowableTransactionCallback {
443+ PowerSyncKotlin . wrapTransactionContextHandler { kotlinContext in
444+ do {
445+ return try PowerSyncKotlin . PowerSyncResult. Success (
446+ value: callback (
447+ KotlinTransactionContext (
448+ ctx: kotlinContext
449+ )
450+ ) )
451+ } catch {
452+ return PowerSyncKotlin . PowerSyncResult. Failure (
453+ exception: error. toPowerSyncError ( )
454+ )
455+ }
456+ }
457+ }
0 commit comments