-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1045 from GarthSnyder/wrapper-revamp
Wrapper revamp
- Loading branch information
Showing
25 changed files
with
1,710 additions
and
1,350 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
100 changes: 59 additions & 41 deletions
100
Sources/CancellableCatchable.swift → ...s/Cancellation/CancellableCatchable.swift
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import Dispatch | ||
|
||
public extension _PMKCatchWrappers { | ||
|
||
/** | ||
The provided closure executes when this promise rejects. | ||
Rejecting a promise cascades: rejecting all subsequent promises (unless | ||
recover is invoked) thus you will typically place your catch at the end | ||
of a chain. Often utility promises will not have a catch, instead | ||
delegating the error handling to the caller. | ||
- Parameter on: The queue to which the provided closure dispatches. | ||
- Parameter flags: `DispatchWorkItemFlags` to be applied when dispatching. | ||
- Parameter policy: The default policy does not execute your handler for cancellation errors. | ||
- Parameter body: The handler to execute if this promise is rejected. | ||
- Returns: A promise finalizer. | ||
- SeeAlso: [Cancellation](http://https://github.com/mxcl/PromiseKit/blob/master/Documents/CommonPatterns.md#cancellation/docs/) | ||
*/ | ||
@discardableResult | ||
func `catch`(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, policy: CatchPolicy = conf.catchPolicy, _ body: @escaping(Error) -> Void) -> Finalizer { | ||
let dispatcher = selectDispatcher(given: on, configured: conf.D.return, flags: flags) | ||
return `catch`(on: dispatcher, policy: policy, body) | ||
} | ||
|
||
/** | ||
The provided closure executes when this promise rejects with the specific error passed in. A final `catch` is still required at the end of the chain. | ||
Rejecting a promise cascades: rejecting all subsequent promises (unless | ||
recover is invoked) thus you will typically place your catch at the end | ||
of a chain. Often utility promises will not have a catch, instead | ||
delegating the error handling to the caller. | ||
- Parameter only: The specific error to be caught and handled (e.g., `PMKError.emptySequence`). | ||
- Parameter on: The queue to which the provided closure dispatches. | ||
- Parameter flags: `DispatchWorkItemFlags` to be applied when dispatching. | ||
- Parameter body: The handler to execute if this promise is rejected with the provided error. | ||
- Note: Since this method handles only specific errors, supplying a `CatchPolicy` is unsupported. | ||
- SeeAlso: [Cancellation](http://promisekit.org/docs/) | ||
*/ | ||
func `catch`<E: Swift.Error>(only: E, on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ body: @escaping(E) -> Void) | ||
-> CascadingFinalizer where E: Equatable | ||
{ | ||
let dispatcher = selectDispatcher(given: on, configured: conf.D.return, flags: flags) | ||
return `catch`(only: only, on: dispatcher, body) | ||
} | ||
|
||
/** | ||
The provided closure executes when this promise rejects with an error of the type passed in. A final `catch` is still required at the end of the chain. | ||
Rejecting a promise cascades: rejecting all subsequent promises (unless | ||
recover is invoked) thus you will typically place your catch at the end | ||
of a chain. Often utility promises will not have a catch, instead | ||
delegating the error handling to the caller. | ||
- Parameter only: The error type to be caught and handled (e.g., `PMKError`). | ||
- Parameter on: The queue to which the provided closure dispatches. | ||
- Parameter flags: `DispatchWorkItemFlags` to be applied when dispatching. | ||
- Parameter policy: A `CatchPolicy` that further constrains the errors this handler will see. E.g., if | ||
you are receiving `PMKError` errors, do you want to see even those that result from cancellation? | ||
- Parameter body: The handler to execute if this promise is rejected with the provided error type. | ||
- SeeAlso: [Cancellation](http://promisekit.org/docs/) | ||
*/ | ||
func `catch`<E: Swift.Error>(only: E.Type, on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, | ||
policy: CatchPolicy = conf.catchPolicy, _ body: @escaping(E) -> Void) -> CascadingFinalizer | ||
{ | ||
let dispatcher = selectDispatcher(given: on, configured: conf.D.return, flags: flags) | ||
return `catch`(only: only, on: dispatcher, policy: policy, body) | ||
} | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import Dispatch | ||
|
||
public extension _PMKSharedWrappers { | ||
|
||
/** | ||
The provided closure executes when this promise resolves, whether it rejects or not. | ||
firstly { | ||
UIApplication.shared.networkActivityIndicatorVisible = true | ||
}.done { | ||
//… | ||
}.ensure { | ||
UIApplication.shared.networkActivityIndicatorVisible = false | ||
}.catch { | ||
//… | ||
} | ||
- Parameter on: The queue to which the provided closure dispatches. | ||
- Parameter flags: `DispatchWorkItemFlags` to be applied when dispatching. | ||
- Parameter body: The closure that executes when this promise resolves. | ||
- Returns: A new promise, resolved with this promise’s resolution. | ||
*/ | ||
func ensure(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ body: @escaping () -> Void) -> BaseOfT { | ||
let dispatcher = selectDispatcher(given: on, configured: conf.D.return, flags: flags) | ||
return ensure(on: dispatcher, body) | ||
} | ||
|
||
/** | ||
The provided closure executes when this promise resolves, whether it rejects or not. | ||
The chain waits on the returned `Guarantee<Void>`. | ||
firstly { | ||
setup() | ||
}.done { | ||
//… | ||
}.ensureThen { | ||
teardown() | ||
}.catch { | ||
//… | ||
} | ||
- Parameter on: The queue to which the provided closure dispatches. | ||
- Parameter flags: `DispatchWorkItemFlags` to be applied when dispatching. | ||
- Parameter body: The closure that executes when this promise resolves. | ||
- Returns: A new promise, resolved with this promise’s resolution. | ||
*/ | ||
func ensureThen(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ body: @escaping () -> VoidReturn) -> BaseOfT { | ||
let dispatcher = selectDispatcher(given: on, configured: conf.D.return, flags: flags) | ||
return ensureThen(on: dispatcher, body) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Dispatch | ||
|
||
public extension _PMKFinallyWrappers { | ||
/// `finally` is the same as `ensure`, but it is not chainable | ||
@discardableResult | ||
func finally(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ body: @escaping () -> Void) -> FinallyReturn { | ||
let dispatcher = selectDispatcher(given: on, configured: conf.D.return, flags: flags) | ||
return finally(on: dispatcher, body) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Since Guarantees have no error path, closures in the API are nonthrowing, which | ||
// makes them different from the shared Promise/CancellablePromise API. | ||
|
||
import Dispatch | ||
|
||
public extension Guarantee { | ||
|
||
@discardableResult | ||
func then<U>(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ body: @escaping(T) -> Guarantee<U>) -> Guarantee<U> { | ||
let dispatcher = selectDispatcher(given: on, configured: conf.D.map, flags: flags) | ||
return then(on: dispatcher, body) | ||
} | ||
|
||
func map<U>(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ body: @escaping(T) -> U) -> Guarantee<U> { | ||
let dispatcher = selectDispatcher(given: on, configured: conf.D.map, flags: flags) | ||
return map(on: dispatcher, body) | ||
} | ||
|
||
@discardableResult | ||
func done(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ body: @escaping(T) -> Void) -> Guarantee<Void> { | ||
let dispatcher = selectDispatcher(given: on, configured: conf.D.return, flags: flags) | ||
return done(on: dispatcher, body) | ||
} | ||
|
||
func get(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ body: @escaping (T) -> Void) -> Guarantee<T> { | ||
let dispatcher = selectDispatcher(given: on, configured: conf.D.return, flags: flags) | ||
return get(on: dispatcher, body) | ||
} | ||
} | ||
|
||
public extension Guarantee where T: Sequence { | ||
|
||
func thenMap<U>(on: DispatchQueue? = .pmkDefault, flags: DispatchWorkItemFlags? = nil, _ transform: @escaping(T.Iterator.Element) -> Guarantee<U>) -> Guarantee<[U]> { | ||
let dispatcher = selectDispatcher(given: on, configured: conf.D.map, flags: flags) | ||
return thenMap(on: dispatcher, transform) | ||
} | ||
|
||
} | ||
|
Oops, something went wrong.