Skip to content

CrudBatch hasMore + onChange improvement #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## unreleased

* Fixed `CrudBatch` `hasMore` always returning false.
* Added `triggerImmediately` to `onChange` method.

## 1.0.0-BETA32

* Added `onChange` method to the PowerSync client. This allows for observing table changes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,22 @@ class DatabaseTest {
fun testTableChangesUpdates() =
databaseTest {
turbineScope {
val query = database.onChange(tables = setOf("users")).testIn(this)
val query =
database
.onChange(
tables = setOf("users"),
).testIn(this)

database.execute(
"INSERT INTO users (id, name, email) VALUES (uuid(), ?, ?)",
listOf("Test", "[email protected]"),
)

val changeSet = query.awaitItem()
var changeSet = query.awaitItem()
// The initial result
changeSet.count() shouldBe 0

changeSet = query.awaitItem()
changeSet.count() shouldBe 1
changeSet.contains("users") shouldBe true

Expand Down Expand Up @@ -418,4 +426,37 @@ class DatabaseTest {

database.getNextCrudTransaction() shouldBe null
}

@Test
fun testCrudBatch() =
databaseTest {
database.execute(
"INSERT INTO users (id, name, email) VALUES (uuid(), ?, ?)",
listOf("a", "[email protected]"),
)

database.writeTransaction {
it.execute(
"INSERT INTO users (id, name, email) VALUES (uuid(), ?, ?)",
listOf("b", "[email protected]"),
)
it.execute(
"INSERT INTO users (id, name, email) VALUES (uuid(), ?, ?)",
listOf("c", "[email protected]"),
)
}

// Purposely limit to less than the number of available ops
var batch = database.getCrudBatch(2) ?: error("Batch should not be null")
batch.hasMore shouldBe true
batch.crud shouldHaveSize 2
batch.complete(null)

batch = database.getCrudBatch(1000) ?: error("Batch should not be null")
batch.crud shouldHaveSize 1
batch.hasMore shouldBe false
batch.complete(null)

database.getCrudBatch() shouldBe null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,10 @@ internal class PowerSyncDatabaseImpl(
return null
}

val entries =
var entries =
internalDb.getAll(
"SELECT id, tx_id, data FROM ps_crud ORDER BY id ASC LIMIT ?",
listOf(limit.toLong()),
listOf(limit.toLong() + 1),
) {
CrudEntry.fromRow(
CrudRow(
Expand All @@ -278,7 +278,7 @@ internal class PowerSyncDatabaseImpl(

val hasMore = entries.size > limit
if (hasMore) {
entries.dropLast(entries.size - limit)
entries = entries.dropLast(1)
}

return CrudBatch(entries, hasMore, complete = { writeCheckpoint ->
Expand Down Expand Up @@ -351,11 +351,12 @@ internal class PowerSyncDatabaseImpl(
override fun onChange(
tables: Set<String>,
throttleMs: Long,
triggerImmediately: Boolean,
): Flow<Set<String>> =
flow {
waitReady()
emitAll(
internalDb.onChange(tables, throttleMs),
internalDb.onChange(tables, throttleMs, triggerImmediately),
)
}

Expand Down
2 changes: 2 additions & 0 deletions core/src/commonMain/kotlin/com/powersync/db/Queries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public interface Queries {
*
* @param tables The set of tables to monitor for changes.
* @param throttleMs The minimum interval, in milliseconds, between emissions. Defaults to [DEFAULT_THROTTLE]. Table changes are accumulated while throttling is active. The accumulated set of tables will be emitted on the trailing edge of the throttle.
* @param triggerImmediately If true (default), the flow will immediately emit an empty set of tables when the flow is first collected. This can be useful for ensuring that the flow emits at least once, even if no changes occur to the monitored tables.
* @return A [Flow] emitting the set of modified tables.
* @throws PowerSyncException If a database error occurs.
* @throws CancellationException If the operation is cancelled.
Expand All @@ -105,6 +106,7 @@ public interface Queries {
public fun onChange(
tables: Set<String>,
throttleMs: Long = DEFAULT_THROTTLE.inWholeMilliseconds,
triggerImmediately: Boolean = true,
): Flow<Set<String>>

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ internal class InternalDatabaseImpl(
override fun onChange(
tables: Set<String>,
throttleMs: Long,
triggerImmediately: Boolean,
): Flow<Set<String>> =
channelFlow {
// Match all possible internal table combinations
Expand All @@ -116,7 +117,12 @@ internal class InternalDatabaseImpl(
val batchedUpdates = AtomicMutableSet<String>()

updatesOnTables()
.transform { updates ->
.onSubscription {
if (triggerImmediately) {
// Emit an initial event (if requested). No changes would be detected at this point
send(setOf())
}
}.transform { updates ->
val intersection = updates.intersect(watchedTables)
if (intersection.isNotEmpty()) {
// Transform table names using friendlyTableName
Expand Down