Skip to content

Commit

Permalink
fix(postgrest): race condition when setting fetchOptions and execute …
Browse files Browse the repository at this point in the history
…method call (#325)
  • Loading branch information
grdsdev authored Apr 5, 2024
1 parent 1a7dad0 commit 97d1900
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
19 changes: 8 additions & 11 deletions Sources/PostgREST/PostgrestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ public class PostgrestBuilder: @unchecked Sendable {
public func execute(
options: FetchOptions = FetchOptions()
) async throws -> PostgrestResponse<Void> {
mutableState.withValue {
$0.fetchOptions = options
}

return try await execute { _ in () }
try await execute(options: options) { _ in () }
}

/// Executes the request and returns a response of the specified type.
Expand All @@ -66,11 +62,7 @@ public class PostgrestBuilder: @unchecked Sendable {
public func execute<T: Decodable>(
options: FetchOptions = FetchOptions()
) async throws -> PostgrestResponse<T> {
mutableState.withValue {
$0.fetchOptions = options
}

return try await execute { [configuration] data in
try await execute(options: options) { [configuration] data in
do {
return try configuration.decoder.decode(T.self, from: data)
} catch {
Expand All @@ -80,8 +72,13 @@ public class PostgrestBuilder: @unchecked Sendable {
}
}

private func execute<T>(decode: (Data) throws -> T) async throws -> PostgrestResponse<T> {
private func execute<T>(
options: FetchOptions,
decode: (Data) throws -> T
) async throws -> PostgrestResponse<T> {
mutableState.withValue {
$0.fetchOptions = options

if $0.fetchOptions.head {
$0.request.method = .head
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/_Helpers/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ package struct Response: Sendable {
package let data: Data
package let response: HTTPURLResponse

public var statusCode: Int {
package var statusCode: Int {
response.statusCode
}

Expand Down

0 comments on commit 97d1900

Please # to comment.