Skip to content

Instantly share code, notes, and snippets.

@RoyalIcing
Created March 16, 2016 04:24
Show Gist options
  • Save RoyalIcing/757479bc51025276d241 to your computer and use it in GitHub Desktop.
Save RoyalIcing/757479bc51025276d241 to your computer and use it in GitHub Desktop.
import Foundation
protocol StageProtocol {
func perform() throws -> Self?
var queue: dispatch_queue_t { get }
func run(completion: (() throws -> Self) -> ())
}
extension StageProtocol {
func run(completion: (() throws -> Self) -> ()) {
dispatch_async(queue) {
do {
if let result = try self.perform() {
// Pass to next stage
result.run(completion)
}
else {
// Completed!
completion({ self })
}
}
catch let error {
// Experienced an error
completion({ throw error })
}
}
}
}
enum FileOpenStage: StageProtocol {
case read(fileURL: NSURL)
case parse(data: NSData)
case finish(object: AnyObject)
func perform() throws -> FileOpenStage? {
switch self {
case let .read(fileURL):
return try .parse(
data: NSData(contentsOfURL: fileURL, options: .DataReadingMappedIfSafe)
)
case let .parse(data):
return try .finish(
object: NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions())
)
case .finish:
return nil
}
}
var queue: dispatch_queue_t {
switch self {
case .read, .parse:
return dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0)
case .finish:
return dispatch_get_main_queue()
}
}
}
func openFile(resource: String, `extension`: String) {
let fileURL = NSBundle.mainBundle().URLForResource(resource, withExtension: `extension`)!
FileOpenStage.read(fileURL: fileURL).run { useOutput in
do {
let output = try useOutput()
if case let .finish(object) = output {
print("OPENED: \(object)")
}
else {
fatalError("Unexpected output \(output)")
}
}
catch {
print("ERROR! \(error)")
}
}
}
openFile("example", `extension`: "json")
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment