File tree Expand file tree Collapse file tree 3 files changed +40
-1
lines changed Expand file tree Collapse file tree 3 files changed +40
-1
lines changed Original file line number Diff line number Diff line change 11# master
22* Please add new entries at the top.*
331 . Fixed Result extensions ambiguity (#733 , kudos to @nekrich )
4-
4+ 1 . Add ` <~ ` binding operator to ` Signal.Observer ` ( # 635 , kudos to @ Marcocanc )
55
66# 6.0.0
771 . Dropped support for Swift 4.2 (Xcode 9)
Original file line number Diff line number Diff line change @@ -102,6 +102,33 @@ extension BindingTargetProvider {
102102 }
103103}
104104
105+ extension Signal . Observer {
106+ /// Binds a source to a target, updating the target's value to the latest
107+ /// value sent by the source.
108+ ///
109+ /// - note: Only `value` events will be forwarded to the Observer.
110+ /// The binding will automatically terminate when the target is
111+ /// deinitialized, or when the source sends a `completed` event.
112+ ///
113+ /// - parameters:
114+ /// - target: A target to be bond to.
115+ /// - source: A source to bind.
116+ ///
117+ /// - returns: A disposable that can be used to terminate binding before the
118+ /// deinitialization of the target or the source's `completed`
119+ /// event.
120+ @discardableResult
121+ public static func <~
122+ < Source: BindingSource >
123+ ( observer: Signal < Value , Error > . Observer , source: Source ) -> Disposable ?
124+ where Source. Value == Value
125+ {
126+ return source. producer. startWithValues { [ weak observer] in
127+ observer? . send ( value: $0)
128+ }
129+ }
130+ }
131+
105132/// A binding target that can be used with the `<~` operator.
106133public struct BindingTarget < Value> : BindingTargetProvider {
107134 public let lifetime : Lifetime
Original file line number Diff line number Diff line change @@ -201,6 +201,18 @@ class UnidirectionalBindingSpec: QuickSpec {
201201 expect ( value) . toEventually ( equal ( 2 ) )
202202 expect ( mainQueueCounter. value) . toEventually ( equal ( 2 ) )
203203 }
204+
205+ describe ( " observer binding operator " ) {
206+ it ( " should forward values to observer " ) {
207+ let targetPipe = Signal < Int ? , Never > . pipe ( )
208+ let sourcePipe = Signal < Int ? , Never > . pipe ( )
209+ let targetProperty = Property < Int ? > ( initial: nil , then: targetPipe. output)
210+ targetPipe. input <~ sourcePipe. output
211+ expect ( targetProperty. value) . to ( beNil ( ) )
212+ sourcePipe. input. send ( value: 1 )
213+ expect ( targetProperty. value) . to ( equal ( 1 ) )
214+ }
215+ }
204216 }
205217 }
206218}
You can’t perform that action at this time.
0 commit comments