CallbackQueue.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // CallbackQueue.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2018/10/15.
  6. //
  7. // Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import Foundation
  27. public typealias ExecutionQueue = CallbackQueue
  28. /// Represents the behavior of the callback queue selection when a closure is dispatched.
  29. public enum CallbackQueue {
  30. /// Dispatches the closure to `DispatchQueue.main` with an `async` behavior.
  31. case mainAsync
  32. /// Dispatches the closure to `DispatchQueue.main` with an `async` behavior if the current queue is not `.main`.
  33. /// Otherwise, it calls the closure immediately on the current main queue.
  34. case mainCurrentOrAsync
  35. /// Does not change the calling queue for the closure.
  36. case untouch
  37. /// Dispatches the closure to a specified `DispatchQueue`.
  38. case dispatch(DispatchQueue)
  39. /// Executes the `block` in a dispatch queue defined by `self`.
  40. /// - Parameter block: The block needs to be executed.
  41. public func execute(_ block: @escaping () -> Void) {
  42. switch self {
  43. case .mainAsync:
  44. DispatchQueue.main.async { block() }
  45. case .mainCurrentOrAsync:
  46. DispatchQueue.main.safeAsync { block() }
  47. case .untouch:
  48. block()
  49. case .dispatch(let queue):
  50. queue.async { block() }
  51. }
  52. }
  53. var queue: DispatchQueue {
  54. switch self {
  55. case .mainAsync: return .main
  56. case .mainCurrentOrAsync: return .main
  57. case .untouch: return OperationQueue.current?.underlyingQueue ?? .main
  58. case .dispatch(let queue): return queue
  59. }
  60. }
  61. }
  62. extension DispatchQueue {
  63. // This method will dispatch the `block` to self.
  64. // If `self` is the main queue, and current thread is main thread, the block
  65. // will be invoked immediately instead of being dispatched.
  66. func safeAsync(_ block: @escaping () -> Void) {
  67. if self === DispatchQueue.main && Thread.isMainThread {
  68. block()
  69. } else {
  70. async { block() }
  71. }
  72. }
  73. }