Skip to content

Instantly share code, notes, and snippets.

@morpheby
Created February 11, 2019 16:11
Show Gist options
  • Save morpheby/30aec3f53bf1077ab72f5bb592aa1716 to your computer and use it in GitHub Desktop.
Save morpheby/30aec3f53bf1077ab72f5bb592aa1716 to your computer and use it in GitHub Desktop.
import Foundation
struct DelayedActions {
let queue: DispatchQueue
private var shouldDelay: Bool = true
private var closures: [() -> Void] = []
init() {
queue = DispatchQueue.main
}
init(dispatchQueue: DispatchQueue) {
queue = dispatchQueue
}
mutating func delay(_ closure: @escaping () -> Void) {
if shouldDelay {
closures.append(closure)
} else {
queue.async {
closure()
}
}
}
mutating func consume() {
for closure in closures {
queue.async(execute: closure)
}
closures = []
shouldDelay = false
}
mutating func startDelaying() {
shouldDelay = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment