Skip to content

Commit

Permalink
Added parallel methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando authored and Fernando committed Feb 8, 2017
1 parent dcf0e94 commit 0ac57a5
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Example/FOTask/ViewController.swift
Expand Up @@ -63,7 +63,7 @@ class ViewController: UIViewController {
GetUserName(),
GetUserName()
],
reduce: { (userNames: [String]) -> [String] in
reduceBy: { (userNames: [String]) -> [String] in
return userNames
}
)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion FOTask.podspec
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'FOTask'
s.version = '0.1.0'
s.version = '0.2.0'
s.summary = 'Object oriented Swift tasks microframework with concurrency and composition.'

# This description is used to generate tags and improve search results.
Expand Down
52 changes: 50 additions & 2 deletions FOTask/Classes/Task.swift
Expand Up @@ -71,7 +71,7 @@ public func => <A, B, C> (left: Task<A, B>, right: Task<B, C>) -> Task<A, C> {
}

public extension Task {
public static func parallel<C>(_ tasks: [Task<A, B>], on queue: DispatchQueue = .main, reduce: @escaping ([B]) -> C) -> Task<A, C> {
public static func parallel<C>(_ tasks: [Task<A, B>], on queue: DispatchQueue = .main, reduceBy reduce: @escaping ([B]) -> C) -> Task<A, C> {
return BasicTask<A, C> { (input: A) in
return { (onSuccess: @escaping (C) -> Void, onError: @escaping (Error) -> Void) in
let group = DispatchGroup()
Expand Down Expand Up @@ -108,10 +108,58 @@ public extension Task {
}
}
}

public func inParallel<U, V>(with task: Task<A, U>, on queue: DispatchQueue = .main, reduceBy reduce: @escaping (B, U) -> V) -> Task<A, V> {
return BasicTask<A, V> { (input: A) in
return { (onSuccess: @escaping (V) -> Void, onError: @escaping (Error) -> Void) in
let group = DispatchGroup()

group.enter()
group.enter()

var firstValue: B!
var secondValue: U!

self.perform(input,
onSuccess: { (output: B) in
firstValue = output
group.leave()
},
onError: { (error: Error) in
onError(error)
return
}
)

task.perform(input,
onSuccess: { (output: U) in
secondValue = output
group.leave()
},
onError: { (error: Error) in
onError(error)
return
}
)

DispatchQueue.global(qos: .background).async {
let dispatchGroupResult = group.wait(timeout: .distantFuture)
queue.async {
switch dispatchGroupResult {
case .success:
onSuccess(reduce(firstValue, secondValue))
case .timedOut:
onError(TaskError.timeout)
}
}
}
}
}
}
}

public extension Task {
public func map<C>(_ f: @escaping (B) -> C) -> Task<A, C> {
public func map<C>(_ f: @escaping (B) -> C) -> Task<A, C> {
return BasicTask<A, C> { (input: A) in
return { (onSuccess: @escaping (C) -> Void, onError: @escaping (Error) -> Void) in
self.perform(input,
Expand Down

0 comments on commit 0ac57a5

Please sign in to comment.