Skip to content
Merged
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
61 changes: 41 additions & 20 deletions SwiftTask/SwiftTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,33 @@ public class Task<Progress, Value, Error>: _Task<Error>

if initClosure == nil { return self }

var nextTask: Task = self
let newTask = Task { [weak self] machine, progress, fulfill, _reject, configure in

var nextTask: Task = self!

for i in 1...maxTryCount-1 {
nextTask = nextTask.failure { _ -> Task in
return Task(weakified: weakified, _initClosure: initClosure!) // create a clone-task when rejected
for i in 1...maxTryCount-1 {
nextTask = nextTask.progress { _, progressValue in
progress(progressValue)
}.failure { _ -> Task in
return Task(weakified: weakified, _initClosure: initClosure!) // create a clone-task when rejected
}
}

nextTask.progress { _, progressValue in
progress(progressValue)
}.success { value -> Void in
fulfill(value)
}.failure { errorInfo -> Void in
_reject(errorInfo)
}

configure.pause = { nextTask.pause(); return }
configure.resume = { nextTask.resume(); return }
configure.cancel = { nextTask.cancel(); return }

}

return nextTask
return newTask
}

///
Expand Down Expand Up @@ -455,11 +473,12 @@ public class Task<Progress, Value, Error>: _Task<Error>
case .Rejected:
bind(nil, self_.errorInfo!)
default:
self_.machine.addEventHandler(.Progress) { context in
if let (_, progressValue) = context.userInfo as? Task<Progress2, Value2, Error>.ProgressTuple {
progress(progressValue)
}
}
// comment-out: only innerTask's progress should be sent to newTask
// self_.machine.addEventHandler(.Progress) { context in
// if let (_, progressValue) = context.userInfo as? Task<Progress2, Value2, Error>.ProgressTuple {
// progress(progressValue)
// }
// }
self_.machine.addEventHandler(.Fulfill) { context in
if let value = context.userInfo as? Value {
bind(value, nil)
Expand Down Expand Up @@ -535,11 +554,12 @@ public class Task<Progress, Value, Error>: _Task<Error>
case .Rejected:
_reject(self_.errorInfo!)
default:
self_.machine.addEventHandler(.Progress) { context in
if let (_, progressValue) = context.userInfo as? Task<Progress2, Value2, Error>.ProgressTuple {
progress(progressValue)
}
}
// comment-out: only innerTask's progress should be sent to newTask
// self_.machine.addEventHandler(.Progress) { context in
// if let (_, progressValue) = context.userInfo as? Task<Progress2, Value2, Error>.ProgressTuple {
// progress(progressValue)
// }
// }
self_.machine.addEventHandler(.Fulfill) { context in
if let value = context.userInfo as? Value {
bind(value)
Expand Down Expand Up @@ -618,11 +638,12 @@ public class Task<Progress, Value, Error>: _Task<Error>
let errorInfo = self_.errorInfo!
bind(errorInfo)
default:
self_.machine.addEventHandler(.Progress) { context in
if let (_, progressValue) = context.userInfo as? Task.ProgressTuple {
progress(progressValue)
}
}
// comment-out: only innerTask's progress should be sent to newTask
// self_.machine.addEventHandler(.Progress) { context in
// if let (_, progressValue) = context.userInfo as? Task.ProgressTuple {
// progress(progressValue)
// }
// }
self_.machine.addEventHandler(.Fulfill) { context in
if let value = context.userInfo as? Value {
fulfill(value)
Expand Down
4 changes: 4 additions & 0 deletions SwiftTaskTests/AlamofireTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class AlamofireTests: _TestCase
return
}

if response?.statusCode >= 300 {
reject(NSError())
}

fulfill("OK")

}
Expand Down
40 changes: 25 additions & 15 deletions SwiftTaskTests/SwiftTaskTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ class SwiftTaskTests: _TestCase
return
}.success { value -> Void in
XCTAssertEqual(value, "OK")
XCTAssertEqual(progressCount, 10, "`task3` should receive progresses from `task` & `task2`, so total `progressCount` should be 5 + 5 = 10 on task3-fulfilled.")
XCTAssertEqual(progressCount, 5, "`task3` should receive progress only from `task2` but not from `task`.")
expect.fulfill()
}

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

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

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

let task = self._interruptableTask()
weak var innerTask: _InterruptableTask?

// chain async-task with then
let task2 = task.then { [weak self] _ -> _InterruptableTask in
return self!._interruptableTask()
innerTask = self!._interruptableTask()
return innerTask!
}

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

XCTAssertNil(innerTask, "`innerTask` has not been created yet.")

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

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

XCTAssertEqual(task2.state, TaskState.Paused)
XCTAssertTrue(task2.progress? == 0.5)
XCTAssertNil(task2.progress, "`task2.progress` should still be nil.")

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

let task = self._interruptableTask()
weak var innerTask: _InterruptableTask?

// chain async-task with success
let task2 = task.success { [weak self] _ -> _InterruptableTask in
return self!._interruptableTask()
innerTask = self!._interruptableTask()
return innerTask!
}

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

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

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

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

XCTAssertNil(innerTask, "`innerTask` has not been created yet.")

XCTAssertEqual(task2.state, TaskState.Paused)
XCTAssertTrue(task2.progress? == 0.5)
XCTAssertNil(task2.progress, "`task2.progress` should still be nil.")

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

let task = self._interruptableTask()
weak var innerTask: _InterruptableTask?

// chain async-task with failure
let task2 = task.failure { [weak self] _ -> _InterruptableTask in
return self!._interruptableTask()
innerTask = self!._interruptableTask()
return innerTask!
}

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

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

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

XCTAssertEqual(task2.state, TaskState.Paused)
XCTAssertTrue(task2.progress? == 0.5)
XCTAssertNil(task2.progress, "`task2.progress` should still be nil.")

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