Skip to content

Commit ab861e4

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 60448b9 commit ab861e4

9 files changed

+28
-28
lines changed

Sources/NSNotificationCenter+Promise.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ extension NotificationCenter {
3636

3737
extension NotificationCenter {
3838
/// Observe the named notification once
39-
public func observeCC(once name: Notification.Name, object: Any? = nil) -> CancellablePromise<Notification> {
39+
public func cancellableObserve(once name: Notification.Name, object: Any? = nil) -> CancellablePromise<Notification> {
4040
let (promise, resolver) = CancellablePromise<Notification>.pending()
4141
#if !os(Linux)
4242
let id = addObserver(forName: name, object: object, queue: nil, using: resolver.fulfill)

Sources/NSObject+Promise.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ extension NSObject {
6464
- Warning: *Important* The promise must not outlive the object under observation.
6565
- SeeAlso: Apple’s KVO documentation.
6666
*/
67-
public func observeCC(_: PMKNamespacer, keyPath: String) -> CancellablePromise<Any?> {
67+
public func cancellableObserve(_: PMKNamespacer, keyPath: String) -> CancellablePromise<Any?> {
6868
var task: CancellableTask!
6969
var reject: ((Error) -> Void)!
7070

Sources/NSURLSession+Promise.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ extension URLSession {
220220
Example usage with explicit cancel context:
221221

222222
let context = firstly {
223-
URLSession.shared.dataTaskCC(.promise, with: rq)
223+
URLSession.shared.cancellableDataTask(.promise, with: rq)
224224
}.compactMap { data, _ in
225225
try JSONSerialization.jsonObject(with: data) as? [String: Any]
226226
}.then { json in
@@ -232,7 +232,7 @@ extension URLSession {
232232
Example usage with implicit cancel context:
233233

234234
let promise = firstly {
235-
URLSession.shared.dataTaskCC(.promise, with: rq)
235+
URLSession.shared.cancellableDataTask(.promise, with: rq)
236236
}.compactMap { data, _ in
237237
try JSONSerialization.jsonObject(with: data) as? [String: Any]
238238
}.then { json in
@@ -245,7 +245,7 @@ extension URLSession {
245245

246246
let context = firstly {
247247
let rq = OMGHTTPURLRQ.POST(url, json: parameters)
248-
URLSession.shared.dataTaskCC(.promise, with: rq)
248+
URLSession.shared.cancellableDataTask(.promise, with: rq)
249249
}.then { data, urlResponse in
250250
//…
251251
}.cancelContext
@@ -255,7 +255,7 @@ extension URLSession {
255255
We provide a convenience initializer for `String` specifically for this promise:
256256

257257
let context = firstly {
258-
URLSession.shared.dataTaskCC(.promise, with: rq)
258+
URLSession.shared.cancellableDataTask(.promise, with: rq)
259259
}.compactMap(String.init).then { string in
260260
// decoded per the string encoding specified by the server
261261
}.then { string in
@@ -267,7 +267,7 @@ extension URLSession {
267267
Other common types can be easily decoded using compactMap also:
268268

269269
let context = firstly {
270-
URLSession.shared.dataTaskCC(.promise, with: rq)
270+
URLSession.shared.cancellableDataTask(.promise, with: rq)
271271
}.compactMap {
272272
UIImage(data: $0)
273273
}.then {
@@ -280,7 +280,7 @@ extension URLSession {
280280
first as this will improve main thread performance when rendering the image:
281281

282282
let context = firstly {
283-
URLSession.shared.dataTaskCC(.promise, with: rq)
283+
URLSession.shared.cancellableDataTask(.promise, with: rq)
284284
}.compactMap(on: QoS.userInitiated) { data, _ in
285285
guard let img = UIImage(data: data) else { return nil }
286286
_ = cgImage?.dataProvider?.data
@@ -298,7 +298,7 @@ extension URLSession {
298298

299299
[OMGHTTPURLRQ]: https://github.com/mxcl/OMGHTTPURLRQ
300300
*/
301-
public func dataTaskCC(_: PMKNamespacer, with convertible: URLRequestConvertible) -> CancellablePromise<(data: Data, response: URLResponse)> {
301+
public func cancellableDataTask(_: PMKNamespacer, with convertible: URLRequestConvertible) -> CancellablePromise<(data: Data, response: URLResponse)> {
302302
var task: URLSessionTask!
303303
var reject: ((Error) -> Void)!
304304

@@ -313,7 +313,7 @@ extension URLSession {
313313
}
314314

315315
/// Wraps the (Data?, URLResponse?, Error?) response from URLSession.uploadTask(with:from:) as CancellablePromise<(Data,URLResponse)>
316-
public func uploadTaskCC(_: PMKNamespacer, with convertible: URLRequestConvertible, from data: Data) -> CancellablePromise<(data: Data, response: URLResponse)> {
316+
public func cancellableUploadTask(_: PMKNamespacer, with convertible: URLRequestConvertible, from data: Data) -> CancellablePromise<(data: Data, response: URLResponse)> {
317317
var task: URLSessionTask!
318318
var reject: ((Error) -> Void)!
319319

@@ -328,7 +328,7 @@ extension URLSession {
328328
}
329329

330330
/// Wraps the (Data?, URLResponse?, Error?) response from URLSession.uploadTask(with:fromFile:) as CancellablePromise<(Data,URLResponse)>
331-
public func uploadTaskCC(_: PMKNamespacer, with convertible: URLRequestConvertible, fromFile file: URL) -> CancellablePromise<(data: Data, response: URLResponse)> {
331+
public func cancellableUploadTask(_: PMKNamespacer, with convertible: URLRequestConvertible, fromFile file: URL) -> CancellablePromise<(data: Data, response: URLResponse)> {
332332
var task: URLSessionTask!
333333
var reject: ((Error) -> Void)!
334334

@@ -346,7 +346,7 @@ extension URLSession {
346346
Wraps the URLSesstionDownloadTask response from URLSession.downloadTask(with:) as CancellablePromise<(URL,URLResponse)>
347347
- Remark: we force a `to` parameter because Apple deletes the downloaded file immediately after the underyling completion handler returns.
348348
*/
349-
public func downloadTaskCC(_: PMKNamespacer, with convertible: URLRequestConvertible, to saveLocation: URL) -> CancellablePromise<(saveLocation: URL, response: URLResponse)> {
349+
public func cancellableDownloadTask(_: PMKNamespacer, with convertible: URLRequestConvertible, to saveLocation: URL) -> CancellablePromise<(saveLocation: URL, response: URLResponse)> {
350350
var task: URLSessionTask!
351351
var reject: ((Error) -> Void)!
352352

Sources/Process+Promise.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ extension Process {
164164
let proc = Process()
165165
proc.launchPath = "/bin/ls"
166166
proc.arguments = ["/bin"]
167-
let context = proc.launchCC(.promise).compactMap { std in
167+
let context = proc.cancellableLaunch(.promise).compactMap { std in
168168
String(data: std.out.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)
169169
}.then { stdout in
170170
print(str)
@@ -174,7 +174,7 @@ extension Process {
174174

175175
context.cancel()
176176
*/
177-
public func launchCC(_: PMKNamespacer) -> CancellablePromise<(out: Pipe, err: Pipe)> {
177+
public func cancellableLaunch(_: PMKNamespacer) -> CancellablePromise<(out: Pipe, err: Pipe)> {
178178
return CancellablePromise(task: self, self.launch(.promise))
179179
}
180180
}

Sources/afterlife.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private class GrimReaper: NSObject {
3131
- Returns: A cancellable promise that resolves when the provided object deallocates, and can be unregistered and rejected by calling 'cancel'
3232
- Important: The promise is not guarenteed to resolve immediately when the provided object is deallocated. So you cannot write code that depends on exact timing.
3333
*/
34-
public func afterCC(life object: NSObject) -> CancellablePromise<Void> {
34+
public func cancellableAfter(life object: NSObject) -> CancellablePromise<Void> {
3535
var reaper = objc_getAssociatedObject(object, &cancellableHandle) as? CancellableGrimReaper
3636
if reaper == nil {
3737
reaper = CancellableGrimReaper()

Tests/TestNSNotificationCenter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ extension NSNotificationCenterTests {
2828
let ex = expectation(description: "")
2929
let userInfo = ["a": 1]
3030

31-
NotificationCenter.default.observeCC(once: PMKTestNotification).done { value in
31+
NotificationCenter.default.cancellableObserve(once: PMKTestNotification).done { value in
3232
XCTFail()
3333
}.catch(policy: .allErrors) {
3434
$0.isCancelled ? ex.fulfill() : XCTFail()

Tests/TestNSObject.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ extension NSObjectTests {
8282
let ex = expectation(description: "")
8383

8484
let foo = Foo()
85-
foo.observeCC(.promise, keyPath: "bar").done { newValue in
85+
foo.cancellableObserve(.promise, keyPath: "bar").done { newValue in
8686
XCTAssertEqual(newValue as? String, "moo")
8787
XCTFail()
8888
// ex.fulfill()
@@ -98,7 +98,7 @@ extension NSObjectTests {
9898
let ex = expectation(description: "")
9999

100100
let foo = Foo()
101-
let p = foo.observeCC(.promise, keyPath: "bar").done { newValue in
101+
let p = foo.cancellableObserve(.promise, keyPath: "bar").done { newValue in
102102
XCTAssertEqual(newValue as? String, "moo")
103103
XCTFail()
104104
// ex.fulfill()
@@ -119,7 +119,7 @@ extension NSObjectTests {
119119
var p: CancellableFinalizer!
120120
func innerScope() {
121121
killme = NSObject()
122-
p = afterCC(life: killme).done { _ in
122+
p = cancellableAfter(life: killme).done { _ in
123123
XCTFail()
124124
}.catch(policy: .allErrors) {
125125
$0.isCancelled ? ex.fulfill() : XCTFail()
@@ -146,12 +146,12 @@ extension NSObjectTests {
146146
var p1, p2: CancellableFinalizer!
147147
func innerScope() {
148148
killme = NSObject()
149-
p1 = afterCC(life: killme).done { _ in
149+
p1 = cancellableAfter(life: killme).done { _ in
150150
XCTFail()
151151
}.catch(policy: .allErrors) {
152152
$0.isCancelled ? ex1.fulfill() : XCTFail()
153153
}
154-
p2 = afterCC(life: killme).done { _ in
154+
p2 = cancellableAfter(life: killme).done { _ in
155155
XCTFail()
156156
}.catch(policy: .allErrors) {
157157
$0.isCancelled ? ex2.fulfill() : XCTFail()

Tests/TestNSTask.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ extension NSTaskTests {
5858
task.launchPath = "/usr/bin/man"
5959
task.arguments = ["ls"]
6060

61-
let context = task.launchCC(.promise).done { stdout, _ in
61+
let context = task.cancellableLaunch(.promise).done { stdout, _ in
6262
let stdout = String(data: stdout.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)
6363
XCTAssertEqual(stdout, "bar\n")
6464
}.catch(policy: .allErrors) { error in
@@ -76,7 +76,7 @@ extension NSTaskTests {
7676
task.launchPath = "/bin/ls"
7777
task.arguments = ["-l", dir]
7878

79-
let context = task.launchCC(.promise).done { _ in
79+
let context = task.cancellableLaunch(.promise).done { _ in
8080
XCTFail("failed to cancel process")
8181
}.catch(policy: .allErrors) { error in
8282
error.isCancelled ? ex.fulfill() : XCTFail("unexpected error \(error)")

Tests/TestNSURLSession.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ extension NSURLSessionTests {
8888
let ex = expectation(description: "")
8989
let rq = URLRequest(url: URL(string: "http://example.com")!)
9090
let context = firstly {
91-
URLSession.shared.dataTaskCC(.promise, with: rq)
91+
URLSession.shared.cancellableDataTask(.promise, with: rq)
9292
}.compactMap {
9393
try JSONSerialization.jsonObject(with: $0.data) as? NSDictionary
9494
}.done { rsp in
@@ -115,8 +115,8 @@ extension NSURLSessionTests {
115115
let ex = expectation(description: "")
116116
let rq = URLRequest(url: URL(string: "http://example.com")!)
117117

118-
let context = afterCC(.milliseconds(100)).then {
119-
URLSession.shared.dataTaskCC(.promise, with: rq)
118+
let context = cancellable(after(.milliseconds(100))).then {
119+
URLSession.shared.cancellableDataTask(.promise, with: rq)
120120
}.done { x in
121121
XCTAssertEqual(x.data, dummy)
122122
ex.fulfill()
@@ -140,8 +140,8 @@ extension NSURLSessionTests {
140140
let ex = expectation(description: "")
141141
let rq = URLRequest(url: URL(string: "http://example.com")!)
142142

143-
let context = afterCC(.milliseconds(100)).then {
144-
URLSession.shared.dataTaskCC(.promise, with: rq)
143+
let context = cancellable(after(.milliseconds(100))).then {
144+
URLSession.shared.cancellableDataTask(.promise, with: rq)
145145
}.map(String.init).done {
146146
XCTAssertEqual($0, dummy)
147147
ex.fulfill()

0 commit comments

Comments
 (0)