Skip to content
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
4 changes: 3 additions & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github "mxcl/PromiseKit" ~> 6.0
#github "mxcl/PromiseKit" ~> 6.0
github "dougzilla32/PromiseKit" "CoreCancel"

github "BoltsFramework/Bolts-ObjC" ~> 1.9
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github "BoltsFramework/Bolts-ObjC" "1.9.0"
github "mxcl/PromiseKit" "6.3.3"
github "dougzilla32/PromiseKit" "087b3cf470890ff9ea841212e2f3e285fecf3988"
21 changes: 19 additions & 2 deletions Sources/BFTask+Promise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ extension Promise {
*/
public func then<U>(on q: DispatchQueue? = conf.Q.map, body: @escaping (T) -> BFTask<U>) -> Promise<U?> {
return then(on: q) { tee -> Promise<U?> in
let tokenSource = BFCancellationTokenSource()
let task = body(tee)
return Promise<U?> { seal in
return Promise<U?>(cancellableTask: tokenSource) { seal in
task.continueWith(block: { task in
if task.isCompleted {
seal.fulfill(task.result)
Expand All @@ -20,8 +21,24 @@ extension Promise {
seal.reject(PMKError.invalidCallingConvention)
}
return nil
})
}, cancellationToken: tokenSource.token)
}
}
}
}

/// Extend BFCancellationTokenSource to be a CancellableTask
extension BFCancellationTokenSource: CancellableTask {
public var isCancelled: Bool {
return token.isCancellationRequested
}
}

extension CancellablePromise {
/**
The provided closure is executed when this cancellable promise is resolved.
*/
public func then<U>(on q: DispatchQueue? = conf.Q.map, body: @escaping (T) -> BFTask<U>) -> CancellablePromise<U?> {
return cancellable(promise.then(on: q, body: body), cancelContext: self.cancelContext)
}
}
28 changes: 28 additions & 0 deletions Tests/TestBolts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,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.then { _ -> 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)
}
}