Skip to content
Open
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
7 changes: 7 additions & 0 deletions common/src/commonMain/kotlin/com/powersync/sync/Stream.kt
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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()
}

Expand All @@ -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()

/**
Expand All @@ -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()
}
39 changes: 39 additions & 0 deletions internal/PowerSyncKotlin/src/appleMain/kotlin/com/powersync/SDK.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>): PersistentConnectionFactory {
Expand Down Expand Up @@ -132,3 +140,34 @@ public fun errorHandledCrudTransactions(db: PowerSyncDatabase): Flow<PowerSyncRe
throw it
}
}

/**
* Calls [SyncStream.subscribe] with types that are more convenient to construct in Swift:
*
* The `ttl` uses a [Double] representing seconds, `priority` is represented as the priority number
* instead of the [StreamPriority].
*/
@Throws(PowerSyncException::class, CancellationException::class)
public suspend fun syncStreamSubscribeSwift(
stream: SyncStream,
ttl: Double?,
priority: Int?,
): SyncStreamSubscription =
stream.subscribe(
ttl = ttl?.seconds,
priority = priority?.let { StreamPriority(it) },
)

public fun syncStatusForStream(
status: SyncStatusData,
name: String,
parameters: Map<String, Any?>?,
): SyncStreamStatus? =
status.forStream(
object : SyncStreamDescription {
override val name: String
get() = name
override val parameters: Map<String, Any?>?
get() = parameters
},
)