Skip to content

Commit

Permalink
feat: update to latest kotlin and apply breaking changes (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicGBauer authored Jan 30, 2025
1 parent 419d2a2 commit 53227f5
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 108 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
# Changelog

## 1.0.0-Beta.3

* BREAKING CHANGE: Update underlying powersync-kotlin package to BETA18.0 which requires transactions to become synchronous as opposed to asynchronous.
```swift
try await database.writeTransaction { transaction in
try await transaction.execute(
sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)",
parameters: ["1", "Test User", "[email protected]"]
)
}
```
to
```swift
try await database.writeTransaction { transaction in
transaction.execute( // <- This has become synchronous
sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)",
parameters: ["1", "Test User", "[email protected]"]
)
}
```

## 1.0.0-Beta.2

* Upgrade PowerSyncSqliteCore to 0.3.8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/powersync-ja/powersync-kotlin.git",
"state" : {
"revision" : "bedbece7be3576010830e0a7240979ec47de5526",
"version" : "1.0.0-BETA15.0"
"revision" : "074001ad7d02b2b70c77168cdf4958c08dd6121b",
"version" : "1.0.0-BETA18.0"
}
},
{
"identity" : "powersync-sqlite-core-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/powersync-ja/powersync-sqlite-core-swift.git",
"state" : {
"revision" : "a43d8c855a5461d0176135445eb6f425a3b38964",
"version" : "0.3.8"
"revision" : "5de629f7ddc649a1e89c64fde6113fe113fe14de",
"version" : "0.3.9"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Demo/PowerSyncExample/Components/AddListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct AddListView: View {
Section {
TextField("Name", text: $newList.name)
Button("Save") {
Task.detached {
Task {
do {
try await system.insertList(newList)
await completion(.success(true))
Expand Down
2 changes: 1 addition & 1 deletion Demo/PowerSyncExample/Components/AddTodoListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct AddTodoListView: View {
Section {
TextField("Description", text: $newTodo.description)
Button("Save") {
Task.detached {
Task{
do {
try await system.insertTodo(newTodo, listId)
await completion(.success(true))
Expand Down
8 changes: 3 additions & 5 deletions Demo/PowerSyncExample/Components/ListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ struct ListView: View {
}
}
.task {
Task {
await system.watchLists { ls in
withAnimation {
self.lists = IdentifiedArrayOf(uniqueElements: ls)
}
await system.watchLists { ls in
withAnimation {
self.lists = IdentifiedArrayOf(uniqueElements: ls)
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions Demo/PowerSyncExample/Components/TodoListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ struct TodoListView: View {
}
}
.task {
Task {
await system.watchTodos(listId) { tds in
withAnimation {
self.todos = IdentifiedArrayOf(uniqueElements: tds)
}
await system.watchTodos(listId) { tds in
withAnimation {
self.todos = IdentifiedArrayOf(uniqueElements: tds)
}
}
}
Expand Down
21 changes: 9 additions & 12 deletions Demo/PowerSyncExample/PowerSync/SystemManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class SystemManager {
func openDb() {
db = PowerSyncDatabase(schema: schema, dbFilename: "powersync-swift.sqlite")
}

func connect() async {
do {
try await db.connect(connector: connector)
Expand Down Expand Up @@ -58,12 +58,12 @@ class SystemManager {
}

func deleteList(id: String) async throws {
try await db.writeTransaction(callback: { transaction in
_ = try await transaction.execute(
_ = try await db.writeTransaction(callback: { transaction in
_ = transaction.execute(
sql: "DELETE FROM \(LISTS_TABLE) WHERE id = ?",
parameters: [id]
)
_ = try await transaction.execute(
_ = transaction.execute(
sql: "DELETE FROM \(TODOS_TABLE) WHERE list_id = ?",
parameters: [id]
)
Expand Down Expand Up @@ -116,14 +116,11 @@ class SystemManager {
}

func deleteTodo(id: String) async throws {
try await db.writeTransaction(callback: { transaction in
_ = try await transaction.execute(
sql: "DELETE FROM \(TODOS_TABLE) WHERE id = ?",
parameters: [id]
)
return
_ = try await db.writeTransaction(callback: { transaction in
transaction.execute(
sql: "DELETE FROM \(TODOS_TABLE) WHERE id = ?",
parameters: [id]
)
})
}
}


8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/powersync-ja/powersync-kotlin.git",
"state" : {
"revision" : "bedbece7be3576010830e0a7240979ec47de5526",
"version" : "1.0.0-BETA15.0"
"revision" : "074001ad7d02b2b70c77168cdf4958c08dd6121b",
"version" : "1.0.0-BETA18.0"
}
},
{
"identity" : "powersync-sqlite-core-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/powersync-ja/powersync-sqlite-core-swift.git",
"state" : {
"revision" : "a43d8c855a5461d0176135445eb6f425a3b38964",
"version" : "0.3.8"
"revision" : "5de629f7ddc649a1e89c64fde6113fe113fe14de",
"version" : "0.3.9"
}
}
],
Expand Down
5 changes: 2 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ let package = Package(
name: packageName,
platforms: [
.iOS(.v13),
.macOS(.v10_13)
],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
Expand All @@ -17,8 +16,8 @@ let package = Package(
targets: ["PowerSync"]),
],
dependencies: [
.package(url: "https://github.com/powersync-ja/powersync-kotlin.git", exact: "1.0.0-BETA15.0"),
.package(url: "https://github.com/powersync-ja/powersync-sqlite-core-swift.git", "0.3.8"..<"0.4.0")
.package(url: "https://github.com/powersync-ja/powersync-kotlin.git", exact: "1.0.0-BETA18.0"),
.package(url: "https://github.com/powersync-ja/powersync-sqlite-core-swift.git", "0.3.9"..<"0.4.0")
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
Expand Down
33 changes: 5 additions & 28 deletions Sources/PowerSync/Kotlin/KotlinPowerSyncDatabaseImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,39 +122,16 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol {
}
}
}

public func writeTransaction<R>(callback: @escaping (any PowerSyncTransaction) async throws -> R) async throws -> R {
return try await kotlinDatabase.writeTransaction(callback: SuspendTaskWrapper { transaction in
return try await callback(transaction)
}) as! R

public func writeTransaction<R>(callback: @escaping (any PowerSyncTransaction) -> R) async throws -> R {
return try await kotlinDatabase.writeTransaction(callback: callback) as! R
}

public func readTransaction<R>(callback: @escaping (any PowerSyncTransaction) async throws -> R) async throws -> R {
return try await kotlinDatabase.writeTransaction(callback: SuspendTaskWrapper { transaction in
return try await callback(transaction)
}) as! R
public func readTransaction<R>(callback: @escaping (any PowerSyncTransaction) -> R) async throws -> R {
return try await kotlinDatabase.readTransaction(callback: callback) as! R
}
}

enum PowerSyncError: Error {
case invalidTransaction
}

class SuspendTaskWrapper: KotlinSuspendFunction1 {
let handle: (any PowerSyncTransaction) async throws -> Any

init(_ handle: @escaping (any PowerSyncTransaction) async throws -> Any) {
self.handle = handle
}

func __invoke(p1: Any?, completionHandler: @escaping (Any?, Error?) -> Void) {
Task {
do {
let result = try await self.handle(p1 as! any PowerSyncTransaction)
completionHandler(result, nil)
} catch {
completionHandler(nil, error)
}
}
}
}
14 changes: 7 additions & 7 deletions Sources/PowerSync/QueriesProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public protocol Queries {
/// Execute a write query (INSERT, UPDATE, DELETE)
/// Using `RETURNING *` will result in an error.
func execute(sql: String, parameters: [Any]?) async throws -> Int64

/// Execute a read-only (SELECT) query and return a single result.
/// If there is no result, throws an IllegalArgumentException.
/// See `getOptional` for queries where the result might be empty.
Expand All @@ -15,34 +15,34 @@ public protocol Queries {
parameters: [Any]?,
mapper: @escaping (SqlCursor) -> RowType
) async throws -> RowType

/// Execute a read-only (SELECT) query and return the results.
func getAll<RowType>(
sql: String,
parameters: [Any]?,
mapper: @escaping (SqlCursor) -> RowType
) async throws -> [RowType]

/// Execute a read-only (SELECT) query and return a single optional result.
func getOptional<RowType>(
sql: String,
parameters: [Any]?,
mapper: @escaping (SqlCursor) -> RowType
) async throws -> RowType?

/// Execute a read-only (SELECT) query every time the source tables are modified
/// and return the results as an array in a Publisher.
func watch<RowType>(
sql: String,
parameters: [Any]?,
mapper: @escaping (SqlCursor) -> RowType
) -> AsyncStream<[RowType]>

/// Execute a write transaction with the given callback
func writeTransaction<R>(callback: @escaping (any PowerSyncTransaction) async throws -> R) async throws -> R
func writeTransaction<R>(callback: @escaping (any PowerSyncTransaction) -> R) async throws -> R

/// Execute a read transaction with the given callback
func readTransaction<R>(callback: @escaping (any PowerSyncTransaction) async throws -> R) async throws -> R
func readTransaction<R>(callback: @escaping (any PowerSyncTransaction) -> R) async throws -> R
}

extension Queries {
Expand Down
Loading

0 comments on commit 53227f5

Please sign in to comment.