Skip to content

Commit 662d686

Browse files
committed
Rename then/catch to onComplete/onSuccess/onFailure.
1 parent 7acd511 commit 662d686

File tree

5 files changed

+143
-163
lines changed

5 files changed

+143
-163
lines changed

SwiftTask/SwiftTask.swift

Lines changed: 32 additions & 44 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 `progressTupleClosure(self.progress, newValue)` be invoked first before updating old `self.progress`
202+
// NOTE: use order > 100 (default) to let `progressClosure(self.progress, newValue)` 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,49 +278,37 @@ public class Task<Progress, Value, Error>
278278
self._cancel(error: nil)
279279
}
280280

281-
/// progress + newValue only
282-
public func progress(progressClosure: Progress -> Void) -> Task
283-
{
284-
self.machine.addEventHandler(.Progress) { [weak self] context in
285-
if let progress = context.userInfo as? Progress {
286-
progressClosure(progress)
287-
}
288-
}
289-
290-
return self
291-
}
292-
293-
/// progress + (oldValue, newValue)
294-
public func progress(progressTupleClosure: (oldValue: Progress?, newValue: Progress) -> Void) -> Task
281+
/// task.onProgress
282+
public func onProgress(progressClosure: (oldValue: Progress?, newValue: Progress) -> Void) -> Task
295283
{
296284
self.machine.addEventHandler(.Progress) { [weak self] context in
297285
if let progress = context.userInfo as? Progress {
298286
if let self_ = self {
299-
progressTupleClosure(oldValue: self_.progress, newValue: progress)
287+
progressClosure(oldValue: self_.progress, newValue: progress)
300288
}
301289
}
302290
}
303291

304292
return self
305293
}
306294

307-
/// then (fulfilled & rejected) + returning value
308-
public func then<Value2>(thenClosure: (Value?, ErrorInfo?) -> Value2) -> Task<Progress, Value2, Error>
295+
/// onComplete (fulfilled & rejected) + closure returning value
296+
public func onComplete<Value2>(completeClosure: (Value?, ErrorInfo?) -> Value2) -> Task<Progress, Value2, Error>
309297
{
310-
return self.then { (value: Value?, errorInfo: ErrorInfo?) -> Task<Progress, Value2, Error> in
311-
return Task<Progress, Value2, Error>(value: thenClosure(value, errorInfo))
298+
return self.onComplete { (value: Value?, errorInfo: ErrorInfo?) -> Task<Progress, Value2, Error> in
299+
return Task<Progress, Value2, Error>(value: completeClosure(value, errorInfo))
312300
}
313301
}
314302

315-
/// then (fulfilled & rejected) + returning task
316-
public func then<Progress2, Value2>(thenClosure: (Value?, ErrorInfo?) -> Task<Progress2, Value2, Error>) -> Task<Progress2, Value2, Error>
303+
/// onComplete (fulfilled & rejected) + closure returning task
304+
public func onComplete<Progress2, Value2>(completeClosure: (Value?, ErrorInfo?) -> Task<Progress2, Value2, Error>) -> Task<Progress2, Value2, Error>
317305
{
318306
let newTask = Task<Progress2, Value2, Error> { [weak self] (progress, fulfill, _reject: _RejectHandler, configure) in
319307

320308
let bind = { (value: Value?, errorInfo: ErrorInfo?) -> Void in
321-
let innerTask = thenClosure(value, errorInfo)
309+
let innerTask = completeClosure(value, errorInfo)
322310

323-
// NOTE: don't call then/catch for innerTask, or recursive bindings may occur
311+
// NOTE: don't call `then` for innerTask, or recursive bindings may occur
324312
// Bad example: https://github.com/inamiy/SwiftTask/blob/e6085465c147fb2211fb2255c48929fcc07acd6d/SwiftTask/SwiftTask.swift#L312-L316
325313
switch innerTask.machine.state {
326314
case .Fulfilled:
@@ -370,23 +358,23 @@ public class Task<Progress, Value, Error>
370358
return newTask
371359
}
372360

373-
/// then (fulfilled only) + returning value
374-
public func then<Value2>(fulfilledClosure: Value -> Value2) -> Task<Progress, Value2, Error>
361+
/// onSuccess + closure returning value
362+
public func onSuccess<Value2>(fulfilledClosure: Value -> Value2) -> Task<Progress, Value2, Error>
375363
{
376-
return self.then { (value: Value) -> Task<Progress, Value2, Error> in
364+
return self.onSuccess { (value: Value) -> Task<Progress, Value2, Error> in
377365
return Task<Progress, Value2, Error>(value: fulfilledClosure(value))
378366
}
379367
}
380368

381-
/// then (fulfilled only) + returning task
382-
public func then<Progress2, Value2>(fulfilledClosure: Value -> Task<Progress2, Value2, Error>) -> Task<Progress2, Value2, Error>
369+
/// onSuccess + closure returning task
370+
public func onSuccess<Progress2, Value2>(fulfilledClosure: Value -> Task<Progress2, Value2, Error>) -> Task<Progress2, Value2, Error>
383371
{
384372
let newTask = Task<Progress2, Value2, Error> { [weak self] (progress, fulfill, _reject: _RejectHandler, configure) in
385373

386374
let bind = { (value: Value) -> Void in
387375
let innerTask = fulfilledClosure(value)
388376

389-
innerTask.then { (value: Value2?, errorInfo: ErrorInfo?) -> Void in
377+
innerTask.onComplete { (value: Value2?, errorInfo: ErrorInfo?) -> Void in
390378
if let value = value {
391379
fulfill(value)
392380
}
@@ -425,23 +413,23 @@ public class Task<Progress, Value, Error>
425413
return newTask
426414
}
427415

428-
/// catch + returning value
429-
public func catch(catchClosure: ErrorInfo -> Value) -> Task
416+
/// onFailure + closure returning value
417+
public func onFailure(failureClosure: ErrorInfo -> Value) -> Task
430418
{
431-
return self.catch { (errorInfo: ErrorInfo) -> Task in
432-
return Task(value: catchClosure(errorInfo))
419+
return self.onFailure { (errorInfo: ErrorInfo) -> Task in
420+
return Task(value: failureClosure(errorInfo))
433421
}
434422
}
435423

436-
/// catch + returning task
437-
public func catch(catchClosure: ErrorInfo -> Task) -> Task
424+
/// onFailure + closure returning task
425+
public func onFailure(failureClosure: ErrorInfo -> Task) -> Task
438426
{
439427
let newTask = Task { [weak self] (progress, fulfill, _reject: _RejectHandler, configure) in
440428

441429
let bind = { (errorInfo: ErrorInfo) -> Void in
442-
let innerTask = catchClosure(errorInfo)
430+
let innerTask = failureClosure(errorInfo)
443431

444-
innerTask.then { (value: Value?, errorInfo: ErrorInfo?) -> Void in
432+
innerTask.onComplete { (value: Value?, errorInfo: ErrorInfo?) -> Void in
445433
if let value = value {
446434
fulfill(value)
447435
}
@@ -512,7 +500,7 @@ extension Task
512500
let totalCount = tasks.count
513501

514502
for task in tasks {
515-
task.then { (value: Value) -> Void in
503+
task.onSuccess { (value: Value) -> Void in
516504

517505
synchronized(self) {
518506
completedCount++
@@ -531,7 +519,7 @@ extension Task
531519
}
532520
}
533521

534-
}.catch { (errorInfo: ErrorInfo) -> Void in
522+
}.onFailure { (errorInfo: ErrorInfo) -> Void in
535523

536524
synchronized(self) {
537525
_reject(errorInfo)
@@ -559,7 +547,7 @@ extension Task
559547
let totalCount = tasks.count
560548

561549
for task in tasks {
562-
task.then { (value: Value) -> Void in
550+
task.onSuccess { (value: Value) -> Void in
563551

564552
synchronized(self) {
565553
completedCount++
@@ -571,7 +559,7 @@ extension Task
571559
}
572560
}
573561

574-
}.catch { (errorInfo: ErrorInfo) -> Void in
562+
}.onFailure { (errorInfo: ErrorInfo) -> Void in
575563

576564
synchronized(self) {
577565
rejectedCount++
@@ -594,7 +582,7 @@ extension Task
594582
}
595583

596584
/// Returns new task which performs all given tasks and stores only fulfilled values.
597-
/// This new task will NEVER be internally rejected (thus uncatchable from outside).
585+
/// This new task will NEVER be internally rejected.
598586
public class func some(tasks: [Task]) -> Task<BulkProgress, [Value], Error>
599587
{
600588
return Task<BulkProgress, [Value], Error> { (progress, fulfill, _reject: _RejectHandler, configure) in
@@ -603,7 +591,7 @@ extension Task
603591
let totalCount = tasks.count
604592

605593
for task in tasks {
606-
task.then { (value: Value?, errorInfo: ErrorInfo?) -> Void in
594+
task.onComplete { (value: Value?, errorInfo: ErrorInfo?) -> Void in
607595

608596
synchronized(self) {
609597
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.then { (value: String) -> Void in
42+
task.onComplete { (value: String) -> Void in
4343
XCTAssertEqual(value, "OK")
4444

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

7575
}
7676

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

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

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

8383
// println(error)
8484

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

124124
}
125125

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

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

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

135135
XCTAssertEqual(value, "OK")
136136

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

175175
// set then
176-
task.then { (value: String) -> Void in
176+
task.onComplete { (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 `then` or `catch` for 1st task cancellation)
229+
} // end of 1st task definition (NOTE: don't chain with `onComplete` or `onFailure` for 1st task cancellation)
230230

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

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

235-
}.catch { (error: NSError?, isCancelled: Bool) -> Void in
235+
}.onFailure { (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.progress { (progress: Float) in
39+
task.onProgress { oldValue, newValue in
4040

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

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

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

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

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

53-
}.then { (value: String?, errorInfo: Task.ErrorInfo?) -> Task in // then(onFulfilled+onRejected)
53+
}.onComplete { (value: String?, errorInfo: Task.ErrorInfo?) -> Task in // `task.onComplete {...}` = 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-
}.then { (value: String?, errorInfo: Task.ErrorInfo?) -> Void in // then(onFulfilled+onRejected)
62+
}.onComplete { (value: String?, errorInfo: Task.ErrorInfo?) -> Void in
6363

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

SwiftTaskTests/RetainCycleTests.swift

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +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-
println("then")
82-
83-
self.task!.then { (value: String) -> Void in
81+
self.task!.onSuccess { (value: String) -> Void in
8482

8583
XCTAssertEqual(value, "OK")
8684
expect.fulfill()
@@ -126,9 +124,7 @@ class RetainCycleTests: _TestCase
126124
XCTAssertNotNil(self.task, "self.task (weak) should NOT be nil because of retain cycle: task <- dispatch_queue.")
127125
XCTAssertNotNil(self.player, "self.player (weak) should NOT be nil because of retain cycle: player <- configure <- task.")
128126

129-
println("then")
130-
131-
self.task!.then { (value: String) -> Void in
127+
self.task!.onSuccess { (value: String) -> Void in
132128

133129
XCTAssertEqual(value, "OK")
134130
expect.fulfill()
@@ -170,9 +166,7 @@ class RetainCycleTests: _TestCase
170166
XCTAssertNotNil(self.task, "self.task (weak) should not be nil because of retain cycle: task <- player <- dispatch_queue.")
171167
XCTAssertNotNil(self.player, "self.player (weak) should not be nil because of retain cycle: player <- configure <- task.")
172168

173-
println("then")
174-
175-
self.task!.then { (value: String) -> Void in
169+
self.task!.onSuccess { (value: String) -> Void in
176170

177171
XCTAssertEqual(value, "OK")
178172
expect.fulfill()
@@ -216,9 +210,7 @@ class RetainCycleTests: _TestCase
216210
XCTAssertNotNil(self.task, "self.task (weak) should not be nil because of retain cycle: task <- player <- dispatch_queue.")
217211
XCTAssertNotNil(self.player, "self.player (weak) should not be nil because of retain cycle: player <- configure <- task.")
218212

219-
println("then")
220-
221-
self.task!.then { (value: String) -> Void in
213+
self.task!.onSuccess { (value: String) -> Void in
222214

223215
XCTAssertEqual(value, "OK")
224216
expect.fulfill()

0 commit comments

Comments
 (0)