Skip to content

Commit 06988a9

Browse files
authored
Merge pull request #7 from Lickability/feature/make-binding-publisher
Adds Publisher Option To MakeBinding Extension
2 parents fa7d476 + 945f9ea commit 06988a9

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

Sources/ViewStore/ViewStore+BindingAdditions.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import SwiftUI
99
import CasePaths
10+
import Combine
1011

1112
/// An extension on `ViewStore` that provides conveniences for creating `Binding`s.
1213
public extension ViewStore {
@@ -46,4 +47,28 @@ public extension ViewStore {
4647
self.send(actionCasePath.embed(()))
4748
}
4849
}
50+
51+
/// Provides a mechanism for creating `Binding`s that send their value to a `PassthroughSubject`, leveraging `KeyPath`s to reduce duplication and errors.
52+
/// - Parameters:
53+
/// - viewStateKeyPath: The `KeyPath` to the `ViewState` property that this binding wraps.
54+
/// - publisher: The publisher to send the value to.
55+
func makeBinding<Value>(viewStateKeyPath: KeyPath<ViewState, Value>, publisher: PassthroughSubject<Value, Never>) -> Binding<Value> {
56+
return .init {
57+
self.viewState[keyPath: viewStateKeyPath]
58+
} set: { value in
59+
publisher.send(value)
60+
}
61+
}
62+
63+
/// Provides a mechanism for creating `Binding`s that send their value to a `CurrentValueSubject`, leveraging `KeyPath`s to reduce duplication and errors.
64+
/// - Parameters:
65+
/// - viewStateKeyPath: The `KeyPath` to the `ViewState` property that this binding wraps.
66+
/// - publisher: The publisher to send the value to.
67+
func makeBinding<Value>(viewStateKeyPath: KeyPath<ViewState, Value>, publisher: CurrentValueSubject<Value, Never>) -> Binding<Value> {
68+
return .init {
69+
self.viewState[keyPath: viewStateKeyPath]
70+
} set: { value in
71+
publisher.send(value)
72+
}
73+
}
4974
}

0 commit comments

Comments
 (0)