You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -24,6 +26,57 @@ AsyncReactor is a reactive architecural pattern to organize your Swift code.
24
26
### General
25
27
AsyncReactor is a reactive architecural pattern. The main component is an `AsyncReactor`. This `AsyncReactor` holds the `State` as well as the `Actions`.
26
28
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
+
importReactor
41
+
42
+
@Observable
43
+
publicclassCatFactsReactor: Reactor
44
+
45
+
public enum Action {
46
+
case loadCatFact
47
+
}
48
+
49
+
@Observable
50
+
publicclassState {
51
+
var fact: AsyncLoad<String> = .none
52
+
}
53
+
54
+
publicprivate(set) var state =State()
55
+
56
+
@ObservationIgnored
57
+
privatevar catFactService: CatFactService
58
+
59
+
publicinit() {
60
+
send(.loadCatFact)
61
+
}
62
+
63
+
publicfuncaction(_action: Action) async {
64
+
switch action {
65
+
case .loadCatFact:
66
+
state.fact= .loading
67
+
68
+
do {
69
+
let fact =tryawait catFactService.getRandomCatFact().fact
0 commit comments