Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ra1028 committed Oct 9, 2024
1 parent c4b5eb5 commit ac7e34d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
62 changes: 62 additions & 0 deletions Sources/Atoms/Atom/AsyncPhaseAtom.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,76 @@
/// An atom that provides an ``AsyncPhase`` value from the asynchronous throwable function.
///
/// The value produced by the given asynchronous throwable function will be converted into
/// an enum representation ``AsyncPhase`` that changes when the process is done or thrown an error.
///
/// ## Output Value
///
/// ``AsyncPhase``<Self.Success, Self.Failure>
///
/// ## Example
///
/// ```swift
/// struct AsyncTextAtom: AsyncPhaseAtom, Hashable {
/// func value(context: Context) async throws -> String {
/// try await Task.sleep(nanoseconds: 1_000_000_000)
/// return "Swift"
/// }
/// }
///
/// struct DelayedTitleView: View {
/// @Watch(AsyncTextAtom())
/// var text
///
/// var body: some View {
/// switch text {
/// case .success(let text):
/// Text(text)
///
/// case .suspending:
/// Text("Loading")
///
/// case .failure:
/// Text("Failed")
/// }
/// }
/// ```
///
public protocol AsyncPhaseAtom: AsyncAtom where Produced == AsyncPhase<Success, Failure> {
/// The type of success value that this atom produces.
associatedtype Success

#if compiler(>=6)
/// The type of errors that this atom produces.
associatedtype Failure: Error

/// Asynchronously produces a value to be provided via this atom.
///
/// Values provided or errors thrown by this method are converted to the unified enum
/// representation ``AsyncPhase``.
///
/// - Parameter context: A context structure to read, watch, and otherwise
/// interact with other atoms.
///
/// - Throws: The error that occurred during the process of creating the resulting value.
///
/// - Returns: The process's result.
@MainActor
func value(context: Context) async throws(Failure) -> Success
#else
/// The type of errors that this atom produces.
typealias Failure = any Error

/// Asynchronously produces a value to be provided via this atom.
///
/// Values provided or errors thrown by this method are converted to the unified enum
/// representation ``AsyncPhase``.
///
/// - Parameter context: A context structure to read, watch, and otherwise
/// interact with other atoms.
///
/// - Throws: The error that occurred during the process of creating the resulting value.
///
/// - Returns: The process's result.
@MainActor
func value(context: Context) async throws -> Success
#endif
Expand Down
2 changes: 1 addition & 1 deletion Sources/Atoms/Atom/TaskAtom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public protocol TaskAtom: AsyncAtom where Produced == Task<Success, Never> {
/// - Parameter context: A context structure to read, watch, and otherwise
/// interact with other atoms.
///
/// - Returns: A nonthrowing `Task` that produces asynchronous value.
/// - Returns: The process's result.
@MainActor
func value(context: Context) async -> Success
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Atoms/Atom/ThrowingTaskAtom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public protocol ThrowingTaskAtom: AsyncAtom where Produced == Task<Success, any
///
/// - Throws: The error that occurred during the process of creating the resulting value.
///
/// - Returns: A throwing `Task` that produces asynchronous value.
/// - Returns: The process's result.
@MainActor
func value(context: Context) async throws -> Success
}
Expand Down

0 comments on commit ac7e34d

Please sign in to comment.