Created
April 17, 2025 08:55
-
-
Save jacobsapps/1ae8677cc762047eafe8bb65c1ee17f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension CADisplayLink { | |
static func events(mode: RunLoop.Mode = .default) -> AsyncStream<CADisplayLink> { | |
AsyncStream { continuation in | |
let displayLink = CADisplayLink( | |
target: DisplayLinkWrapper(continuation), | |
selector: #selector(DisplayLinkWrapper.tick) | |
) | |
displayLink.add(to: .main, forMode: mode) | |
continuation.onTermination = { @Sendable _ in | |
displayLink.invalidate() | |
} | |
} | |
} | |
} | |
private final class DisplayLinkWrapper { | |
let continuation: AsyncStream<CADisplayLink>.Continuation | |
init(_ continuation: AsyncStream<CADisplayLink>.Continuation) { | |
self.continuation = continuation | |
} | |
@objc func tick(_ displayLink: CADisplayLink) { | |
continuation.yield(displayLink) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment