Skip to content

Commit b824a21

Browse files
committed
'Cancel' for PromiseKit --
* Eliminate duplicate code in the cancellable extensions * When using the 'cancellable' function with the extensions, ensure that the underlying task (if any) is always cancelled * When using the 'cancellable' function, call 'reject' on the underlying promise (wherever possible) if 'cancel' is invoked * Create cancellable wrappers for the extensions only for the cases where there is an underlying task that supports cancellation
1 parent cad8bc8 commit b824a21

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

Sources/MKDirections+Promise.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@ import PromiseKit
1616
extension MKDirections {
1717
/// Begins calculating the requested route information asynchronously.
1818
public func calculate() -> Promise<MKDirectionsResponse> {
19-
return Promise { calculate(completionHandler: $0.resolve) }
19+
return Promise(cancellableTask: MKDirectionsTask(self)) { calculate(completionHandler: $0.resolve) }
2020
}
2121

2222
/// Begins calculating the requested travel-time information asynchronously.
2323
public func calculateETA() -> Promise<MKETAResponse> {
24-
return Promise { calculateETA(completionHandler: $0.resolve) }
24+
return Promise(cancellableTask: MKDirectionsTask(self)) { calculateETA(completionHandler: $0.resolve) }
2525
}
2626
}
2727

28-
//////////////////////////////////////////////////////////// Cancellation
29-
30-
fileprivate class MKDirectionsTask: CancellableTask {
28+
private class MKDirectionsTask: CancellableTask {
3129
let directions: MKDirections
3230
var cancelAttempted = false
3331

@@ -45,14 +43,16 @@ fileprivate class MKDirectionsTask: CancellableTask {
4543
}
4644
}
4745

46+
//////////////////////////////////////////////////////////// Cancellable wrappers
47+
4848
extension MKDirections {
4949
/// Begins calculating the requested route information asynchronously.
5050
public func cancellableCalculate() -> CancellablePromise<MKDirectionsResponse> {
51-
return CancellablePromise(task: MKDirectionsTask(self)) { calculate(completionHandler: $0.resolve) }
51+
return cancellable(calculate())
5252
}
5353

5454
/// Begins calculating the requested travel-time information asynchronously.
5555
public func cancellableCalculateETA() -> CancellablePromise<MKETAResponse> {
56-
return CancellablePromise(task: MKDirectionsTask(self)) { calculateETA(completionHandler: $0.resolve) }
56+
return cancellable(calculateETA())
5757
}
5858
}

Sources/MKMapSnapshotter+Promise.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@ import PromiseKit
1616
extension MKMapSnapshotter {
1717
/// Starts generating the snapshot using the options set in this object.
1818
public func start() -> Promise<MKMapSnapshot> {
19-
return Promise { start(completionHandler: $0.resolve) }
19+
return Promise(cancellableTask: MKMapSnapshotterTask(self)) { start(completionHandler: $0.resolve) }
2020
}
2121
}
2222

23-
//////////////////////////////////////////////////////////// Cancellation
24-
25-
fileprivate class MKMapSnapshotterTask: CancellableTask {
23+
private class MKMapSnapshotterTask: CancellableTask {
2624
let snapshotter: MKMapSnapshotter
2725
var cancelAttempted = false
2826

@@ -40,9 +38,11 @@ fileprivate class MKMapSnapshotterTask: CancellableTask {
4038
}
4139
}
4240

41+
//////////////////////////////////////////////////////////// Cancellable wrapper
42+
4343
extension MKMapSnapshotter {
4444
/// Starts generating the snapshot using the options set in this object.
4545
public func cancellableStart() -> CancellablePromise<MKMapSnapshot> {
46-
return CancellablePromise(task: MKMapSnapshotterTask(self)) { start(completionHandler: $0.resolve) }
46+
return cancellable(start())
4747
}
4848
}

0 commit comments

Comments
 (0)