|
| 1 | +#if compiler(>=6) |
| 2 | + /// An atom type that provides asynchronous, sequential elements of the given `AsyncSequence` |
| 3 | + /// as an ``AsyncPhase`` value. |
| 4 | + /// |
| 5 | + /// The sequential elements emitted by the `AsyncSequence` will be converted into an enum representation |
| 6 | + /// ``AsyncPhase`` that changes overtime. When the sequence emits new elements, it notifies changes to |
| 7 | + /// downstream atoms and views, so that they can consume it without suspension points which spawn with |
| 8 | + /// `await` keyword. |
| 9 | + /// |
| 10 | + /// ## Output Value |
| 11 | + /// |
| 12 | + /// ``AsyncPhase``<Self.Sequence.Element, Self.Sequence.Failure> |
| 13 | + /// |
| 14 | + /// ## Example |
| 15 | + /// |
| 16 | + /// ```swift |
| 17 | + /// struct QuakeMonitorAtom: AsyncSequenceAtom, Hashable { |
| 18 | + /// func sequence(context: Context) -> AsyncThrowingStream<Quake, QuakeMonitorError> { |
| 19 | + /// AsyncStream { continuation in |
| 20 | + /// let monitor = QuakeMonitor() |
| 21 | + /// monitor.quakeHandler = { result in |
| 22 | + /// continuation.yield(with: result) |
| 23 | + /// } |
| 24 | + /// continuation.onTermination = { @Sendable _ in |
| 25 | + /// monitor.stopMonitoring() |
| 26 | + /// } |
| 27 | + /// monitor.startMonitoring() |
| 28 | + /// } |
| 29 | + /// } |
| 30 | + /// } |
| 31 | + /// |
| 32 | + /// struct QuakeMonitorView: View { |
| 33 | + /// @Watch(QuakeMonitorAtom()) |
| 34 | + /// var quakes |
| 35 | + /// |
| 36 | + /// var body: some View { |
| 37 | + /// switch quakes { |
| 38 | + /// case .suspending, .failure: |
| 39 | + /// Text("Calm") |
| 40 | + /// |
| 41 | + /// case .failure(let error): |
| 42 | + /// Text("Failed: \(error.localizedDescription)") |
| 43 | + /// |
| 44 | + /// case .success(let quake): |
| 45 | + /// Text("Quake: \(quake.date)") |
| 46 | + /// } |
| 47 | + /// } |
| 48 | + /// } |
| 49 | + /// ``` |
| 50 | + /// |
| 51 | + @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
| 52 | + public protocol AsyncSequenceAtom: AsyncAtom where Produced == AsyncPhase<Sequence.Element, Sequence.Failure> { |
| 53 | + /// The type of asynchronous sequence that this atom manages. |
| 54 | + associatedtype Sequence: AsyncSequence where Sequence.Element: Sendable |
| 55 | + |
| 56 | + /// Creates an asynchronous sequence to be started when this atom is actually used. |
| 57 | + /// |
| 58 | + /// The sequence that is produced by this method must be instantiated anew each time this method |
| 59 | + /// is called. Otherwise, it could throw a fatal error because Swift Concurrency doesn't allow |
| 60 | + /// single `AsyncSequence` instance to be shared between multiple subscriptions. |
| 61 | + /// |
| 62 | + /// - Parameter context: A context structure to read, watch, and otherwise |
| 63 | + /// interact with other atoms. |
| 64 | + /// |
| 65 | + /// - Returns: An asynchronous sequence that produces asynchronous, sequential elements. |
| 66 | + @MainActor |
| 67 | + func sequence(context: Context) -> Sequence |
| 68 | + } |
| 69 | + |
| 70 | + @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
| 71 | + public extension AsyncSequenceAtom { |
| 72 | + var producer: AtomProducer<Produced> { |
| 73 | + AtomProducer { context in |
| 74 | + let sequence = context.transaction(sequence) |
| 75 | + let task = Task { |
| 76 | + do throws(Sequence.Failure) { |
| 77 | + for try await element in sequence { |
| 78 | + if !Task.isCancelled { |
| 79 | + context.update(with: .success(element)) |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + catch { |
| 84 | + if !Task.isCancelled { |
| 85 | + context.update(with: .failure(error)) |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + context.onTermination = task.cancel |
| 91 | + return .suspending |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + var refreshProducer: AtomRefreshProducer<Produced> { |
| 96 | + AtomRefreshProducer { context in |
| 97 | + let sequence = context.transaction(sequence) |
| 98 | + let task = Task { |
| 99 | + var phase = Produced.suspending |
| 100 | + |
| 101 | + do throws(Sequence.Failure) { |
| 102 | + for try await element in sequence { |
| 103 | + if !Task.isCancelled { |
| 104 | + phase = .success(element) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + catch { |
| 109 | + if !Task.isCancelled { |
| 110 | + phase = .failure(error) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return phase |
| 115 | + } |
| 116 | + |
| 117 | + context.onTermination = task.cancel |
| 118 | + |
| 119 | + return await withTaskCancellationHandler { |
| 120 | + await task.value |
| 121 | + } onCancel: { |
| 122 | + task.cancel() |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +#else |
| 128 | + /// Deprecated. |
| 129 | + /// |
| 130 | + /// - SeeAlso: ``AsyncThrowingSequenceAtom`` |
| 131 | + @available(*, deprecated, renamed: "AsyncThrowingSequenceAtom") |
| 132 | + public typealias AsyncSequenceAtom = AsyncThrowingSequenceAtom |
| 133 | +#endif |
0 commit comments