|
| 1 | +/// MIT License |
| 2 | +/// |
| 3 | +/// Copyright (c) 2020 Point-Free, Inc. |
| 4 | +/// |
| 5 | +/// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +/// of this software and associated documentation files (the "Software"), to deal |
| 7 | +/// in the Software without restriction, including without limitation the rights |
| 8 | +/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +/// copies of the Software, and to permit persons to whom the Software is |
| 10 | +/// furnished to do so, subject to the following conditions: |
| 11 | +/// |
| 12 | +/// The above copyright notice and this permission notice shall be included in all |
| 13 | +/// copies or substantial portions of the Software. |
| 14 | +/// |
| 15 | +/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +/// SOFTWARE. |
| 22 | + |
| 23 | +import Combine |
| 24 | +import Dispatch |
| 25 | + |
| 26 | +/// A scheduler that executes its work on the main queue as soon as possible. |
| 27 | +/// |
| 28 | +/// This scheduler is inspired by the |
| 29 | +/// [equivalent](https://github.com/ReactiveCocoa/ReactiveSwift/blob/58d92aa01081301549c48a4049e215210f650d07/Sources/Scheduler.swift#L92) |
| 30 | +/// scheduler in the [ReactiveSwift](https://github.com/ReactiveCocoa/ReactiveSwift) project. |
| 31 | +/// |
| 32 | +/// If `UIScheduler.shared.schedule` is invoked from the main thread then the unit of work will be |
| 33 | +/// performed immediately. This is in contrast to `DispatchQueue.main.schedule`, which will incur |
| 34 | +/// a thread hop before executing since it uses `DispatchQueue.main.async` under the hood. |
| 35 | +/// |
| 36 | +/// This scheduler can be useful for situations where you need work executed as quickly as |
| 37 | +/// possible on the main thread, and for which a thread hop would be problematic, such as when |
| 38 | +/// performing animations. |
| 39 | +@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) |
| 40 | +public struct UIScheduler: Scheduler { |
| 41 | + public typealias SchedulerOptions = Never |
| 42 | + public typealias SchedulerTimeType = DispatchQueue.SchedulerTimeType |
| 43 | + |
| 44 | + /// The shared instance of the UI scheduler. |
| 45 | + /// |
| 46 | + /// You cannot create instances of the UI scheduler yourself. Use only the shared instance. |
| 47 | + public static let shared = Self() |
| 48 | + |
| 49 | + public var now: SchedulerTimeType { DispatchQueue.main.now } |
| 50 | + public var minimumTolerance: SchedulerTimeType.Stride { DispatchQueue.main.minimumTolerance } |
| 51 | + |
| 52 | + public func schedule(options: SchedulerOptions? = nil, _ action: @escaping () -> Void) { |
| 53 | + if DispatchQueue.getSpecific(key: key) == value { |
| 54 | + action() |
| 55 | + } else { |
| 56 | + DispatchQueue.main.schedule(action) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public func schedule( |
| 61 | + after date: SchedulerTimeType, |
| 62 | + tolerance: SchedulerTimeType.Stride, |
| 63 | + options: SchedulerOptions? = nil, |
| 64 | + _ action: @escaping () -> Void |
| 65 | + ) { |
| 66 | + DispatchQueue.main.schedule(after: date, tolerance: tolerance, options: nil, action) |
| 67 | + } |
| 68 | + |
| 69 | + public func schedule( |
| 70 | + after date: SchedulerTimeType, |
| 71 | + interval: SchedulerTimeType.Stride, |
| 72 | + tolerance: SchedulerTimeType.Stride, |
| 73 | + options: SchedulerOptions? = nil, |
| 74 | + _ action: @escaping () -> Void |
| 75 | + ) -> Cancellable { |
| 76 | + DispatchQueue.main.schedule( |
| 77 | + after: date, interval: interval, tolerance: tolerance, options: nil, action |
| 78 | + ) |
| 79 | + } |
| 80 | + |
| 81 | + private init() { _ = setSpecific } |
| 82 | +} |
| 83 | + |
| 84 | +private let key = DispatchSpecificKey<UInt8>() |
| 85 | +private let value: UInt8 = 0 |
| 86 | +private var setSpecific: () = { DispatchQueue.main.setSpecific(key: key, value: value) }() |
0 commit comments