@@ -124,53 +124,41 @@ public struct BindingTarget<Value>: BindingTargetProvider {
124124 return self
125125 }
126126
127- /// Creates a binding target.
127+ /// Creates a binding target which consumes values on the specified scheduler.
128+ ///
129+ /// If no scheduler is specified, the binding target would consume the value
130+ /// immediately.
128131 ///
129132 /// - parameters:
133+ /// - scheduler: The scheduler on which the `action` consumes the values.
130134 /// - lifetime: The expected lifetime of any bindings towards `self`.
131135 /// - action: The action to consume values.
132- public init ( lifetime: Lifetime , action: @escaping ( Value ) -> Void ) {
133- self . action = action
136+ public init ( on scheduler: Scheduler = ImmediateScheduler ( ) , lifetime: Lifetime , action: @escaping ( Value ) -> Void ) {
134137 self . lifetime = lifetime
135- }
136138
137- /// Creates a binding target which consumes values on the specified scheduler.
138- ///
139- /// - parameters:
140- /// - scheduler: The scheduler on which the `setter` consumes the values.
141- /// - lifetime: The expected lifetime of any bindings towards `self`.
142- /// - action: The action to consume values.
143- public init ( on scheduler: Scheduler , lifetime: Lifetime , action: @escaping ( Value ) -> Void ) {
144- let setter : ( Value ) -> Void = { value in
145- scheduler. schedule {
146- action ( value)
139+ if scheduler is ImmediateScheduler {
140+ self . action = action
141+ } else {
142+ self . action = { value in
143+ scheduler. schedule {
144+ action ( value)
145+ }
147146 }
148147 }
149- self . init ( lifetime: lifetime, action: setter)
150148 }
151149
152150 #if swift(>=3.2)
153- // `Lifetime` is required on these overloads. RAC would provide convenience overloads
154- // for these with `lifetime(of:)`.
155-
156- /// Creates a binding target.
157- ///
158- /// - parameters:
159- /// - lifetime: The expected lifetime of any bindings towards `self`.
160- /// - object: The object to consume values.
161- /// - keyPath: The key path of the object that consumes values.
162- public init < Object: AnyObject > ( lifetime: Lifetime , object: Object , keyPath: ReferenceWritableKeyPath < Object , Value > ) {
163- self . init ( lifetime: lifetime) { [ weak object] in object ? [ keyPath: keyPath] = $0 }
164- }
165-
166151 /// Creates a binding target which consumes values on the specified scheduler.
167152 ///
153+ /// If no scheduler is specified, the binding target would consume the value
154+ /// immediately.
155+ ///
168156 /// - parameters:
169- /// - scheduler: The scheduler on which the `setter` consumes the values.
157+ /// - scheduler: The scheduler on which the key path consumes the values.
170158 /// - lifetime: The expected lifetime of any bindings towards `self`.
171159 /// - object: The object to consume values.
172160 /// - keyPath: The key path of the object that consumes values.
173- public init < Object: AnyObject > ( on scheduler: Scheduler , lifetime: Lifetime , object: Object , keyPath: ReferenceWritableKeyPath < Object , Value > ) {
161+ public init < Object: AnyObject > ( on scheduler: Scheduler = ImmediateScheduler ( ) , lifetime: Lifetime , object: Object , keyPath: ReferenceWritableKeyPath < Object , Value > ) {
174162 self . init ( on: scheduler, lifetime: lifetime) { [ weak object] in object ? [ keyPath: keyPath] = $0 }
175163 }
176164 #endif
0 commit comments