Skip to content

Commit 0547ed2

Browse files
Update connector for better Sendable support
1 parent 17e4c5f commit 0547ed2

File tree

6 files changed

+21
-19
lines changed

6 files changed

+21
-19
lines changed

Demo/PowerSyncExample/PowerSync/SupabaseConnector.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private enum PostgresFatalCodes {
3838
}
3939

4040
@Observable
41-
class SupabaseConnector: PowerSyncBackendConnector {
41+
final class SupabaseConnector: PowerSyncBackendConnectorProtocol {
4242
let powerSyncEndpoint: String = Secrets.powerSyncEndpoint
4343
let client: SupabaseClient = .init(
4444
supabaseURL: Secrets.supabaseURL,
@@ -50,8 +50,7 @@ class SupabaseConnector: PowerSyncBackendConnector {
5050
@ObservationIgnored
5151
private var observeAuthStateChangesTask: Task<Void, Error>?
5252

53-
override init() {
54-
super.init()
53+
init() {
5554
session = client.auth.currentSession
5655
observeAuthStateChangesTask = Task { [weak self] in
5756
guard let self = self else { return }
@@ -80,7 +79,7 @@ class SupabaseConnector: PowerSyncBackendConnector {
8079
return client.storage.from(bucket)
8180
}
8281

83-
override func fetchCredentials() async throws -> PowerSyncCredentials? {
82+
func fetchCredentials() async throws -> PowerSyncCredentials? {
8483
session = try await client.auth.session
8584

8685
if session == nil {
@@ -92,7 +91,7 @@ class SupabaseConnector: PowerSyncBackendConnector {
9291
return PowerSyncCredentials(endpoint: powerSyncEndpoint, token: token)
9392
}
9493

95-
override func uploadData(database: PowerSyncDatabaseProtocol) async throws {
94+
func uploadData(database: PowerSyncDatabaseProtocol) async throws {
9695
guard let transaction = try await database.getNextCrudTransaction() else { return }
9796

9897
var lastEntry: CrudEntry?

Demo/PowerSyncExample/PowerSync/SystemManager.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ func getAttachmentsDirectoryPath() throws -> String {
1313

1414
let logTag = "SystemManager"
1515

16-
@MainActor
1716
@Observable
1817
class SystemManager {
1918
let connector = SupabaseConnector()
@@ -242,7 +241,7 @@ class SystemManager {
242241
}
243242
}
244243

245-
private nonisolated func deleteTodoInTX(id: String, tx: ConnectionContext) throws {
244+
private func deleteTodoInTX(id: String, tx: ConnectionContext) throws {
246245
_ = try tx.execute(
247246
sql: "DELETE FROM \(TODOS_TABLE) WHERE id = ?",
248247
parameters: [id]

Sources/PowerSync/Kotlin/KotlinPowerSyncDatabaseImpl.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol,
4545
}
4646

4747
func connect(
48-
connector: PowerSyncBackendConnector,
48+
connector: PowerSyncBackendConnectorProtocol,
4949
options: ConnectOptions?
5050
) async throws {
5151
let connectorAdapter = PowerSyncBackendConnectorAdapter(

Sources/PowerSync/Kotlin/PowerSyncBackendConnectorAdapter.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ final class PowerSyncBackendConnectorAdapter: KotlinPowerSyncBackendConnector,
44
// We need to declare this since we declared KotlinPowerSyncBackendConnector as @unchecked Sendable
55
@unchecked Sendable
66
{
7-
let swiftBackendConnector: PowerSyncBackendConnector
7+
let swiftBackendConnector: PowerSyncBackendConnectorProtocol
88
let db: any PowerSyncDatabaseProtocol
99
let logTag = "PowerSyncBackendConnector"
1010

1111
init(
12-
swiftBackendConnector: PowerSyncBackendConnector,
12+
swiftBackendConnector: PowerSyncBackendConnectorProtocol,
1313
db: any PowerSyncDatabaseProtocol
1414
) {
1515
self.swiftBackendConnector = swiftBackendConnector

Sources/PowerSync/Protocol/PowerSyncBackendConnector.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2+
/// Implement this to connect an app backend.
3+
///
4+
/// The connector is responsible for:
5+
/// 1. Creating credentials for connecting to the PowerSync service.
6+
/// 2. Applying local changes against the backend application server.
7+
///
18
public protocol PowerSyncBackendConnectorProtocol: Sendable {
29
///
310
/// Get credentials for PowerSync.
@@ -22,14 +29,11 @@ public protocol PowerSyncBackendConnectorProtocol: Sendable {
2229
func uploadData(database: PowerSyncDatabaseProtocol) async throws
2330
}
2431

25-
/// Implement this to connect an app backend.
26-
///
27-
/// The connector is responsible for:
28-
/// 1. Creating credentials for connecting to the PowerSync service.
29-
/// 2. Applying local changes against the backend application server.
30-
///
31-
@MainActor // This class is non-final, we can use actor isolation to make it Sendable
32-
open class PowerSyncBackendConnector: PowerSyncBackendConnectorProtocol {
32+
@available(*, deprecated, message: "PowerSyncBackendConnector is deprecated. Please implement PowerSyncBackendConnectorProtocol directly in your own class.")
33+
open class PowerSyncBackendConnector: PowerSyncBackendConnectorProtocol,
34+
// This class is non-final, implementations should strictly conform to Sendable
35+
@unchecked Sendable
36+
{
3337
public init() {}
3438

3539
open func fetchCredentials() async throws -> PowerSyncCredentials? {

Sources/PowerSync/Protocol/PowerSyncDatabaseProtocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public protocol PowerSyncDatabaseProtocol: Queries, Sendable {
169169
///
170170
/// - Throws: An error if the connection fails or if the database is not properly configured.
171171
func connect(
172-
connector: PowerSyncBackendConnector,
172+
connector: PowerSyncBackendConnectorProtocol,
173173
options: ConnectOptions?
174174
) async throws
175175

0 commit comments

Comments
 (0)