Skip to content

Commit dfc4c3e

Browse files
committed
'Cancel' for PromiseKit
1 parent 1716ee6 commit dfc4c3e

File tree

4 files changed

+127
-1
lines changed

4 files changed

+127
-1
lines changed

Cartfile.resolved

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
github "mxcl/PromiseKit" "6.3.3"
1+
github "mxcl/PromiseKit" "6.3.4"

Sources/MKDirections+Promise.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,35 @@ extension MKDirections {
2424
return Promise { calculateETA(completionHandler: $0.resolve) }
2525
}
2626
}
27+
28+
//////////////////////////////////////////////////////////// Cancellation
29+
30+
fileprivate class MKDirectionsTask: CancellableTask {
31+
let directions: MKDirections
32+
var cancelAttempted = false
33+
34+
init(_ directions: MKDirections) {
35+
self.directions = directions
36+
}
37+
38+
func cancel() {
39+
directions.cancel()
40+
cancelAttempted = true
41+
}
42+
43+
var isCancelled: Bool {
44+
return cancelAttempted && !directions.isCalculating
45+
}
46+
}
47+
48+
extension MKDirections {
49+
/// Begins calculating the requested route information asynchronously.
50+
public func calculateCC() -> CancellablePromise<MKDirectionsResponse> {
51+
return CancellablePromise(task: MKDirectionsTask(self)) { calculate(completionHandler: $0.resolve) }
52+
}
53+
54+
/// Begins calculating the requested travel-time information asynchronously.
55+
public func calculateETACC() -> CancellablePromise<MKETAResponse> {
56+
return CancellablePromise(task: MKDirectionsTask(self)) { calculateETA(completionHandler: $0.resolve) }
57+
}
58+
}

Sources/MKMapSnapshotter+Promise.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,30 @@ extension MKMapSnapshotter {
1919
return Promise { start(completionHandler: $0.resolve) }
2020
}
2121
}
22+
23+
//////////////////////////////////////////////////////////// Cancellation
24+
25+
fileprivate class MKMapSnapshotterTask: CancellableTask {
26+
let snapshotter: MKMapSnapshotter
27+
var cancelAttempted = false
28+
29+
init(_ snapshotter: MKMapSnapshotter) {
30+
self.snapshotter = snapshotter
31+
}
32+
33+
func cancel() {
34+
snapshotter.cancel()
35+
cancelAttempted = true
36+
}
37+
38+
var isCancelled: Bool {
39+
return cancelAttempted && !snapshotter.isLoading
40+
}
41+
}
42+
43+
extension MKMapSnapshotter {
44+
/// Starts generating the snapshot using the options set in this object.
45+
public func startCC() -> CancellablePromise<MKMapSnapshot> {
46+
return CancellablePromise(task: MKMapSnapshotterTask(self)) { start(completionHandler: $0.resolve) }
47+
}
48+
}

Tests/TestMapKit.swift

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,70 @@ class Test_MKSnapshotter_Swift: XCTestCase {
6161
waitForExpectations(timeout: 1, handler: nil)
6262
}
6363
}
64+
65+
//////////////////////////////////////////////////////////// Cancellation
66+
67+
extension Test_MKDirections_Swift {
68+
func test_cancel_directions_response() {
69+
let ex = expectation(description: "")
70+
71+
class MockDirections: MKDirections {
72+
override func calculate(completionHandler: @escaping MKDirectionsHandler) {
73+
completionHandler(MKDirectionsResponse(), nil)
74+
}
75+
}
76+
77+
let rq = MKDirectionsRequest()
78+
let directions = MockDirections(request: rq)
79+
80+
directions.calculateCC().done { _ in
81+
XCTFail()
82+
}.catch(policy: .allErrors) {
83+
$0.isCancelled ? ex.fulfill() : XCTFail()
84+
}.cancel()
85+
86+
waitForExpectations(timeout: 1, handler: nil)
87+
}
88+
89+
90+
func test_cancel_ETA_response() {
91+
let ex = expectation(description: "")
92+
93+
class MockDirections: MKDirections {
94+
override func calculateETA(completionHandler: @escaping MKETAHandler) {
95+
completionHandler(MKETAResponse(), nil)
96+
}
97+
}
98+
99+
let rq = MKDirectionsRequest()
100+
MockDirections(request: rq).calculateETACC().done { _ in
101+
XCTFail()
102+
}.catch(policy: .allErrors) {
103+
$0.isCancelled ? ex.fulfill() : XCTFail()
104+
}.cancel()
105+
106+
waitForExpectations(timeout: 1, handler: nil)
107+
}
108+
109+
}
110+
111+
extension Test_MKSnapshotter_Swift {
112+
func test_cancel() {
113+
let ex = expectation(description: "")
114+
115+
class MockSnapshotter: MKMapSnapshotter {
116+
override func start(completionHandler: @escaping MKMapSnapshotCompletionHandler) {
117+
completionHandler(MKMapSnapshot(), nil)
118+
}
119+
}
120+
121+
let snapshotter = MockSnapshotter()
122+
snapshotter.startCC().done { _ in
123+
XCTFail()
124+
}.catch(policy: .allErrors) {
125+
$0.isCancelled ? ex.fulfill() : XCTFail()
126+
}.cancel()
127+
128+
waitForExpectations(timeout: 1, handler: nil)
129+
}
130+
}

0 commit comments

Comments
 (0)