Skip to content

Commit

Permalink
Refactor async operations
Browse files Browse the repository at this point in the history
  • Loading branch information
koher committed Jan 14, 2017
1 parent 4efb5c7 commit 9a57b25
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 57 deletions.
42 changes: 5 additions & 37 deletions Sources/Async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,6 @@ 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 Expand Up @@ -103,7 +77,7 @@ internal func flatten<T, U, R>(_ operation1: @escaping (T, @escaping (() throws

internal func waiting<T, R>(operation: @escaping (T, @escaping (() throws -> R) -> ()) -> (), with interval: TimeInterval) -> (T, @escaping (() throws -> R) -> ()) -> () {
let wait: ((), @escaping (() throws -> ()) -> ()) -> () = { _, completion in
Async.waitQueue.asyncAfter(deadline: .now() + .milliseconds(Int(interval * 1000.0))) {
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(Int(interval * 1000.0))) {
completion {
()
}
Expand All @@ -129,7 +103,7 @@ internal func join<T1, R1, T2, R2>(_ operation1: @escaping (T1, @escaping (() th
operation1(value1) { getValue in
do {
let result = try getValue()
Async.executionQueue.async {
DispatchQueue.main.async {
guard let result2 = result2 else {
result1 = result
return
Expand All @@ -139,7 +113,7 @@ internal func join<T1, R1, T2, R2>(_ operation1: @escaping (T1, @escaping (() th
}
}
} catch let error {
Async.executionQueue.async {
DispatchQueue.main.async {
if hasThrownError {
return
}
Expand All @@ -154,7 +128,7 @@ internal func join<T1, R1, T2, R2>(_ operation1: @escaping (T1, @escaping (() th
operation2(value2) { getValue in
do {
let result = try getValue()
Async.executionQueue.async {
DispatchQueue.main.async {
guard let result1 = result1 else {
result2 = result
return
Expand All @@ -164,7 +138,7 @@ internal func join<T1, R1, T2, R2>(_ operation1: @escaping (T1, @escaping (() th
}
}
} catch let error {
Async.executionQueue.async {
DispatchQueue.main.async {
if hasThrownError {
return
}
Expand All @@ -177,9 +151,3 @@ internal func join<T1, R1, T2, R2>(_ operation1: @escaping (T1, @escaping (() th
}
}
}

internal struct Async {
internal static let sessionQueue = OperationQueue()
internal static let executionQueue = DispatchQueue(label: "TweetupKit")
fileprivate static let waitQueue = DispatchQueue(label: "TweetupKit.wait")
}
2 changes: 0 additions & 2 deletions Sources/CodeRenderer.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import WebKit
import Foundation

private let queue = DispatchQueue(label: "TweetupKit.CodeRenderer")

internal class CodeRenderer: NSObject {
private var webView: WebView!
fileprivate var loading = true
Expand Down
2 changes: 1 addition & 1 deletion Sources/Gist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation

internal struct Gist {
static func createGist(description: String, code: Code, accessToken: String, callback: @escaping (() throws -> String) -> ()) {
let session = URLSession(configuration: .ephemeral, delegate: nil, delegateQueue: Async.sessionQueue)
let session = URLSession(configuration: .ephemeral)
var request = URLRequest(url: URL(string: "https://api.github.com/gists")!)
request.httpMethod = "POST"
request.addValue("token \(accessToken)", forHTTPHeaderField: "Authorization")
Expand Down
6 changes: 0 additions & 6 deletions Sources/OAuth.swift → Sources/OAuthCredential.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import Foundation

internal struct OAuth {
internal static let executionContext: (@escaping () -> Void) -> Void = { block in
return Async.executionQueue.async(execute: block)
}
}

public struct OAuthCredential {
public let consumerKey: String
public let consumerSecret: String
Expand Down
9 changes: 0 additions & 9 deletions Sources/Twitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import Foundation

internal struct Twitter {
static func update(status: String, mediaId: String? = nil, credential: OAuthCredential, callback: @escaping (() throws -> (String, String)) -> ()) {
OAuthSwiftHTTPRequest.executionContext = OAuth.executionContext

let client = OAuthSwiftClient(credential: credential)
client.sessionFactory.queue = Async.sessionQueue

var parameters = [
"status": status
Expand All @@ -26,10 +23,7 @@ internal struct Twitter {
}

static func upload(media: Data, credential: OAuthCredential, callback: @escaping (() throws -> String) -> ()) {
OAuthSwiftHTTPRequest.executionContext = OAuth.executionContext

let client = OAuthSwiftClient(credential: credential)
client.sessionFactory.queue = Async.sessionQueue

_ = client.post(
"https://upload.twitter.com/1.1/media/upload.json",
Expand All @@ -56,9 +50,6 @@ extension OAuthSwiftClient {
}

fileprivate func post<T>(_ url: String, parameters: OAuthSwift.Parameters, callback: @escaping (() throws -> T) -> (), completion: @escaping (OAuthSwiftResponse) throws -> (T)) -> OAuthSwiftRequestHandle? {
OAuthSwiftHTTPRequest.executionContext = OAuth.executionContext
sessionFactory.queue = Async.sessionQueue

return post(
url,
parameters: parameters,
Expand Down
4 changes: 2 additions & 2 deletions Tests/TweetupKitTests/AsyncTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class AsyncTests: XCTestCase {
}

private func asyncIncrement(value: Int, completion: @escaping (() throws -> Int) -> ()) {
Async.executionQueue.async {
DispatchQueue.main.async {
completion {
value + 1
}
Expand All @@ -98,7 +98,7 @@ private func asyncIncrement(value: Int, completion: @escaping (() throws -> Int)


private func asyncTime(_ value: (), completion: @escaping (() throws -> TimeInterval) -> ()) {
Async.executionQueue.async {
DispatchQueue.main.async {
completion {
Date.timeIntervalSinceReferenceDate
}
Expand Down

0 comments on commit 9a57b25

Please sign in to comment.