forked from ReactiveCocoa/ReactiveSwift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnidirectionalBinding.swift
More file actions
165 lines (149 loc) · 5.26 KB
/
UnidirectionalBinding.swift
File metadata and controls
165 lines (149 loc) · 5.26 KB
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
import Foundation
import Dispatch
import enum Result.NoError
precedencegroup BindingPrecedence {
associativity: right
// Binds tighter than assignment but looser than everything else
higherThan: AssignmentPrecedence
}
infix operator <~ : BindingPrecedence
// FIXME: Swift 4.x - Conditional Conformance
// public protocol BindingSource: SignalProducerConvertible where Error == NoError {}
// extension Signal: BindingSource where Error == NoError {}
// extension SignalProducer: BindingSource where Error == NoError {}
/// Describes a source which can be bound.
public protocol BindingSource: SignalProducerConvertible {
// FIXME: Swift 4 compiler regression.
// All requirements are replicated to workaround the type checker issue.
// https://bugs.swift.org/browse/SR-5090
associatedtype Value
associatedtype Error: Swift.Error
var producer: SignalProducer<Value, Error> { get }
}
extension Signal: BindingSource {}
extension SignalProducer: BindingSource {}
/// Describes an entity which be bond towards.
public protocol BindingTargetProvider {
associatedtype Value
var bindingTarget: BindingTarget<Value> { get }
}
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 bond 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, Source.Error == NoError
{
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 bond 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?, Source.Error == NoError
{
return provider <~ source.producer.optionalize()
}
}
/// 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 }
}
}