Skip to content

Commit eccb7f7

Browse files
committed
Notify updates from raw statements
1 parent f4d98d5 commit eccb7f7

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

core/src/commonMain/kotlin/com/powersync/db/internal/InternalDatabaseImpl.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import kotlinx.coroutines.flow.channelFlow
2222
import kotlinx.coroutines.flow.filter
2323
import kotlinx.coroutines.flow.onSubscription
2424
import kotlinx.coroutines.flow.transform
25+
import kotlinx.coroutines.launch
2526
import kotlinx.coroutines.sync.Mutex
2627
import kotlinx.coroutines.withContext
2728
import 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

Comments
 (0)