Skip to content

Commit 97d1900

Browse files
authored
fix(postgrest): race condition when setting fetchOptions and execute method call (#325)
1 parent 1a7dad0 commit 97d1900

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

Sources/PostgREST/PostgrestBuilder.swift

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@ public class PostgrestBuilder: @unchecked Sendable {
5151
public func execute(
5252
options: FetchOptions = FetchOptions()
5353
) async throws -> PostgrestResponse<Void> {
54-
mutableState.withValue {
55-
$0.fetchOptions = options
56-
}
57-
58-
return try await execute { _ in () }
54+
try await execute(options: options) { _ in () }
5955
}
6056

6157
/// Executes the request and returns a response of the specified type.
@@ -66,11 +62,7 @@ public class PostgrestBuilder: @unchecked Sendable {
6662
public func execute<T: Decodable>(
6763
options: FetchOptions = FetchOptions()
6864
) async throws -> PostgrestResponse<T> {
69-
mutableState.withValue {
70-
$0.fetchOptions = options
71-
}
72-
73-
return try await execute { [configuration] data in
65+
try await execute(options: options) { [configuration] data in
7466
do {
7567
return try configuration.decoder.decode(T.self, from: data)
7668
} catch {
@@ -80,8 +72,13 @@ public class PostgrestBuilder: @unchecked Sendable {
8072
}
8173
}
8274

83-
private func execute<T>(decode: (Data) throws -> T) async throws -> PostgrestResponse<T> {
75+
private func execute<T>(
76+
options: FetchOptions,
77+
decode: (Data) throws -> T
78+
) async throws -> PostgrestResponse<T> {
8479
mutableState.withValue {
80+
$0.fetchOptions = options
81+
8582
if $0.fetchOptions.head {
8683
$0.request.method = .head
8784
}

Sources/_Helpers/Request.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ package struct Response: Sendable {
186186
package let data: Data
187187
package let response: HTTPURLResponse
188188

189-
public var statusCode: Int {
189+
package var statusCode: Int {
190190
response.statusCode
191191
}
192192

0 commit comments

Comments
 (0)