Skip to content
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
20 changes: 20 additions & 0 deletions PowerSyncKotlin/src/appleMain/kotlin/com/powersync/Connector.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.powersync

import com.powersync.connectors.PowerSyncBackendConnector
import com.powersync.connectors.PowerSyncCredentials

public interface SwiftBackendConnector {
public suspend fun fetchCredentials(): PowerSyncResult

public suspend fun uploadData(): PowerSyncResult
}

public fun swiftBackendConnectorToPowerSyncConnector(connector: SwiftBackendConnector): PowerSyncBackendConnector =
object : PowerSyncBackendConnector() {
override suspend fun fetchCredentials(): PowerSyncCredentials? =
handleLockResult(connector.fetchCredentials()) as PowerSyncCredentials?

override suspend fun uploadData(database: PowerSyncDatabase) {
handleLockResult(connector.uploadData())
}
}
8 changes: 4 additions & 4 deletions PowerSyncKotlin/src/appleMain/kotlin/com/powersync/Locks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import com.powersync.db.internal.PowerSyncTransaction
*/
public sealed class PowerSyncResult {
public data class Success(
val value: Any,
val value: Any?,
) : PowerSyncResult()

public data class Failure(
Expand All @@ -26,7 +26,7 @@ public sealed class PowerSyncResult {
// Throws the [PowerSyncException] if the result is a failure, or returns the value if it is a success.
// We throw the exception on behalf of the Swift SDK.
@Throws(PowerSyncException::class)
private fun handleLockResult(result: PowerSyncResult): Any {
internal fun handleLockResult(result: PowerSyncResult): Any? {
when (result) {
is PowerSyncResult.Failure -> {
throw result.exception
Expand All @@ -41,13 +41,13 @@ private fun handleLockResult(result: PowerSyncResult): Any {
public class LockContextWrapper(
private val handler: (context: ConnectionContext) -> PowerSyncResult,
) : ThrowableLockCallback<Any> {
override fun execute(context: ConnectionContext): Any = handleLockResult(handler(context))
override fun execute(context: ConnectionContext): Any = handleLockResult(handler(context))!!
}

public class TransactionContextWrapper(
private val handler: (context: PowerSyncTransaction) -> PowerSyncResult,
) : ThrowableTransactionCallback<Any> {
override fun execute(transaction: PowerSyncTransaction): Any = handleLockResult(handler(transaction))
override fun execute(transaction: PowerSyncTransaction): Any = handleLockResult(handler(transaction))!!
}

public fun wrapContextHandler(handler: (context: ConnectionContext) -> PowerSyncResult): ThrowableLockCallback<Any> =
Expand Down
17 changes: 17 additions & 0 deletions PowerSyncKotlin/src/appleMain/kotlin/com/powersync/SDK.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

package com.powersync

import com.powersync.db.crud.CrudTransaction
import com.powersync.sync.SyncClientConfiguration
import com.powersync.sync.SyncOptions
import io.ktor.client.plugins.logging.LogLevel
import io.ktor.client.plugins.logging.Logging
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.map
import io.ktor.client.plugins.logging.Logger as KtorLogger

/**
Expand Down Expand Up @@ -88,3 +92,16 @@ public fun createSyncOptions(
userAgent = userAgent,
clientConfiguration = createExtendedSyncClientConfiguration(loggingConfig),
)

public fun errorHandledCrudTransactions(db: PowerSyncDatabase): Flow<PowerSyncResult> =
db
.getCrudTransactions()
.map<CrudTransaction, PowerSyncResult> {
PowerSyncResult.Success(it)
}.catch {
if (it is PowerSyncException) {
emit(PowerSyncResult.Failure(it))
} else {
throw it
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ public interface PowerSyncDatabase : Queries {
*
* If there is no local data to upload, returns an empty flow.
*/
@Throws(PowerSyncException::class, CancellationException::class)
public suspend fun getCrudTransactions(): Flow<CrudTransaction>
public fun getCrudTransactions(): Flow<CrudTransaction>

/**
* Convenience method to get the current version of PowerSync.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ internal class PowerSyncDatabaseImpl(
})
}

override suspend fun getCrudTransactions(): Flow<CrudTransaction> =
override fun getCrudTransactions(): Flow<CrudTransaction> =
flow {
waitReady()
var lastItemId = -1
Expand Down