forked from ReactiveCocoa/ReactiveCocoa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProperty.swift
241 lines (199 loc) · 7.26 KB
/
Property.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/// Represents a property that allows observation of its changes.
public protocol PropertyType {
typealias Value
/// The current value of the property.
var value: Value { get }
/// A producer for Signals that will send the property's current value,
/// followed by all changes over time.
var producer: SignalProducer<Value, NoError> { get }
}
/// A read-only property that allows observation of its changes.
public struct AnyProperty<Value>: PropertyType {
private let _value: () -> Value
private let _producer: () -> SignalProducer<Value, NoError>
public var value: Value {
return _value()
}
public var producer: SignalProducer<Value, NoError> {
return _producer()
}
/// Initializes a property as a read-only view of the given property.
public init<P: PropertyType where P.Value == Value>(_ property: P) {
_value = { property.value }
_producer = { property.producer }
}
/// Initializes a property that first takes on `initialValue`, then each value
/// sent on a signal created by `producer`.
public init(initialValue: Value, producer: SignalProducer<Value, NoError>) {
let mutableProperty = MutableProperty(initialValue)
mutableProperty <~ producer
self.init(mutableProperty)
}
/// Initializes a property that first takes on `initialValue`, then each value
/// sent on `signal`.
public init(initialValue: Value, signal: Signal<Value, NoError>) {
let mutableProperty = MutableProperty(initialValue)
mutableProperty <~ signal
self.init(mutableProperty)
}
}
/// A property that never changes.
public struct ConstantProperty<Value>: PropertyType {
public let value: Value
public let producer: SignalProducer<Value, NoError>
/// Initializes the property to have the given value.
public init(_ value: Value) {
self.value = value
self.producer = SignalProducer(value: value)
}
}
/// Represents an observable property that can be mutated directly.
///
/// Only classes can conform to this protocol, because instances must support
/// weak references (and value types currently do not).
public protocol MutablePropertyType: class, PropertyType {
var value: Value { get set }
}
/// A mutable property of type `Value` that allows observation of its changes.
///
/// Instances of this class are thread-safe.
public final class MutableProperty<Value>: MutablePropertyType {
private let observer: Signal<Value, NoError>.Observer
/// Need a recursive lock around `value` to allow recursive access to
/// `value`. Note that recursive sets will still deadlock because the
/// underlying producer prevents sending recursive events.
private let lock = NSRecursiveLock()
private var _value: Value
/// The current value of the property.
///
/// Setting this to a new value will notify all observers of any Signals
/// created from the `values` producer.
public var value: Value {
get {
lock.lock()
let value = _value
lock.unlock()
return value
}
set {
lock.lock()
_value = newValue
observer.sendNext(newValue)
lock.unlock()
}
}
/// A producer for Signals that will send the property's current value,
/// followed by all changes over time, then complete when the property has
/// deinitialized.
public let producer: SignalProducer<Value, NoError>
/// Initializes the property with the given value to start.
public init(_ initialValue: Value) {
lock.name = "org.reactivecocoa.ReactiveCocoa.MutableProperty"
(producer, observer) = SignalProducer<Value, NoError>.buffer(1)
_value = initialValue
observer.sendNext(initialValue)
}
deinit {
observer.sendCompleted()
}
}
/// Wraps a `dynamic` property, or one defined in Objective-C, using Key-Value
/// Coding and Key-Value Observing.
///
/// Use this class only as a last resort! `MutableProperty` is generally better
/// unless KVC/KVO is required by the API you're using (for example,
/// `NSOperation`).
@objc public final class DynamicProperty: RACDynamicPropertySuperclass, MutablePropertyType {
public typealias Value = AnyObject?
private weak var object: NSObject?
private let keyPath: String
/// The current value of the property, as read and written using Key-Value
/// Coding.
public var value: AnyObject? {
@objc(rac_value) get {
return object?.valueForKeyPath(keyPath)
}
@objc(setRac_value:) set(newValue) {
object?.setValue(newValue, forKeyPath: keyPath)
}
}
/// A producer that will create a Key-Value Observer for the given object,
/// send its initial value then all changes over time, and then complete
/// when the observed object has deallocated.
///
/// By definition, this only works if the object given to init() is
/// KVO-compliant. Most UI controls are not!
public var producer: SignalProducer<AnyObject?, NoError> {
if let object = object {
return object.rac_valuesForKeyPath(keyPath, observer: nil).toSignalProducer()
// Errors aren't possible, but the compiler doesn't know that.
.flatMapError { error in
fatalError("Received unexpected error from KVO signal: \(error)")
return .empty
}
} else {
return .empty
}
}
/// Initializes a property that will observe and set the given key path of
/// the given object. `object` must support weak references!
public init(object: NSObject?, keyPath: String) {
self.object = object
self.keyPath = keyPath
/// DynamicProperty stay alive as long as object is alive.
/// This is made possible by strong reference cycles.
super.init()
object?.rac_willDeallocSignal()?.toSignalProducer().startWithCompleted { self }
}
}
infix operator <~ {
associativity right
// Binds tighter than assignment but looser than everything else
precedence 93
}
/// Binds a signal to a property, updating the property's value to the latest
/// value sent by the signal.
///
/// The binding will automatically terminate when the property is deinitialized,
/// or when the signal sends a `Completed` event.
public func <~ <P: MutablePropertyType>(property: P, signal: Signal<P.Value, NoError>) -> Disposable {
let disposable = CompositeDisposable()
disposable += property.producer.startWithCompleted {
disposable.dispose()
}
disposable += signal.observe { [weak property] event in
switch event {
case let .Next(value):
property?.value = value
case .Completed:
disposable.dispose()
default:
break
}
}
return disposable
}
/// Creates a signal from the given producer, which will be immediately bound to
/// the given property, updating the property's value to the latest value sent
/// by the signal.
///
/// The binding will automatically terminate when the property is deinitialized,
/// or when the created signal sends a `Completed` event.
public func <~ <P: MutablePropertyType>(property: P, producer: SignalProducer<P.Value, NoError>) -> Disposable {
var disposable: Disposable!
producer.startWithSignal { signal, signalDisposable in
property <~ signal
disposable = signalDisposable
property.producer.startWithCompleted {
signalDisposable.dispose()
}
}
return disposable
}
/// Binds `destinationProperty` to the latest values of `sourceProperty`.
///
/// The binding will automatically terminate when either property is
/// deinitialized.
public func <~ <Destination: MutablePropertyType, Source: PropertyType where Source.Value == Destination.Value>(destinationProperty: Destination, sourceProperty: Source) -> Disposable {
return destinationProperty <~ sourceProperty.producer
}