Skip to content

Commit 369c304

Browse files
authored
Merge pull request #16 from lucatp/updated-readme-ios17
Updated Readme
2 parents 2329227 + afc3ce9 commit 369c304

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ AsyncReactor is a reactive architecural pattern to organize your Swift code.
1313
## Table of Contents
1414
- [Usage](#usage)
1515
- [General](#general)
16+
- [Reactor Package (iOS 17+)](#reactor-package-ios-17)
17+
- [AsyncReactor Package (iOS <17)](#asyncreactor-package-ios-17)
1618
- [Integration](#integration)
1719
- [State updates](#state-updates)
1820
- [Actions](#actions)
@@ -24,6 +26,57 @@ AsyncReactor is a reactive architecural pattern to organize your Swift code.
2426
### General
2527
AsyncReactor is a reactive architecural pattern. The main component is an `AsyncReactor`. This `AsyncReactor` holds the `State` as well as the `Actions`.
2628

29+
### Reactor Package (iOS 17+)
30+
31+
With iOS 17 and later, we introduced the new **`Reactor`** package, which includes a protocol also named `Reactor`.
32+
33+
Thanks to the new `@Observable` macro in Swift, there’s no longer the need to explicitly mark the `State` as `@Published`. By addibg the `@Observable` macro to your reactor class, the entire class is now automatically observable. If you want to exclude specific properties from observation, you can use the `@ObservationIgnored` macro.
34+
35+
The way actions are handled remains the same as before — simply define an `Action` enum and implement the `action(_:)` method asynchronously.
36+
37+
> ✅ Both versions of the Reactor (pre-iOS 17 and the new iOS 17+ version) can coexist in your app, as they are implemented in separate packages.
38+
39+
```Swift
40+
import Reactor
41+
42+
@Observable
43+
public class CatFactsReactor: Reactor
44+
45+
public enum Action {
46+
case loadCatFact
47+
}
48+
49+
@Observable
50+
public class State {
51+
var fact: AsyncLoad<String> = .none
52+
}
53+
54+
public private(set) var state = State()
55+
56+
@ObservationIgnored
57+
private var catFactService: CatFactService
58+
59+
public init() {
60+
send(.loadCatFact)
61+
}
62+
63+
public func action(_ action: Action) async {
64+
switch action {
65+
case .loadCatFact:
66+
state.fact = .loading
67+
68+
do {
69+
let fact = try await catFactService.getRandomCatFact().fact
70+
state.fact = .loaded(fact)
71+
} catch {
72+
state.fact = .error(error)
73+
}
74+
}
75+
}
76+
```
77+
78+
## AsyncReactor Package (iOS <17)
79+
2780
```Swift
2881
class RepositorySearchReactor: AsyncReactor {
2982
enum Action {

0 commit comments

Comments
 (0)