Skip to content

Commit 22acdd7

Browse files
authored
Add helpers for Swift 6.2 compatibility (#260)
* Add helpers for Swift 6.2 compatibility * Reformat
1 parent d1dd055 commit 22acdd7

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.powersync
2+
3+
import com.powersync.connectors.PowerSyncBackendConnector
4+
import com.powersync.connectors.PowerSyncCredentials
5+
6+
public interface SwiftBackendConnector {
7+
public suspend fun fetchCredentials(): PowerSyncResult
8+
9+
public suspend fun uploadData(): PowerSyncResult
10+
}
11+
12+
public fun swiftBackendConnectorToPowerSyncConnector(connector: SwiftBackendConnector): PowerSyncBackendConnector =
13+
object : PowerSyncBackendConnector() {
14+
override suspend fun fetchCredentials(): PowerSyncCredentials? =
15+
handleLockResult(connector.fetchCredentials()) as PowerSyncCredentials?
16+
17+
override suspend fun uploadData(database: PowerSyncDatabase) {
18+
handleLockResult(connector.uploadData())
19+
}
20+
}

PowerSyncKotlin/src/appleMain/kotlin/com/powersync/Locks.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import com.powersync.db.internal.PowerSyncTransaction
1515
*/
1616
public sealed class PowerSyncResult {
1717
public data class Success(
18-
val value: Any,
18+
val value: Any?,
1919
) : PowerSyncResult()
2020

2121
public data class Failure(
@@ -26,7 +26,7 @@ public sealed class PowerSyncResult {
2626
// Throws the [PowerSyncException] if the result is a failure, or returns the value if it is a success.
2727
// We throw the exception on behalf of the Swift SDK.
2828
@Throws(PowerSyncException::class)
29-
private fun handleLockResult(result: PowerSyncResult): Any {
29+
internal fun handleLockResult(result: PowerSyncResult): Any? {
3030
when (result) {
3131
is PowerSyncResult.Failure -> {
3232
throw result.exception
@@ -41,13 +41,13 @@ private fun handleLockResult(result: PowerSyncResult): Any {
4141
public class LockContextWrapper(
4242
private val handler: (context: ConnectionContext) -> PowerSyncResult,
4343
) : ThrowableLockCallback<Any> {
44-
override fun execute(context: ConnectionContext): Any = handleLockResult(handler(context))
44+
override fun execute(context: ConnectionContext): Any = handleLockResult(handler(context))!!
4545
}
4646

4747
public class TransactionContextWrapper(
4848
private val handler: (context: PowerSyncTransaction) -> PowerSyncResult,
4949
) : ThrowableTransactionCallback<Any> {
50-
override fun execute(transaction: PowerSyncTransaction): Any = handleLockResult(handler(transaction))
50+
override fun execute(transaction: PowerSyncTransaction): Any = handleLockResult(handler(transaction))!!
5151
}
5252

5353
public fun wrapContextHandler(handler: (context: ConnectionContext) -> PowerSyncResult): ThrowableLockCallback<Any> =

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
package com.powersync
44

5+
import com.powersync.db.crud.CrudTransaction
56
import com.powersync.sync.SyncClientConfiguration
67
import com.powersync.sync.SyncOptions
78
import io.ktor.client.plugins.logging.LogLevel
89
import io.ktor.client.plugins.logging.Logging
10+
import kotlinx.coroutines.flow.Flow
11+
import kotlinx.coroutines.flow.catch
12+
import kotlinx.coroutines.flow.map
913
import io.ktor.client.plugins.logging.Logger as KtorLogger
1014

1115
/**
@@ -88,3 +92,16 @@ public fun createSyncOptions(
8892
userAgent = userAgent,
8993
clientConfiguration = createExtendedSyncClientConfiguration(loggingConfig),
9094
)
95+
96+
public fun errorHandledCrudTransactions(db: PowerSyncDatabase): Flow<PowerSyncResult> =
97+
db
98+
.getCrudTransactions()
99+
.map<CrudTransaction, PowerSyncResult> {
100+
PowerSyncResult.Success(it)
101+
}.catch {
102+
if (it is PowerSyncException) {
103+
emit(PowerSyncResult.Failure(it))
104+
} else {
105+
throw it
106+
}
107+
}

core/src/commonMain/kotlin/com/powersync/PowerSyncDatabase.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,7 @@ public interface PowerSyncDatabase : Queries {
173173
*
174174
* If there is no local data to upload, returns an empty flow.
175175
*/
176-
@Throws(PowerSyncException::class, CancellationException::class)
177-
public suspend fun getCrudTransactions(): Flow<CrudTransaction>
176+
public fun getCrudTransactions(): Flow<CrudTransaction>
178177

179178
/**
180179
* Convenience method to get the current version of PowerSync.

core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ internal class PowerSyncDatabaseImpl(
270270
})
271271
}
272272

273-
override suspend fun getCrudTransactions(): Flow<CrudTransaction> =
273+
override fun getCrudTransactions(): Flow<CrudTransaction> =
274274
flow {
275275
waitReady()
276276
var lastItemId = -1

0 commit comments

Comments
 (0)