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

Commits on Mar 8, 2023

  1. Add ReducerReader for building reducers out of state and action

    We've experimented with this before and called it `_Observe`, but
    weren't using it. More and more community members have built their own
    versions of `_Observe` and called them `StateReader` and `ActionReader`,
    etc., which evokes `ScrollViewReader` and `GeometryReader` nicely, so
    let's consider shipping this tool as a first-class citizen.
    
    A couple example uses:
    
    ```swift
    var body: some ReducerProtocol<State, Action> {
      ReducerReader { state, _ in
        switch state {  // Compile-time failure if new cases are added
        case .loggedIn:
          Scope(state: /AppFeature.loggedIn, action: /AppFeature.loggedIn) {
            LoggedInFeature()
          }
        case .loggedOut:
          Scope(state: /AppFeature.loggedOut, action: /AppFeature.loggedOut) {
            LoggedOutFeature()
          }
        }
      }
    }
    ```
    
    ```swift
    var body: some ReducerProtocol<State, Action> {
      ReducerReader { state, action in
        if state.isAdmin && action.isAdmin {
          AdminFeature()
        }
      }
    }
    ```
    
    We'd love any feedback the community may have, especially from those
    that have used this kind of reducer in their applications.
    
    We think a single `ReducerReader` entity is the way to go vs. three
    reducers, one for reading state _and_ action (`ReducerReader`), and then
    one for just reading state (`StateReader`) and one for just reading
    actions (`ActionReader`), since you can simply ignore the value you
    don't need to read:
    
    ```swift
    // Instead of:
    StateReader { state in /* ... */ }
    // Do:
    ReducerReader { state, _ in /* ... */ }
    
    // Instead of:
    ActionReader { action in /* ... */ }
    // Do:
    ReducerReader { _, action in /* ... */ }
    ```
    stephencelis committed Mar 8, 2023
    Configuration menu
    Copy the full SHA
    559647b View commit details
    Browse the repository at this point in the history
  2. wip

    stephencelis committed Mar 8, 2023
    Configuration menu
    Copy the full SHA
    1a120ca View commit details
    Browse the repository at this point in the history
  3. fix

    stephencelis committed Mar 8, 2023
    Configuration menu
    Copy the full SHA
    b6d3747 View commit details
    Browse the repository at this point in the history