Skip to content

Commit 9eb31d2

Browse files
committed
'Cancel' for PromiseKit --
* Eliminate duplicate code in the cancellable extensions * When using the 'cancellable' function with the extensions, ensure that the underlying task (if any) is always cancelled * When using the 'cancellable' function, call 'reject' on the underlying promise (wherever possible) if 'cancel' is invoked * Create cancellable wrappers for the extensions only for the cases where there is an underlying task that supports cancellation
1 parent 3331425 commit 9eb31d2

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

Sources/UIViewPropertyAnimator+Promise.swift

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,28 @@ import PromiseKit
44
import UIKit
55

66
@available(iOS 10, tvOS 10, *)
7-
public extension UIViewPropertyAnimator {
7+
public extension UIViewPropertyAnimator: CancellableTask {
88
func startAnimation(_: PMKNamespacer) -> Guarantee<UIViewAnimatingPosition> {
9-
return Guarantee {
9+
return Guarantee(cancellableTask: self) {
1010
addCompletion($0)
1111
startAnimation()
1212
}
1313
}
14-
}
15-
16-
//////////////////////////////////////////////////////////// Cancellation
17-
18-
@available(iOS 10, tvOS 10, *)
19-
extension UIViewPropertyAnimator: CancellableTask {
14+
2015
public func cancel() {
2116
stopAnimation(true)
2217
}
2318

2419
public var isCancelled: Bool {
2520
return (state == .inactive) && (fractionComplete < 1.0)
2621
}
27-
22+
}
23+
24+
//////////////////////////////////////////////////////////// Cancellable wrapper
25+
26+
@available(iOS 10, tvOS 10, *)
27+
extension UIViewPropertyAnimator {
2828
public func cancellableStartAnimation(_: PMKNamespacer) -> CancellablePromise<UIViewAnimatingPosition> {
29-
return CancellablePromise(task: self) {
30-
addCompletion($0.fulfill)
31-
startAnimation()
32-
}
29+
return cancellable(startAnimation(.promise))
3330
}
3431
}

0 commit comments

Comments
 (0)