CallbackQueue.swift 3.3 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 callback queue behaviors when an calling of closure be dispatched.
  29. ///
  30. /// - asyncMain: Dispatch the calling to `DispatchQueue.main` with an `async` behavior.
  31. /// - currentMainOrAsync: Dispatch the calling to `DispatchQueue.main` with an `async` behavior if current queue is not
  32. /// `.main`. Otherwise, call the closure immediately in current main queue.
  33. /// - untouch: Do not change the calling queue for closure.
  34. /// - dispatch: Dispatches to a specified `DispatchQueue`.
  35. public enum CallbackQueue {
  36. /// Dispatch the calling to `DispatchQueue.main` with an `async` behavior.
  37. case mainAsync
  38. /// Dispatch the calling to `DispatchQueue.main` with an `async` behavior if current queue is not
  39. /// `.main`. Otherwise, call the closure immediately in current main queue.
  40. case mainCurrentOrAsync
  41. /// Do not change the calling queue for closure.
  42. case untouch
  43. /// Dispatches to a specified `DispatchQueue`.
  44. case dispatch(DispatchQueue)
  45. public func execute(_ block: @escaping () -> Void) {
  46. switch self {
  47. case .mainAsync:
  48. DispatchQueue.main.async { block() }
  49. case .mainCurrentOrAsync:
  50. DispatchQueue.main.safeAsync { block() }
  51. case .untouch:
  52. block()
  53. case .dispatch(let queue):
  54. queue.async { block() }
  55. }
  56. }
  57. var queue: DispatchQueue {
  58. switch self {
  59. case .mainAsync: return .main
  60. case .mainCurrentOrAsync: return .main
  61. case .untouch: return OperationQueue.current?.underlyingQueue ?? .main
  62. case .dispatch(let queue): return queue
  63. }
  64. }
  65. }
  66. extension DispatchQueue {
  67. // This method will dispatch the `block` to self.
  68. // If `self` is the main queue, and current thread is main thread, the block
  69. // will be invoked immediately instead of being dispatched.
  70. func safeAsync(_ block: @escaping () -> Void) {
  71. if self === DispatchQueue.main && Thread.isMainThread {
  72. block()
  73. } else {
  74. async { block() }
  75. }
  76. }
  77. }