|
| 1 | +/// An atom that provides an ``AsyncPhase`` value from the asynchronous throwable function. |
| 2 | +/// |
| 3 | +/// The value produced by the given asynchronous throwable function will be converted into |
| 4 | +/// an enum representation ``AsyncPhase`` that changes when the process is done or thrown an error. |
| 5 | +/// |
| 6 | +/// ## Output Value |
| 7 | +/// |
| 8 | +/// ``AsyncPhase``<Self.Success, Self.Failure> |
| 9 | +/// |
| 10 | +/// ## Example |
| 11 | +/// |
| 12 | +/// ```swift |
| 13 | +/// struct AsyncTextAtom: AsyncPhaseAtom, Hashable { |
| 14 | +/// func value(context: Context) async throws -> String { |
| 15 | +/// try await Task.sleep(nanoseconds: 1_000_000_000) |
| 16 | +/// return "Swift" |
| 17 | +/// } |
| 18 | +/// } |
| 19 | +/// |
| 20 | +/// struct DelayedTitleView: View { |
| 21 | +/// @Watch(AsyncTextAtom()) |
| 22 | +/// var text |
| 23 | +/// |
| 24 | +/// var body: some View { |
| 25 | +/// switch text { |
| 26 | +/// case .success(let text): |
| 27 | +/// Text(text) |
| 28 | +/// |
| 29 | +/// case .suspending: |
| 30 | +/// Text("Loading") |
| 31 | +/// |
| 32 | +/// case .failure: |
| 33 | +/// Text("Failed") |
| 34 | +/// } |
| 35 | +/// } |
| 36 | +/// ``` |
| 37 | +/// |
| 38 | +public protocol AsyncPhaseAtom: AsyncAtom where Produced == AsyncPhase<Success, Failure> { |
| 39 | + /// The type of success value that this atom produces. |
| 40 | + associatedtype Success |
| 41 | + |
| 42 | + #if compiler(>=6) |
| 43 | + /// The type of errors that this atom produces. |
| 44 | + associatedtype Failure: Error |
| 45 | + |
| 46 | + /// Asynchronously produces a value to be provided via this atom. |
| 47 | + /// |
| 48 | + /// Values provided or errors thrown by this method are converted to the unified enum |
| 49 | + /// representation ``AsyncPhase``. |
| 50 | + /// |
| 51 | + /// - Parameter context: A context structure to read, watch, and otherwise |
| 52 | + /// interact with other atoms. |
| 53 | + /// |
| 54 | + /// - Throws: The error that occurred during the process of creating the resulting value. |
| 55 | + /// |
| 56 | + /// - Returns: The process's result. |
| 57 | + @MainActor |
| 58 | + func value(context: Context) async throws(Failure) -> Success |
| 59 | + #else |
| 60 | + /// The type of errors that this atom produces. |
| 61 | + typealias Failure = any Error |
| 62 | + |
| 63 | + /// Asynchronously produces a value to be provided via this atom. |
| 64 | + /// |
| 65 | + /// Values provided or errors thrown by this method are converted to the unified enum |
| 66 | + /// representation ``AsyncPhase``. |
| 67 | + /// |
| 68 | + /// - Parameter context: A context structure to read, watch, and otherwise |
| 69 | + /// interact with other atoms. |
| 70 | + /// |
| 71 | + /// - Throws: The error that occurred during the process of creating the resulting value. |
| 72 | + /// |
| 73 | + /// - Returns: The process's result. |
| 74 | + @MainActor |
| 75 | + func value(context: Context) async throws -> Success |
| 76 | + #endif |
| 77 | +} |
| 78 | + |
| 79 | +public extension AsyncPhaseAtom { |
| 80 | + var producer: AtomProducer<Produced> { |
| 81 | + AtomProducer { context in |
| 82 | + let task = Task { |
| 83 | + #if compiler(>=6) |
| 84 | + do throws(Failure) { |
| 85 | + let value = try await context.transaction(value) |
| 86 | + |
| 87 | + if !Task.isCancelled { |
| 88 | + context.update(with: .success(value)) |
| 89 | + } |
| 90 | + } |
| 91 | + catch { |
| 92 | + if !Task.isCancelled { |
| 93 | + context.update(with: .failure(error)) |
| 94 | + } |
| 95 | + } |
| 96 | + #else |
| 97 | + do { |
| 98 | + let value = try await context.transaction(value) |
| 99 | + |
| 100 | + if !Task.isCancelled { |
| 101 | + context.update(with: .success(value)) |
| 102 | + } |
| 103 | + } |
| 104 | + catch { |
| 105 | + if !Task.isCancelled { |
| 106 | + context.update(with: .failure(error)) |
| 107 | + } |
| 108 | + } |
| 109 | + #endif |
| 110 | + } |
| 111 | + |
| 112 | + context.onTermination = task.cancel |
| 113 | + return .suspending |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + var refreshProducer: AtomRefreshProducer<Produced> { |
| 118 | + AtomRefreshProducer { context in |
| 119 | + var phase = Produced.suspending |
| 120 | + |
| 121 | + let task = Task { |
| 122 | + #if compiler(>=6) |
| 123 | + do throws(Failure) { |
| 124 | + let value = try await context.transaction(value) |
| 125 | + |
| 126 | + if !Task.isCancelled { |
| 127 | + phase = .success(value) |
| 128 | + } |
| 129 | + } |
| 130 | + catch { |
| 131 | + if !Task.isCancelled { |
| 132 | + phase = .failure(error) |
| 133 | + } |
| 134 | + } |
| 135 | + #else |
| 136 | + do { |
| 137 | + let value = try await context.transaction(value) |
| 138 | + |
| 139 | + if !Task.isCancelled { |
| 140 | + phase = .success(value) |
| 141 | + } |
| 142 | + } |
| 143 | + catch { |
| 144 | + if !Task.isCancelled { |
| 145 | + phase = .failure(error) |
| 146 | + } |
| 147 | + } |
| 148 | + #endif |
| 149 | + } |
| 150 | + |
| 151 | + return await withTaskCancellationHandler { |
| 152 | + await task.value |
| 153 | + return phase |
| 154 | + } onCancel: { |
| 155 | + task.cancel() |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments