Skip to content

Commit

Permalink
More Sendable types
Browse files Browse the repository at this point in the history
  • Loading branch information
lightsprint09 committed Mar 5, 2024
1 parent 801a125 commit d9c4dc4
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 54 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ let package = Package(
dependencies: [],
path: "Source",
swiftSettings: [
.enableExperimentalFeature("StrictConcurrencyComplete")
.enableExperimentalFeature("StrictConcurrency")
]
),
.testTarget(
Expand Down
2 changes: 1 addition & 1 deletion Source/HTTPMethod.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import Foundation

See [IETF document](https://tools.ietf.org/html/rfc7231#section-4.3)
*/
public enum HTTPMethod: String {
public enum HTTPMethod: String, Sendable {
case GET
case POST
case PUT
Expand Down
2 changes: 1 addition & 1 deletion Source/NetworkError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import Foundation

/// `NetworkError` provides a collection of error types which can occur during execution.
public enum NetworkError: Error {
public enum NetworkError: Error, Sendable {
/// The error is unkonw
case unknownError
/// The request was cancelled before it finished
Expand Down
2 changes: 1 addition & 1 deletion Source/NetworkServices/NetworkServiceMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public final class NetworkServiceMock: NetworkService, @unchecked Sendable {

*/
@MainActor
public func requestResultWithResponse<Success>(for resource: Resource<Success, NetworkError>) async -> Result<(Success, HTTPURLResponse), NetworkError> {
public func requestResultWithResponse<Success: Sendable>(for resource: Resource<Success, NetworkError>) async -> Result<(Success, HTTPURLResponse), NetworkError> {
lastRequests.append(resource.request)
if !responses.isEmpty {
let index = responses.firstIndex(where: {
Expand Down
2 changes: 1 addition & 1 deletion Source/Resource+Decodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extension Resource where Model: Decodable {
/// - request: The request to get the remote data payload
/// - decoder: a decoder which can decode the payload into the model type
/// - mapError: a closure which maps to Error
public init(request: URLRequest, decoder: JSONDecoder, mapError: @escaping (_ networkError: NetworkError) -> E) {
public init(request: URLRequest, decoder: JSONDecoder, mapError: @escaping @Sendable (_ networkError: NetworkError) -> E) {
self.init(request: request, parse: { try decoder.decode(Model.self, from: $0) }, mapError: mapError)
}
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Resource+Inspect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ extension Resource {
- parameter inspector: closure which gets passed the data
- returns: a new resource which gets instepcted before parsing
*/
public func inspectData(_ inspector: @escaping (Data) -> Void) -> Resource<Model, E> {
let parse: (Data) throws -> Model = { data in
public func inspectData(_ inspector: @escaping @Sendable (Data) -> Void) -> Resource<Model, E> {
let parse: @Sendable (Data) throws -> Model = { data in
inspector(data)
return try self.parse(data)
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Resource+Map.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension Resource {
///
/// - Parameter transform: transforms the original result of the resource
/// - Returns: the transformed resource
public func map<T>(transform: @escaping (Model) throws -> T) -> Resource<T, E> {
public func map<T>(transform: @escaping @Sendable (Model) throws -> T) -> Resource<T, E> {
return Resource<T, E>(
request: request,
parse: { return try transform(try self.parse($0)) },
Expand Down
2 changes: 1 addition & 1 deletion Source/Resource+Void.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public extension Resource where Model == Void {
/// - Parameters:
/// - request: The request to get the remote data payload
/// - mapError: a closure which maps to Error
init(request: URLRequest, mapError: @escaping (_ networkError: NetworkError) -> E) {
init(request: URLRequest, mapError: @escaping @Sendable (_ networkError: NetworkError) -> E) {
self.init(request: request, parse: { _ in }, mapError: mapError)
}
}
Expand Down
8 changes: 4 additions & 4 deletions Source/Resource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ import Foundation
})
```
*/
public struct Resource<Model, E: Error> {
public struct Resource<Model, E: Error>: Sendable {
/// The request to fetch the resource remote payload
public let request: URLRequest

/// Parses data into given model.
public let parse: (_ data: Data) throws -> Model
public let mapError: (_ networkError: NetworkError) -> E
public let parse: @Sendable (_ data: Data) throws -> Model
public let mapError: @Sendable (_ networkError: NetworkError) -> E

/// Creates a type safe resource, which can be used to fetch it with NetworkService
///
/// - Parameters:
/// - request: The request to get the remote data payload
/// - parse: Parses data fetched with the request into given Model

public init(request: URLRequest, parse: @escaping (Data) throws -> Model, mapError: @escaping (_ networkError: NetworkError) -> E) {
public init(request: URLRequest, parse: @escaping @Sendable (Data) throws -> Model, mapError: @escaping @Sendable (_ networkError: NetworkError) -> E) {
self.request = request
self.parse = parse
self.mapError = mapError
Expand Down
2 changes: 1 addition & 1 deletion Source/ResourceWithError+NetworkErrorConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation

public extension Resource where E: NetworkErrorConvertible {

init(request: URLRequest, parse: @escaping (Data) throws -> Model) {
init(request: URLRequest, parse: @escaping @Sendable (Data) throws -> Model) {
self.request = request
self.parse = parse
self.mapError = { E(networkError: $0) }
Expand Down
40 changes: 0 additions & 40 deletions Source/ResourceWithError.swift

This file was deleted.

0 comments on commit d9c4dc4

Please sign in to comment.