Skip to content

Commit a290cbd

Browse files
committed
'Cancel' for PromiseKit --
* Delete CancellableGuarantee and all its usages * In PromiseKit Core: instead of using additional functions and methods with the 'CC' suffix, created a new 'cancellable' function to wrap any Promise or Guarantee * In the extensions: Rename methods with 'CC' suffix to 'cancellable' prefix (may revisit this) * Fix the CancellableCatchable 'recover' methods to properly handle cancellation errors * Added an 'afterTask' property to Guarantee, to keep track of the task for calls to 'after'. This way the timer can be appropriately cancelled if the returned Guarantee is passed to the new 'cancellable' function. (may revisit this) * Update all cancellable tests for the cancellable API changes
1 parent 300366c commit a290cbd

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

Sources/CLGeocoder+Promise.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,29 +76,29 @@ extension CLGeocoder {
7676

7777
extension CLGeocoder {
7878
/// Submits a reverse-geocoding request for the specified location.
79-
public func reverseGeocodeCC(location: CLLocation) -> CancellablePromise<[CLPlacemark]> {
79+
public func cancellableReverseGeocode(location: CLLocation) -> CancellablePromise<[CLPlacemark]> {
8080
return CancellablePromise { seal in
8181
reverseGeocodeLocation(location, completionHandler: seal.resolve)
8282
}
8383
}
8484

8585
/// Submits a forward-geocoding request using the specified address dictionary.
8686
@available(iOS, deprecated: 11.0)
87-
public func geocodeCC(_ addressDictionary: [String: String]) -> CancellablePromise<[CLPlacemark]> {
87+
public func cancellableGeocode(_ addressDictionary: [String: String]) -> CancellablePromise<[CLPlacemark]> {
8888
return CancellablePromise { seal in
8989
geocodeAddressDictionary(addressDictionary, completionHandler: seal.resolve)
9090
}
9191
}
9292

9393
/// Submits a forward-geocoding request using the specified address string.
94-
public func geocodeCC(_ addressString: String) -> CancellablePromise<[CLPlacemark]> {
94+
public func cancellableGeocode(_ addressString: String) -> CancellablePromise<[CLPlacemark]> {
9595
return CancellablePromise { seal in
9696
geocodeAddressString(addressString, completionHandler: seal.resolve)
9797
}
9898
}
9999

100100
/// Submits a forward-geocoding request using the specified address string within the specified region.
101-
public func geocodeCC(_ addressString: String, region: CLRegion?) -> CancellablePromise<[CLPlacemark]> {
101+
public func cancellableGeocode(_ addressString: String, region: CLRegion?) -> CancellablePromise<[CLPlacemark]> {
102102
return CancellablePromise { seal in
103103
geocodeAddressString(addressString, in: region, completionHandler: seal.resolve)
104104
}
@@ -107,15 +107,15 @@ extension CLGeocoder {
107107
#if !os(tvOS) && swift(>=3.2)
108108
/// Submits a forward-geocoding request using the specified postal address.
109109
@available(iOS 11.0, OSX 10.13, watchOS 4.0, *)
110-
public func geocodePostalAddressCC(_ postalAddress: CNPostalAddress) -> CancellablePromise<[CLPlacemark]> {
110+
public func cancellableGeocodePostalAddress(_ postalAddress: CNPostalAddress) -> CancellablePromise<[CLPlacemark]> {
111111
return CancellablePromise { seal in
112112
geocodePostalAddress(postalAddress, completionHandler: seal.resolve)
113113
}
114114
}
115115

116116
/// Submits a forward-geocoding requesting using the specified locale and postal address
117117
@available(iOS 11.0, OSX 10.13, watchOS 4.0, *)
118-
public func geocodePostalAddressCC(_ postalAddress: CNPostalAddress, preferredLocale locale: Locale?) -> CancellablePromise<[CLPlacemark]> {
118+
public func cancellableGeocodePostalAddress(_ postalAddress: CNPostalAddress, preferredLocale locale: Locale?) -> CancellablePromise<[CLPlacemark]> {
119119
return CancellablePromise { seal in
120120
geocodePostalAddress(postalAddress, preferredLocale: locale, completionHandler: seal.resolve)
121121
}

Sources/CLLocationManager+Promise.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ extension CLLocationManager {
324324
the provided block if it exists. If the block does not exist, simply return the
325325
last location.
326326
*/
327-
public class func requestLocationCC(authorizationType: RequestAuthorizationType = .automatic, satisfying block: ((CLLocation) -> Bool)? = nil) -> CancellablePromise<[CLLocation]> {
327+
public class func cancellableRequestLocation(authorizationType: RequestAuthorizationType = .automatic, satisfying block: ((CLLocation) -> Bool)? = nil) -> CancellablePromise<[CLLocation]> {
328328

329329
func std() -> CancellablePromise<[CLLocation]> {
330330
return CancellableLocationManager(satisfying: block).promise
@@ -434,15 +434,15 @@ extension CLLocationManager {
434434
- Note: This method will not perform upgrades from “when-in-use” to “always” unless you specify `.always` for the value of `type`.
435435
*/
436436
@available(iOS 8, tvOS 9, watchOS 2, *)
437-
public class func requestAuthorizationCC(type requestedAuthorizationType: RequestAuthorizationType = .automatic) -> CancellablePromise<CLAuthorizationStatus> {
437+
public class func cancellableRequestAuthorization(type requestedAuthorizationType: RequestAuthorizationType = .automatic) -> CancellablePromise<CLAuthorizationStatus> {
438438

439439
let currentStatus = CLLocationManager.authorizationStatus()
440440

441441
func std(type: PMKCLAuthorizationType) -> CancellablePromise<CLAuthorizationStatus> {
442442
if currentStatus == .notDetermined {
443443
return CancellableAuthorizationCatcher(type: type).promise
444444
} else {
445-
return .valueCC(currentStatus)
445+
return cancellable(Promise.value(currentStatus))
446446
}
447447
}
448448

@@ -453,7 +453,7 @@ extension CLLocationManager {
453453
case .notDetermined, .authorizedWhenInUse:
454454
return CancellableAuthorizationCatcher(type: .always).promise
455455
default:
456-
return .valueCC(currentStatus)
456+
return cancellable(Promise.value(currentStatus))
457457
}
458458
}
459459
#if PMKiOS11
@@ -480,7 +480,7 @@ extension CLLocationManager {
480480
return CancellableAuthorizationCatcher(type: .always).promise
481481
}
482482
} else {
483-
return .valueCC(currentStatus)
483+
return cancellable(Promise.value(currentStatus))
484484
}
485485
}
486486
}

Tests/CLGeocoderTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ extension CLGeocoderTests {
114114
}
115115

116116
let ex = expectation(description: "")
117-
MockGeocoder().reverseGeocodeCC(location: CLLocation()).done { _ in
117+
MockGeocoder().cancellableReverseGeocode(location: CLLocation()).done { _ in
118118
XCTFail("not cancelled")
119119
}.catch(policy: .allErrors) { error in
120120
error.isCancelled ? ex.fulfill() : XCTFail("error \(error)")
@@ -133,7 +133,7 @@ extension CLGeocoderTests {
133133
}
134134

135135
let ex = expectation(description: "")
136-
let context = MockGeocoder().geocodeCC([:]).done { _ in
136+
let context = MockGeocoder().cancellableGeocode([:]).done { _ in
137137
XCTFail("not cancelled")
138138
}.catch(policy: .allErrors) { error in
139139
error.isCancelled ? ex.fulfill() : XCTFail("error \(error)")
@@ -155,7 +155,7 @@ extension CLGeocoderTests {
155155
}
156156

157157
let ex = expectation(description: "")
158-
let p = MockGeocoder().geocodeCC("").done { _ in
158+
let p = MockGeocoder().cancellableGeocode("").done { _ in
159159
XCTFail("not cancelled")
160160
}.catch(policy: .allErrors) { error in
161161
error.isCancelled ? ex.fulfill() : XCTFail("error \(error)")
@@ -179,7 +179,7 @@ extension CLGeocoderTests {
179179
}
180180

181181
let ex = expectation(description: "")
182-
let p = MockGeocoder().geocodePostalAddressCC(CNPostalAddress()).done { _ in
182+
let p = MockGeocoder().cancellableGeocodePostalAddress(CNPostalAddress()).done { _ in
183183
XCTFail("not cancelled")
184184
}.catch(policy: .allErrors) { error in
185185
error.isCancelled ? ex.fulfill() : XCTFail("error \(error)")
@@ -202,7 +202,7 @@ extension CLGeocoderTests {
202202
}
203203

204204
let ex = expectation(description: "")
205-
let p = MockGeocoder().geocodePostalAddressCC(CNPostalAddress(), preferredLocale: nil).done { _ in
205+
let p = MockGeocoder().cancellableGeocodePostalAddress(CNPostalAddress(), preferredLocale: nil).done { _ in
206206
XCTFail("not cancelled")
207207
}.catch(policy: .allErrors) { error in
208208
error.isCancelled ? ex.fulfill() : XCTFail("error \(error)")

Tests/CLLocationManagerTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ extension Test_CLLocationManager_Swift {
5959
swizzle(CLLocationManager.self, #selector(CLLocationManager.authorizationStatus), isClassMethod: true) {
6060
let ex = expectation(description: "")
6161

62-
let p = CLLocationManager.requestLocationCC().done { _ in
62+
let p = CLLocationManager.cancellableRequestLocation().done { _ in
6363
XCTFail("not cancelled")
6464
}.catch(policy: .allErrors) { error in
6565
error.isCancelled ? ex.fulfill() : XCTFail("error \(error)")
@@ -80,7 +80,7 @@ extension Test_CLLocationManager_Swift {
8080
let block: ((CLLocation) -> Bool) = { location in
8181
return location.coordinate.latitude == dummy.last?.coordinate.latitude
8282
}
83-
let p = CLLocationManager.requestLocationCC(satisfying: block).done { _ in
83+
let p = CLLocationManager.cancellableRequestLocation(satisfying: block).done { _ in
8484
XCTFail("not cancelled")
8585
}.catch(policy: .allErrors) { error in
8686
error.isCancelled ? ex.fulfill() : XCTFail("error \(error)")
@@ -97,7 +97,7 @@ extension Test_CLLocationManager_Swift {
9797
func testCancel_requestAuthorization() {
9898
let ex = expectation(description: "")
9999

100-
let p = CLLocationManager.requestAuthorizationCC().done { _ in
100+
let p = CLLocationManager.cancellableRequestAuthorization().done { _ in
101101
XCTFail("not cancelled")
102102
}.catch(policy: .allErrors) { error in
103103
error.isCancelled ? ex.fulfill() : XCTFail("error \(error)")

0 commit comments

Comments
 (0)