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

Disfavor new subscript #2875

Merged
merged 3 commits into from
Feb 29, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@
}

extension Case where Value: BindableAction, Value.State: ObservableState {
@_disfavoredOverload
public subscript<Member: Equatable & Sendable>(
dynamicMember keyPath: WritableKeyPath<Value.State, Member>
) -> Case<Member> {
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 @@ -184,7 +184,7 @@ public struct BindingAction<Root>: CasePathable, Equatable, @unchecked Sendable
) -> AnyCasePath<BindingAction, Value> where Root: ObservableState {
AnyCasePath(
embed: { .set(keyPath, $0) },
extract: { $0.keyPath == keyPath ? $0.value as? Value : nil }
extract: { $0.keyPath == keyPath ? $0.value.base as? Value : nil }
)
}
#endif
Expand Down
35 changes: 35 additions & 0 deletions Tests/ComposableArchitectureTests/TestStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,17 @@
}
await store.receive(\.delegate.success, 42)
}

func testBindingTestStore_WhenStateAndActionHaveSameName() async {
let store = TestStore(initialState: .init()) {
SameNameForStateAndAction()
}
await store.send(.onAppear)
await store.receive(\.isOn)
await store.receive(\.binding.isOn) {
$0.isOn = true
}
}
}

private struct Client: DependencyKey {
Expand Down Expand Up @@ -625,4 +636,28 @@
case delete(IndexSet)
}
}

@Reducer
struct SameNameForStateAndAction {
@ObservableState
struct State: Equatable { var isOn = false }
enum Action: BindableAction {
case binding(BindingAction<State>)
case onAppear
case isOn(Bool)
}
var body: some ReducerOf<Self> {
BindingReducer()
Reduce { state, action in
switch action {
case .binding:
return .none
case .onAppear:
return .send(.isOn(true))
case .isOn:
return .send(.set(\.isOn, true))
}
}
}
}
#endif