-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
ObservableProperty.swift
169 lines (141 loc) · 4.08 KB
/
ObservableProperty.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
//
// ObservableProperty.swift
// ReactiveCocoa
//
// Created by Justin Spahr-Summers on 2014-10-13.
// Copyright (c) 2014 GitHub. All rights reserved.
//
import Foundation
import LlamaKit
/// A mutable property of type T that allows observation of its changes.
///
/// Instances of this class are thread-safe.
public final class ObservableProperty<T> {
private let queue = dispatch_queue_create("org.reactivecocoa.ReactiveCocoa.ObservableProperty", DISPATCH_QUEUE_CONCURRENT)
private var sinks = Bag<SinkOf<Event<T>>>()
private var _value: T
/// The file in which this property was defined, if known.
internal let file: String?
/// The function in which this property was defined, if known.
internal let function: String?
/// The line number upon which this property was defined, if known.
internal let line: Int?
/// The current value of the property.
///
/// Setting this to a new value will notify all sinks attached to
/// `values`.
public var value: T {
get {
var readValue: T?
dispatch_sync(queue) {
readValue = self._value
}
return readValue!
}
set(value) {
dispatch_barrier_sync(queue) {
self._value = value
for sink in self.sinks {
sink.put(.Next(Box(value)))
}
}
}
}
/// A signal that will send the property's current value, followed by all
/// changes over time. The signal will complete when the property
/// deinitializes.
public var values: ColdSignal<T> {
return ColdSignal { [weak self] (sink, disposable) in
if let strongSelf = self {
var token: RemovalToken?
dispatch_barrier_sync(strongSelf.queue) {
token = strongSelf.sinks.insert(sink)
sink.put(.Next(Box(strongSelf._value)))
}
disposable.addDisposable {
if let strongSelf = self {
dispatch_barrier_async(strongSelf.queue) {
strongSelf.sinks.removeValueForToken(token!)
}
}
}
} else {
sink.put(.Completed)
}
}
}
public init(_ value: T, file: String = __FILE__, line: Int = __LINE__, function: String = __FUNCTION__) {
_value = value
self.file = file
self.line = line
self.function = function
}
deinit {
dispatch_barrier_sync(queue) {
for sink in self.sinks {
sink.put(.Completed)
}
}
}
}
extension ObservableProperty: SinkType {
public func put(value: T) {
self.value = value
}
}
extension ObservableProperty: DebugPrintable {
public var debugDescription: String {
let function = self.function ?? ""
let file = self.file ?? ""
let line = self.line?.description ?? ""
return "\(function).ObservableProperty (\(file):\(line))"
}
}
infix operator <~ {
associativity right
precedence 90
}
/// Binds the given 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.
public func <~ <T> (property: ObservableProperty<T>, signal: HotSignal<T>) {
let disposable = signal.observe { [weak property] value in
property?.value = value
return
}
// Dispose of the binding when the property deallocates.
property.values.start(completed: {
disposable.dispose()
})
}
infix operator <~! {
associativity right
precedence 90
}
/// Binds the given signal to a property, updating the property's value to the
/// latest value sent by the signal.
///
/// Note that the signal MUST NOT send an error, or the program will terminate.
///
/// The binding will automatically terminate when the property is deinitialized
/// or the signal completes.
public func <~! <T> (property: ObservableProperty<T>, signal: ColdSignal<T>) {
let disposable = CompositeDisposable()
// Dispose of the binding when the property deallocates.
let propertyDisposable = property.values.start(completed: {
disposable.dispose()
})
disposable.addDisposable(propertyDisposable)
signal.startWithSink { [weak property] signalDisposable in
disposable.addDisposable(signalDisposable)
return Event.sink(next: { value in
property?.value = value
return
}, error: { error in
fatalError("Unhandled error in ColdSignal binding: \(error)")
}, completed: {
disposable.dispose()
})
}
}