Skip to content

Commit 79dfe36

Browse files
committed
Rename onComplete/onSuccess/onFailure to then/success/failure.
1 parent 7e93d19 commit 79dfe36

File tree

6 files changed

+138
-140
lines changed

6 files changed

+138
-140
lines changed

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ SwiftTask
55

66
![SwiftTask](Screenshots/diagram.png)
77

8-
### Ver 2.0.0 Changelog
8+
### Ver 2.0.0 Changelog (2014/11/18)
99

10-
- `task.progress()` is renamed to `task.onProgress()`
11-
- `progressClosure`'s closure type changed from `Progress -> Void` to `(oldProgress: Progress?, newProgress: Progress) -> Void`
12-
- `task.then(thenClosure)` is renamed to `task.onComplete()`
13-
- `task.then(fulfilledClosure)` is renamed to `task.onSuccess()`
14-
- `task.catch(catchClosure)` is renamed to `task.onFailure()`
10+
- `task.progress()`'s `progressClosure` type changed from `Progress -> Void` to `(oldProgress: Progress?, newProgress: Progress) -> Void`
11+
- `task.then(fulfilledClosure)` is renamed to `task.success()`
12+
- `task.catch(catchClosure)` is renamed to `task.failure()`
13+
- `task.then()` is no longer used for fulfilled-only handling (this will improve Swift type-inference)
1514

1615

1716
## Example

SwiftTask/SwiftTask.swift

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public class Task<Progress, Value, Error>
199199
}
200200

201201
// TODO: how to nest these inside StateMachine's initClosure? (using `self` is not permitted)
202-
// NOTE: use order > 100 (default) to let `progressClosure(self.progress, newValue)` be invoked first before updating old `self.progress`
202+
// NOTE: use order > 100 (default) to let `progressClosure(self.progress, newProgress)` be invoked first before updating old `self.progress`
203203
self.machine.addEventHandler(.Progress, order: 110) { [weak self] context in
204204
if let progress = context.userInfo as? Progress {
205205
if let self_ = self {
@@ -278,33 +278,32 @@ public class Task<Progress, Value, Error>
278278
self._cancel(error: nil)
279279
}
280280

281-
/// task.onProgress
282-
public func onProgress(progressClosure: (oldValue: Progress?, newValue: Progress) -> Void) -> Task
281+
public func progress(progressClosure: (oldProgress: Progress?, newProgress: Progress) -> Void) -> Task
283282
{
284283
self.machine.addEventHandler(.Progress) { [weak self] context in
285284
if let progress = context.userInfo as? Progress {
286-
progressClosure(oldValue: self?.progress, newValue: progress)
285+
progressClosure(oldProgress: self?.progress, newProgress: progress)
287286
}
288287
}
289288

290289
return self
291290
}
292291

293-
/// onComplete (fulfilled & rejected) + closure returning value
294-
public func onComplete<Value2>(completeClosure: (Value?, ErrorInfo?) -> Value2) -> Task<Progress, Value2, Error>
292+
/// then (fulfilled & rejected) + closure returning value
293+
public func then<Value2>(thenClosure: (Value?, ErrorInfo?) -> Value2) -> Task<Progress, Value2, Error>
295294
{
296-
return self.onComplete { (value: Value?, errorInfo: ErrorInfo?) -> Task<Progress, Value2, Error> in
297-
return Task<Progress, Value2, Error>(value: completeClosure(value, errorInfo))
295+
return self.then { (value: Value?, errorInfo: ErrorInfo?) -> Task<Progress, Value2, Error> in
296+
return Task<Progress, Value2, Error>(value: thenClosure(value, errorInfo))
298297
}
299298
}
300299

301-
/// onComplete (fulfilled & rejected) + closure returning task
302-
public func onComplete<Progress2, Value2>(completeClosure: (Value?, ErrorInfo?) -> Task<Progress2, Value2, Error>) -> Task<Progress2, Value2, Error>
300+
/// then (fulfilled & rejected) + closure returning task
301+
public func then<Progress2, Value2>(thenClosure: (Value?, ErrorInfo?) -> Task<Progress2, Value2, Error>) -> Task<Progress2, Value2, Error>
303302
{
304303
let newTask = Task<Progress2, Value2, Error> { [weak self] (progress, fulfill, _reject: _RejectHandler, configure) in
305304

306305
let bind = { (value: Value?, errorInfo: ErrorInfo?) -> Void in
307-
let innerTask = completeClosure(value, errorInfo)
306+
let innerTask = thenClosure(value, errorInfo)
308307

309308
// NOTE: don't call `then` for innerTask, or recursive bindings may occur
310309
// Bad example: https://github.com/inamiy/SwiftTask/blob/e6085465c147fb2211fb2255c48929fcc07acd6d/SwiftTask/SwiftTask.swift#L312-L316
@@ -356,23 +355,23 @@ public class Task<Progress, Value, Error>
356355
return newTask
357356
}
358357

359-
/// onSuccess + closure returning value
360-
public func onSuccess<Value2>(fulfilledClosure: Value -> Value2) -> Task<Progress, Value2, Error>
358+
/// success (fulfilled) + closure returning value
359+
public func success<Value2>(fulfilledClosure: Value -> Value2) -> Task<Progress, Value2, Error>
361360
{
362-
return self.onSuccess { (value: Value) -> Task<Progress, Value2, Error> in
361+
return self.success { (value: Value) -> Task<Progress, Value2, Error> in
363362
return Task<Progress, Value2, Error>(value: fulfilledClosure(value))
364363
}
365364
}
366365

367-
/// onSuccess + closure returning task
368-
public func onSuccess<Progress2, Value2>(fulfilledClosure: Value -> Task<Progress2, Value2, Error>) -> Task<Progress2, Value2, Error>
366+
/// success (fulfilled) + closure returning task
367+
public func success<Progress2, Value2>(fulfilledClosure: Value -> Task<Progress2, Value2, Error>) -> Task<Progress2, Value2, Error>
369368
{
370369
let newTask = Task<Progress2, Value2, Error> { [weak self] (progress, fulfill, _reject: _RejectHandler, configure) in
371370

372371
let bind = { (value: Value) -> Void in
373372
let innerTask = fulfilledClosure(value)
374373

375-
innerTask.onComplete { (value: Value2?, errorInfo: ErrorInfo?) -> Void in
374+
innerTask.then { (value: Value2?, errorInfo: ErrorInfo?) -> Void in
376375
if let value = value {
377376
fulfill(value)
378377
}
@@ -411,23 +410,23 @@ public class Task<Progress, Value, Error>
411410
return newTask
412411
}
413412

414-
/// onFailure + closure returning value
415-
public func onFailure(failureClosure: ErrorInfo -> Value) -> Task
413+
/// failure (rejected) + closure returning value
414+
public func failure(failureClosure: ErrorInfo -> Value) -> Task
416415
{
417-
return self.onFailure { (errorInfo: ErrorInfo) -> Task in
416+
return self.failure { (errorInfo: ErrorInfo) -> Task in
418417
return Task(value: failureClosure(errorInfo))
419418
}
420419
}
421420

422-
/// onFailure + closure returning task
423-
public func onFailure(failureClosure: ErrorInfo -> Task) -> Task
421+
/// failure (rejected) + closure returning task
422+
public func failure(failureClosure: ErrorInfo -> Task) -> Task
424423
{
425424
let newTask = Task { [weak self] (progress, fulfill, _reject: _RejectHandler, configure) in
426425

427426
let bind = { (errorInfo: ErrorInfo) -> Void in
428427
let innerTask = failureClosure(errorInfo)
429428

430-
innerTask.onComplete { (value: Value?, errorInfo: ErrorInfo?) -> Void in
429+
innerTask.then { (value: Value?, errorInfo: ErrorInfo?) -> Void in
431430
if let value = value {
432431
fulfill(value)
433432
}
@@ -498,7 +497,7 @@ extension Task
498497
let totalCount = tasks.count
499498

500499
for task in tasks {
501-
task.onSuccess { (value: Value) -> Void in
500+
task.success { (value: Value) -> Void in
502501

503502
synchronized(self) {
504503
completedCount++
@@ -517,7 +516,7 @@ extension Task
517516
}
518517
}
519518

520-
}.onFailure { (errorInfo: ErrorInfo) -> Void in
519+
}.failure { (errorInfo: ErrorInfo) -> Void in
521520

522521
synchronized(self) {
523522
_reject(errorInfo)
@@ -545,7 +544,7 @@ extension Task
545544
let totalCount = tasks.count
546545

547546
for task in tasks {
548-
task.onSuccess { (value: Value) -> Void in
547+
task.success { (value: Value) -> Void in
549548

550549
synchronized(self) {
551550
completedCount++
@@ -557,7 +556,7 @@ extension Task
557556
}
558557
}
559558

560-
}.onFailure { (errorInfo: ErrorInfo) -> Void in
559+
}.failure { (errorInfo: ErrorInfo) -> Void in
561560

562561
synchronized(self) {
563562
rejectedCount++
@@ -589,7 +588,7 @@ extension Task
589588
let totalCount = tasks.count
590589

591590
for task in tasks {
592-
task.onComplete { (value: Value?, errorInfo: ErrorInfo?) -> Void in
591+
task.then { (value: Value?, errorInfo: ErrorInfo?) -> Void in
593592

594593
synchronized(self) {
595594
completedCount++

SwiftTaskTests/AlamofireTests.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class AlamofireTests: _TestCase
3939

4040
}
4141

42-
task.onComplete { (value: String) -> Void in
42+
task.then { (value: String) -> Void in
4343
XCTAssertEqual(value, "OK")
4444

4545
expect.fulfill()
@@ -74,11 +74,11 @@ class AlamofireTests: _TestCase
7474

7575
}
7676

77-
task.onComplete { (value: String?) -> Void in
77+
task.then { (value: String?) -> Void in
7878

7979
XCTFail("Should never reach here.")
8080

81-
}.onFailure { (error: NSError?, isCancelled: Bool) -> Void in
81+
}.failure { (error: NSError?, isCancelled: Bool) -> Void in
8282

8383
// println(error)
8484

@@ -123,14 +123,14 @@ class AlamofireTests: _TestCase
123123

124124
}
125125

126-
// set onProgress & onComplete
127-
task.onProgress { progress in
126+
// set progress & then
127+
task.progress { progress in
128128

129129
println("bytesWritten = \(progress.bytesWritten)")
130130
println("totalBytesWritten = \(progress.totalBytesWritten)")
131131
println("totalBytesExpectedToWrite = \(progress.totalBytesExpectedToWrite)")
132132

133-
}.onComplete { (value: String) -> Void in
133+
}.then { (value: String) -> Void in
134134

135135
XCTAssertEqual(value, "OK")
136136

@@ -173,7 +173,7 @@ class AlamofireTests: _TestCase
173173
}
174174

175175
// set then
176-
task.onComplete { (value: String) -> Void in
176+
task.then { (value: String) -> Void in
177177

178178
XCTAssertEqual(value, "OK")
179179
XCTAssertEqual(nsProgress.completedUnitCount, 50)
@@ -226,13 +226,13 @@ class AlamofireTests: _TestCase
226226
}
227227
}
228228

229-
} // end of 1st task definition (NOTE: don't chain with `onComplete` or `onFailure` for 1st task cancellation)
229+
} // end of 1st task definition (NOTE: don't chain with `then` or `failure` for 1st task cancellation)
230230

231-
task.onComplete { (value: String?) -> Void in
231+
task.then { (value: String?) -> Void in
232232

233233
XCTFail("Should never reach here because task is cancelled.")
234234

235-
}.onFailure { (error: NSError?, isCancelled: Bool) -> Void in
235+
}.failure { (error: NSError?, isCancelled: Bool) -> Void in
236236

237237
// println(error)
238238

SwiftTaskTests/BasicTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,21 @@ class BasicTests: _TestCase
3636

3737
}
3838

39-
task.onProgress { oldValue, newValue in
39+
task.progress { oldProgress, newProgress in
4040

41-
println("progress = \(newValue)")
41+
println("progress = \(newProgress)")
4242

43-
}.onSuccess { value -> String in // `task.onSuccess {...}` = JavaScript's `promise.then(onFulfilled)`
43+
}.success { value -> String in // `task.success {...}` = JavaScript's `promise.then(onFulfilled)`
4444

4545
XCTAssertEqual(value, "OK")
4646
return "Now OK"
4747

48-
}.onFailure { error, isCancelled -> String in // `task.onFailure {...}` = JavaScript's `promise.catch(onRejected)`
48+
}.failure { error, isCancelled -> String in // `task.failure {...}` = JavaScript's `promise.catch(onRejected)`
4949

5050
XCTAssertEqual(error!, "ERROR")
5151
return "Now RECOVERED"
5252

53-
}.onComplete { (value: String?, errorInfo: Task.ErrorInfo?) -> Task in // `task.onComplete {...}` = JavaScript's `promise.then(onFulfilled, onRejected)`
53+
}.then { value, errorInfo -> Task in // `task.then {...}` = JavaScript's `promise.then(onFulfilled, onRejected)`
5454

5555
println("value = \(value)") // either "Now OK" or "Now RECOVERED"
5656

@@ -59,7 +59,7 @@ class BasicTests: _TestCase
5959

6060
return Task(error: "ABORT")
6161

62-
}.onComplete { (value: String?, errorInfo: Task.ErrorInfo?) -> Void in
62+
}.then { value, errorInfo -> Void in
6363

6464
println("errorInfo = \(errorInfo)")
6565

SwiftTaskTests/RetainCycleTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class RetainCycleTests: _TestCase
7878
XCTAssertNotNil(self.task, "self.task (weak) should NOT be nil because of retain cycle: task <- dispatch_queue.")
7979
XCTAssertNotNil(self.player, "self.player (weak) should NOT nil because player is not retained by dispatch_queue.")
8080

81-
self.task!.onSuccess { value -> Void in
81+
self.task!.success { value -> Void in
8282

8383
XCTAssertEqual(value, "OK")
8484
expect.fulfill()
@@ -124,7 +124,7 @@ class RetainCycleTests: _TestCase
124124
XCTAssertNotNil(self.task, "self.task (weak) should NOT be nil because of retain cycle: task <- dispatch_queue.")
125125
XCTAssertNotNil(self.player, "self.player (weak) should NOT be nil because of retain cycle: player <- configure <- task.")
126126

127-
self.task!.onSuccess { value -> Void in
127+
self.task!.success { value -> Void in
128128

129129
XCTAssertEqual(value, "OK")
130130
expect.fulfill()
@@ -166,7 +166,7 @@ class RetainCycleTests: _TestCase
166166
XCTAssertNotNil(self.task, "self.task (weak) should not be nil because of retain cycle: task <- player <- dispatch_queue.")
167167
XCTAssertNotNil(self.player, "self.player (weak) should not be nil because of retain cycle: player <- configure <- task.")
168168

169-
self.task!.onSuccess { value -> Void in
169+
self.task!.success { value -> Void in
170170

171171
XCTAssertEqual(value, "OK")
172172
expect.fulfill()
@@ -210,7 +210,7 @@ class RetainCycleTests: _TestCase
210210
XCTAssertNotNil(self.task, "self.task (weak) should not be nil because of retain cycle: task <- player <- dispatch_queue.")
211211
XCTAssertNotNil(self.player, "self.player (weak) should not be nil because of retain cycle: player <- configure <- task.")
212212

213-
self.task!.onSuccess { value -> Void in
213+
self.task!.success { value -> Void in
214214

215215
XCTAssertEqual(value, "OK")
216216
expect.fulfill()

0 commit comments

Comments
 (0)