|
| 1 | +import Foundation |
| 2 | +import QuartzCore |
| 3 | +import Combine |
| 4 | + |
| 5 | + |
| 6 | +// A publisher that emits new values when the system is about to update the display. |
| 7 | +public final class DisplayLink: Publisher { |
| 8 | + public typealias Output = Frame |
| 9 | + public typealias Failure = Never |
| 10 | + |
| 11 | + private let platformDisplayLink: PlatformDisplayLink |
| 12 | + |
| 13 | + private var subscribers: [CombineIdentifier:AnySubscriber<Frame, Never>] = [:] { |
| 14 | + didSet { |
| 15 | + dispatchPrecondition(condition: .onQueue(.main)) |
| 16 | + platformDisplayLink.isPaused = subscribers.isEmpty |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + fileprivate init(platformDisplayLink: PlatformDisplayLink) { |
| 21 | + dispatchPrecondition(condition: .onQueue(.main)) |
| 22 | + self.platformDisplayLink = platformDisplayLink |
| 23 | + self.platformDisplayLink.onFrame = { [weak self] frame in |
| 24 | + self?.send(frame: frame) |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + public func receive<S>(subscriber: S) where S : Subscriber, S.Failure == Never, S.Input == Frame { |
| 29 | + dispatchPrecondition(condition: .onQueue(.main)) |
| 30 | + |
| 31 | + let typeErased = AnySubscriber(subscriber) |
| 32 | + let identifier = typeErased.combineIdentifier |
| 33 | + let subscription = Subscription(onCancel: { [weak self] in |
| 34 | + self?.cancelSubscription(for: identifier) |
| 35 | + }) |
| 36 | + subscribers[identifier] = typeErased |
| 37 | + subscriber.receive(subscription: subscription) |
| 38 | + } |
| 39 | + |
| 40 | + private func cancelSubscription(for identifier: CombineIdentifier) { |
| 41 | + dispatchPrecondition(condition: .onQueue(.main)) |
| 42 | + subscribers.removeValue(forKey: identifier) |
| 43 | + } |
| 44 | + |
| 45 | + private func send(frame: Frame) { |
| 46 | + dispatchPrecondition(condition: .onQueue(.main)) |
| 47 | + let subscribers = self.subscribers.values |
| 48 | + subscribers.forEach { |
| 49 | + _ = $0.receive(frame) // Ignore demand |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | +} |
| 54 | + |
| 55 | +extension DisplayLink { |
| 56 | + |
| 57 | + // Represents a frame that is about to be drawn |
| 58 | + public struct Frame { |
| 59 | + |
| 60 | + // The system timestamp for the frame to be drawn |
| 61 | + public var timestamp: TimeInterval |
| 62 | + |
| 63 | + // The duration between each display update |
| 64 | + public var duration: TimeInterval |
| 65 | + } |
| 66 | + |
| 67 | +} |
| 68 | + |
| 69 | +extension DisplayLink { |
| 70 | + |
| 71 | + @available(iOS 13.0, tvOS 13.0, *) |
| 72 | + public convenience init() { |
| 73 | + self.init(platformDisplayLink: CADisplayLinkPlatformDisplayLink()) |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | +} |
| 78 | + |
| 79 | +extension DisplayLink { |
| 80 | + public static let shared = DisplayLink() |
| 81 | +} |
| 82 | + |
| 83 | +extension DisplayLink { |
| 84 | + |
| 85 | + fileprivate final class Subscription: Combine.Subscription { |
| 86 | + |
| 87 | + var onCancel: () -> Void |
| 88 | + |
| 89 | + init(onCancel: @escaping () -> Void) { |
| 90 | + self.onCancel = onCancel |
| 91 | + } |
| 92 | + |
| 93 | + func request(_ demand: Subscribers.Demand) { |
| 94 | + // Do nothing – subscribers can't impact how often the system draws frames. |
| 95 | + } |
| 96 | + |
| 97 | + func cancel() { |
| 98 | + onCancel() |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | +} |
| 103 | + |
| 104 | +fileprivate protocol PlatformDisplayLink: class { |
| 105 | + var onFrame: (DisplayLink.Frame) -> Void { get set } |
| 106 | + var isPaused: Bool { get set } |
| 107 | +} |
| 108 | + |
| 109 | + |
| 110 | +@available(iOS 13.0, tvOS 13.0, *) |
| 111 | +final class CADisplayLinkPlatformDisplayLink: PlatformDisplayLink { |
| 112 | + |
| 113 | + private var displayLink: CADisplayLink! |
| 114 | + |
| 115 | + var onFrame: (DisplayLink.Frame) -> Void = { _ in } |
| 116 | + |
| 117 | + var isPaused: Bool { |
| 118 | + get { displayLink.isPaused } |
| 119 | + set { displayLink.isPaused = newValue } |
| 120 | + } |
| 121 | + |
| 122 | + init() { |
| 123 | + displayLink = CADisplayLink(target: self, selector: #selector(displayLinkDidFire(_:))) |
| 124 | + displayLink.add(to: RunLoop.main, forMode: .common) |
| 125 | + displayLink.isPaused = true |
| 126 | + } |
| 127 | + |
| 128 | + @objc private func displayLinkDidFire(_ link: CADisplayLink) { |
| 129 | + let frame = DisplayLink.Frame( |
| 130 | + timestamp: link.timestamp, |
| 131 | + duration: link.duration) |
| 132 | + onFrame(frame) |
| 133 | + } |
| 134 | +} |
0 commit comments