Last active
March 6, 2020 08:53
-
-
Save behrank/1cb21d12baadb49353f4036208860a7c to your computer and use it in GitHub Desktop.
Queue
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 | |
protocol ExecutableQueue { | |
var queue: DispatchQueue { get } | |
} | |
extension ExecutableQueue { | |
func execute(closure: @escaping () -> Void) { | |
queue.async(execute: closure) | |
} | |
func executeAfter(delay: DelayTime, closure: @escaping () -> Void) { | |
queue.asyncAfter(deadline: delay.asDispatchtime, execute: closure) | |
} | |
} | |
enum Queue: ExecutableQueue { | |
case main | |
case userInteractive | |
case userInitiated | |
case utility | |
case background | |
var queue: DispatchQueue { | |
switch self { | |
case .main: return DispatchQueue.main | |
case .userInteractive: return DispatchQueue.global(qos: .userInteractive) | |
case .userInitiated: return DispatchQueue.global(qos: .userInitiated) | |
case .utility: return DispatchQueue.global(qos: .utility) | |
case .background: return DispatchQueue.global(qos: .background) | |
} | |
} | |
} | |
enum DelayTime { | |
case halfSecond | |
case oneSecond | |
case twoSeconds | |
case fiveSeconds | |
case customSeconds(Int) | |
case customMiliseconds(Int) | |
var asDispatchtime: DispatchTime { | |
switch self { | |
case .halfSecond: return DispatchTime.now() + DispatchTimeInterval.milliseconds(500) | |
case .oneSecond: return DispatchTime.now() + DispatchTimeInterval.seconds(1) | |
case .twoSeconds: return DispatchTime.now() + DispatchTimeInterval.seconds(2) | |
case .fiveSeconds: return DispatchTime.now() + DispatchTimeInterval.seconds(5) | |
case .customSeconds(let seconds): return DispatchTime.now() + DispatchTimeInterval.seconds(seconds) | |
case .customMiliseconds(let miliSeconds): return DispatchTime.now() + DispatchTimeInterval.milliseconds(miliSeconds) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment