@@ -124,53 +124,41 @@ public struct BindingTarget<Value>: BindingTargetProvider {
124
124
return self
125
125
}
126
126
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.
128
131
///
129
132
/// - parameters:
133
+ /// - scheduler: The scheduler on which the `action` consumes the values.
130
134
/// - lifetime: The expected lifetime of any bindings towards `self`.
131
135
/// - 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 ) {
134
137
self . lifetime = lifetime
135
- }
136
138
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
+ }
147
146
}
148
147
}
149
- self . init ( lifetime: lifetime, action: setter)
150
148
}
151
149
152
150
#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
-
166
151
/// Creates a binding target which consumes values on the specified scheduler.
167
152
///
153
+ /// If no scheduler is specified, the binding target would consume the value
154
+ /// immediately.
155
+ ///
168
156
/// - parameters:
169
- /// - scheduler: The scheduler on which the `setter` consumes the values.
157
+ /// - scheduler: The scheduler on which the key path consumes the values.
170
158
/// - lifetime: The expected lifetime of any bindings towards `self`.
171
159
/// - object: The object to consume values.
172
160
/// - 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 > ) {
174
162
self . init ( on: scheduler, lifetime: lifetime) { [ weak object] in object ? [ keyPath: keyPath] = $0 }
175
163
}
176
164
#endif
0 commit comments