Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Cancel' for PromiseKit #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
github "mxcl/PromiseKit" ~> 6.0
#github "mxcl/PromiseKit" ~> 6.0
github "dougzilla32/PromiseKit" "CoreCancel"
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "mxcl/PromiseKit" "6.3.3"
github "dougzilla32/PromiseKit" "087b3cf470890ff9ea841212e2f3e285fecf3988"
21 changes: 19 additions & 2 deletions Sources/SKProductsRequest+Promise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ 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()
let proxy = SKDelegate(request: self)
delegate = proxy
proxy.retainCycle = proxy
start()
Expand All @@ -29,10 +31,17 @@ extension SKProductsRequest {
}


fileprivate class SKDelegate: NSObject, SKProductsRequestDelegate {
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
Expand All @@ -42,6 +51,14 @@ fileprivate class SKDelegate: NSObject, SKProductsRequestDelegate {
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…
Expand Down
19 changes: 18 additions & 1 deletion Sources/SKReceiptRefreshRequest+Promise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extension SKReceiptRefreshRequest {
}
}

private class ReceiptRefreshObserver: NSObject, SKRequestDelegate {
private class ReceiptRefreshObserver: NSObject, SKRequestDelegate, CancellableTask {
let (promise, seal) = Promise<SKReceiptRefreshRequest>.pending()
let request: SKReceiptRefreshRequest
var retainCycle: ReceiptRefreshObserver?
Expand All @@ -32,4 +32,21 @@ private class ReceiptRefreshObserver: NSObject, SKRequestDelegate {
seal.reject(error)
retainCycle = nil
}

var isCancelled = false

func cancel() {
request.cancel()
retainCycle = nil
isCancelled = true
}
}

//////////////////////////////////////////////////////////// Cancellation

extension SKReceiptRefreshRequest {
public func cancellablePromise() -> CancellablePromise<SKReceiptRefreshRequest> {
let rro = ReceiptRefreshObserver(request: self)
return CancellablePromise(task: rro, promise: rro.promise, resolver: rro.seal)
}
}
30 changes: 30 additions & 0 deletions Tests/TestStoreKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,33 @@ class SKProductsRequestTests: XCTestCase {
waitForExpectations(timeout: 1, handler: nil)
}
}

//////////////////////////////////////////////////////////// Cancellation

extension SKProductsRequestTests {
func testCancel() {
class MockProductsRequest: SKProductsRequest {
var isCancelled = false

override func start() {
after(seconds: 0.1).done {
if !self.isCancelled {
self.delegate?.productsRequest(self, didReceive: SKProductsResponse())
}
}
}
}

let ex = expectation(description: "")

let request = MockProductsRequest()
cancellable(request.start(.promise)).done { _ in
XCTFail()
}.catch(policy: .allErrors) {
$0.isCancelled ? ex.fulfill() : XCTFail()
}.cancel()
request.isCancelled = true

waitForExpectations(timeout: 1, handler: nil)
}
}