Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ReducerReader for building reducers out of state and action #1969

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -24,6 +24,7 @@
- ``Reduce``
- ``CombineReducers``
- ``EmptyReducer``
- ``ReducerReader``
- ``BindingReducer``

### Reducer modifiers
Expand Down
@@ -0,0 +1,24 @@
/// A reducer that builds a reducer from the current state and action.
public struct ReducerReader<State, Action, Reader: ReducerProtocol>: ReducerProtocol
where Reader.State == State, Reader.Action == Action {
@usableFromInline
let reader: (State, Action) -> Reader

/// Initializes a reducer that builds a reducer from the current state and action.
///
/// - Parameter reader: A reducer builder that has access to the current state and action.
@inlinable
public init(@ReducerBuilder<State, Action> _ reader: @escaping (State, Action) -> Reader) {
self.init(internal: reader)
}

@usableFromInline
init(internal reader: @escaping (State, Action) -> Reader) {
self.reader = reader
}

@inlinable
public func reduce(into state: inout State, action: Action) -> EffectTask<Action> {
self.reader(state, action).reduce(into: &state, action: action)
}
}
32 changes: 22 additions & 10 deletions Tests/ComposableArchitectureTests/ReducerBuilderTests.swift
Expand Up @@ -191,20 +191,32 @@ private struct Root: ReducerProtocol {

#if swift(>=5.7)
var body: some ReducerProtocol<State, Action> {
Scope(state: /State.featureA, action: /Action.featureA) {
Feature()
}
Scope(state: /State.featureB, action: /Action.featureB) {
Feature()
ReducerReader { state, _ in
switch state {
case .featureA:
Scope(state: /State.featureA, action: /Action.featureA) {
Feature()
}
case .featureB:
Scope(state: /State.featureB, action: /Action.featureB) {
Feature()
}
}
}
}
#else
var body: Reduce<State, Action> {
Scope(state: /State.featureA, action: /Action.featureA) {
Feature()
}
Scope(state: /State.featureB, action: /Action.featureB) {
Feature()
ReducerReader { state, _ in
switch state {
case .featureA:
Scope(state: /State.featureA, action: /Action.featureA) {
Feature()
}
case .featureB:
Scope(state: /State.featureB, action: /Action.featureB) {
Feature()
}
}
}
}
#endif
Expand Down
36 changes: 36 additions & 0 deletions Tests/ComposableArchitectureTests/ReducerReaderTests.swift
@@ -0,0 +1,36 @@
import ComposableArchitecture
import XCTest

@MainActor
final class StateReaderTests: XCTestCase {
func testDependenciesPropagate() async {
struct Feature: ReducerProtocol {
struct State: Equatable {}

enum Action: Equatable {
case tap
}

@Dependency(\.date.now) var now

#if swift(>=5.7)
var body: some ReducerProtocol<State, Action> {
ReducerReader { _, _ in
let _ = self.now
}
}
#else
var body: Reduce<State, Action> {
ReducerReader { _, _ in
let _ = self.now
}
}
#endif
}

let store = TestStore(initialState: Feature.State(), reducer: Feature()) {
$0.date.now = Date(timeIntervalSince1970: 1234567890)
}
await store.send(.tap)
}
}