Skip to content

'Cancel' for PromiseKit option 2 #6

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

Closed
wants to merge 3 commits into from
Closed
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
5 changes: 4 additions & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
github "mxcl/PromiseKit" ~> 6.0
#github "mxcl/PromiseKit" ~> 6.0
github "dougzilla32/PromiseKit" "PMKCancel"
#github "PromiseKit/Cancel" ~> 1.0
github "dougzilla32/Cancel" ~> 1.0
3 changes: 2 additions & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
github "mxcl/PromiseKit" "6.3.3"
github "dougzilla32/Cancel" "1.0.0"
github "dougzilla32/PromiseKit" "a0217bd7b69af68237dcdeee0197e63259b9d445"
1 change: 1 addition & 0 deletions PMKStoreKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@
);
inputPaths = (
PromiseKit,
PMKCancel,
);
name = "Embed Carthage Frameworks";
outputPaths = (
Expand Down
38 changes: 38 additions & 0 deletions Sources/SKProductsRequest+Promise.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import StoreKit
#if !PMKCocoaPods
import PMKCancel
import PromiseKit
#endif

Expand Down Expand Up @@ -50,3 +51,40 @@ fileprivate class SKDelegate: NSObject, SKProductsRequestDelegate {
// return true
// }
//}

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

extension SKProductsRequest {
/**
Sends the request to the Apple App Store.

- Returns: A cancellable promise that fulfills if the request succeeds.
*/
public func startCC(_: PMKNamespacer) -> CancellablePromise<SKProductsResponse> {
let proxy = SKDelegate()
delegate = proxy
proxy.retainCycle = proxy
let cp = CancellablePromise(task: SKRequestTask(self, proxy), promise: proxy.promise, resolver: proxy.seal)

start()
return cp
}
}

fileprivate class SKRequestTask: CancellableTask {
let request: SKRequest
let proxy: SKDelegate

init(_ request: SKRequest, _ proxy: SKDelegate) {
self.request = request
self.proxy = proxy
}

var isCancelled = false

func cancel() {
request.cancel()
proxy.retainCycle = nil
isCancelled = true
}
}
20 changes: 19 additions & 1 deletion Sources/SKReceiptRefreshRequest+Promise.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if !PMKCocoaPods
import PMKCancel
import PromiseKit
#endif
import StoreKit
Expand All @@ -9,7 +10,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 +33,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 promiseCC() -> CancellablePromise<SKReceiptRefreshRequest> {
let rro = ReceiptRefreshObserver(request: self)
return CancellablePromise(task: rro, promise: rro.promise, resolver: rro.seal)
}
}
31 changes: 31 additions & 0 deletions Tests/TestStoreKit.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import PMKCancel
import PMKStoreKit
import PromiseKit
import StoreKit
Expand All @@ -20,3 +21,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()
request.startCC(.promise).done { _ in
XCTFail()
}.catch(policy: .allErrors) {
$0.isCancelled ? ex.fulfill() : XCTFail()
}.cancel()
request.isCancelled = true

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