Skip to content

Commit cbf8a45

Browse files
authored
Revert pointfreeco#1784: Resolve binding key path crash (pointfreeco#1799)
* Revert "Simplify/fix pointfreeco#1784 (pointfreeco#1785)" This reverts commit 626c35c. * Revert "resolve binding key path crash (pointfreeco#1784)" This reverts commit fbe6847.
1 parent 30015d1 commit cbf8a45

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

Sources/ComposableArchitecture/ViewStore.swift

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -466,14 +466,8 @@ public final class ViewStore<ViewState, ViewAction>: ObservableObject {
466466
get: @escaping (ViewState) -> Value,
467467
send valueToAction: @escaping (Value) -> ViewAction
468468
) -> Binding<Value> {
469-
ObservedObject(
470-
wrappedValue: ObservedState(
471-
initialValue: get(self.state),
472-
send: { self.send(valueToAction($0)) }
473-
)
474-
)
475-
.projectedValue
476-
.wrappedValue
469+
ObservedObject(wrappedValue: self)
470+
.projectedValue[get: .init(rawValue: get), send: .init(rawValue: valueToAction)]
477471
}
478472

479473
/// Derives a binding from the store that prevents direct writes to state and instead sends
@@ -565,6 +559,14 @@ public final class ViewStore<ViewState, ViewAction>: ObservableObject {
565559
public func binding(send action: ViewAction) -> Binding<ViewState> {
566560
self.binding(send: { _ in action })
567561
}
562+
563+
private subscript<Value>(
564+
get state: HashableWrapper<(ViewState) -> Value>,
565+
send action: HashableWrapper<(Value) -> ViewAction>
566+
) -> Value {
567+
get { state.rawValue(self.state) }
568+
set { self.send(action.rawValue(newValue)) }
569+
}
568570
}
569571

570572
/// A convenience type alias for referring to a view store of a given reducer's domain.
@@ -748,12 +750,8 @@ public struct StorePublisher<State>: Publisher {
748750
}
749751
}
750752

751-
private final class ObservedState<Value>: ObservableObject {
752-
@Published var wrappedValue: Value
753-
var cancellable: AnyCancellable?
754-
755-
init(initialValue: Value, send: @escaping (Value) -> Void) {
756-
self.wrappedValue = initialValue
757-
self.cancellable = self.$wrappedValue.dropFirst().sink(receiveValue: send)
758-
}
753+
private struct HashableWrapper<Value>: Hashable {
754+
let rawValue: Value
755+
static func == (lhs: Self, rhs: Self) -> Bool { false }
756+
func hash(into hasher: inout Hasher) {}
759757
}

0 commit comments

Comments
 (0)