Skip to content
Open
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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ You add the ViewModel as a property wrapper to your view:
@ViewModel var model: AnyReactor = MyViewModel().eraseToAnyReactor()
```

`AnyReactor` does not start the wrapped reactor automatically. Start it explicitly:

```swift
.task { model.start() }
```

To access the current `State` you use:

```swift
Expand Down Expand Up @@ -200,4 +206,3 @@ You can easily mock state for Xcode Previews by using `Stub` reactor implementat

# License
GoodReactor repository is released under the MIT license. See [LICENSE](LICENSE.md) for details.

8 changes: 5 additions & 3 deletions Sources/GoodReactor/Core/Erased/AnyReactor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import SwiftUI
///
/// Behavior:
/// - State is accessed dynamically and mutated by reducing events on a concrete underlying reactor.
/// - Lifecycle: external subscriptions start automatically when `AnyReactor` is initialized (by `start()`-ing the base reactor).
/// - Lifecycle: `AnyReactor` does not automatically start the wrapped reactor.
/// Call `start()` explicitly to initialize external subscriptions.
/// - Events from `send(action:)`, `send(action:) async`, and `send(destination:)` are forwarded to the base reactor.
///
/// Example:
Expand Down Expand Up @@ -58,9 +59,11 @@ import SwiftUI

// MARK: - Initialization

/// Creates a type-erased wrapper around `base`.
///
/// - note: This initializer does not call `start()` on `base`.
public init<R: Reactor>(_ base: R) where R.Action == Action, R.Destination == Destination, R.State == State {
self._box = AnyReactorBox(base)
base.start()
}

}
Expand Down Expand Up @@ -151,4 +154,3 @@ public extension Reactor {
}

}

8 changes: 8 additions & 0 deletions Sources/GoodReactor/Core/Reactor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ import SwiftUI
/// of the reactor.
///
/// You can use multiple subscriptions to multiple external events.
///
/// - important: Keep this function free of side effects, except calls to
/// ``subscribe(to:map:)``. This function may be invoked multiple times.
func transform()

#if canImport(Combine)
Expand Down Expand Up @@ -329,6 +332,11 @@ public extension Reactor {
/// uses Combine, subscribes the event stream and publishes the
/// initial state.
func start() {
let hasSubscriptions = MapTables.subscriptions.value(forKey: self)?.isEmpty == false
guard !hasSubscriptions else {
return
}

self.transform()

#if canImport(Combine)
Expand Down