Skip to content

Commit 72000b3

Browse files
committed
Added basic implementation of each method with its basic test, but it needs more tests, still working on it.
1 parent a93217c commit 72000b3

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

Ax.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'Ax'
11-
s.version = '0.1.1'
11+
s.version = '0.1.2'
1212
s.summary = 'Ax is a library written in Swift that helps you to control the flow of asynchronous executions in a simplified way.'
1313

1414
# This description is used to generate tags and improve search results.

Ax/Ax.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,33 @@ final public class Ax {
1313
public typealias ResultClosure = (NSError?) -> Void
1414
public typealias DoneClosure = (NSError?) -> Void
1515
public typealias TaskClosure = (@escaping DoneClosure) -> Void
16+
public typealias IterateeClosure<T> = (T, DoneClosure) -> Void
17+
18+
public static func each<T>(collection: [T], iteratee: @escaping IterateeClosure<T>, result: @escaping ResultClosure) {
19+
let group = DispatchGroup()
20+
var errorFound: NSError?
21+
22+
for item in collection {
23+
group.enter()
24+
25+
DispatchQueue.global(qos: .background).async {
26+
iteratee(item) { error in
27+
if let error = error {
28+
errorFound = error
29+
result(error)
30+
}
31+
32+
group.leave()
33+
}
34+
}
35+
}
36+
37+
group.notify(queue: DispatchQueue.global()) {
38+
if errorFound == nil {
39+
result(nil)
40+
}
41+
}
42+
}
1643

1744
public static func parallel(tasks: [TaskClosure], result: @escaping ResultClosure) {
1845
let group = DispatchGroup()

AxTests/AxTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class AxTests: XCTestCase {
3232
}
3333
}
3434

35+
36+
3537

3638
// Serial tests
3739
func testRunningThreeTasksAndEnsureAreBeingCalledSerially() {
@@ -533,6 +535,30 @@ class AxTests: XCTestCase {
533535
}
534536
}
535537

538+
//Each tests
539+
func testRunningAnArrayItemsInParallel() {
540+
let ex = expectation(description: "")
541+
let array = [0, 1, 2, 3]
542+
543+
Ax.each(
544+
collection: array,
545+
iteratee: { item, done in
546+
print(item)
547+
done(nil)
548+
},
549+
result: { error in
550+
XCTAssertNil(error)
551+
ex.fulfill()
552+
}
553+
)
554+
555+
waitForExpectations(timeout: 10) { (error) in
556+
if let error = error {
557+
XCTFail("error: \(error)")
558+
}
559+
}
560+
}
561+
536562

537563

538564
}

0 commit comments

Comments
 (0)