-
Notifications
You must be signed in to change notification settings - Fork 431
/
Copy pathUnidirectionalBinding.swift
204 lines (187 loc) · 6.31 KB
/
UnidirectionalBinding.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import Foundation
import Dispatch
precedencegroup BindingPrecedence {
associativity: right
// Binds tighter than assignment but looser than everything else
higherThan: AssignmentPrecedence
}
infix operator <~ : BindingPrecedence
#if swift(>=5.7)
/// Describes a source which can be bound.
public protocol BindingSource<Value>: SignalProducerConvertible where Error == Never {}
#else
/// Describes a source which can be bound.
public protocol BindingSource: SignalProducerConvertible where Error == Never {}
#endif
extension Signal: BindingSource where Error == Never {}
extension SignalProducer: BindingSource where Error == Never {}
#if swift(>=5.7)
/// Describes an entity which be bound towards.
public protocol BindingTargetProvider<Value> {
associatedtype Value
var bindingTarget: BindingTarget<Value> { get }
}
#else
/// Describes an entity which be bound towards.
public protocol BindingTargetProvider {
associatedtype Value
var bindingTarget: BindingTarget<Value> { get }
}
#endif
extension BindingTargetProvider {
/// Binds a source to a target, updating the target's value to the latest
/// value sent by the source.
///
/// - note: The binding will automatically terminate when the target is
/// deinitialized, or when the source sends a `completed` event.
///
/// ````
/// let property = MutableProperty(0)
/// let signal = Signal({ /* do some work after some time */ })
/// property <~ signal
/// ````
///
/// ````
/// let property = MutableProperty(0)
/// let signal = Signal({ /* do some work after some time */ })
/// let disposable = property <~ signal
/// ...
/// // Terminates binding before property dealloc or signal's
/// // `completed` event.
/// disposable.dispose()
/// ````
///
/// - parameters:
/// - target: A target to be bound to.
/// - source: A source to bind.
///
/// - returns: A disposable that can be used to terminate binding before the
/// deinitialization of the target or the source's `completed`
/// event.
@discardableResult
public static func <~
<Source: BindingSource>
(provider: Self, source: Source) -> Disposable?
where Source.Value == Value
{
return source.producer
.take(during: provider.bindingTarget.lifetime)
.startWithValues(provider.bindingTarget.action)
}
/// Binds a source to a target, updating the target's value to the latest
/// value sent by the source.
///
/// - note: The binding will automatically terminate when the target is
/// deinitialized, or when the source sends a `completed` event.
///
/// ````
/// let property = MutableProperty(0)
/// let signal = Signal({ /* do some work after some time */ })
/// property <~ signal
/// ````
///
/// ````
/// let property = MutableProperty(0)
/// let signal = Signal({ /* do some work after some time */ })
/// let disposable = property <~ signal
/// ...
/// // Terminates binding before property dealloc or signal's
/// // `completed` event.
/// disposable.dispose()
/// ````
///
/// - parameters:
/// - target: A target to be bound to.
/// - source: A source to bind.
///
/// - returns: A disposable that can be used to terminate binding before the
/// deinitialization of the target or the source's `completed`
/// event.
@discardableResult
public static func <~
<Source: BindingSource>
(provider: Self, source: Source) -> Disposable?
where Value == Source.Value?
{
return provider <~ source.producer.optionalize()
}
}
extension Signal.Observer {
/// Binds a source to a target, updating the target's value to the latest
/// value sent by the source.
///
/// - note: Only `value` events will be forwarded to the Observer.
/// The binding will automatically terminate when the target is
/// deinitialized, or when the source sends a `completed` event.
///
/// - parameters:
/// - target: A target to be bound to.
/// - source: A source to bind.
///
/// - returns: A disposable that can be used to terminate binding before the
/// deinitialization of the target or the source's `completed`
/// event.
@discardableResult
public static func <~
<Source: BindingSource>
(observer: Signal<Value, Error>.Observer, source: Source) -> Disposable
where Source.Value == Value
{
return source.producer.startWithValues { [weak observer] in
observer?.send(value: $0)
}
}
}
/// A binding target that can be used with the `<~` operator.
public struct BindingTarget<Value>: BindingTargetProvider {
public let lifetime: Lifetime
public let action: (Value) -> Void
public var bindingTarget: BindingTarget<Value> {
return self
}
/// Creates a binding target which consumes values on the specified scheduler.
///
/// If no scheduler is specified, the binding target would consume the value
/// immediately.
///
/// - parameters:
/// - scheduler: The scheduler on which the `action` consumes the values.
/// - lifetime: The expected lifetime of any bindings towards `self`.
/// - action: The action to consume values.
public init(on scheduler: Scheduler = ImmediateScheduler(), lifetime: Lifetime, action: @escaping (Value) -> Void) {
self.lifetime = lifetime
if scheduler is ImmediateScheduler {
self.action = action
} else {
self.action = { value in
scheduler.schedule {
action(value)
}
}
}
}
/// Creates a binding target which consumes values on the specified scheduler.
///
/// If no scheduler is specified, the binding target would consume the value
/// immediately.
///
/// - parameters:
/// - scheduler: The scheduler on which the key path consumes the values.
/// - lifetime: The expected lifetime of any bindings towards `self`.
/// - object: The object to consume values.
/// - keyPath: The key path of the object that consumes values.
public init<Object: AnyObject>(on scheduler: Scheduler = ImmediateScheduler(), lifetime: Lifetime, object: Object, keyPath: WritableKeyPath<Object, Value>) {
self.init(on: scheduler, lifetime: lifetime) { [weak object] in object?[keyPath: keyPath] = $0 }
}
}
extension Optional: BindingTargetProvider where Wrapped: BindingTargetProvider {
public typealias Value = Wrapped.Value
public var bindingTarget: BindingTarget<Wrapped.Value> {
switch self {
case let .some(provider):
return provider.bindingTarget
case .none:
return BindingTarget(lifetime: .empty, action: { _ in })
}
}
}