Skip to content
Closed
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:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to go to BREAKING CHANGES section after.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@plajdo can you please create an MD.file tracking the breaking changes for each version. Its easier to maintain if its part of the PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot push to this branch anymore, I will create a new PR from fork. If changelog is needed, please create one separately.

However: I checked that this change should not affect any of company's projects, so no further changes are necessary (calling start manually was a recommended pattern anyway).


```swift
.task { model.start() }
```
Comment thread
plajdo marked this conversation as resolved.

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()
}
Comment thread
plajdo marked this conversation as resolved.

}
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()
Comment thread
plajdo marked this conversation as resolved.

#if canImport(Combine)
Expand Down