@@ -631,8 +631,74 @@ internal func _bindInnerTask<Progress2, Value2, Error, Error2>(
631
631
}
632
632
}
633
633
634
+ private func _toAny< A> ( a: A ) -> Any
635
+ {
636
+ return a
637
+ }
638
+
639
+ extension Task
640
+ {
641
+ /// converts Task<P, V, E> to Task<V2, V2, E2>
642
+ public func convert< Progress2, Value2, Error2> ( progress toP2: Progress -> Progress2 , value toV2: Value -> Value2 , error toE2: Error -> Error2 ) -> Task < Progress2 , Value2 , Error2 >
643
+ {
644
+ return Task < Progress2 , Value2 , Error2 > { machine, progress, fulfill, _reject, configure in
645
+ configure. pause = { self . pause ( ) }
646
+ configure. resume = { self . resume ( ) }
647
+ configure. cancel = { self . cancel ( ) }
648
+
649
+ self . progress { _, p in
650
+ progress ( toP2 ( p) )
651
+ } . then { value, errorInfo -> Void in
652
+ if let value = value {
653
+ fulfill ( toV2 ( value) )
654
+ }
655
+ else if let errorInfo = errorInfo {
656
+ let ( error, isCancelled) = errorInfo
657
+ _reject ( ( error. map ( toE2) , isCancelled) )
658
+ }
659
+ }
660
+ }
661
+ }
662
+ }
663
+
634
664
// MARK: - Multiple Tasks
635
665
666
+ extension Task
667
+ {
668
+ /// combines 2 tasks into one which fulfills when both tasks are fulfilled
669
+ public func zip< Progress2, Value2, Error2> ( task2: Task < Progress2 , Value2 , Error2 > ) -> Task < ( Progress ? , Progress2 ? ) , ( Value , Value2 ) , ( Error ? , Error2 ? ) >
670
+ {
671
+ return Task < ( Progress ? , Progress2 ? ) , ( Value , Value2 ) , ( Error ? , Error2 ? ) > { machine, progress, fulfill, _reject, configure in
672
+
673
+ let t1 = self . convert ( progress: _toAny, value: _toAny, error: _toAny)
674
+ let t2 = task2. convert ( progress: _toAny, value: _toAny, error: _toAny)
675
+ let zipTask = Task < Any , Any , Any > . all ( [ t1, t2] )
676
+
677
+ configure. pause = { zipTask. pause ( ) }
678
+ configure. resume = { zipTask. resume ( ) }
679
+ configure. cancel = { zipTask. cancel ( ) }
680
+
681
+ t1. progress { _, p in
682
+ if t2. value == nil && t2. errorInfo == nil {
683
+ progress ( ( p as? Progress , t2. progress as? Progress2 ) )
684
+ }
685
+ }
686
+ t2. progress { _, p in
687
+ if t1. value == nil && t1. errorInfo == nil {
688
+ progress ( ( t1. progress as? Progress , p as? Progress2 ) )
689
+ }
690
+ }
691
+
692
+ zipTask. success { ( valueTuple: [ Any ] ) -> Void in
693
+ fulfill ( ( valueTuple [ 0 ] as! Value , valueTuple [ 1 ] as! Value2 ) )
694
+ } . failure { _, isCancelled -> Void in
695
+ _reject ( ( error: ( t1. errorInfo? . error as? Error , t2. errorInfo? . error as? Error2 ) , isCancelled: isCancelled) )
696
+ }
697
+
698
+ } . name ( " \( self . name) -zip( \( task2. name) ) " )
699
+ }
700
+ }
701
+
636
702
extension Task
637
703
{
638
704
public typealias BulkProgress = ( completedCount: Int , totalCount: Int )
0 commit comments