Skip to content

Commit 7acd511

Browse files
committed
Add progress(progressTupleClosure)
1 parent 361fba6 commit 7acd511

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

SwiftTask/SwiftTask.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,13 @@ public class Task<Progress, Value, Error>
9393

9494
internal var machine: Machine!
9595

96+
/// progress value
9697
public internal(set) var progress: Progress?
98+
99+
/// fulfilled value
97100
public internal(set) var value: Value?
101+
102+
/// rejected/cancelled tuple info
98103
public internal(set) var errorInfo: ErrorInfo?
99104

100105
public var state: TaskState
@@ -194,7 +199,8 @@ public class Task<Progress, Value, Error>
194199
}
195200

196201
// TODO: how to nest these inside StateMachine's initClosure? (using `self` is not permitted)
197-
self.machine.addEventHandler(.Progress, order: 90) { [weak self] context in
202+
// NOTE: use order > 100 (default) to let `progressTupleClosure(self.progress, newValue)` be invoked first before updating old `self.progress`
203+
self.machine.addEventHandler(.Progress, order: 110) { [weak self] context in
198204
if let progress = context.userInfo as? Progress {
199205
if let self_ = self {
200206
self_.progress = progress
@@ -272,6 +278,7 @@ public class Task<Progress, Value, Error>
272278
self._cancel(error: nil)
273279
}
274280

281+
/// progress + newValue only
275282
public func progress(progressClosure: Progress -> Void) -> Task
276283
{
277284
self.machine.addEventHandler(.Progress) { [weak self] context in
@@ -283,6 +290,20 @@ public class Task<Progress, Value, Error>
283290
return self
284291
}
285292

293+
/// progress + (oldValue, newValue)
294+
public func progress(progressTupleClosure: (oldValue: Progress?, newValue: Progress) -> Void) -> Task
295+
{
296+
self.machine.addEventHandler(.Progress) { [weak self] context in
297+
if let progress = context.userInfo as? Progress {
298+
if let self_ = self {
299+
progressTupleClosure(oldValue: self_.progress, newValue: progress)
300+
}
301+
}
302+
}
303+
304+
return self
305+
}
306+
286307
/// then (fulfilled & rejected) + returning value
287308
public func then<Value2>(thenClosure: (Value?, ErrorInfo?) -> Value2) -> Task<Progress, Value2, Error>
288309
{

SwiftTaskTests/SwiftTaskTests.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,50 @@ class SwiftTaskTests: _TestCase
478478
self.wait()
479479
}
480480

481+
func testProgressTuple()
482+
{
483+
var expect = self.expectationWithDescription(__FUNCTION__)
484+
var progressCount = 0
485+
486+
Task<Float, String, ErrorString> { (progress, fulfill, reject, configure) in
487+
488+
self.perform {
489+
progress(0.5)
490+
progress(1.0)
491+
fulfill("OK")
492+
}
493+
494+
}.progress { (oldValue: Float?, newValue: Float) in // progressTupleClosure
495+
496+
progressCount++
497+
498+
if self.isAsync {
499+
if !((oldValue == nil && newValue == 0.5) || (oldValue! == 0.5 && newValue == 1.0)) {
500+
XCTFail("Invalid progressTuple (\(oldValue), \(newValue)).")
501+
}
502+
}
503+
else {
504+
XCTFail("When isAsync=false, 1st task closure is already performed before registering this progress closure, so this closure should not be reached.")
505+
}
506+
507+
}.then { (value: String) -> Void in
508+
509+
XCTAssertEqual(value, "OK")
510+
511+
if self.isAsync {
512+
XCTAssertEqual(progressCount, 2)
513+
}
514+
else {
515+
XCTAssertLessThanOrEqual(progressCount, 0, "progressCount should be 0 because progress closure should not be invoked when isAsync=false")
516+
}
517+
518+
expect.fulfill()
519+
520+
}
521+
522+
self.wait()
523+
}
524+
481525
//--------------------------------------------------
482526
// MARK: - Cancel
483527
//--------------------------------------------------

0 commit comments

Comments
 (0)