Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Jun 28, 2023
1 parent 47d302c commit 35721f9
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,16 +277,16 @@ store.send(.set(\.$protectMyPosts, true)) {
```

> Tip: If you use `@BindingState` on a larger struct and would like to observe changes to smaller
> fields, apply the ``ReducerProtocol/onChange(of:_:)`` modifier to the ``BindingReducer``:
> fields, apply the ``Reducer/onChange(of:_:)`` modifier to the ``BindingReducer``:
>
> ```swift
> struct Settings: ReducerProtocol {
> struct Settings: Reducer {
> struct State {
> @BindingState var developerSettings: DeveloperSettings
> // ...
> }
> // ...
> var body: some ReducerProtocol<State, Action> {
> var body: some Reducer<State, Action> {
> BindingReducer()
> .onChange(of: \.developerSettings.showDiagnostics) { oldValue, newValue in
> // Logic for when `showDiagnostics` changes...
Expand Down
2 changes: 1 addition & 1 deletion Sources/ComposableArchitecture/Effect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public struct EffectPublisher<Action, Failure: Error> {
/// ```swift
/// let effect: EffectOf<Feature>
/// ```
public typealias EffectOf<R: ReducerProtocol> = EffectPublisher<R.Action, Never>
public typealias EffectOf<R: Reducer> = EffectPublisher<R.Action, Never>

// MARK: - Creating Effects

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import SwiftUI
/// If you forget to compose the ``BindingReducer`` into your feature's reducer, then when a binding
/// is written to it will cause a runtime purple Xcode warning letting you know what needs to be
/// fixed.
public struct BindingReducer<State, Action, ViewAction: BindableAction>: ReducerProtocol
public struct BindingReducer<State, Action, ViewAction: BindableAction>: Reducer
where State == ViewAction.State {
@usableFromInline
let toViewAction: (Action) -> ViewAction?
Expand Down
11 changes: 5 additions & 6 deletions Sources/ComposableArchitecture/Reducer/Reducers/OnChange.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
extension ReducerProtocol {
extension Reducer {
/// Adds a reducer to run when this reducer changes the given value in state.
///
/// Use this operator to trigger additional logic when a value changes, like when a
/// ``BindingReducer`` makes a deeper change to a struct held in ``BindingState``.
///
/// ```swift
/// struct Settings: ReducerProtocol {
/// struct Settings: Reducer {
/// struct State {
/// @BindingState var userSettings: UserSettings
/// // ...
Expand All @@ -16,7 +16,7 @@ extension ReducerProtocol {
/// // ...
/// }
///
/// var body: some ReducerProtocol<State, Action> {
/// var body: some Reducer<State, Action> {
/// BindingReducer()
/// .onChange(of: \.userSettings.isHapticFeedbackEnabled) { oldValue, newValue in
/// Reduce { state, action in
Expand Down Expand Up @@ -44,16 +44,15 @@ extension ReducerProtocol {
/// - newValue: The new value that failed the comparison check.
/// - Returns: A reducer that performs the
@inlinable
public func onChange<V: Equatable, R: ReducerProtocol>(
public func onChange<V: Equatable, R: Reducer>(
of toValue: @escaping (State) -> V,
@ReducerBuilder<State, Action> _ reducer: @escaping (_ oldValue: V, _ newValue: V) -> R
) -> _OnChangeReducer<Self, V, R> {
_OnChangeReducer(base: self, toValue: toValue, reducer: reducer)
}
}

public struct _OnChangeReducer<Base: ReducerProtocol, Value: Equatable, Body: ReducerProtocol>:
ReducerProtocol
public struct _OnChangeReducer<Base: Reducer, Value: Equatable, Body: Reducer>: Reducer
where Base.State == Body.State, Base.Action == Body.Action {
@usableFromInline
let base: Base
Expand Down
2 changes: 1 addition & 1 deletion Sources/ComposableArchitecture/SwiftUI/Binding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public struct BindingViewStore<State> {
) -> BindingViewState<Value> {
BindingViewState(
// OPTIMIZE: Can we derive bindings directly from `Store` and avoid the work of creating a `ViewStore`?
binding: ViewStore(self.store, removeDuplicates: { _, _ in false }).binding(
binding: ViewStore(self.store, observe: { $0 }, removeDuplicates: { _, _ in false }).binding(
get: { $0[keyPath: keyPath].wrappedValue },
send: { value in
#if DEBUG
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import XCTest
@MainActor
final class OnChangeReducerTests: BaseTCATestCase {
func testOnChange() async {
struct Feature: ReducerProtocol {
struct Feature: Reducer {
struct State: Equatable {
var count = 0
var description = ""
Expand All @@ -13,7 +13,7 @@ final class OnChangeReducerTests: BaseTCATestCase {
case incrementButtonTapped
case decrementButtonTapped
}
var body: some ReducerProtocolOf<Self> {
var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .decrementButtonTapped:
Expand Down

0 comments on commit 35721f9

Please sign in to comment.