Skip to content

Commit fb53850

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 29b28d2 commit fb53850

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

Sources/SKProductsRequest+Promise.swift

+19-28
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extension SKProductsRequest {
2020
- Returns: A promise that fulfills if the request succeeds.
2121
*/
2222
public func start(_: PMKNamespacer) -> Promise<SKProductsResponse> {
23-
let proxy = SKDelegate()
23+
let proxy = SKDelegate(request: self)
2424
delegate = proxy
2525
proxy.retainCycle = proxy
2626
start()
@@ -29,10 +29,17 @@ extension SKProductsRequest {
2929
}
3030

3131

32-
fileprivate class SKDelegate: NSObject, SKProductsRequestDelegate {
32+
fileprivate class SKDelegate: NSObject, SKProductsRequestDelegate, CancellableTask {
3333
let (promise, seal) = Promise<SKProductsResponse>.pending()
34+
let request: SKRequest
3435
var retainCycle: SKDelegate?
3536

37+
init(request: SKRequest) {
38+
self.request = request
39+
super.init()
40+
promise.setCancellableTask(self, reject: seal.reject)
41+
}
42+
3643
@objc fileprivate func request(_ request: SKRequest, didFailWithError error: Error) {
3744
seal.reject(error)
3845
retainCycle = nil
@@ -42,6 +49,14 @@ fileprivate class SKDelegate: NSObject, SKProductsRequestDelegate {
4249
seal.fulfill(response)
4350
retainCycle = nil
4451
}
52+
53+
var isCancelled = false
54+
55+
func cancel() {
56+
request.cancel()
57+
retainCycle = nil
58+
isCancelled = true
59+
}
4560
}
4661

4762
// perhaps one day Apple will actually make their errors into Errors…
@@ -51,7 +66,7 @@ fileprivate class SKDelegate: NSObject, SKProductsRequestDelegate {
5166
// }
5267
//}
5368

54-
//////////////////////////////////////////////////////////// Cancellation
69+
//////////////////////////////////////////////////////////// Cancellable wrapper
5570

5671
extension SKProductsRequest {
5772
/**
@@ -60,30 +75,6 @@ extension SKProductsRequest {
6075
- Returns: A cancellable promise that fulfills if the request succeeds.
6176
*/
6277
public func cancellableStart(_: PMKNamespacer) -> CancellablePromise<SKProductsResponse> {
63-
let proxy = SKDelegate()
64-
delegate = proxy
65-
proxy.retainCycle = proxy
66-
let cp = CancellablePromise(task: SKRequestTask(self, proxy), promise: proxy.promise, resolver: proxy.seal)
67-
68-
start()
69-
return cp
70-
}
71-
}
72-
73-
fileprivate class SKRequestTask: CancellableTask {
74-
let request: SKRequest
75-
let proxy: SKDelegate
76-
77-
init(_ request: SKRequest, _ proxy: SKDelegate) {
78-
self.request = request
79-
self.proxy = proxy
80-
}
81-
82-
var isCancelled = false
83-
84-
func cancel() {
85-
request.cancel()
86-
proxy.retainCycle = nil
87-
isCancelled = true
78+
return cancellable(start(.promise))
8879
}
8980
}

0 commit comments

Comments
 (0)