@@ -7,15 +7,13 @@ import androidx.room3.useReaderConnection
77import androidx.room3.useWriterConnection
88import androidx.sqlite.SQLiteException
99import androidx.sqlite.SQLiteStatement
10+ import androidx.sqlite.async.step
1011import com.powersync.db.driver.SQLiteConnectionLease
1112import com.powersync.db.driver.SQLiteConnectionPool
1213import com.powersync.db.schema.Schema
13- import kotlinx.coroutines.currentCoroutineContext
1414import kotlinx.coroutines.flow.MutableSharedFlow
1515import kotlinx.coroutines.flow.SharedFlow
1616import kotlinx.coroutines.launch
17- import kotlinx.coroutines.runBlocking
18- import kotlin.coroutines.CoroutineContext
1917
2018/* *
2119 * A [SQLiteConnectionPool] implementation for the PowerSync SDK that is backed by a [RoomDatabase].
@@ -52,7 +50,7 @@ public class RoomConnectionPool(
5250
5351 override suspend fun <T > read (callback : suspend (SQLiteConnectionLease ) -> T ): T =
5452 db.useReaderConnection {
55- callback(RoomTransactionLease (it, currentCoroutineContext() ))
53+ callback(it.asConnectionLease( ))
5654 }
5755
5856 /* *
@@ -97,7 +95,7 @@ public class RoomConnectionPool(
9795 }
9896
9997 try {
100- callback(RoomTransactionLease (it, currentCoroutineContext() ))
98+ callback(it.asConnectionLease( ))
10199 } finally {
102100 // List changed tables, excluding virtual and shadow tables for e.g. fts5
103101 val statement =
@@ -138,36 +136,15 @@ public class RoomConnectionPool(
138136 }
139137}
140138
141- private class RoomTransactionLease (
142- private val transactor : Transactor ,
143- /* *
144- * The context to use for [runBlocking] calls to avoid the "Attempted to use connection on a
145- * different coroutine" error.
146- */
147- private val context : CoroutineContext ,
139+ internal expect suspend fun Transactor.asConnectionLease (): SQLiteConnectionLease
140+
141+ internal abstract class RoomTransactionLease (
142+ protected val transactor : Transactor ,
148143) : SQLiteConnectionLease {
149144 override suspend fun isInTransaction (): Boolean = transactor.inTransaction()
150145
151- override suspend fun <R > usePrepared (
152- sql : String ,
153- block : (SQLiteStatement ) -> R ,
154- ): R = transactor.usePrepared(sql, block)
155-
156146 override suspend fun <R > usePreparedAsync (
157147 sql : String ,
158148 block : suspend (SQLiteStatement ) -> R ,
159149 ): R = transactor.usePrepared(sql, block)
160-
161- override fun isInTransactionSync (): Boolean =
162- runBlocking(context) {
163- isInTransaction()
164- }
165-
166- override fun <R > usePreparedSync (
167- sql : String ,
168- block : (SQLiteStatement ) -> R ,
169- ): R =
170- runBlocking(context) {
171- usePrepared(sql, block)
172- }
173150}
0 commit comments