@@ -22,6 +22,7 @@ import kotlinx.coroutines.flow.channelFlow
2222import kotlinx.coroutines.flow.filter
2323import kotlinx.coroutines.flow.onSubscription
2424import kotlinx.coroutines.flow.transform
25+ import kotlinx.coroutines.launch
2526import kotlinx.coroutines.sync.Mutex
2627import kotlinx.coroutines.withContext
2728import kotlin.time.Duration.Companion.milliseconds
@@ -212,9 +213,24 @@ internal class InternalDatabaseImpl(
212213 readPool.obtainConnection()
213214 } else {
214215 writeLockMutex.lock()
215- RawConnectionLease (writeConnection, writeLockMutex::unlock)
216+ RawConnectionLease (writeConnection) {
217+ scope.launch {
218+ // When we've leased a write connection, we may have to update table update
219+ // flows after users ran their custom statements.
220+ // For internal queries, this happens with leaseWrite() and an asynchronous call
221+ // in internalWriteLock
222+ updates.fireTableUpdates()
223+ }
224+
225+ writeLockMutex.unlock()
226+ }
216227 }
217228
229+ private suspend fun leaseWrite (): SQLiteConnection {
230+ writeLockMutex.lock()
231+ return RawConnectionLease (writeConnection, writeLockMutex::unlock)
232+ }
233+
218234 /* *
219235 * Creates a read lock while providing an internal transactor for transactions
220236 */
@@ -251,7 +267,7 @@ internal class InternalDatabaseImpl(
251267 @OptIn(ExperimentalPowerSyncAPI ::class )
252268 private suspend fun <R > internalWriteLock (callback : (SQLiteConnection ) -> R ): R =
253269 withContext(dbContext) {
254- val lease = leaseConnection(readOnly = false )
270+ val lease = leaseWrite( )
255271 try {
256272 runWrapped {
257273 catchSwiftExceptions {
0 commit comments