Skip to content

Commit c37d774

Browse files
committed
change placements of access and withMutation calls
1 parent 549985a commit c37d774

File tree

3 files changed

+216
-211
lines changed

3 files changed

+216
-211
lines changed

Sources/FoundationEssentials/ProgressManager/ProgressManager+Properties+Accessors.swift

Lines changed: 145 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ extension ProgressManager {
2525
/// - Parameter key: A key path to the custom integer property type.
2626
public subscript<P: Property>(dynamicMember key: KeyPath<ProgressManager.Properties, P.Type>) -> Int where P.Value == Int, P.Summary == Int {
2727
get {
28-
state.withLock { state in
28+
accessObservation(keyPath: ProgressManager.additionalPropertiesKeyPath.withLock { $0 })
29+
return state.withLock { state in
2930
if P.self == ProgressManager.Properties.TotalFileCount.self {
3031
return state.totalFileCount
3132
} else if P.self == ProgressManager.Properties.CompletedFileCount.self {
@@ -37,35 +38,37 @@ extension ProgressManager {
3738
}
3839

3940
set {
40-
let parents: [ParentState]? = state.withLock { state in
41-
if P.self == ProgressManager.Properties.TotalFileCount.self {
42-
guard newValue != state.totalFileCount else {
43-
return nil
44-
}
45-
state.totalFileCount = newValue
46-
return state.parents
47-
48-
} else if P.self == ProgressManager.Properties.CompletedFileCount.self {
49-
guard newValue != state.completedFileCount else {
50-
return nil
51-
}
52-
state.completedFileCount = newValue
53-
return state.parents
54-
} else {
55-
guard newValue != state.propertiesInt[MetatypeWrapper(P.self)] else {
56-
return nil
41+
mutateObservation(of: ProgressManager.additionalPropertiesKeyPath.withLock { $0 }) {
42+
let parents: [ParentState]? = state.withLock { state in
43+
if P.self == ProgressManager.Properties.TotalFileCount.self {
44+
guard newValue != state.totalFileCount else {
45+
return nil
46+
}
47+
state.totalFileCount = newValue
48+
return state.parents
49+
50+
} else if P.self == ProgressManager.Properties.CompletedFileCount.self {
51+
guard newValue != state.completedFileCount else {
52+
return nil
53+
}
54+
state.completedFileCount = newValue
55+
return state.parents
56+
} else {
57+
guard newValue != state.propertiesInt[MetatypeWrapper(P.self)] else {
58+
return nil
59+
}
60+
state.propertiesInt[MetatypeWrapper(P.self)] = newValue
61+
return state.parents
5762
}
58-
state.propertiesInt[MetatypeWrapper(P.self)] = newValue
59-
return state.parents
6063
}
61-
}
62-
if let parents = parents {
63-
if P.self == ProgressManager.Properties.TotalFileCount.self {
64-
markSelfDirty(property: ProgressManager.Properties.TotalFileCount.self, parents: parents)
65-
} else if P.self == ProgressManager.Properties.CompletedFileCount.self {
66-
markSelfDirty(property: ProgressManager.Properties.CompletedFileCount.self, parents: parents)
67-
} else {
68-
markSelfDirty(property: MetatypeWrapper(P.self), parents: parents)
64+
if let parents = parents {
65+
if P.self == ProgressManager.Properties.TotalFileCount.self {
66+
markSelfDirty(property: ProgressManager.Properties.TotalFileCount.self, parents: parents)
67+
} else if P.self == ProgressManager.Properties.CompletedFileCount.self {
68+
markSelfDirty(property: ProgressManager.Properties.CompletedFileCount.self, parents: parents)
69+
} else {
70+
markSelfDirty(property: MetatypeWrapper(P.self), parents: parents)
71+
}
6972
}
7073
}
7174
}
@@ -80,7 +83,8 @@ extension ProgressManager {
8083
/// - Parameter key: A key path to the custom unsigned integer property type.
8184
public subscript<P: Property>(dynamicMember key: KeyPath<ProgressManager.Properties, P.Type>) -> UInt64 where P.Value == UInt64, P.Summary == UInt64 {
8285
get {
83-
state.withLock { state in
86+
accessObservation(keyPath: ProgressManager.additionalPropertiesKeyPath.withLock { $0 })
87+
return state.withLock { state in
8488
if P.self == ProgressManager.Properties.TotalByteCount.self {
8589
return state.totalByteCount
8690
} else if P.self == ProgressManager.Properties.CompletedByteCount.self {
@@ -92,34 +96,36 @@ extension ProgressManager {
9296
}
9397

9498
set {
95-
let parents: [ParentState]? = state.withLock { state in
96-
if P.self == ProgressManager.Properties.TotalByteCount.self {
97-
guard newValue != state.totalByteCount else {
98-
return nil
99+
mutateObservation(of: ProgressManager.additionalPropertiesKeyPath.withLock { $0 }) {
100+
let parents: [ParentState]? = state.withLock { state in
101+
if P.self == ProgressManager.Properties.TotalByteCount.self {
102+
guard newValue != state.totalByteCount else {
103+
return nil
104+
}
105+
state.totalByteCount = newValue
106+
return state.parents
107+
} else if P.self == ProgressManager.Properties.CompletedByteCount.self {
108+
guard newValue != state.completedByteCount else {
109+
return nil
110+
}
111+
state.completedByteCount = newValue
112+
return state.parents
113+
} else {
114+
guard newValue != state.propertiesUInt64[MetatypeWrapper(P.self)] else {
115+
return nil
116+
}
117+
state.propertiesUInt64[MetatypeWrapper(P.self)] = newValue
118+
return state.parents
99119
}
100-
state.totalByteCount = newValue
101-
return state.parents
102-
} else if P.self == ProgressManager.Properties.CompletedByteCount.self {
103-
guard newValue != state.completedByteCount else {
104-
return nil
105-
}
106-
state.completedByteCount = newValue
107-
return state.parents
108-
} else {
109-
guard newValue != state.propertiesUInt64[MetatypeWrapper(P.self)] else {
110-
return nil
111-
}
112-
state.propertiesUInt64[MetatypeWrapper(P.self)] = newValue
113-
return state.parents
114120
}
115-
}
116-
if let parents = parents {
117-
if P.self == ProgressManager.Properties.TotalByteCount.self {
118-
markSelfDirty(property: ProgressManager.Properties.TotalByteCount.self, parents: parents)
119-
} else if P.self == ProgressManager.Properties.CompletedByteCount.self {
120-
markSelfDirty(property: ProgressManager.Properties.CompletedByteCount.self, parents: parents)
121-
} else {
122-
markSelfDirty(property: MetatypeWrapper(P.self), parents: parents)
121+
if let parents = parents {
122+
if P.self == ProgressManager.Properties.TotalByteCount.self {
123+
markSelfDirty(property: ProgressManager.Properties.TotalByteCount.self, parents: parents)
124+
} else if P.self == ProgressManager.Properties.CompletedByteCount.self {
125+
markSelfDirty(property: ProgressManager.Properties.CompletedByteCount.self, parents: parents)
126+
} else {
127+
markSelfDirty(property: MetatypeWrapper(P.self), parents: parents)
128+
}
123129
}
124130
}
125131
}
@@ -134,21 +140,24 @@ extension ProgressManager {
134140
/// - Parameter key: A key path to the custom double property type.
135141
public subscript<P: Property>(dynamicMember key: KeyPath<ProgressManager.Properties, P.Type>) -> P.Value where P.Value == Double, P.Summary == Double {
136142
get {
137-
state.withLock { state in
143+
accessObservation(keyPath: ProgressManager.additionalPropertiesKeyPath.withLock { $0 })
144+
return state.withLock { state in
138145
return state.propertiesDouble[MetatypeWrapper(P.self)] ?? P.defaultValue
139146
}
140147
}
141148

142149
set {
143-
let parents: [ParentState]? = state.withLock { state in
144-
guard newValue != state.propertiesDouble[MetatypeWrapper(P.self)] else {
145-
return nil
150+
mutateObservation(of: ProgressManager.additionalPropertiesKeyPath.withLock { $0 }) {
151+
let parents: [ParentState]? = state.withLock { state in
152+
guard newValue != state.propertiesDouble[MetatypeWrapper(P.self)] else {
153+
return nil
154+
}
155+
state.propertiesDouble[MetatypeWrapper(P.self)] = newValue
156+
return state.parents
157+
}
158+
if let parents = parents {
159+
markSelfDirty(property: MetatypeWrapper(P.self), parents: parents)
146160
}
147-
state.propertiesDouble[MetatypeWrapper(P.self)] = newValue
148-
return state.parents
149-
}
150-
if let parents = parents {
151-
markSelfDirty(property: MetatypeWrapper(P.self), parents: parents)
152161
}
153162
}
154163
}
@@ -162,21 +171,24 @@ extension ProgressManager {
162171
/// - Parameter key: A key path to the custom string property type.
163172
public subscript<P: Property>(dynamicMember key: KeyPath<ProgressManager.Properties, P.Type>) -> String? where P.Value == String?, P.Summary == [String?] {
164173
get {
165-
state.withLock { state in
166-
return state.propertiesString[MetatypeWrapper(P.self)] ?? P.self.defaultValue
174+
accessObservation(keyPath: ProgressManager.additionalPropertiesKeyPath.withLock { $0 })
175+
return state.withLock { state in
176+
return state.propertiesString[MetatypeWrapper(P.self)] ?? P.defaultValue
167177
}
168178
}
169179

170180
set {
171-
let parents: [ParentState]? = state.withLock { state in
172-
guard newValue != state.propertiesString[MetatypeWrapper(P.self)] else {
173-
return nil
181+
mutateObservation(of: ProgressManager.additionalPropertiesKeyPath.withLock { $0 }) {
182+
let parents: [ParentState]? = state.withLock { state in
183+
guard newValue != state.propertiesString[MetatypeWrapper(P.self)] else {
184+
return nil
185+
}
186+
state.propertiesString[MetatypeWrapper(P.self)] = newValue
187+
return state.parents
188+
}
189+
if let parents = parents {
190+
markSelfDirty(property: MetatypeWrapper(P.self), parents: parents)
174191
}
175-
state.propertiesString[MetatypeWrapper(P.self)] = newValue
176-
return state.parents
177-
}
178-
if let parents = parents {
179-
markSelfDirty(property: MetatypeWrapper(P.self), parents: parents)
180192
}
181193
}
182194
}
@@ -190,21 +202,24 @@ extension ProgressManager {
190202
/// - Parameter key: A key path to the custom URL property type.
191203
public subscript<P: Property>(dynamicMember key: KeyPath<ProgressManager.Properties, P.Type>) -> URL? where P.Value == URL?, P.Summary == [URL?] {
192204
get {
193-
state.withLock { state in
194-
return state.propertiesURL[MetatypeWrapper(P.self)] ?? P.self.defaultValue
205+
accessObservation(keyPath: ProgressManager.additionalPropertiesKeyPath.withLock { $0 })
206+
return state.withLock { state in
207+
return state.propertiesURL[MetatypeWrapper(P.self)] ?? P.defaultValue
195208
}
196209
}
197210

198211
set {
199-
let parents: [ParentState]? = state.withLock { state in
200-
guard newValue != state.propertiesURL[MetatypeWrapper(P.self)] else {
201-
return nil
212+
mutateObservation(of: ProgressManager.additionalPropertiesKeyPath.withLock { $0 }) {
213+
let parents: [ParentState]? = state.withLock { state in
214+
guard newValue != state.propertiesURL[MetatypeWrapper(P.self)] else {
215+
return nil
216+
}
217+
state.propertiesURL[MetatypeWrapper(P.self)] = newValue
218+
return state.parents
219+
}
220+
if let parents = parents {
221+
markSelfDirty(property: MetatypeWrapper(P.self), parents: parents)
202222
}
203-
state.propertiesURL[MetatypeWrapper(P.self)] = newValue
204-
return state.parents
205-
}
206-
if let parents = parents {
207-
markSelfDirty(property: MetatypeWrapper(P.self), parents: parents)
208223
}
209224
}
210225
}
@@ -218,36 +233,39 @@ extension ProgressManager {
218233
/// - Parameter key: A key path to the custom unsigned integer property type.
219234
public subscript<P: Property>(dynamicMember key: KeyPath<ProgressManager.Properties, P.Type>) -> UInt64 where P.Value == UInt64, P.Summary == [UInt64] {
220235
get {
221-
state.withLock { state in
236+
accessObservation(keyPath: ProgressManager.additionalPropertiesKeyPath.withLock { $0 })
237+
return state.withLock { state in
222238
if P.self == ProgressManager.Properties.Throughput.self {
223239
return state.throughput
224240
} else {
225-
return state.propertiesUInt64Array[MetatypeWrapper(P.self)] ?? P.self.defaultValue
241+
return state.propertiesUInt64Array[MetatypeWrapper(P.self)] ?? P.defaultValue
226242
}
227243
}
228244
}
229245

230246
set {
231-
let parents: [ParentState]? = state.withLock { state in
232-
if P.self == ProgressManager.Properties.Throughput.self {
233-
guard newValue != state.throughput else {
234-
return nil
235-
}
236-
state.throughput = newValue
237-
return state.parents
238-
} else {
239-
guard newValue != state.propertiesUInt64Array[MetatypeWrapper(P.self)] else {
240-
return nil
247+
mutateObservation(of: ProgressManager.additionalPropertiesKeyPath.withLock { $0 }) {
248+
let parents: [ParentState]? = state.withLock { state in
249+
if P.self == ProgressManager.Properties.Throughput.self {
250+
guard newValue != state.throughput else {
251+
return nil
252+
}
253+
state.throughput = newValue
254+
return state.parents
255+
} else {
256+
guard newValue != state.propertiesUInt64Array[MetatypeWrapper(P.self)] else {
257+
return nil
258+
}
259+
state.propertiesUInt64Array[MetatypeWrapper(P.self)] = newValue
260+
return state.parents
241261
}
242-
state.propertiesUInt64Array[MetatypeWrapper(P.self)] = newValue
243-
return state.parents
244262
}
245-
}
246-
if let parents = parents {
247-
if P.self == ProgressManager.Properties.Throughput.self {
248-
markSelfDirty(property: ProgressManager.Properties.Throughput.self, parents: parents)
249-
} else {
250-
markSelfDirty(property: MetatypeWrapper(P.self), parents: parents)
263+
if let parents = parents {
264+
if P.self == ProgressManager.Properties.Throughput.self {
265+
markSelfDirty(property: ProgressManager.Properties.Throughput.self, parents: parents)
266+
} else {
267+
markSelfDirty(property: MetatypeWrapper(P.self), parents: parents)
268+
}
251269
}
252270
}
253271
}
@@ -262,36 +280,39 @@ extension ProgressManager {
262280
/// - Parameter key: A key path to the custom duration property type.
263281
public subscript<P: Property>(dynamicMember key: KeyPath<ProgressManager.Properties, P.Type>) -> Duration where P.Value == Duration, P.Summary == Duration {
264282
get {
265-
state.withLock { state in
283+
accessObservation(keyPath: ProgressManager.additionalPropertiesKeyPath.withLock { $0 })
284+
return state.withLock { state in
266285
if P.self == ProgressManager.Properties.EstimatedTimeRemaining.self {
267286
return state.estimatedTimeRemaining
268287
} else {
269-
return state.propertiesDuration[MetatypeWrapper(P.self)] ?? P.self.defaultValue
288+
return state.propertiesDuration[MetatypeWrapper(P.self)] ?? P.defaultValue
270289
}
271290
}
272291
}
273292

274293
set {
275-
let parents: [ParentState]? = state.withLock { state in
276-
if P.self == ProgressManager.Properties.EstimatedTimeRemaining.self {
277-
guard newValue != state.estimatedTimeRemaining else {
278-
return nil
294+
mutateObservation(of: ProgressManager.additionalPropertiesKeyPath.withLock { $0 }) {
295+
let parents: [ParentState]? = state.withLock { state in
296+
if P.self == ProgressManager.Properties.EstimatedTimeRemaining.self {
297+
guard newValue != state.estimatedTimeRemaining else {
298+
return nil
299+
}
300+
state.estimatedTimeRemaining = newValue
301+
return state.parents
302+
} else {
303+
guard newValue != state.propertiesDuration[MetatypeWrapper(P.self)] else {
304+
return nil
305+
}
306+
state.propertiesDuration[MetatypeWrapper(P.self)] = newValue
307+
return state.parents
279308
}
280-
state.estimatedTimeRemaining = newValue
281-
return state.parents
282-
} else {
283-
guard newValue != state.propertiesDuration[MetatypeWrapper(P.self)] else {
284-
return nil
285-
}
286-
state.propertiesDuration[MetatypeWrapper(P.self)] = newValue
287-
return state.parents
288309
}
289-
}
290-
if let parents = parents {
291-
if P.self == ProgressManager.Properties.EstimatedTimeRemaining.self {
292-
markSelfDirty(property: ProgressManager.Properties.EstimatedTimeRemaining.self, parents: parents)
293-
} else {
294-
markSelfDirty(property: MetatypeWrapper(P.self), parents: parents)
310+
if let parents = parents {
311+
if P.self == ProgressManager.Properties.EstimatedTimeRemaining.self {
312+
markSelfDirty(property: ProgressManager.Properties.EstimatedTimeRemaining.self, parents: parents)
313+
} else {
314+
markSelfDirty(property: MetatypeWrapper(P.self), parents: parents)
315+
}
295316
}
296317
}
297318
}

0 commit comments

Comments
 (0)