forked from wikimedia/wikipedia-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheGatekeeper.swift
214 lines (149 loc) · 7.28 KB
/
CacheGatekeeper.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
import Foundation
final class CacheGatekeeper {
private let queue = DispatchQueue(label: "org.wikimedia.cache.gatekeeper")
// Used when adding or removing the same uniqueKey rapidly. Individual completion block is queued here until uniqueKey is determined complete in CacheController. Note this complete can come from another groupKey. Queued completions are then called and cleaned out.
private var individualCompletions: [CacheController.UniqueKey: [CacheController.IndividualCompletionBlock]] = [:]
// Used when adding or removing the same groupKey rapidly. Completion block is queued here until group is determined complete in CacheController. Queued completions are then called and cleaned out.
private var groupCompletions: [CacheController.GroupKey: [CacheController.GroupCompletionBlock]] = [:]
// Used when adding THEN removing (or vice versa) the same group key rapidly. Completion blocks are queued here until an add or remove completes. Queued completions are then called and cleaned out.
private var queuedAddsWhileWaitingOnRemoves: [CacheController.GroupKey: [() -> Void]] = [:]
private var queuedRemovesWhileWaitingOnAdds: [CacheController.GroupKey: [() -> Void]] = [:]
private var currentlyAdding: [CacheController.GroupKey] = []
private var currentlyRemoving: [CacheController.GroupKey] = []
func numberOfQueuedGroupCompletions(for groupKey: CacheController.GroupKey) -> Int {
queue.sync {
return groupCompletions[groupKey]?.count ?? 0
}
}
func numberOfQueuedIndividualCompletions(for uniqueKey: CacheController.UniqueKey) -> Int {
queue.sync {
return individualCompletions[uniqueKey]?.count ?? 0
}
}
func queueGroupCompletion(groupKey: CacheController.GroupKey, groupCompletion: @escaping CacheController.GroupCompletionBlock) {
queue.async { [weak self] in
guard let self = self else {
return
}
var currentCompletions = self.groupCompletions[groupKey] ?? []
currentCompletions.append(groupCompletion)
self.groupCompletions[groupKey] = currentCompletions
}
}
func queueIndividualCompletion(uniqueKey: CacheController.UniqueKey, individualCompletion: @escaping CacheController.IndividualCompletionBlock) {
queue.async { [weak self] in
guard let self = self else {
return
}
var currentCompletions = self.individualCompletions[uniqueKey] ?? []
currentCompletions.append(individualCompletion)
self.individualCompletions[uniqueKey] = currentCompletions
}
}
func runAndRemoveGroupCompletions(groupKey: CacheController.GroupKey, groupResult: CacheController.FinalGroupResult) {
queue.async { [weak self] in
guard let self = self else {
return
}
if let completions = self.groupCompletions[groupKey] {
for completion in completions {
completion(groupResult)
}
}
self.groupCompletions[groupKey]?.removeAll()
}
}
func runAndRemoveIndividualCompletions(uniqueKey: CacheController.UniqueKey, individualResult: CacheController.FinalIndividualResult) {
queue.async { [weak self] in
guard let self = self else {
return
}
if let completions = self.individualCompletions[uniqueKey] {
for completion in completions {
completion(individualResult)
}
}
self.individualCompletions[uniqueKey]?.removeAll()
}
}
func addCurrentlyAddingGroupKey(_ groupKey: CacheController.GroupKey) {
queue.async { [weak self] in
self?.currentlyAdding.append(groupKey)
}
}
func addCurrentlyRemovingGroupKey(_ groupKey: CacheController.GroupKey) {
queue.async { [weak self] in
self?.currentlyRemoving.append(groupKey)
}
}
func removeCurrentlyAddingGroupKey(_ groupKey: CacheController.GroupKey) {
queue.async { [weak self] in
guard let self = self else {
return
}
let filteredCurrentlyAdding = self.currentlyAdding.filter { $0 != groupKey }
self.currentlyAdding = filteredCurrentlyAdding
}
}
func removeCurrentlyRemovingGroupKey(_ groupKey: CacheController.GroupKey) {
queue.async { [weak self] in
guard let self = self else {
return
}
let filteredCurrentlyRemoving = self.currentlyRemoving.filter { $0 != groupKey }
self.currentlyRemoving = filteredCurrentlyRemoving
}
}
func shouldQueueAddCompletion(groupKey: CacheController.GroupKey) -> Bool {
queue.sync {
return currentlyRemoving.contains(groupKey)
}
}
func shouldQueueRemoveCompletion(groupKey: CacheController.GroupKey) -> Bool {
queue.sync {
return currentlyAdding.contains(groupKey)
}
}
func queueAddCompletion(groupKey: CacheController.GroupKey, completion: @escaping () -> Void) {
queue.async { [weak self] in
guard let self = self else {
return
}
var currentCompletions = self.queuedAddsWhileWaitingOnRemoves[groupKey] ?? []
currentCompletions.append(completion)
self.queuedAddsWhileWaitingOnRemoves[groupKey] = currentCompletions
}
}
func queueRemoveCompletion(groupKey: CacheController.GroupKey, completion: @escaping () -> Void) {
queue.async { [weak self] in
guard let self = self else {
return
}
var currentCompletions = self.queuedRemovesWhileWaitingOnAdds[groupKey] ?? []
currentCompletions.append(completion)
self.queuedRemovesWhileWaitingOnAdds[groupKey] = currentCompletions
}
}
func runAndRemoveQueuedRemoves(groupKey: CacheController.GroupKey) {
queue.async { [weak self] in
guard let self = self else {
return
}
if let completion = self.queuedRemovesWhileWaitingOnAdds[groupKey]?.first {
completion()
}
self.queuedRemovesWhileWaitingOnAdds[groupKey]?.removeAll()
}
}
func runAndRemoveQueuedAdds(groupKey: CacheController.GroupKey) {
queue.async { [weak self] in
guard let self = self else {
return
}
if let completion = self.queuedAddsWhileWaitingOnRemoves[groupKey]?.first {
completion()
}
self.queuedAddsWhileWaitingOnRemoves[groupKey]?.removeAll()
}
}
}