Skip to content

Commit 16dd0c0

Browse files
committed
Add Swift sync stream helpers
1 parent b027608 commit 16dd0c0

File tree

2 files changed

+38
-0
lines changed
  • common/src/commonMain/kotlin/com/powersync/sync
  • internal/PowerSyncKotlin/src/appleMain/kotlin/com/powersync

2 files changed

+38
-0
lines changed

common/src/commonMain/kotlin/com/powersync/sync/Stream.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.powersync.sync
22

3+
import com.powersync.PowerSyncException
34
import com.powersync.bucket.StreamPriority
5+
import kotlinx.coroutines.CancellationException
6+
import kotlin.native.HiddenFromObjC
47
import kotlin.time.Duration
58
import kotlin.time.Instant
69

@@ -73,11 +76,13 @@ public interface SyncSubscriptionDescription : SyncStreamDescription {
7376
* To obtain an instance of [SyncStream], call [com.powersync.PowerSyncDatabase.syncStream].
7477
*/
7578
public interface SyncStream : SyncStreamDescription {
79+
@HiddenFromObjC
7680
public suspend fun subscribe(
7781
ttl: Duration? = null,
7882
priority: StreamPriority? = null,
7983
): SyncStreamSubscription
8084

85+
@Throws(PowerSyncException::class, CancellationException::class)
8186
public suspend fun unsubscribeAll()
8287
}
8388

@@ -89,6 +94,7 @@ public interface SyncStreamSubscription : SyncStreamDescription {
8994
* A variant of [com.powersync.PowerSyncDatabase.waitForFirstSync] that is specific to this
9095
* stream subscription.
9196
*/
97+
@Throws(PowerSyncException::class, CancellationException::class)
9298
public suspend fun waitForFirstSync()
9399

94100
/**
@@ -98,5 +104,6 @@ public interface SyncStreamSubscription : SyncStreamDescription {
98104
* that stream starts running. When it expires without subscribing again, the stream will be
99105
* evicted.
100106
*/
107+
@Throws(PowerSyncException::class, CancellationException::class)
101108
public suspend fun unsubscribe()
102109
}

internal/PowerSyncKotlin/src/appleMain/kotlin/com/powersync/SDK.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,23 @@ package com.powersync
44

55
import androidx.sqlite.SQLiteConnection
66
import androidx.sqlite.execSQL
7+
import com.powersync.bucket.StreamPriority
78
import com.powersync.db.NativeConnectionFactory
89
import com.powersync.db.crud.CrudTransaction
910
import com.powersync.sync.SyncClientConfiguration
1011
import com.powersync.sync.SyncOptions
12+
import com.powersync.sync.SyncStatusData
13+
import com.powersync.sync.SyncStream
14+
import com.powersync.sync.SyncStreamDescription
15+
import com.powersync.sync.SyncStreamStatus
16+
import com.powersync.sync.SyncStreamSubscription
1117
import io.ktor.client.plugins.logging.LogLevel
1218
import io.ktor.client.plugins.logging.Logging
19+
import kotlinx.coroutines.CancellationException
1320
import kotlinx.coroutines.flow.Flow
1421
import kotlinx.coroutines.flow.catch
1522
import kotlinx.coroutines.flow.map
23+
import kotlin.time.Duration.Companion.seconds
1624
import io.ktor.client.plugins.logging.Logger as KtorLogger
1725

1826
public fun sqlite3DatabaseFactory(initialStatements: List<String>): PersistentConnectionFactory {
@@ -132,3 +140,26 @@ public fun errorHandledCrudTransactions(db: PowerSyncDatabase): Flow<PowerSyncRe
132140
throw it
133141
}
134142
}
143+
144+
/**
145+
* Calls [SyncStream.subscribe] with types that are more convenient to construct in Swift:
146+
*
147+
* The `ttl` uses a [Double] representing seconds, `priority` is represented as the priority number
148+
* instead of the [StreamPriority].
149+
*/
150+
@Throws(PowerSyncException::class, CancellationException::class)
151+
public suspend fun syncStreamSubscribeSwift(stream: SyncStream, ttl: Double?, priority: Int?): SyncStreamSubscription {
152+
return stream.subscribe(
153+
ttl = ttl?.seconds,
154+
priority = priority?.let { StreamPriority(it) },
155+
)
156+
}
157+
158+
public fun syncStatusForStream(status: SyncStatusData, name: String, parameters: Map<String, Any?>?): SyncStreamStatus? {
159+
return status.forStream(object: SyncStreamDescription {
160+
override val name: String
161+
get() = name
162+
override val parameters: Map<String, Any?>?
163+
get() = parameters
164+
})
165+
}

0 commit comments

Comments
 (0)