Skip to content

Commit

Permalink
Add Sendable conformance (#116)
Browse files Browse the repository at this point in the history
* added Sendable conformance

* added LossyArray sendable conformance

* updated request failure delegate

* fixed concurrency warnings

* small conformance additions

* finished conformance via the main actor additions

* returned removed Sendable in closure

* removed un-needed isolation flag

* self warnings solved

* updated based on convo with nigel

---------

Co-authored-by: Amy <[email protected]>
  • Loading branch information
amyoulton and Amy authored Feb 6, 2023
1 parent 6912b93 commit efdd8df
Show file tree
Hide file tree
Showing 18 changed files with 54 additions and 184 deletions.
2 changes: 1 addition & 1 deletion Netable/Example/Models/User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation
import Netable

struct User: Decodable {
struct User: Decodable, Sendable {
let firstName: String
let lastName: String
let location: String
Expand Down
6 changes: 3 additions & 3 deletions Netable/Example/Services/AuthNetworkService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
import Netable

class AuthNetworkService {
static var shared = AuthNetworkService()
static let shared = AuthNetworkService()

private let unauthNetable: Netable

Expand Down Expand Up @@ -65,13 +65,13 @@ class AuthNetworkService {
try await netable.request(GetPostsRequest())
}

func createPost(title: String, content: String) {
@MainActor func createPost(title: String, content: String) {
// this request is deliberately failing. Since there is a retry configuration set to the authNetable request,
// we are going to make use of `cancel()` to cancel the task after sending it so it doesn't try again.

let createRequest = Task {
do {
let result = try await netable.request(CreatePostRequest(parameters: CreatePostParameters(title: title, content: content)))
try await netable.request(CreatePostRequest(parameters: CreatePostParameters(title: title, content: content)))
} catch {
print("Create request error: \(error)")
}
Expand Down
14 changes: 9 additions & 5 deletions Netable/Example/Services/ErrorService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ import Combine
import Foundation
import Netable

class ErrorService {
static var shared = ErrorService()
@MainActor
final class ErrorService {
static let shared = ErrorService()

var errors = PassthroughSubject<NetableError?, Never>()
let errors = PassthroughSubject<NetableError?, Never>()
}


extension ErrorService: RequestFailureDelegate {
func requestDidFail<T>(_ request: T, error: NetableError) {
nonisolated func requestDidFail<T>(_ request: T, error: NetableError) {
if case let NetableError.httpError(statusCode, _) = error, statusCode == 401 {
return
}
errors.send(error)
Task { @MainActor in
errors.send(error)
}
}
}
2 changes: 1 addition & 1 deletion Netable/Example/Services/GraphQLNetworkService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import Netable

class GraphQLNetworkService {
static var shared = GraphQLNetworkService()
static let shared = GraphQLNetworkService()

private let netable = Netable(baseURL: URL(string: "http://localhost:8080/graphql")!)

Expand Down
12 changes: 3 additions & 9 deletions Netable/Example/Services/NetworkService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,11 @@ import Foundation
import Netable

class SimpleNetworkService {
static var shared = SimpleNetworkService()
static let shared = SimpleNetworkService()

private let netable = Netable(baseURL: URL(string: "http://localhost:8080/")!)

func getVersion() {
Task {
do {
let version = try await netable.request(GetVersionRequest())
} catch {
print(error)
}
}
func getVersion() async throws {
try await netable.request(GetVersionRequest())
}
}
1 change: 1 addition & 0 deletions Netable/Example/ViewModels/GraphQLVM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Foundation

@MainActor
class GraphQLVM: ObservableObject {
@Published var posts: [Post]?
@Published var title: String = ""
Expand Down
1 change: 1 addition & 0 deletions Netable/Example/ViewModels/HomeVM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import Foundation
import Netable

@MainActor
class HomeVM: ObservableVM {
@Published var title: String = ""
@Published var content: String = ""
Expand Down
1 change: 1 addition & 0 deletions Netable/Example/ViewModels/LoginVM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import Combine
import Foundation

@MainActor
class LoginVM: ObservableVM {
@Published var user: User?
@Published var username: String = ""
Expand Down
1 change: 1 addition & 0 deletions Netable/Example/ViewModels/ObservableVM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Combine
import Foundation
import Netable

@MainActor
class ObservableVM: ObservableObject {

var cancellables: [AnyCancellable] = []
Expand Down
4 changes: 3 additions & 1 deletion Netable/Example/ViewModels/RootVM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class RootVM: ObservableVM {
}

func getVersion() {
SimpleNetworkService.shared.getVersion()
Task {
try await SimpleNetworkService.shared.getVersion()
}
}

func clearError() {
Expand Down
Loading

0 comments on commit efdd8df

Please sign in to comment.