Skip to content

Commit

Permalink
add when(fulfilled:) and when(resolved:) tuple arity functions fo…
Browse files Browse the repository at this point in the history
…r thenables and cancellable thenables

add an associated type R representing the result type in Thenable which improves when(resolved:) return type
  • Loading branch information
Skoti authored and mxcl committed Jul 14, 2021
1 parent 2ed4003 commit 7a65168
Show file tree
Hide file tree
Showing 11 changed files with 1,397 additions and 501 deletions.
35 changes: 24 additions & 11 deletions Sources/PromiseKit/Cancellation/CancellableThenable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,6 @@ public extension CancellableThenable {
}

public extension CancellableThenable {
/**
- Returns: The error with which this cancellable promise was rejected; `nil` if this promise is not rejected.
*/
var error: Error? {
return thenable.error
}

/**
- Returns: `true` if the cancellable promise has not yet resolved.
*/
Expand All @@ -324,10 +317,10 @@ public extension CancellableThenable {
}

/**
- Returns: `true` if the cancellable promise was rejected.
*/
var isRejected: Bool {
return thenable.isRejected
- Returns: The result with which this cancellable promise was resolved or `nil` if this cancellable promise is pending.
*/
var result: U.R? {
return thenable.result
}

/**
Expand All @@ -338,6 +331,22 @@ public extension CancellableThenable {
}
}

public extension CancellableThenable where U.R == Result<U.T, Error> {
/**
- Returns: The error with which this cancellable promise was rejected; `nil` if this promise is not rejected.
*/
var error: Error? {
return thenable.error
}

/**
- Returns: `true` if the cancellable promise was rejected.
*/
var isRejected: Bool {
return thenable.isRejected
}
}

public extension CancellableThenable where U.T: Sequence {
/**
`CancellablePromise<[U.T]>` => `U.T` -> `V` => `CancellablePromise<[V]>`
Expand Down Expand Up @@ -518,3 +527,7 @@ public extension CancellableThenable where U.T: Sequence, U.T.Iterator.Element:
return map(on: on) { $0.sorted() }
}
}

func asThenables<CT: CancellableThenable>(_ cancellableThenables: [CT]) -> [CT.U] {
cancellableThenables.map { $0.thenable }
}
5 changes: 2 additions & 3 deletions Sources/PromiseKit/Guarantee.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public final class Guarantee<T>: Thenable {
}

/// - See: `Thenable.result`
public var result: Result<T, Error>? {
public var result: T? {
switch box.inspect() {
case .pending:
return nil
case .resolved(let value):
return .success(value)
return value
}
}

Expand Down Expand Up @@ -163,7 +163,6 @@ public extension Guarantee {
any part of your chain may use. Like the main thread for example.
*/
func wait() -> T {

if Thread.isMainThread {
conf.logHandler(.waitOnMainThread)
}
Expand Down
13 changes: 13 additions & 0 deletions Sources/PromiseKit/Result+Utils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation

extension Result {

var error: Failure? {
switch self {
case let .failure(error):
return error
case .success:
return nil
}
}
}
55 changes: 29 additions & 26 deletions Sources/PromiseKit/Thenable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ import Dispatch
public protocol Thenable: AnyObject {
/// The type of the wrapped value
associatedtype T
/// The type of the result
associatedtype R

/// `pipe` is immediately executed when this `Thenable` is resolved
func pipe(to: @escaping(Result<T, Error>) -> Void)

/// The resolved result or nil if pending.
var result: Result<T, Error>? { get }
var result: R? { get }

/// The resolved result's value or nil if pending.
var value: T? { get }
}

public extension Thenable {

/**
The provided closure executes when this promise is fulfilled.
Expand Down Expand Up @@ -218,17 +224,10 @@ public extension Thenable {

public extension Thenable {
/**
- Returns: The error with which this promise was rejected; `nil` if this promise is not rejected.
- Returns: `true` if the promise was fulfilled.
*/
var error: Error? {
switch result {
case .none:
return nil
case .some(.success):
return nil
case .some(.failure(let error)):
return error
}
var isFulfilled: Bool {
return value != nil
}

/**
Expand All @@ -244,33 +243,37 @@ public extension Thenable {
var isResolved: Bool {
return !isPending
}
}

public extension Thenable where R == T {
/**
- Returns: `true` if the promise was fulfilled.
- Returns: The value with which this promise was fulfilled or `nil` if this promise is pending or rejected.
*/
var isFulfilled: Bool {
return value != nil
var value: T? {
return result
}
}

public extension Thenable where R == Result<Self.T, Error> {
/**
- Returns: The value with which this promise was fulfilled or `nil` if this promise is pending or rejected.
*/
var value: T? {
return try? result?.get()
}

/**
- Returns: `true` if the promise was rejected.
- Returns: `true` if this promise was rejected.
*/
var isRejected: Bool {
return error != nil
}

/**
- Returns: The value with which this promise was fulfilled or `nil` if this promise is pending or rejected.
- Returns: The error with which this promise was rejected; `nil` if this promise is not rejected.
*/
var value: T? {
switch result {
case .none:
return nil
case .some(.success(let value)):
return value
case .some(.failure):
return nil
}
var error: Error? {
return result?.error
}
}

Expand Down
Loading

0 comments on commit 7a65168

Please sign in to comment.