From a4d11923c0262b55ae6a329c273f78875048c29a Mon Sep 17 00:00:00 2001 From: ra1028 Date: Wed, 9 Oct 2024 17:29:08 +0900 Subject: [PATCH] Update README --- README.md | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 751862ff..7bcc80de 100644 --- a/README.md +++ b/README.md @@ -514,11 +514,63 @@ struct MoviesView: View { +#### [AsyncPhaseAtom](https://ra1028.github.io/swiftui-atom-properties/documentation/atoms/asyncphaseatom) + +| |Description| +|:----------|:----------| +|Summary |Provides an `AsyncPhase` value that represents a result of the given asynchronous throwable function.| +|Output |`AsyncPhase` (`AsyncPhase` in Swift 5)| +|Use Case |Throwing or non-throwing asynchronous operation e.g. API call| + +Note: +The [typed throws](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0413-typed-throws.md) feature introduced in Swift 6 allows the Failure type of the produced AsyncPhase to be specified as any type or even non-throwing, but in Swift 5 without it, the Failure type is always be `any Error`. +Here is a chart of the syntax in `typed throws` and the type of resulting `AsyncPhase`. + +|Syntax |Shorthand |Produced | +|:------------------|:------------------|:-------------------------| +|`throws(E)` |`throws(E)` |`AsyncPhase` | +|`throws(any Error)`|`throws` |`AsyncPhase`| +|`throws(Never)` |`` |`AsyncPhase` | + +
📖 Example + +```swift +struct FetchTrendingSongsAtom: AsyncPhaseAtom, Hashable { + func value(context: Context) async throws(FetchSongsError) -> [Song] { + try await fetchTrendingSongs() + } +} + +struct TrendingSongsView: View { + @Watch(FetchTrendingSongsAtom()) + var phase + + var body: some View { + List { + switch phase { + case .success(let songs): + ForEach(songs, id: \.id) { song in + Text(song.title) + } + + case .failure(.noData): + Text("There are no currently trending songs.") + + case .failure(let error): + Text(error.localizedDescription) + } + } + } +} +``` + +
+ #### [AsyncSequenceAtom](https://ra1028.github.io/swiftui-atom-properties/documentation/atoms/asyncsequenceatom) | |Description| |:----------|:----------| -|Summary |Provides a `AsyncPhase` value that represents asynchronous, sequential elements of the given `AsyncSequence`.| +|Summary |Provides an `AsyncPhase` value that represents asynchronous, sequential elements of the given `AsyncSequence`.| |Output |`AsyncPhase`| |Use Case |Handle multiple asynchronous values e.g. web-sockets| @@ -555,7 +607,7 @@ struct NotificationView: View { | |Description| |:------------|:----------| -|Summary |Provides a `AsyncPhase` value that represents sequence of values of the given `Publisher`.| +|Summary |Provides an `AsyncPhase` value that represents sequence of values of the given `Publisher`.| |Output |`AsyncPhase`| |Use Case |Handle single or multiple asynchronous value(s) e.g. API call|