Skip to content

Commit 91f9ad9

Browse files
committed
Merge pull request #16 from ReactKit/chained-task-progress
Fix chained-task-progress.
2 parents 72ff341 + e3def10 commit 91f9ad9

File tree

3 files changed

+70
-35
lines changed

3 files changed

+70
-35
lines changed

SwiftTask/SwiftTask.swift

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -359,15 +359,33 @@ public class Task<Progress, Value, Error>: _Task<Error>
359359

360360
if initClosure == nil { return self }
361361

362-
var nextTask: Task = self
362+
let newTask = Task { [weak self] machine, progress, fulfill, _reject, configure in
363+
364+
var nextTask: Task = self!
363365

364-
for i in 1...maxTryCount-1 {
365-
nextTask = nextTask.failure { _ -> Task in
366-
return Task(weakified: weakified, _initClosure: initClosure!) // create a clone-task when rejected
366+
for i in 1...maxTryCount-1 {
367+
nextTask = nextTask.progress { _, progressValue in
368+
progress(progressValue)
369+
}.failure { _ -> Task in
370+
return Task(weakified: weakified, _initClosure: initClosure!) // create a clone-task when rejected
371+
}
372+
}
373+
374+
nextTask.progress { _, progressValue in
375+
progress(progressValue)
376+
}.success { value -> Void in
377+
fulfill(value)
378+
}.failure { errorInfo -> Void in
379+
_reject(errorInfo)
367380
}
381+
382+
configure.pause = { nextTask.pause(); return }
383+
configure.resume = { nextTask.resume(); return }
384+
configure.cancel = { nextTask.cancel(); return }
385+
368386
}
369387

370-
return nextTask
388+
return newTask
371389
}
372390

373391
///
@@ -455,11 +473,12 @@ public class Task<Progress, Value, Error>: _Task<Error>
455473
case .Rejected:
456474
bind(nil, self_.errorInfo!)
457475
default:
458-
self_.machine.addEventHandler(.Progress) { context in
459-
if let (_, progressValue) = context.userInfo as? Task<Progress2, Value2, Error>.ProgressTuple {
460-
progress(progressValue)
461-
}
462-
}
476+
// comment-out: only innerTask's progress should be sent to newTask
477+
// self_.machine.addEventHandler(.Progress) { context in
478+
// if let (_, progressValue) = context.userInfo as? Task<Progress2, Value2, Error>.ProgressTuple {
479+
// progress(progressValue)
480+
// }
481+
// }
463482
self_.machine.addEventHandler(.Fulfill) { context in
464483
if let value = context.userInfo as? Value {
465484
bind(value, nil)
@@ -535,11 +554,12 @@ public class Task<Progress, Value, Error>: _Task<Error>
535554
case .Rejected:
536555
_reject(self_.errorInfo!)
537556
default:
538-
self_.machine.addEventHandler(.Progress) { context in
539-
if let (_, progressValue) = context.userInfo as? Task<Progress2, Value2, Error>.ProgressTuple {
540-
progress(progressValue)
541-
}
542-
}
557+
// comment-out: only innerTask's progress should be sent to newTask
558+
// self_.machine.addEventHandler(.Progress) { context in
559+
// if let (_, progressValue) = context.userInfo as? Task<Progress2, Value2, Error>.ProgressTuple {
560+
// progress(progressValue)
561+
// }
562+
// }
543563
self_.machine.addEventHandler(.Fulfill) { context in
544564
if let value = context.userInfo as? Value {
545565
bind(value)
@@ -618,11 +638,12 @@ public class Task<Progress, Value, Error>: _Task<Error>
618638
let errorInfo = self_.errorInfo!
619639
bind(errorInfo)
620640
default:
621-
self_.machine.addEventHandler(.Progress) { context in
622-
if let (_, progressValue) = context.userInfo as? Task.ProgressTuple {
623-
progress(progressValue)
624-
}
625-
}
641+
// comment-out: only innerTask's progress should be sent to newTask
642+
// self_.machine.addEventHandler(.Progress) { context in
643+
// if let (_, progressValue) = context.userInfo as? Task.ProgressTuple {
644+
// progress(progressValue)
645+
// }
646+
// }
626647
self_.machine.addEventHandler(.Fulfill) { context in
627648
if let value = context.userInfo as? Value {
628649
fulfill(value)

SwiftTaskTests/AlamofireTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ class AlamofireTests: _TestCase
6060
return
6161
}
6262

63+
if response?.statusCode >= 300 {
64+
reject(NSError())
65+
}
66+
6367
fulfill("OK")
6468

6569
}

SwiftTaskTests/SwiftTaskTests.swift

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ class SwiftTaskTests: _TestCase
495495
return
496496
}.success { value -> Void in
497497
XCTAssertEqual(value, "OK")
498-
XCTAssertEqual(progressCount, 10, "`task3` should receive progresses from `task` & `task2`, so total `progressCount` should be 5 + 5 = 10 on task3-fulfilled.")
498+
XCTAssertEqual(progressCount, 5, "`task3` should receive progress only from `task2` but not from `task`.")
499499
expect.fulfill()
500500
}
501501

@@ -522,10 +522,10 @@ class SwiftTaskTests: _TestCase
522522
progressCount++
523523
println(progressValues)
524524
return
525-
}.success { value -> Void in
526-
XCTAssertEqual(value, "OK")
527-
XCTAssertEqual(progressCount, 10, "`task3` should receive progresses from `task` & `task2`, so total `progressCount` should be 5 + 5 = 10 on task3-fulfilled.")
528-
expect.fulfill()
525+
}.success { value -> Void in
526+
XCTAssertEqual(value, "OK")
527+
XCTAssertEqual(progressCount, 5, "`task3` should receive progress only from `task2` but not from `task`.")
528+
expect.fulfill()
529529
}
530530

531531
self.wait()
@@ -553,7 +553,7 @@ class SwiftTaskTests: _TestCase
553553
return
554554
}.success { value -> Void in
555555
XCTAssertEqual(value, "OK")
556-
XCTAssertEqual(progressCount, 10, "`task3` should receive progresses from `task` & `task2`, so total `progressCount` should be 5 + 5 = 10 on task3-fulfilled.")
556+
XCTAssertEqual(progressCount, 5, "`task3` should receive progress only from `task2` but not from `task`.")
557557
expect.fulfill()
558558
}
559559

@@ -768,10 +768,12 @@ class SwiftTaskTests: _TestCase
768768
var expect = self.expectationWithDescription(__FUNCTION__)
769769

770770
let task = self._interruptableTask()
771+
weak var innerTask: _InterruptableTask?
771772

772773
// chain async-task with then
773774
let task2 = task.then { [weak self] _ -> _InterruptableTask in
774-
return self!._interruptableTask()
775+
innerTask = self!._interruptableTask()
776+
return innerTask!
775777
}
776778

777779
task2.success { value -> Void in
@@ -785,8 +787,10 @@ class SwiftTaskTests: _TestCase
785787
// NOTE: parentTask should also be paused (if not, `task` will never be fulfilled/rejected)
786788
task2.pause()
787789

790+
XCTAssertNil(innerTask, "`innerTask` has not been created yet.")
791+
788792
XCTAssertEqual(task2.state, TaskState.Paused)
789-
XCTAssertTrue(task2.progress? == 0.5)
793+
XCTAssertNil(task2.progress, "`task2.progress` should be nil because `innerTask.progress()` has not been invoked yet.")
790794

791795
XCTAssertEqual(task.state, TaskState.Paused, "Parent task should also be paused.")
792796
XCTAssertTrue(task.progress? == 0.5)
@@ -795,7 +799,7 @@ class SwiftTaskTests: _TestCase
795799
Async.main(after: 0.3) {
796800

797801
XCTAssertEqual(task2.state, TaskState.Paused)
798-
XCTAssertTrue(task2.progress? == 0.5)
802+
XCTAssertNil(task2.progress, "`task2.progress` should still be nil.")
799803

800804
XCTAssertEqual(task.state, TaskState.Paused, "Parent task should also be paused.")
801805
XCTAssertTrue(task.progress? == 0.5)
@@ -818,10 +822,12 @@ class SwiftTaskTests: _TestCase
818822
var expect = self.expectationWithDescription(__FUNCTION__)
819823

820824
let task = self._interruptableTask()
825+
weak var innerTask: _InterruptableTask?
821826

822827
// chain async-task with success
823828
let task2 = task.success { [weak self] _ -> _InterruptableTask in
824-
return self!._interruptableTask()
829+
innerTask = self!._interruptableTask()
830+
return innerTask!
825831
}
826832

827833
task2.success { value -> Void in
@@ -836,16 +842,18 @@ class SwiftTaskTests: _TestCase
836842
task2.pause()
837843

838844
XCTAssertEqual(task2.state, TaskState.Paused)
839-
XCTAssertTrue(task2.progress? == 0.5)
845+
XCTAssertNil(task2.progress, "`task2.progress` should be nil because `innerTask.progress()` has not been invoked yet.")
840846

841847
XCTAssertEqual(task.state, TaskState.Paused, "Parent task should also be paused.")
842848
XCTAssertTrue(task.progress? == 0.5)
843849

844850
// resume after 0.3sec (t=0.6)
845851
Async.main(after: 0.3) {
846852

853+
XCTAssertNil(innerTask, "`innerTask` has not been created yet.")
854+
847855
XCTAssertEqual(task2.state, TaskState.Paused)
848-
XCTAssertTrue(task2.progress? == 0.5)
856+
XCTAssertNil(task2.progress, "`task2.progress` should still be nil.")
849857

850858
XCTAssertEqual(task.state, TaskState.Paused, "Parent task should also be paused.")
851859
XCTAssertTrue(task.progress? == 0.5)
@@ -868,10 +876,12 @@ class SwiftTaskTests: _TestCase
868876
var expect = self.expectationWithDescription(__FUNCTION__)
869877

870878
let task = self._interruptableTask()
879+
weak var innerTask: _InterruptableTask?
871880

872881
// chain async-task with failure
873882
let task2 = task.failure { [weak self] _ -> _InterruptableTask in
874-
return self!._interruptableTask()
883+
innerTask = self!._interruptableTask()
884+
return innerTask!
875885
}
876886

877887
task2.success { value -> Void in
@@ -886,7 +896,7 @@ class SwiftTaskTests: _TestCase
886896
task2.pause()
887897

888898
XCTAssertEqual(task2.state, TaskState.Paused)
889-
XCTAssertTrue(task2.progress? == 0.5)
899+
XCTAssertNil(task2.progress, "`task2.progress` should be nil because `innerTask.progress()` has not been invoked yet.")
890900

891901
XCTAssertEqual(task.state, TaskState.Paused, "Parent task should also be paused.")
892902
XCTAssertTrue(task.progress? == 0.5)
@@ -895,7 +905,7 @@ class SwiftTaskTests: _TestCase
895905
Async.main(after: 0.3) {
896906

897907
XCTAssertEqual(task2.state, TaskState.Paused)
898-
XCTAssertTrue(task2.progress? == 0.5)
908+
XCTAssertNil(task2.progress, "`task2.progress` should still be nil.")
899909

900910
XCTAssertEqual(task.state, TaskState.Paused, "Parent task should also be paused.")
901911
XCTAssertTrue(task.progress? == 0.5)

0 commit comments

Comments
 (0)