Skip to content

Commit

Permalink
fix: add completion-based future
Browse files Browse the repository at this point in the history
  • Loading branch information
sbertix committed Aug 27, 2021
1 parent 0fa17ce commit db7e6b5
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
25 changes: 25 additions & 0 deletions Sources/Requests/Receivable/Receivables+Future.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Receivables+Future.swift
// ComposableRequest
//
// Created by Stefano Bertagno on 27/08/21.
//

import Foundation

public extension Receivables {
/// A `struct` defining a future receivable.
struct Future<Requester: Requests.Requester, Success>: Receivable {
/// The completion handler.
public let completion: (@escaping (Result<Success, Error>) -> Void) -> Void

/// Init.
///
/// - parameters:
/// - requester: A valid `Requester`.
/// - completion: A valid completion handler.
public init(with requester: Requester, _ completion: @escaping (@escaping (Result<Success, Error>) -> Void) -> Void) {
self.completion = completion
}
}
}
15 changes: 15 additions & 0 deletions Sources/Requests/Requester/Async/URLSessionAsyncReceivable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ where Parent: URLSessionAsyncReceivable {
}
}

@available(iOS 15, macOS 12, watchOS 8, tvOS 15, *)
extension Receivables.Future: URLSessionAsyncReceivable, URLSessionAsyncMockReceivable
where Requester.Output: URLSessionAsyncReceivable {
// swiftlint:disable empty_parentheses_with_trailing_closure
/// The underlying response.
public var response: URLSessionAsyncRequester.Response<Success> {
.init(priority: nil) {
try await withCheckedContinuation { resolve in
self.completion() { resolve.resume(returning: $0) }
}.get()
}
}
// swiftlint:enable empty_parentheses_with_trailing_closure
}

@available(iOS 15, macOS 12, watchOS 8, tvOS 15, *)
extension Receivables.If: URLSessionAsyncReceivable, URLSessionAsyncMockReceivable
where O1: URLSessionAsyncReceivable, O2: URLSessionAsyncReceivable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ where Parent: URLSessionCombineReceivable {
}
}

@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *)
extension Receivables.Future: URLSessionCombineReceivable, URLSessionCombineMockReceivable
where Requester: URLSessionCombineReceivable {
/// The underlying response.
public var response: URLSessionCombineRequester.Response<Success> {
.init(publisher: Future { resolve in self.completion { resolve($0) } })
}
}

@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *)
extension Receivables.If: URLSessionCombineReceivable, URLSessionCombineMockReceivable
where O1: URLSessionCombineReceivable, O2: URLSessionCombineReceivable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ where Parent: URLSessionCompletionReceivable {
}
}

extension Receivables.Future: URLSessionCompletionReceivable, URLSessionCompletionMockReceivable
where Requester.Output: URLSessionCompletionReceivable {
/// The response.
public var response: URLSessionCompletionRequester.Response<Success> {
let promise = Promise<Success, Error>()
completion { promise.resolve(result: $0) }
return .init(future: promise.future)
}
}

extension Receivables.If: URLSessionCompletionReceivable, URLSessionCompletionMockReceivable
where O1: URLSessionCompletionReceivable, O2: URLSessionCompletionReceivable {
/// The response.
Expand Down Expand Up @@ -107,7 +117,7 @@ extension Receivables.Once: URLSessionCompletionReceivable, URLSessionCompletion
where Requester.Output: URLSessionCompletionReceivable {
/// The response.
public var response: URLSessionCompletionRequester.Response<Success> {
.init(task: nil, future: .init(result: result))
.init(future: .init(result: result))
}
}

Expand Down

0 comments on commit db7e6b5

Please sign in to comment.