You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
3.3 KiB

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