-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
'Cancel' for PromiseKit -- provides the ability to cancel promises an…
…d promise chains
- Loading branch information
1 parent
1716ee6
commit c6de0ed
Showing
6 changed files
with
222 additions
and
49 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
github "mxcl/PromiseKit" ~> 6.0 | ||
#github "mxcl/PromiseKit" ~> 6.0 | ||
github "dougzilla32/PromiseKit" "CoreCancel" |
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 |
---|---|---|
@@ -1 +1 @@ | ||
github "mxcl/PromiseKit" "6.3.3" | ||
github "dougzilla32/PromiseKit" "087b3cf470890ff9ea841212e2f3e285fecf3988" |
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 |
---|---|---|
@@ -1,26 +1,82 @@ | ||
import MapKit | ||
#if !PMKCocoaPods | ||
import PromiseKit | ||
#endif | ||
|
||
/** | ||
To import the `MKDirections` category: | ||
|
||
use_frameworks! | ||
pod "PromiseKit/MapKit" | ||
|
||
And then in your sources: | ||
|
||
import PromiseKit | ||
*/ | ||
extension MKDirections { | ||
/// Begins calculating the requested route information asynchronously. | ||
public func calculate() -> Promise<MKDirectionsResponse> { | ||
return Promise { calculate(completionHandler: $0.resolve) } | ||
} | ||
|
||
/// Begins calculating the requested travel-time information asynchronously. | ||
public func calculateETA() -> Promise<MKETAResponse> { | ||
return Promise { calculateETA(completionHandler: $0.resolve) } | ||
} | ||
} | ||
import MapKit | ||
#if !PMKCocoaPods | ||
import PromiseKit | ||
#endif | ||
|
||
/** | ||
To import the `MKDirections` category: | ||
|
||
use_frameworks! | ||
pod "PromiseKit/MapKit" | ||
|
||
And then in your sources: | ||
|
||
import PromiseKit | ||
*/ | ||
extension MKDirections { | ||
#if swift(>=4.2) | ||
/// Begins calculating the requested route information asynchronously. | ||
public func calculate() -> Promise<Response> { | ||
return Promise<Response>(cancellableTask: MKDirectionsTask(self)) { calculate(completionHandler: $0.resolve) } | ||
} | ||
|
||
/// Begins calculating the requested travel-time information asynchronously. | ||
public func calculateETA() -> Promise<ETAResponse> { | ||
return Promise<ETAResponse>(cancellableTask: MKDirectionsTask(self)) { calculateETA(completionHandler: $0.resolve) } | ||
} | ||
#else | ||
/// Begins calculating the requested route information asynchronously. | ||
public func calculate() -> Promise<MKDirectionsResponse> { | ||
return Promise<MKDirectionsResponse>(cancellableTask: MKDirectionsTask(self)) { calculate(completionHandler: $0.resolve) } | ||
} | ||
|
||
/// Begins calculating the requested travel-time information asynchronously. | ||
public func calculateETA() -> Promise<MKETAResponse> { | ||
return Promise<MKETAResponse>(cancellableTask: MKDirectionsTask(self)) { calculateETA(completionHandler: $0.resolve) } | ||
} | ||
#endif | ||
} | ||
|
||
private class MKDirectionsTask: CancellableTask { | ||
let directions: MKDirections | ||
var cancelAttempted = false | ||
|
||
init(_ directions: MKDirections) { | ||
self.directions = directions | ||
} | ||
|
||
func cancel() { | ||
directions.cancel() | ||
cancelAttempted = true | ||
} | ||
|
||
var isCancelled: Bool { | ||
return cancelAttempted && !directions.isCalculating | ||
} | ||
} | ||
|
||
//////////////////////////////////////////////////////////// Cancellable wrappers | ||
|
||
extension MKDirections { | ||
#if swift(>=4.2) | ||
/// Begins calculating the requested route information asynchronously. | ||
public func cancellableCalculate() -> CancellablePromise<Response> { | ||
return cancellable(calculate()) | ||
} | ||
|
||
/// Begins calculating the requested travel-time information asynchronously. | ||
public func cancellableCalculateETA() -> CancellablePromise<ETAResponse> { | ||
return cancellable(calculateETA()) | ||
} | ||
#else | ||
/// Begins calculating the requested route information asynchronously. | ||
public func cancellableCalculate() -> CancellablePromise<MKDirectionsResponse> { | ||
return cancellable(calculate()) | ||
} | ||
|
||
/// Begins calculating the requested travel-time information asynchronously. | ||
public func cancellableCalculateETA() -> CancellablePromise<MKETAResponse> { | ||
return cancellable(calculateETA()) | ||
} | ||
#endif | ||
} |
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 |
---|---|---|
@@ -1,21 +1,62 @@ | ||
import MapKit | ||
#if !PMKCocoaPods | ||
import PromiseKit | ||
#endif | ||
|
||
/** | ||
To import the `MKMapSnapshotter` category: | ||
|
||
use_frameworks! | ||
pod "PromiseKit/MapKit" | ||
|
||
And then in your sources: | ||
|
||
import PromiseKit | ||
*/ | ||
extension MKMapSnapshotter { | ||
/// Starts generating the snapshot using the options set in this object. | ||
public func start() -> Promise<MKMapSnapshot> { | ||
return Promise { start(completionHandler: $0.resolve) } | ||
} | ||
} | ||
import MapKit | ||
#if !PMKCocoaPods | ||
import PromiseKit | ||
#endif | ||
|
||
/** | ||
To import the `MKMapSnapshotter` category: | ||
|
||
use_frameworks! | ||
pod "PromiseKit/MapKit" | ||
|
||
And then in your sources: | ||
|
||
import PromiseKit | ||
*/ | ||
extension MKMapSnapshotter { | ||
#if swift(>=4.2) | ||
/// Starts generating the snapshot using the options set in this object. | ||
public func start() -> Promise<Snapshot> { | ||
return Promise<Snapshot>(cancellableTask: MKMapSnapshotterTask(self)) { start(completionHandler: $0.resolve) } | ||
} | ||
#else | ||
/// Starts generating the snapshot using the options set in this object. | ||
public func start() -> Promise<MKMapSnapshot> { | ||
return Promise<MKMapSnapshot>(cancellableTask: MKMapSnapshotterTask(self)) { start(completionHandler: $0.resolve) } | ||
} | ||
#endif | ||
} | ||
|
||
private class MKMapSnapshotterTask: CancellableTask { | ||
let snapshotter: MKMapSnapshotter | ||
var cancelAttempted = false | ||
|
||
init(_ snapshotter: MKMapSnapshotter) { | ||
self.snapshotter = snapshotter | ||
} | ||
|
||
func cancel() { | ||
snapshotter.cancel() | ||
cancelAttempted = true | ||
} | ||
|
||
var isCancelled: Bool { | ||
return cancelAttempted && !snapshotter.isLoading | ||
} | ||
} | ||
|
||
//////////////////////////////////////////////////////////// Cancellable wrapper | ||
|
||
extension MKMapSnapshotter { | ||
#if swift(>=4.2) | ||
/// Starts generating the snapshot using the options set in this object. | ||
public func cancellableStart() -> CancellablePromise<Snapshot> { | ||
return cancellable(start()) | ||
} | ||
#else | ||
/// Starts generating the snapshot using the options set in this object. | ||
public func cancellableStart() -> CancellablePromise<MKMapSnapshot> { | ||
return cancellable(start()) | ||
} | ||
#endif | ||
} |
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