Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 0 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -865,75 +865,6 @@ struct FetchMasterDataAtom: ThrowingTaskAtom, KeepAlive, Hashable {

</details>

#### [Refreshable](https://ra1028.github.io/swiftui-atom-properties/documentation/atoms/refreshable)

`Refreshable` allows you to implement a custom refreshable behavior to an atom.

<details><summary><code>📖 Example</code></summary>

It adds custom refresh behavior to `ValueAtom` which is inherently unable to refresh.
It's useful when need to have arbitrary refresh behavior or implementing refresh when value depends on private atom.
In this example, `FetchMoviesPhaseAtom` transparently exposes the value of `FetchMoviesTaskAtom` as `AsyncPhase` so that the error can be handled easily inside the atom, and `Refreshable` gives refreshing behavior to `FetchMoviesPhaseAtom` itself.

```swift
private struct FetchMoviesTaskAtom: ThrowingTaskAtom, Hashable {
func value(context: Context) async throws -> [Movies] {
try await fetchMovies()
}
}

struct FetchMoviesPhaseAtom: ValueAtom, Refreshable, Hashable {
func value(context: Context) -> AsyncPhase<[Movies], any Error> {
context.watch(FetchMoviesTaskAtom().phase)
}

func refresh(context: CurrentContext) async -> AsyncPhase<[Movies], any Error> {
await context.refresh(FetchMoviesTaskAtom().phase)
}

func effect(context: CurrentContext) -> some AtomEffect {
UpdateEffect {
if case .failure = context.read(self) {
print("Failed to fetch movies.")
}
}
}
}
```

</details>

#### [Resettable](https://ra1028.github.io/swiftui-atom-properties/documentation/atoms/resettable)

`Resettable` allows you to implement a custom reset behavior to an atom.

<details><summary><code>📖 Example</code></summary>

It adds custom reset behavior to an atom that will be executed upon atom reset.
It's useful when need to have arbitrary reset behavior or implementing reset when value depends on private atom.
In following example, `RandomIntAtom` generates a random value using generated from private `RandomNumberGeneratorAtom`, and `Resettable` gives ability to replace exposed reset with `RandomNumberGeneratorAtom` reset.

```swift
struct RandomIntAtom: ValueAtom, Resettable, Hashable {
func value(context: Context) -> Int {
var generator = context.watch(RandomNumberGeneratorAtom())
return .random(in: 0..<100, using: &generator)
}

func reset(context: CurrentContext) {
context.reset(RandomNumberGeneratorAtom())
}
}

private struct RandomNumberGeneratorAtom: ValueAtom, Hashable {
func value(context: Context) -> CustomRandomNumberGenerator {
CustomRandomNumberGenerator()
}
}
```

</details>

---

### Property Wrapper
Expand Down
4 changes: 2 additions & 2 deletions Sources/Atoms/Atoms.docc/Atoms.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ Building state by compositing atoms automatically optimizes rendering based on i

- ``Scoped``
- ``KeepAlive``
- ``Refreshable``
- ``Resettable``

### Property Wrappers

Expand Down Expand Up @@ -95,5 +93,7 @@ Building state by compositing atoms automatically optimizes rendering based on i

### Deprecated

- ``Refreshable``
- ``Resettable``
- ``EmptyEffect``
- ``MergedEffect``