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 option 2 #2

Closed
wants to merge 4 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,2 +1,5 @@
github "mxcl/PromiseKit" ~> 6.0
#github "mxcl/PromiseKit" ~> 6.0
github "dougzilla32/PromiseKit" "PMKCancel"
github "BoltsFramework/Bolts-ObjC" ~> 1.9
#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,2 +1,3 @@
github "BoltsFramework/Bolts-ObjC" "1.9.0"
github "mxcl/PromiseKit" "6.3.3"
github "dougzilla32/Cancel" "1.0.0"
github "dougzilla32/PromiseKit" "a0217bd7b69af68237dcdeee0197e63259b9d445"
1 change: 1 addition & 0 deletions PMKBolts.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
inputPaths = (
PromiseKit,
Bolts,
PMKCancel,
);
name = "Embed Carthage Frameworks";
outputPaths = (
Expand Down
33 changes: 33 additions & 0 deletions Sources/BFTask+Promise.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if !PMKCocoaPods
import PMKCancel
import PromiseKit
#endif
import Bolts
Expand All @@ -25,3 +26,35 @@ extension Promise {
}
}
}

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

extension BFCancellationTokenSource: CancellableTask {
public var isCancelled: Bool {
return token.isCancellationRequested
}
}

extension CancellablePromise {
/**
The provided closure is executed when this promise is resolved.
*/
public func thenCC<U>(on q: DispatchQueue? = conf.Q.map, body: @escaping (T) -> BFTask<U>) -> CancellablePromise<U?> {
return then(on: q) { tee -> CancellablePromise<U?> in
let tokenSource = BFCancellationTokenSource()
let task = body(tee)
return CancellablePromise<U?>(task: tokenSource) { seal in
task.continueWith(block: { task in
if task.isCompleted {
seal.fulfill(task.result)
} else if let error = task.error {
seal.reject(error)
} else {
seal.reject(PMKError.invalidCallingConvention)
}
return nil
}, cancellationToken: tokenSource.token)
}
}
}
}
29 changes: 29 additions & 0 deletions Tests/TestBolts.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import PMKCancel
import PromiseKit
import PMKBolts
import XCTest
Expand All @@ -21,3 +22,31 @@ class TestBolts: XCTestCase {
waitForExpectations(timeout: 1)
}
}

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

extension TestBolts {
func testCancel() {
let ex = expectation(description: "")

let value = { NSString(string: "1") }
var task: BFTask<NSString>?

let p = firstly { () -> CancellablePromise<Void> in
return CancellablePromise()
}
p.thenCC { _ -> BFTask<NSString> in
task = BFTask(result: value())
p.cancel()
return task!
}.done { obj in
XCTAssertEqual(obj, value())
XCTFail()
}.catch(policy: .allErrors) {
$0.isCancelled ? ex.fulfill() : XCTFail()
}

waitForExpectations(timeout: 1)
}
}