Created
February 11, 2019 16:11
-
-
Save morpheby/30aec3f53bf1077ab72f5bb592aa1716 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 | |
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