@@ -14,13 +14,69 @@ import PromiseKit
1414 import PromiseKit
1515*/
1616extension MKDirections {
17+ #if swift(>=4.2)
18+ /// Begins calculating the requested route information asynchronously.
19+ public func calculate( ) -> Promise < Response > {
20+ return Promise < Response > ( cancellableTask: MKDirectionsTask ( self ) ) { calculate ( completionHandler: $0. resolve) }
21+ }
22+
23+ /// Begins calculating the requested travel-time information asynchronously.
24+ public func calculateETA( ) -> Promise < ETAResponse > {
25+ return Promise < ETAResponse > ( cancellableTask: MKDirectionsTask ( self ) ) { calculateETA ( completionHandler: $0. resolve) }
26+ }
27+ #else
1728 /// Begins calculating the requested route information asynchronously.
1829 public func calculate( ) -> Promise < MKDirectionsResponse > {
19- return Promise { calculate ( completionHandler: $0. resolve) }
30+ return Promise < MKDirectionsResponse > ( cancellableTask : MKDirectionsTask ( self ) ) { calculate ( completionHandler: $0. resolve) }
2031 }
2132
2233 /// Begins calculating the requested travel-time information asynchronously.
2334 public func calculateETA( ) -> Promise < MKETAResponse > {
24- return Promise { calculateETA ( completionHandler: $0. resolve) }
35+ return Promise < MKETAResponse > ( cancellableTask : MKDirectionsTask ( self ) ) { calculateETA ( completionHandler: $0. resolve) }
2536 }
37+ #endif
2638}
39+
40+ private class MKDirectionsTask : CancellableTask {
41+ let directions : MKDirections
42+ var cancelAttempted = false
43+
44+ init ( _ directions: MKDirections ) {
45+ self . directions = directions
46+ }
47+
48+ func cancel( ) {
49+ directions. cancel ( )
50+ cancelAttempted = true
51+ }
52+
53+ var isCancelled : Bool {
54+ return cancelAttempted && !directions. isCalculating
55+ }
56+ }
57+
58+ //////////////////////////////////////////////////////////// Cancellable wrappers
59+
60+ extension MKDirections {
61+ #if swift(>=4.2)
62+ /// Begins calculating the requested route information asynchronously.
63+ public func cancellableCalculate( ) -> CancellablePromise < Response > {
64+ return cancellable ( calculate ( ) )
65+ }
66+
67+ /// Begins calculating the requested travel-time information asynchronously.
68+ public func cancellableCalculateETA( ) -> CancellablePromise < ETAResponse > {
69+ return cancellable ( calculateETA ( ) )
70+ }
71+ #else
72+ /// Begins calculating the requested route information asynchronously.
73+ public func cancellableCalculate( ) -> CancellablePromise < MKDirectionsResponse > {
74+ return cancellable ( calculate ( ) )
75+ }
76+
77+ /// Begins calculating the requested travel-time information asynchronously.
78+ public func cancellableCalculateETA( ) -> CancellablePromise < MKETAResponse > {
79+ return cancellable ( calculateETA ( ) )
80+ }
81+ #endif
82+ }
0 commit comments