1
-
2
- //
3
- // Buffer.swift
4
- // Buffer
5
- //
6
- // Created by Alex Usbergo on 02/05/16.
7
- //
8
- // Copyright (c) 2016 Alex Usbergo.
9
- //
10
- // Permission is hereby granted, free of charge, to any person obtaining a copy
11
- // of this software and associated documentation files (the "Software"), to deal
12
- // in the Software without restriction, including without limitation the rights
13
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
- // copies of the Software, and to permit persons to whom the Software is
15
- // furnished to do so, subject to the following conditions:
16
- //
17
- // The above copyright notice and this permission notice shall be included in
18
- // all copies or substantial portions of the Software.
19
- //
20
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
- // THE SOFTWARE.
27
- //
28
-
29
1
import Foundation
30
2
31
3
public protocol BufferType { }
32
4
33
5
public protocol BufferDelegate : class {
34
6
35
- /// Notifies the receiver that the content is about to change
7
+ /** Notifies the receiver that the content is about to change */
36
8
func buffer( willChangeContent buffer: BufferType )
37
9
38
- /// Notifies the receiver that rows were deleted.
10
+ /** Notifies the receiver that rows were deleted. */
39
11
func buffer( didDeleteElementAtIndices buffer: BufferType , indices: [ UInt ] )
40
12
41
- /// Notifies the receiver that rows were inserted.
13
+ /** Notifies the receiver that rows were inserted. */
42
14
func buffer( didInsertElementsAtIndices buffer: BufferType , indices: [ UInt ] )
43
15
44
- /// Notifies the receiver that the content updates has ended.
16
+ /** Notifies the receiver that the content updates has ended. */
45
17
func buffer( didChangeContent buffer: BufferType )
46
18
47
- /// Notifies the receiver that the content updates has ended.
48
- /// This callback method is called when the number of changes are too many to be
49
- /// handled for the UI thread - it's recommendable to just reload the whole data in this case.
50
- /// - Note: The 'diffThreshold' property in 'Buffer' defines what is the maximum number
51
- /// of changes
52
- /// that you want the receiver to be notified for.
19
+ /** Notifies the receiver that the content updates has ended.
20
+ * This callback method is called when the number of changes are too many to be
21
+ * handled for the UI thread - it's recommendable to just reload the whole data in this case.
22
+ * - Note: The 'diffThreshold' property in 'Buffer' defines what is the maximum number
23
+ * of changes
24
+ * that you want the receiver to be notified for.
25
+ */
53
26
func buffer( didChangeAllContent buffer: BufferType )
54
27
55
- /// Called when one of the observed properties for this object changed
28
+ /** Called when one of the observed properties for this object changed. */
56
29
func buffer( didChangeElementAtIndex buffer: BufferType , index: UInt )
57
30
}
58
31
59
32
public class Buffer < ElementType: Equatable > : NSObject , BufferType {
60
33
61
- /// The object that will get notified every time changes occures to the array.
34
+ /** The object that will get notified every time changes occures to the array. */
62
35
public weak var delegate : BufferDelegate ?
63
36
64
- /// The elements in the array observer's buffer.
37
+ /** The elements in the array observer's buffer. */
65
38
public var currentElements : [ ElementType ] {
66
39
return self . frontBuffer
67
40
}
68
41
69
- /// Defines what is the maximum number of changes that you want the receiver to be notified for.
42
+ /** Defines what is the maximum number of changes that you want the receiver to be notified for. */
70
43
public var diffThreshold = 50
71
44
72
- /// If set to 'true' the LCS algorithm is run synchronously on the main thread.
45
+ // If set to 'true' the LCS algorithm is run synchronously on the main thread.
73
46
fileprivate var synchronous : Bool = false
74
47
75
- /// The two buffers.
48
+ // The two buffers.
76
49
fileprivate var frontBuffer = [ ElementType] ( ) {
77
50
willSet {
78
51
assert ( Thread . isMainThread)
@@ -85,13 +58,13 @@ public class Buffer<ElementType: Equatable>: NSObject, BufferType {
85
58
}
86
59
fileprivate var backBuffer = [ ElementType] ( )
87
60
88
- /// Sort closure.
61
+ // Sort closure.
89
62
fileprivate var sort : ( ( ElementType , ElementType ) -> Bool ) ?
90
63
91
- /// Filter closure.
64
+ // Filter closure.
92
65
fileprivate var filter : ( ( ElementType ) -> Bool ) ?
93
66
94
- /// The serial operation queue for this controller.
67
+ // The serial operation queue for this controller.
95
68
fileprivate let serialOperationQueue : OperationQueue = {
96
69
let operationQueue = OperationQueue ( )
97
70
operationQueue. maxConcurrentOperationCount = 1
@@ -101,7 +74,7 @@ public class Buffer<ElementType: Equatable>: NSObject, BufferType {
101
74
fileprivate var flags = ( isRefreshing: false ,
102
75
shouldRefresh: false )
103
76
104
- /// Used if 'Element' is KVO-compliant.
77
+ // Used if 'Element' is KVO-compliant.
105
78
fileprivate var trackedKeyPaths = [ String] ( )
106
79
107
80
public init ( initialArray: [ ElementType ] ,
@@ -117,7 +90,7 @@ public class Buffer<ElementType: Equatable>: NSObject, BufferType {
117
90
self . observe ( shouldObserveTrackedKeyPaths: false )
118
91
}
119
92
120
- /// Compute the diffs between the current array and the new one passed as argument.
93
+ /** Compute the diffs between the current array and the new one passed as argument. */
121
94
public func update(
122
95
with values: [ ElementType ] ? = nil ,
123
96
synchronous: Bool = false ,
@@ -182,8 +155,9 @@ public class Buffer<ElementType: Equatable>: NSObject, BufferType {
182
155
}
183
156
}
184
157
185
- /// This message is sent to the receiver when the value at the specified key path relative
186
- /// to the given object has changed.
158
+ /** This message is sent to the receiver when the value at the specified key path relative
159
+ * to the given object has changed.
160
+ */
187
161
public override func observeValue( forKeyPath keyPath: String ? ,
188
162
of object: Any ? ,
189
163
change: [ NSKeyValueChangeKey : Any ] ? ,
@@ -200,11 +174,11 @@ public class Buffer<ElementType: Equatable>: NSObject, BufferType {
200
174
}
201
175
}
202
176
203
- //MARK: KVO Extension
177
+ // MARK: KVO Extension
204
178
205
179
extension Buffer where ElementType: AnyObject {
206
180
207
- /// Observe the keypaths passed as argument.
181
+ /** Observe the keypaths passed as argument. */
208
182
public func trackKeyPaths( _ keypaths: [ String ] ) {
209
183
self . observe ( shouldObserveTrackedKeyPaths: false )
210
184
self . trackedKeyPaths = keypaths
@@ -214,8 +188,9 @@ extension Buffer where ElementType: AnyObject {
214
188
215
189
extension Buffer {
216
190
217
- /// Adds or remove observations.
218
- /// - Note: This code is executed only when 'Element: AnyObject'.
191
+ /** Adds or remove observations.
192
+ * - Note: This code is executed only when 'Element: AnyObject'.
193
+ */
219
194
fileprivate func observe( shouldObserveTrackedKeyPaths: Bool = true ) {
220
195
if self . trackedKeyPaths. count == 0 {
221
196
return
@@ -237,7 +212,7 @@ extension Buffer {
237
212
}
238
213
}
239
214
240
- /// - Note: This code is executed only when 'Element: AnyObject'.
215
+ // - Note: This code is executed only when 'Element: AnyObject'.
241
216
fileprivate func objectDidChangeValue( for keyPath: String ? , in object: AnyObject ? ) {
242
217
dispatchOnMainThread {
243
218
self . update ( ) {
0 commit comments