|
| 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 | +/// |
1 | 38 | public protocol AsyncPhaseAtom: AsyncAtom where Produced == AsyncPhase<Success, Failure> { |
| 39 | + /// The type of success value that this atom produces. |
2 | 40 | associatedtype Success |
3 | 41 |
|
4 | 42 | #if compiler(>=6) |
| 43 | + /// The type of errors that this atom produces. |
5 | 44 | associatedtype Failure: Error |
6 | 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. |
7 | 57 | @MainActor |
8 | 58 | func value(context: Context) async throws(Failure) -> Success |
9 | 59 | #else |
| 60 | + /// The type of errors that this atom produces. |
10 | 61 | typealias Failure = any Error |
11 | 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. |
12 | 74 | @MainActor |
13 | 75 | func value(context: Context) async throws -> Success |
14 | 76 | #endif |
|
0 commit comments