Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(postgrest): race condition when setting fetchOptions and execute method call #325

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading