This repository was archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathReactiveArray.swift
More file actions
244 lines (193 loc) · 6.48 KB
/
ReactiveArray.swift
File metadata and controls
244 lines (193 loc) · 6.48 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
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
242
243
244
import Foundation
import ReactiveSwift
import Result
public final class ReactiveArray<Element>: RandomAccessCollection {
public typealias Snapshot = ReactiveCollections.Snapshot<ContiguousArray<Element>>
fileprivate let storage: Storage<ContiguousArray<Element>>
fileprivate let observer: Signal<Snapshot, NoError>.Observer
public let signal: Signal<Snapshot, NoError>
public var producer: SignalProducer<Snapshot, NoError> {
return SignalProducer { [weak self, storage] observer, disposable in
storage.modify { elements in
let changeset = Changeset(initial: elements)
let delta = Snapshot(previous: nil, current: elements, changeset: changeset)
observer.send(value: delta)
if let strongSelf = self {
disposable += strongSelf.signal.observe(observer)
} else {
observer.sendCompleted()
}
}
}
}
public var startIndex: Int {
return storage.elements.startIndex
}
public var endIndex: Int {
return storage.elements.endIndex
}
public subscript(position: Int) -> Element {
return storage.elements[position]
}
public init<S: Sequence>(_ sequence: S) where S.Iterator.Element == Element {
(signal, observer) = Signal<Snapshot, NoError>.pipe()
storage = Storage(ContiguousArray(sequence))
}
public convenience init() {
self.init([])
}
public func modify<Result>(_ action: (inout MutableView) -> Result) -> Result {
return storage.modify { elements in
var view = MutableView(original: elements)
let result = action(&view)
if !view.isOriginal {
view.changeset.removals = IndexSet(integersIn: elements.startIndex ..< elements.endIndex)
}
let snapshot = Snapshot(previous: elements, current: view.elements, changeset: view.changeset)
elements = view.elements
observer.send(value: snapshot)
return result
}
}
deinit {
observer.sendCompleted()
}
}
extension ReactiveArray: ExpressibleByArrayLiteral {
public convenience init(arrayLiteral elements: Element...) {
self.init(elements)
}
}
extension ReactiveArray where Element: Equatable {
public static func ==<C: Collection>(left: ReactiveArray<Element>, right: C) -> Bool where C.Iterator.Element == Element {
return left.elementsEqual(right, by: ==)
}
}
extension ReactiveArray {
public struct MutableView: RandomAccessCollection, MutableCollection, RangeReplaceableCollection {
fileprivate var isOriginal: Bool
fileprivate var elements: ContiguousArray<Element>
fileprivate var changeset: Changeset
public init() {
self.elements = []
self.isOriginal = false
changeset = Changeset()
}
fileprivate init(original elements: ContiguousArray<Element>) {
self.elements = elements
self.isOriginal = true
changeset = Changeset()
}
public mutating func replaceSubrange<C>(_ subrange: Range<Int>, with newElements: C) where C: Collection, C.Iterator.Element == Element {
elements.replaceSubrange(subrange, with: newElements)
let elementsCount = newElements.count
if subrange.count > elementsCount {
let deleteCount = subrange.count - elementsCount
let deleteRange = (subrange.upperBound - deleteCount) ..< subrange.upperBound
let updateRange = subrange.lowerBound ..< deleteRange.lowerBound
if !updateRange.isEmpty {
var newUpdates = IndexSet(integersIn: updateRange)
// Handle updates to insertions.
newUpdates.subtract(changeset.inserts)
// Record the update set.
changeset.mutations.formUnion(newUpdates.ignoringInserts(changeset.inserts))
}
if !deleteRange.isEmpty {
var newDeletes = IndexSet(integersIn: deleteRange)
// Handle the deletion of insertions.
let uncommittedInsertDeletes = changeset.inserts.intersection(newDeletes)
changeset.inserts.subtract(uncommittedInsertDeletes)
newDeletes.subtract(uncommittedInsertDeletes)
// Record the delete set and update set.
newDeletes = newDeletes.ignoringInserts(changeset.inserts)
changeset.removals.formUnion(newDeletes)
changeset.mutations.subtract(newDeletes)
}
} else {
let insertCount = elementsCount - subrange.count
let insertRange = subrange.upperBound ..< (subrange.upperBound + insertCount)
let updateRange = subrange
if !updateRange.isEmpty {
var newUpdates = IndexSet(integersIn: updateRange)
// Handle updates to insertions.
newUpdates.subtract(changeset.inserts)
// Record the update set.
changeset.mutations.formUnion(newUpdates.ignoringInserts(changeset.inserts))
}
if !insertRange.isEmpty {
var newInserts = IndexSet(integersIn: insertRange)
// Record the insert set.
newInserts.formUnion(changeset.inserts.shifted(byInserts: newInserts))
changeset.inserts = newInserts
}
}
}
public var startIndex: Int {
return elements.startIndex
}
public var endIndex: Int {
return elements.endIndex
}
public subscript(position: Int) -> Element {
get {
return elements[position]
}
set {
replaceSubrange(position ..< position + 1, with: CollectionOfOne(newValue))
}
}
}
}
extension ReactiveArray.MutableView: ExpressibleByArrayLiteral {
public init(arrayLiteral elements: Element...) {
self.init(elements)
}
}
extension ReactiveArray: CustomDebugStringConvertible {
public var debugDescription: String {
return storage.elements.description
}
}
extension IndexSet {
fileprivate func ignoringInserts(_ indices: IndexSet) -> IndexSet {
var shifted = IndexSet()
for i in self {
shifted.insert(i - indices.count(in: 0 ... i))
}
return shifted
}
fileprivate func shifted(byInserts indices: IndexSet) -> IndexSet {
var shifted = IndexSet()
for i in self {
shifted.insert(i + indices.count(in: 0 ... i))
}
return shifted
}
}
private final class Storage<Elements> {
private var _elements: Elements
var elements: Elements {
get { return _elements }
set {
writeLock.lock()
_elements = newValue
writeLock.unlock()
}
}
/// A lock to protect mutations and their subsequent event emission. Reads do
/// not need to be protected, since the copy-on-write already guards reads.
/// The producer, however, has to acquire the lock to block new mutations
/// before it observes the delta signal.
fileprivate let writeLock: NSLock
init(_ elements: Elements) {
self._elements = elements
writeLock = NSLock()
writeLock.name = "org.RACCommunity.ReactiveCollections.ReactiveArray.writeLock"
}
func modify<Result>(_ action: (inout Elements) throws -> Result) rethrows -> Result {
writeLock.lock()
let returnValue = try action(&_elements)
writeLock.unlock()
return returnValue
}
}