@@ -199,7 +199,7 @@ public class Task<Progress, Value, Error>
199
199
}
200
200
201
201
// 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`
203
203
self . machine. addEventHandler ( . Progress, order: 110 ) { [ weak self] context in
204
204
if let progress = context. userInfo as? Progress {
205
205
if let self_ = self {
@@ -278,49 +278,37 @@ public class Task<Progress, Value, Error>
278
278
self . _cancel ( error: nil )
279
279
}
280
280
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
295
283
{
296
284
self . machine. addEventHandler ( . Progress) { [ weak self] context in
297
285
if let progress = context. userInfo as? Progress {
298
286
if let self_ = self {
299
- progressTupleClosure ( oldValue: self_. progress, newValue: progress)
287
+ progressClosure ( oldValue: self_. progress, newValue: progress)
300
288
}
301
289
}
302
290
}
303
291
304
292
return self
305
293
}
306
294
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 >
309
297
{
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) )
312
300
}
313
301
}
314
302
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 >
317
305
{
318
306
let newTask = Task < Progress2 , Value2 , Error > { [ weak self] ( progress, fulfill, _reject: _RejectHandler , configure) in
319
307
320
308
let bind = { ( value: Value ? , errorInfo: ErrorInfo ? ) -> Void in
321
- let innerTask = thenClosure ( value, errorInfo)
309
+ let innerTask = completeClosure ( value, errorInfo)
322
310
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
324
312
// Bad example: https://github.com/inamiy/SwiftTask/blob/e6085465c147fb2211fb2255c48929fcc07acd6d/SwiftTask/SwiftTask.swift#L312-L316
325
313
switch innerTask. machine. state {
326
314
case . Fulfilled:
@@ -370,23 +358,23 @@ public class Task<Progress, Value, Error>
370
358
return newTask
371
359
}
372
360
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 >
375
363
{
376
- return self . then { ( value: Value ) -> Task < Progress , Value2 , Error > in
364
+ return self . onSuccess { ( value: Value ) -> Task < Progress , Value2 , Error > in
377
365
return Task < Progress , Value2 , Error > ( value: fulfilledClosure ( value) )
378
366
}
379
367
}
380
368
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 >
383
371
{
384
372
let newTask = Task < Progress2 , Value2 , Error > { [ weak self] ( progress, fulfill, _reject: _RejectHandler , configure) in
385
373
386
374
let bind = { ( value: Value ) -> Void in
387
375
let innerTask = fulfilledClosure ( value)
388
376
389
- innerTask. then { ( value: Value2 ? , errorInfo: ErrorInfo ? ) -> Void in
377
+ innerTask. onComplete { ( value: Value2 ? , errorInfo: ErrorInfo ? ) -> Void in
390
378
if let value = value {
391
379
fulfill ( value)
392
380
}
@@ -425,23 +413,23 @@ public class Task<Progress, Value, Error>
425
413
return newTask
426
414
}
427
415
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
430
418
{
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) )
433
421
}
434
422
}
435
423
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
438
426
{
439
427
let newTask = Task { [ weak self] ( progress, fulfill, _reject: _RejectHandler , configure) in
440
428
441
429
let bind = { ( errorInfo: ErrorInfo ) -> Void in
442
- let innerTask = catchClosure ( errorInfo)
430
+ let innerTask = failureClosure ( errorInfo)
443
431
444
- innerTask. then { ( value: Value ? , errorInfo: ErrorInfo ? ) -> Void in
432
+ innerTask. onComplete { ( value: Value ? , errorInfo: ErrorInfo ? ) -> Void in
445
433
if let value = value {
446
434
fulfill ( value)
447
435
}
@@ -512,7 +500,7 @@ extension Task
512
500
let totalCount = tasks. count
513
501
514
502
for task in tasks {
515
- task. then { ( value: Value ) -> Void in
503
+ task. onSuccess { ( value: Value ) -> Void in
516
504
517
505
synchronized ( self ) {
518
506
completedCount++
@@ -531,7 +519,7 @@ extension Task
531
519
}
532
520
}
533
521
534
- } . catch { ( errorInfo: ErrorInfo ) -> Void in
522
+ } . onFailure { ( errorInfo: ErrorInfo ) -> Void in
535
523
536
524
synchronized ( self ) {
537
525
_reject ( errorInfo)
@@ -559,7 +547,7 @@ extension Task
559
547
let totalCount = tasks. count
560
548
561
549
for task in tasks {
562
- task. then { ( value: Value ) -> Void in
550
+ task. onSuccess { ( value: Value ) -> Void in
563
551
564
552
synchronized ( self ) {
565
553
completedCount++
@@ -571,7 +559,7 @@ extension Task
571
559
}
572
560
}
573
561
574
- } . catch { ( errorInfo: ErrorInfo ) -> Void in
562
+ } . onFailure { ( errorInfo: ErrorInfo ) -> Void in
575
563
576
564
synchronized ( self ) {
577
565
rejectedCount++
@@ -594,7 +582,7 @@ extension Task
594
582
}
595
583
596
584
/// 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.
598
586
public class func some( tasks: [ Task ] ) -> Task < BulkProgress , [ Value ] , Error >
599
587
{
600
588
return Task < BulkProgress , [ Value ] , Error > { ( progress, fulfill, _reject: _RejectHandler , configure) in
@@ -603,7 +591,7 @@ extension Task
603
591
let totalCount = tasks. count
604
592
605
593
for task in tasks {
606
- task. then { ( value: Value ? , errorInfo: ErrorInfo ? ) -> Void in
594
+ task. onComplete { ( value: Value ? , errorInfo: ErrorInfo ? ) -> Void in
607
595
608
596
synchronized ( self ) {
609
597
completedCount++
0 commit comments