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() |
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
@State private var fps: Double = 0 | |
@State private var lastTimestamp: TimeInterval = 0 | |
private let smoothingFactor: Double = 0.1 | |
// ... | |
.task { | |
for await link in CADisplayLink.events() { | |
updateFPS(link) | |
} |
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
private var continuation: | |
AsyncThrowingStream<Double, Error>.Continuation? | |
func urlSession(_ session: URLSession, | |
downloadTask: URLSessionDownloadTask, | |
didWriteData bytesWritten: Int64, | |
totalBytesWritten: Int64, | |
totalBytesExpectedToWrite: Int64) { | |
guard let continuation else { return } | |
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
func downloadStream() -> AsyncThrowingStream<Double, Error> { | |
AsyncThrowingStream { continuation in | |
Task { | |
var percentage = 0.0 | |
while percentage < 70 { | |
try await Task.sleep(for: .milliseconds(10)) | |
let bits = Double.random(in: 0...0.3) | |
percentage += bits | |
continuation.yield(with: .success(percentage)) | |
} |
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
@State private var progress: Double = 0 | |
@State private var showError: Bool = false | |
// ... | |
.task { | |
do { | |
for try await percentage in DownloadAPI.downloadStream() { | |
progress = percentage | |
} |
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
func locationManager(_ manager: CLLocationManager, | |
didUpdateHeading newHeading: CLHeading) { | |
rotationCallback?(-newHeading.magneticHeading) | |
} |
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
final class LocationManager: CLLocationManager, CLLocationManagerDelegate { | |
private var rotationCallback: ((Double) -> Void)? | |
var rotationAngleStream: AsyncStream<Double> { | |
AsyncStream(bufferingPolicy: .bufferingNewest(1)) { continuation in | |
rotationCallback = { | |
continuation.yield($0) | |
} | |
} |
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
@State private var rotationAngle: Angle = .degrees(0) | |
// ... | |
.task { | |
for await angle in LocationManager.shared.rotationAngleStream { | |
rotationAngle = Angle.degrees(angle) | |
} | |
} |
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
struct Counter: AsyncSequence { | |
typealias Element = Int | |
let howHigh: Int | |
struct AsyncIterator: AsyncIteratorProtocol { | |
let howHigh: Int | |
var current = 1 | |
mutating func next() async -> Int? { | |
guard current <= howHigh else { |
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
.task { | |
for try await nextValue in asyncSequence { | |
print(nextValue) | |
} | |
} |
NewerOlder