-
-
Save Fykec/f7546fcde6f8c1efbab7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class SwiftSequencer { | |
typealias SequencerNext = (AnyObject? -> Void) | |
typealias SequencerStep = (AnyObject?, SequencerNext) -> Void | |
var steps: [SequencerStep] = [] | |
func run() { | |
runNextStepWithResult(nil) | |
} | |
func enqueueStep(step: SequencerStep) { | |
steps.append(step) | |
} | |
func dequeueNextStep() -> (SequencerStep) { | |
return steps.removeAtIndex(0) | |
} | |
func runNextStepWithResult(result: AnyObject?) { | |
if (steps.count <= 0) { | |
return | |
} | |
let step = dequeueNextStep() | |
step(result, { self.runNextStepWithResult($0) }) | |
} | |
} | |
class Playground : NSObject { | |
static func play() { | |
let sequncer = Sequencer() | |
sequncer.enqueueStep { (result:AnyObject!, | |
completion:(AnyObject!->Void)!) -> Void in | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { () -> Void in | |
dispatch_async(dispatch_get_main_queue(), { () -> Void in | |
completion("fhjkahf") | |
}) | |
}) | |
} | |
sequncer.run() | |
} | |
static func playSwift() { | |
let sequncer = SwiftSequencer() | |
sequncer.enqueueStep {(result:AnyObject?, | |
completion:(AnyObject?->Void)) -> Void in | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { () -> Void in | |
dispatch_async(dispatch_get_main_queue(), { () -> Void in | |
completion("haha ok") | |
}) | |
}) | |
} | |
sequncer.run() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment