Skip to content

Commit 4f112b2

Browse files
Avoid casting in kotlinWithSession
1 parent 5efbef4 commit 4f112b2

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

Sources/PowerSync/Kotlin/kotlinWithSession.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,32 @@ func kotlinWithSession<ReturnType>(
44
db: OpaquePointer,
55
action: @escaping () throws -> ReturnType,
66
) throws -> WithSessionResult<ReturnType> {
7+
var innerResult: ReturnType?
78
let baseResult = try withSession(
89
db: UnsafeMutableRawPointer(db),
910
block: {
1011
do {
11-
return try PowerSyncResult.Success(value: action())
12+
innerResult = try action()
13+
// We'll use the innerResult closure above to return the result
14+
return PowerSyncResult.Success(value: nil)
1215
} catch {
1316
return PowerSyncResult.Failure(exception: error.toPowerSyncError())
1417
}
1518
}
1619
)
1720

1821
var outputResult: Result<ReturnType, Error>
19-
switch baseResult.blockResult {
20-
case let success as PowerSyncResult.Success:
21-
do {
22-
let casted = try safeCast(success.value, to: ReturnType.self)
23-
outputResult = .success(casted)
24-
} catch {
25-
outputResult = .failure(error)
26-
}
27-
28-
case let failure as PowerSyncResult.Failure:
22+
if let failure = baseResult.blockResult as? PowerSyncResult.Failure {
2923
outputResult = .failure(failure.exception.asError())
30-
31-
default:
32-
outputResult = .failure(PowerSyncError.operationFailed(message: "Unknown error encountered when processing session"))
24+
} else if let result = innerResult {
25+
outputResult = .success(result)
26+
} else {
27+
// The return type is not nullable, so we should have a result
28+
outputResult = .failure(
29+
PowerSyncError.operationFailed(
30+
message: "Unknown error encountered when processing session",
31+
)
32+
)
3333
}
3434

3535
return WithSessionResult(

0 commit comments

Comments
 (0)