-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSKProductsRequest+Promise.swift
More file actions
69 lines (56 loc) · 1.7 KB
/
SKProductsRequest+Promise.swift
File metadata and controls
69 lines (56 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import StoreKit
#if !PMKCocoaPods
import PromiseKit
#endif
/**
To import the `SKRequest` category:
use_frameworks!
pod "PromiseKit/StoreKit"
And then in your sources:
import PromiseKit
*/
extension SKProductsRequest {
/**
Sends the request to the Apple App Store.
- Returns: A promise that fulfills if the request succeeds.
- Note: cancelling this promise will cancel the underlying task
- SeeAlso: [Cancellation](http://promisekit.org/docs/)
*/
public func start(_: PMKNamespacer) -> Promise<SKProductsResponse> {
let proxy = SKDelegate(request: self)
delegate = proxy
proxy.retainCycle = proxy
start()
return proxy.promise
}
}
fileprivate class SKDelegate: NSObject, SKProductsRequestDelegate, CancellableTask {
let (promise, seal) = Promise<SKProductsResponse>.pending()
let request: SKRequest
var retainCycle: SKDelegate?
init(request: SKRequest) {
self.request = request
super.init()
promise.setCancellableTask(self, reject: seal.reject)
}
@objc fileprivate func request(_ request: SKRequest, didFailWithError error: Error) {
seal.reject(error)
retainCycle = nil
}
@objc fileprivate func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
seal.fulfill(response)
retainCycle = nil
}
var isCancelled = false
func cancel() {
request.cancel()
retainCycle = nil
isCancelled = true
}
}
// perhaps one day Apple will actually make their errors into Errors…
//extension SKError: CancellableError {
// public var isCancelled: Bool {
// return true
// }
//}