-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathBuffer.swift
192 lines (169 loc) · 6.17 KB
/
Buffer.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
import Foundation
public protocol BufferType {}
public protocol BufferDelegate: class {
/// Notifies the receiver that the content is about to change.
func buffer(willChangeContent buffer: BufferType)
/// Notifies the receiver that rows were deleted.
func buffer(didDeleteElementAtIndices buffer: BufferType, indices: [UInt])
/// Notifies the receiver that rows were inserted.
func buffer(didInsertElementsAtIndices buffer: BufferType, indices: [UInt])
/// Notifies the receiver that an element has been moved to a different position.
func buffer(didMoveElement buffer: BufferType, from: UInt, to: UInt)
/// Notifies the receiver that the content updates has ended.
func buffer(didChangeContent buffer: BufferType)
/// Notifies the receiver that the content updates has ended.
/// This callback method is called when the number of changes are too many to be
/// handled for the UI thread - it's recommendable to just reload the whole data in this case.
/// - Note: The 'diffThreshold' property in 'Buffer' defines what is the maximum number
/// of changes
/// that you want the receiver to be notified for.
func buffer(didChangeAllContent buffer: BufferType)
/// Called when one of the observed properties for this object changed.
func buffer(didChangeElementAtIndex buffer: BufferType, index: UInt)
}
public class Buffer<E: Diffable>: NSObject, BufferType {
/// The object that will get notified every time chavbcnges occures to the array.
public weak var delegate: BufferDelegate?
/// The elements in the array observer's buffer.
public var currentElements: [E] {
return self.frontBuffer
}
/// Defines what is the maximum number of changes that you want the receiver to be notified for.
/// - note: If the number of changes exceeds this number, *buffer(didChangeAllContent:)* is going
/// to be invoked instead.
public var diffThreshold = 100
/// If set to 'true' the LCS algorithm is run synchronously on the main thread.
private var synchronous: Bool = false
/// The exposed array.
private var frontBuffer = [E]()
/// The internal array.
private var backBuffer = [E]()
/// Sort closure.
private var sort: ((E, E) -> Bool)?
/// Filter closure.
private var filter: ((E) -> Bool)?
/// The serial operation queue for this controller.
private let serialOperationQueue: OperationQueue = {
let operationQueue = OperationQueue()
operationQueue.maxConcurrentOperationCount = 1
return operationQueue
}()
/// Internal state flags.
private var flags = (
isRefreshing: false,
shouldRefresh: false
)
/// Constructs a new buffer instance.
public init(
initialArray: [E],
sort: ((E, E) -> Bool)? = nil,
filter: ((E) -> Bool)? = nil
) {
self.frontBuffer = initialArray
self.sort = sort
self.filter = filter
}
/// Compute the diffs between the current array and the new one passed as argument.
public func update(
with values: [E]? = nil,
synchronous: Bool = false,
completion: (() -> Void)? = nil
) {
// Should be called on the main thread.
assert(Thread.isMainThread)
let new = values ?? self.frontBuffer
self.backBuffer = new
// Is already refreshing.
if self.flags.isRefreshing {
self.flags.shouldRefresh = true
return
}
self.flags.isRefreshing = true
self.dispatchOnSerialQueue(synchronous: synchronous) { [weak self] in
guard let `self` = self else {
return
}
var backBuffer = self.backBuffer
// Filter and sorts (if necessary).
if let filter = self.filter {
backBuffer = backBuffer.filter(filter)
}
if let sort = self.sort {
backBuffer = backBuffer.sorted(by: sort)
}
// Compute the diffing.
let diff = Diff.diffing(
oldArray: self.frontBuffer.map { $0.diffIdentifier },
newArray: backBuffer.map { $0.diffIdentifier }
) {
return $0 == $1
}
// Update the front buffer on the main thread.
self.dispatchOnMainThread(synchronous: synchronous) { [weak self] in
guard let `self` = self else {
return
}
// Swaps the buffers.
self.frontBuffer = backBuffer
if diff.inserts.count < self.diffThreshold && diff.deletes.count < self.diffThreshold {
self.delegate?.buffer(willChangeContent: self)
self.delegate?.buffer(
didInsertElementsAtIndices: self,
indices: diff.inserts.map({ UInt($0) }))
self.delegate?.buffer(
didDeleteElementAtIndices: self,
indices: diff.deletes.map({ UInt($0) }))
for move in diff.moves {
self.delegate?.buffer(didMoveElement: self, from: UInt(move.from), to: UInt(move.to))
}
self.delegate?.buffer(didChangeContent: self)
} else {
self.delegate?.buffer(didChangeAllContent: self)
}
// Re-rerun refresh if necessary.
self.flags.isRefreshing = false
if self.flags.shouldRefresh {
self.flags.shouldRefresh = false
self.update(with: self.backBuffer)
}
completion?()
}
}
}
}
//MARK: Dispatch Helpers
extension Buffer {
/// Dispatches the block passed as argument on the main thread.
/// - parameter synchronous: If true the block is going to be called on the current call stack.
/// - parameter block: The block that is going to be executed.
fileprivate func dispatchOnMainThread(
synchronous: Bool = false,
block: @escaping () -> Void
) {
if synchronous {
assert(Thread.isMainThread)
block()
return
}
if Thread.isMainThread {
block()
} else {
DispatchQueue.main.async(execute: block)
}
}
/// Dispatches the block passed as argument on the ad-hoc serial queue (if necesary).
/// - parameter synchronous: If true the block is going to be called on the current call stack.
/// - parameter block: The block that is going to be executed.
fileprivate func dispatchOnSerialQueue(
synchronous: Bool = false,
block: @escaping () -> Void
) {
if synchronous {
block()
return
}
self.serialOperationQueue.addOperation {
DispatchQueue.global().async(execute: block)
}
}
}