Skip to content

Commit 2ddf47f

Browse files
Disfavor new subscript (#2875)
* Disfavor case key path for binding actions. * wip * fix --------- Co-authored-by: Stephen Celis <[email protected]>
1 parent 9dcbfd9 commit 2ddf47f

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

Sources/ComposableArchitecture/Observation/Binding+Observation.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@
224224
}
225225

226226
extension Case where Value: BindableAction, Value.State: ObservableState {
227+
@_disfavoredOverload
227228
public subscript<Member: Equatable & Sendable>(
228229
dynamicMember keyPath: WritableKeyPath<Value.State, Member>
229230
) -> Case<Member> {

Sources/ComposableArchitecture/SwiftUI/Binding.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public struct BindingAction<Root>: CasePathable, Equatable, @unchecked Sendable
184184
) -> AnyCasePath<BindingAction, Value> where Root: ObservableState {
185185
AnyCasePath(
186186
embed: { .set(keyPath, $0) },
187-
extract: { $0.keyPath == keyPath ? $0.value as? Value : nil }
187+
extract: { $0.keyPath == keyPath ? $0.value.base as? Value : nil }
188188
)
189189
}
190190
#endif

Tests/ComposableArchitectureTests/TestStoreTests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,17 @@
597597
}
598598
await store.receive(\.delegate.success, 42)
599599
}
600+
601+
func testBindingTestStore_WhenStateAndActionHaveSameName() async {
602+
let store = TestStore(initialState: .init()) {
603+
SameNameForStateAndAction()
604+
}
605+
await store.send(.onAppear)
606+
await store.receive(\.isOn)
607+
await store.receive(\.binding.isOn) {
608+
$0.isOn = true
609+
}
610+
}
600611
}
601612

602613
private struct Client: DependencyKey {
@@ -625,4 +636,28 @@
625636
case delete(IndexSet)
626637
}
627638
}
639+
640+
@Reducer
641+
struct SameNameForStateAndAction {
642+
@ObservableState
643+
struct State: Equatable { var isOn = false }
644+
enum Action: BindableAction {
645+
case binding(BindingAction<State>)
646+
case onAppear
647+
case isOn(Bool)
648+
}
649+
var body: some ReducerOf<Self> {
650+
BindingReducer()
651+
Reduce { state, action in
652+
switch action {
653+
case .binding:
654+
return .none
655+
case .onAppear:
656+
return .send(.isOn(true))
657+
case .isOn:
658+
return .send(.set(\.isOn, true))
659+
}
660+
}
661+
}
662+
}
628663
#endif

0 commit comments

Comments
 (0)