File tree Expand file tree Collapse file tree 4 files changed +70
-10
lines changed
Test/ParallelTest/ParallelTests Expand file tree Collapse file tree 4 files changed +70
-10
lines changed Original file line number Diff line number Diff line change 88
99Pod ::Spec . new do |s |
1010 s . name = 'ParallelSwift'
11- s . version = '1.2 .0'
11+ s . version = '1.3 .0'
1212 s . summary = 'Execute multiple methods in parallel with simple API.'
1313
1414
Original file line number Diff line number Diff line change @@ -51,7 +51,18 @@ public class ParallelSwift {
5151 public func addPhase( _ executionThread: ExecutionThread = . background, phase: @escaping ( @escaping ( ) -> ( ) ) -> ( ) ) {
5252 phases. append ( ( executionThread, phase) )
5353 }
54-
54+
55+ /// Add execution phases as array (variadic function). Once the input closure of a phase is called phase in considered finnished.
56+ ///
57+ /// With optional thread parameter (all phases in the iput array are executed in the same way.
58+ /// - .main : phase is executed in main thread
59+ /// - .background (default) : phase is excuted at background
60+ public func addPhases( _ executionThread: ExecutionThread = . background, phases: ( @escaping ( ) -> ( ) ) -> ( ) ... ) {
61+ phases. forEach {
62+ self . phases. append ( ( executionThread, $0) )
63+ }
64+ }
65+
5566 /// Start all phases in parallel. ExecutionMode defines when completion is called (see documentation)
5667 public func execute( _ type: ExecutionType = . all, complete: @escaping ( ) -> ( ) ) {
5768 self . numberOfPhases = phases. count
Original file line number Diff line number Diff line change @@ -34,10 +34,11 @@ Execution closure is executed immediately after phases are started.
3434
3535## Usage
3636
37- Add methods to be executed as closure with ``` addPhases ```
37+ Add methods to be executed as closure with ``` addPhase ``` (one by one) or with ``` addPhases ``` (as array).
38+ Both methods take one parameter that defines where the phase/s are executed (.main or .background (default))
3839Once excute is called all phases are launch simultaneusly (in parallel).
3940
40- Methods will mark themself finnished a by calling ``` done ``` .
41+ Methods will mark themself finnished a by calling input closure (e.g. ``` done ``` in the example) .
4142
4243Execution completion closure is always executed in main thread. See modes.
4344
@@ -52,7 +53,7 @@ Execution completion closure is always executed in main thread. See modes.
5253 print("2")
5354 done()
5455 }
55- p.addPhase { done in
56+ p.addPhase(.main) { done in
5657 print("3")
5758 done()
5859 }
@@ -61,6 +62,8 @@ Execution completion closure is always executed in main thread. See modes.
6162 }
6263```
6364
65+ See tests for more use cases.
66+
6467### Parameters
6568
6669``` timeout ``` : Time in seconds after which execute completion is called even if phases are still running. * Default: 0 (no timeout)*
Original file line number Diff line number Diff line change @@ -51,7 +51,36 @@ class ParallelTests: XCTestCase {
5151 }
5252 }
5353 }
54-
54+
55+ func doPhasesVariadic( ) {
56+ p = ParallelSwift ( )
57+ result = " "
58+ p? . timeout = 0
59+ p? . addPhases ( phases:
60+ { done in
61+ DispatchQueue . global ( ) . asyncAfter ( deadline: DispatchTime . now ( ) + 1 ) {
62+ print ( " 1 " )
63+ self . result. append ( " 1 " )
64+ done ( )
65+ }
66+ } ,
67+ { done in
68+ DispatchQueue . global ( ) . asyncAfter ( deadline: DispatchTime . now ( ) + 2 ) {
69+ print ( " 2 " )
70+ self . result. append ( " 2 " )
71+ done ( )
72+ }
73+ } ,
74+ { done in
75+ DispatchQueue . global ( ) . asyncAfter ( deadline: DispatchTime . now ( ) + 3 ) {
76+ print ( " 3 " )
77+ self . result. append ( " 3 " )
78+ done ( )
79+ }
80+ }
81+ )
82+ }
83+
5584 func testTimeoutAll( ) {
5685
5786 let e = expectation ( description: " time " )
@@ -90,20 +119,37 @@ class ParallelTests: XCTestCase {
90119
91120 let e = expectation ( description: " all " )
92121 doPhases ( )
93-
122+
94123 p? . sufflePhases = true
95-
124+
96125 p? . execute ( . all) {
97126 XCTAssert ( self . result == " 123 " )
98127 e. fulfill ( )
99128 print ( " all done " )
100129 }
101-
130+
102131 waitForExpectations ( timeout: 4 , handler: { _ in
103132 print ( " Test done " )
104133 } )
105134 }
106-
135+ func testModeAllVariadic( ) {
136+
137+ let e = expectation ( description: " all " )
138+ doPhasesVariadic ( )
139+
140+ p? . sufflePhases = true
141+
142+ p? . execute ( . all) {
143+ XCTAssert ( self . result == " 123 " )
144+ e. fulfill ( )
145+ print ( " all done " )
146+ }
147+
148+ waitForExpectations ( timeout: 4 , handler: { _ in
149+ print ( " Test done " )
150+ } )
151+ }
152+
107153 func testModeAny( ) {
108154
109155 let e = expectation ( description: " any " )
You can’t perform that action at this time.
0 commit comments