Use AsyncStream.makeStream to fix Sendable conformance

This commit is contained in:
Sami Samhuri 2024-08-18 09:18:37 -07:00
parent 8e46d79376
commit 64edbb2ccd
No known key found for this signature in database

View file

@ -7,19 +7,13 @@
public import AVFoundation public import AVFoundation
public final class ExportSession: @unchecked Sendable { public final class ExportSession: Sendable {
// @unchecked Sendable because progress properties are mutable, it's safe though. public let progressStream: AsyncStream<Float>
public typealias ProgressStream = AsyncStream<Float> private let progressContinuation: AsyncStream<Float>.Continuation
public var progressStream: ProgressStream = ProgressStream(unfolding: { 0.0 })
private var progressContinuation: ProgressStream.Continuation?
public init() { public init() {
progressStream = AsyncStream { continuation in (progressStream, progressContinuation) = AsyncStream<Float>.makeStream()
progressContinuation = continuation
}
} }
public func export( public func export(
@ -49,7 +43,7 @@ public final class ExportSession: @unchecked Sendable {
) )
Task { [progressContinuation] in Task { [progressContinuation] in
for await progress in await sampleWriter.progressStream { for await progress in await sampleWriter.progressStream {
progressContinuation?.yield(progress) progressContinuation.yield(progress)
} }
} }
try await sampleWriter.writeSamples() try await sampleWriter.writeSamples()
@ -129,7 +123,7 @@ public final class ExportSession: @unchecked Sendable {
) )
Task { [progressContinuation] in Task { [progressContinuation] in
for await progress in await sampleWriter.progressStream { for await progress in await sampleWriter.progressStream {
progressContinuation?.yield(progress) progressContinuation.yield(progress)
} }
} }
try await sampleWriter.writeSamples() try await sampleWriter.writeSamples()