Skip to content

Commit

Permalink
Add temporary _sync
Browse files Browse the repository at this point in the history
  • Loading branch information
koher committed Jan 13, 2017
1 parent 97e7c14 commit 4efb5c7
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Sources/Async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,32 @@ public func sync<T, R>(operation: (@escaping (T, @escaping (() throws -> R) -> (
}
}

public func _sync<T, R>(operation: (@escaping (T, @escaping (() throws -> R) -> ()) -> ())) -> (T) throws -> R {
return { value in
var resultValue: R!
var resultError: Error?

let semaphore = DispatchSemaphore(value: 0)
operation(value) { getValue in
defer {
semaphore.signal()
}
do {
resultValue = try getValue()
} catch let error {
resultError = error
}
}
semaphore.wait()

if let error = resultError {
throw error
}

return resultValue
}
}

internal func repeated<T, R>(operation: (@escaping (T, @escaping (() throws -> R) -> ()) -> ()), interval: TimeInterval? = nil) -> ([T], @escaping (() throws -> [R]) -> ()) -> () {
return { values, callback in
_repeat(operation: operation, for: values[0..<values.count], interval: interval, callback: callback)
Expand Down

0 comments on commit 4efb5c7

Please sign in to comment.