Skip to content

Commit 8a681c2

Browse files
committed
Add helpers for Swift 6.2 compatibility
1 parent d1dd055 commit 8a681c2

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
public suspend fun uploadData(): PowerSyncResult
9+
}
10+
11+
public fun swiftBackendConnectorToPowerSyncConnector(connector: SwiftBackendConnector): PowerSyncBackendConnector {
12+
return object : PowerSyncBackendConnector() {
13+
override suspend fun fetchCredentials(): PowerSyncCredentials? {
14+
return handleLockResult(connector.fetchCredentials()) as PowerSyncCredentials?
15+
}
16+
17+
override suspend fun uploadData(database: PowerSyncDatabase) {
18+
handleLockResult(connector.uploadData())
19+
}
20+
}
21+
}

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: 16 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,15 @@ public fun createSyncOptions(
8892
userAgent = userAgent,
8993
clientConfiguration = createExtendedSyncClientConfiguration(loggingConfig),
9094
)
95+
96+
public fun errorHandledCrudTransactions(db: PowerSyncDatabase): Flow<PowerSyncResult> {
97+
return db.getCrudTransactions().map<CrudTransaction, PowerSyncResult> {
98+
PowerSyncResult.Success(it)
99+
}.catch {
100+
if (it is PowerSyncException) {
101+
emit(PowerSyncResult.Failure(it))
102+
} else {
103+
throw it
104+
}
105+
}
106+
}

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)