From 16dd0c09042900f0cf8cf125c782fcb6e67ec7ac Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Wed, 29 Oct 2025 15:56:16 +0100 Subject: [PATCH 1/2] Add Swift sync stream helpers --- .../kotlin/com/powersync/sync/Stream.kt | 7 +++++ .../src/appleMain/kotlin/com/powersync/SDK.kt | 31 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/common/src/commonMain/kotlin/com/powersync/sync/Stream.kt b/common/src/commonMain/kotlin/com/powersync/sync/Stream.kt index c5977157..f40fd5b2 100644 --- a/common/src/commonMain/kotlin/com/powersync/sync/Stream.kt +++ b/common/src/commonMain/kotlin/com/powersync/sync/Stream.kt @@ -1,6 +1,9 @@ package com.powersync.sync +import com.powersync.PowerSyncException import com.powersync.bucket.StreamPriority +import kotlinx.coroutines.CancellationException +import kotlin.native.HiddenFromObjC import kotlin.time.Duration import kotlin.time.Instant @@ -73,11 +76,13 @@ public interface SyncSubscriptionDescription : SyncStreamDescription { * To obtain an instance of [SyncStream], call [com.powersync.PowerSyncDatabase.syncStream]. */ public interface SyncStream : SyncStreamDescription { + @HiddenFromObjC public suspend fun subscribe( ttl: Duration? = null, priority: StreamPriority? = null, ): SyncStreamSubscription + @Throws(PowerSyncException::class, CancellationException::class) public suspend fun unsubscribeAll() } @@ -89,6 +94,7 @@ public interface SyncStreamSubscription : SyncStreamDescription { * A variant of [com.powersync.PowerSyncDatabase.waitForFirstSync] that is specific to this * stream subscription. */ + @Throws(PowerSyncException::class, CancellationException::class) public suspend fun waitForFirstSync() /** @@ -98,5 +104,6 @@ public interface SyncStreamSubscription : SyncStreamDescription { * that stream starts running. When it expires without subscribing again, the stream will be * evicted. */ + @Throws(PowerSyncException::class, CancellationException::class) public suspend fun unsubscribe() } diff --git a/internal/PowerSyncKotlin/src/appleMain/kotlin/com/powersync/SDK.kt b/internal/PowerSyncKotlin/src/appleMain/kotlin/com/powersync/SDK.kt index 39382e2a..88a9a2d7 100644 --- a/internal/PowerSyncKotlin/src/appleMain/kotlin/com/powersync/SDK.kt +++ b/internal/PowerSyncKotlin/src/appleMain/kotlin/com/powersync/SDK.kt @@ -4,15 +4,23 @@ package com.powersync import androidx.sqlite.SQLiteConnection import androidx.sqlite.execSQL +import com.powersync.bucket.StreamPriority import com.powersync.db.NativeConnectionFactory import com.powersync.db.crud.CrudTransaction import com.powersync.sync.SyncClientConfiguration import com.powersync.sync.SyncOptions +import com.powersync.sync.SyncStatusData +import com.powersync.sync.SyncStream +import com.powersync.sync.SyncStreamDescription +import com.powersync.sync.SyncStreamStatus +import com.powersync.sync.SyncStreamSubscription import io.ktor.client.plugins.logging.LogLevel import io.ktor.client.plugins.logging.Logging +import kotlinx.coroutines.CancellationException import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.map +import kotlin.time.Duration.Companion.seconds import io.ktor.client.plugins.logging.Logger as KtorLogger public fun sqlite3DatabaseFactory(initialStatements: List): PersistentConnectionFactory { @@ -132,3 +140,26 @@ public fun errorHandledCrudTransactions(db: PowerSyncDatabase): Flow?): SyncStreamStatus? { + return status.forStream(object: SyncStreamDescription { + override val name: String + get() = name + override val parameters: Map? + get() = parameters + }) +} From 5af82c170b76603b9d2bafb22994acfb0678c6d2 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Wed, 29 Oct 2025 16:57:02 +0100 Subject: [PATCH 2/2] Reformat --- .../src/appleMain/kotlin/com/powersync/SDK.kt | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/internal/PowerSyncKotlin/src/appleMain/kotlin/com/powersync/SDK.kt b/internal/PowerSyncKotlin/src/appleMain/kotlin/com/powersync/SDK.kt index 88a9a2d7..af504a24 100644 --- a/internal/PowerSyncKotlin/src/appleMain/kotlin/com/powersync/SDK.kt +++ b/internal/PowerSyncKotlin/src/appleMain/kotlin/com/powersync/SDK.kt @@ -148,18 +148,26 @@ public fun errorHandledCrudTransactions(db: PowerSyncDatabase): Flow?): SyncStreamStatus? { - return status.forStream(object: SyncStreamDescription { - override val name: String - get() = name - override val parameters: Map? - get() = parameters - }) -} +public fun syncStatusForStream( + status: SyncStatusData, + name: String, + parameters: Map?, +): SyncStreamStatus? = + status.forStream( + object : SyncStreamDescription { + override val name: String + get() = name + override val parameters: Map? + get() = parameters + }, + )