From d0d969a9aca8f392ce6311fc337fe8efe52ad5e1 Mon Sep 17 00:00:00 2001 From: Steven Ontong Date: Tue, 19 Mar 2024 20:00:13 +0200 Subject: [PATCH] fix watched queries --- .gitignore | 3 ++- .../db/internal/PsInternalDatabase.kt | 16 +++++++++--- ...h.sqldelight.dialect.api.SqlDelightDialect | 1 + .../com/powersync/sqlite/PowerSyncDialect.kt | 25 +++++++++++++++++++ 4 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 dialect/bin/main/META-INF/services/app.cash.sqldelight.dialect.api.SqlDelightDialect create mode 100644 dialect/bin/main/com/powersync/sqlite/PowerSyncDialect.kt diff --git a/.gitignore b/.gitignore index 25cb8fab..052ece8b 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,5 @@ captures !*.xcodeproj/project.xcworkspace/ !*.xcworkspace/contents.xcworkspacedata **/xcshareddata/WorkspaceSettings.xcsettings -Pods/ \ No newline at end of file +Pods/ +plugins/sonatype \ No newline at end of file diff --git a/core/src/commonMain/kotlin/com/powersync/db/internal/PsInternalDatabase.kt b/core/src/commonMain/kotlin/com/powersync/db/internal/PsInternalDatabase.kt index 967d0468..b6f5b73e 100644 --- a/core/src/commonMain/kotlin/com/powersync/db/internal/PsInternalDatabase.kt +++ b/core/src/commonMain/kotlin/com/powersync/db/internal/PsInternalDatabase.kt @@ -19,6 +19,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.debounce +import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import kotlinx.serialization.encodeToString @@ -38,10 +39,17 @@ class PsInternalDatabase(val driver: PsSqlDriver, private val scope: CoroutineSc init { scope.launch { - tableUpdates().debounce(DEFAULT_WATCH_THROTTLE_MS).collect { tables -> - val dataTables = tables.map { toFriendlyTableName(it) }.filter { it.isNotBlank() } - driver.notifyListeners(queryKeys = dataTables.toTypedArray()) - } + val accumulatedUpdates = mutableSetOf(); + tableUpdates() +// Debounce will discard any events which occur inside the debounce window +// This will accumulate those table updates + .onEach { tables -> accumulatedUpdates.addAll(tables) } + .debounce(DEFAULT_WATCH_THROTTLE_MS) + .collect { + val dataTables = accumulatedUpdates.map { toFriendlyTableName(it) }.filter { it.isNotBlank() } + driver.notifyListeners(queryKeys = dataTables.toTypedArray()); + accumulatedUpdates.clear(); + } } } diff --git a/dialect/bin/main/META-INF/services/app.cash.sqldelight.dialect.api.SqlDelightDialect b/dialect/bin/main/META-INF/services/app.cash.sqldelight.dialect.api.SqlDelightDialect new file mode 100644 index 00000000..2d4118ed --- /dev/null +++ b/dialect/bin/main/META-INF/services/app.cash.sqldelight.dialect.api.SqlDelightDialect @@ -0,0 +1 @@ +com.powersync.sqlite.PowerSyncDialect diff --git a/dialect/bin/main/com/powersync/sqlite/PowerSyncDialect.kt b/dialect/bin/main/com/powersync/sqlite/PowerSyncDialect.kt new file mode 100644 index 00000000..a97e3639 --- /dev/null +++ b/dialect/bin/main/com/powersync/sqlite/PowerSyncDialect.kt @@ -0,0 +1,25 @@ +package com.powersync.sqlite + +import app.cash.sqldelight.dialect.api.IntermediateType +import app.cash.sqldelight.dialect.api.PrimitiveType +import app.cash.sqldelight.dialect.api.SqlDelightDialect +import app.cash.sqldelight.dialect.api.TypeResolver +import app.cash.sqldelight.dialects.sqlite_3_35.SqliteTypeResolver +import app.cash.sqldelight.dialects.sqlite_3_38.SqliteDialect as Sqlite338Dialect +import com.alecstrong.sql.psi.core.psi.SqlFunctionExpr + +class PowerSyncDialect : SqlDelightDialect by Sqlite338Dialect() { + override fun typeResolver(parentResolver: TypeResolver) = PowerSyncTypeResolver(parentResolver) +} + +class PowerSyncTypeResolver(private val parentResolver: TypeResolver) : + TypeResolver by SqliteTypeResolver(parentResolver) { + override fun functionType(functionExpr: SqlFunctionExpr): IntermediateType? { + when (functionExpr.functionName.text) { + "sqlite_version", "powersync_rs_version", "powersync_replace_schema" -> return IntermediateType( + PrimitiveType.TEXT + ) + } + return parentResolver.functionType(functionExpr) + } +}