Skip to content

Commit bab3b11

Browse files
committed
add withMutation to markDirty + correct some access keypaths
1 parent 279eb79 commit bab3b11

File tree

2 files changed

+70
-59
lines changed

2 files changed

+70
-59
lines changed

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

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -337,45 +337,57 @@ extension ProgressManager {
337337
}
338338

339339
internal func markChildDirty(property: ProgressManager.Properties.TotalFileCount.Type, at position: Int) {
340-
let parents = state.withLock { state in
341-
state.markChildDirty(property: property, at: position)
340+
mutateObservation(of: ProgressManager.additionalPropertiesKeyPath.withLock { $0 }) {
341+
let parents = state.withLock { state in
342+
state.markChildDirty(property: property, at: position)
343+
}
344+
markSelfDirty(property: property, parents: parents)
342345
}
343-
markSelfDirty(property: property, parents: parents)
344346
}
345347

346348
internal func markChildDirty(property: ProgressManager.Properties.CompletedFileCount.Type, at position: Int) {
347-
let parents = state.withLock { state in
348-
state.markChildDirty(property: property, at: position)
349+
mutateObservation(of: ProgressManager.additionalPropertiesKeyPath.withLock { $0 }) {
350+
let parents = state.withLock { state in
351+
state.markChildDirty(property: property, at: position)
352+
}
353+
markSelfDirty(property: property, parents: parents)
349354
}
350-
markSelfDirty(property: property, parents: parents)
351355
}
352356

353357
internal func markChildDirty(property: ProgressManager.Properties.TotalByteCount.Type, at position: Int) {
354-
let parents = state.withLock { state in
355-
state.markChildDirty(property: property, at: position)
358+
mutateObservation(of: ProgressManager.additionalPropertiesKeyPath.withLock { $0 }) {
359+
let parents = state.withLock { state in
360+
state.markChildDirty(property: property, at: position)
361+
}
362+
markSelfDirty(property: property, parents: parents)
356363
}
357-
markSelfDirty(property: property, parents: parents)
358364
}
359365

360366
internal func markChildDirty(property: ProgressManager.Properties.CompletedByteCount.Type, at position: Int) {
361-
let parents = state.withLock { state in
362-
state.markChildDirty(property: property, at: position)
367+
mutateObservation(of: ProgressManager.additionalPropertiesKeyPath.withLock { $0 }) {
368+
let parents = state.withLock { state in
369+
state.markChildDirty(property: property, at: position)
370+
}
371+
markSelfDirty(property: property, parents: parents)
363372
}
364-
markSelfDirty(property: property, parents: parents)
365373
}
366374

367375
internal func markChildDirty(property: ProgressManager.Properties.Throughput.Type, at position: Int) {
368-
let parents = state.withLock { state in
369-
state.markChildDirty(property: property, at: position)
376+
mutateObservation(of: ProgressManager.additionalPropertiesKeyPath.withLock { $0 }) {
377+
let parents = state.withLock { state in
378+
state.markChildDirty(property: property, at: position)
379+
}
380+
markSelfDirty(property: property, parents: parents)
370381
}
371-
markSelfDirty(property: property, parents: parents)
372382
}
373383

374384
internal func markChildDirty(property: ProgressManager.Properties.EstimatedTimeRemaining.Type, at position: Int) {
375-
let parents = state.withLock { state in
376-
state.markChildDirty(property: property, at: position)
385+
mutateObservation(of: ProgressManager.additionalPropertiesKeyPath.withLock { $0 }) {
386+
let parents = state.withLock { state in
387+
state.markChildDirty(property: property, at: position)
388+
}
389+
markSelfDirty(property: property, parents: parents)
377390
}
378-
markSelfDirty(property: property, parents: parents)
379391
}
380392

381393
//MARK: Method to preserve values of properties upon deinit

Sources/FoundationEssentials/ProgressManager/ProgressManager.swift

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ internal import _FoundationCollections
5050
/// This takes into account the fraction completed in its children instances if children are present.
5151
/// If `self` is indeterminate, the value will be 0.0.
5252
public var fractionCompleted: Double {
53-
_$observationRegistrar.access(self, keyPath: \.fractionCompleted)
53+
_$observationRegistrar.access(self, keyPath: \.totalCount)
54+
_$observationRegistrar.access(self, keyPath: \.completedCount)
5455
return state.withLock { state in
5556
state.getFractionCompleted()
5657
}
@@ -59,7 +60,7 @@ internal import _FoundationCollections
5960
/// The state of initialization of `totalCount`.
6061
/// If `totalCount` is `nil`, the value will be `true`.
6162
public var isIndeterminate: Bool {
62-
_$observationRegistrar.access(self, keyPath: \.isIndeterminate)
63+
_$observationRegistrar.access(self, keyPath: \.totalCount)
6364
return state.withLock { state in
6465
state.getIsIndeterminate()
6566
}
@@ -68,7 +69,8 @@ internal import _FoundationCollections
6869
/// The state of completion of work.
6970
/// If `completedCount` >= `totalCount`, the value will be `true`.
7071
public var isFinished: Bool {
71-
_$observationRegistrar.access(self, keyPath: \.isFinished)
72+
_$observationRegistrar.access(self, keyPath: \.totalCount)
73+
_$observationRegistrar.access(self, keyPath: \.completedCount)
7274
return state.withLock { state in
7375
state.getIsFinished()
7476
}
@@ -178,52 +180,48 @@ internal import _FoundationCollections
178180
/// Increases `completedCount` by `count`.
179181
/// - Parameter count: Units of work.
180182
public func complete(count: Int) {
181-
_$observationRegistrar.withMutation(of: self, keyPath: \.fractionCompleted) {
182-
_$observationRegistrar.withMutation(of: self, keyPath: \.completedCount) {
183+
_$observationRegistrar.withMutation(of: self, keyPath: \.completedCount) {
184+
let parents: [ParentState]? = state.withLock { state in
185+
guard state.selfFraction.completed != (state.selfFraction.completed + count) else {
186+
return nil
187+
}
188+
189+
state.complete(by: count)
190+
191+
return state.parents
192+
}
193+
if let parents = parents {
194+
markSelfDirty(parents: parents)
195+
}
196+
}
197+
}
198+
199+
public func setCounts(_ counts: (_ completed: inout Int, _ total: inout Int?) -> Void) {
200+
_$observationRegistrar.withMutation(of: self, keyPath: \.completedCount) {
201+
_$observationRegistrar.withMutation(of: self, keyPath: \.totalCount) {
183202
let parents: [ParentState]? = state.withLock { state in
184-
guard state.selfFraction.completed != (state.selfFraction.completed + count) else {
203+
var completed = state.selfFraction.completed
204+
var total = state.selfFraction.total
205+
206+
counts(&completed, &total)
207+
208+
guard state.selfFraction.completed != completed || state.selfFraction.total != total else {
185209
return nil
186210
}
187211

188-
state.complete(by: count)
212+
state.selfFraction.completed = completed
213+
state.selfFraction.total = total
189214

190215
return state.parents
191216
}
217+
192218
if let parents = parents {
193219
markSelfDirty(parents: parents)
194220
}
195221
}
196222
}
197223
}
198224

199-
public func setCounts(_ counts: (_ completed: inout Int, _ total: inout Int?) -> Void) {
200-
_$observationRegistrar.withMutation(of: self, keyPath: \.fractionCompleted) {
201-
_$observationRegistrar.withMutation(of: self, keyPath: \.completedCount) {
202-
_$observationRegistrar.withMutation(of: self, keyPath: \.totalCount) {
203-
let parents: [ParentState]? = state.withLock { state in
204-
var completed = state.selfFraction.completed
205-
var total = state.selfFraction.total
206-
207-
counts(&completed, &total)
208-
209-
guard state.selfFraction.completed != completed || state.selfFraction.total != total else {
210-
return nil
211-
}
212-
213-
state.selfFraction.completed = completed
214-
state.selfFraction.total = total
215-
216-
return state.parents
217-
}
218-
219-
if let parents = parents {
220-
markSelfDirty(parents: parents)
221-
}
222-
}
223-
}
224-
}
225-
}
226-
227225
// MARK: Internal Observation Support for Extensions
228226

229227
/// Provides access to the observation registrar for use in extensions.
@@ -242,7 +240,6 @@ internal import _FoundationCollections
242240
internal func getProgressFraction() -> ProgressFraction {
243241
return state.withLock { state in
244242
return state.selfFraction
245-
246243
}
247244
}
248245

@@ -263,11 +260,13 @@ internal import _FoundationCollections
263260
}
264261

265262
private func markChildDirty(at position: Int) {
266-
let parents: [ParentState]? = state.withLock { state in
267-
state.markChildDirty(at: position)
268-
}
269-
if let parents = parents {
270-
markSelfDirty(parents: parents)
263+
_$observationRegistrar.withMutation(of: self, keyPath: \.completedCount) {
264+
let parents: [ParentState]? = state.withLock { state in
265+
state.markChildDirty(at: position)
266+
}
267+
if let parents = parents {
268+
markSelfDirty(parents: parents)
269+
}
271270
}
272271
}
273272

0 commit comments

Comments
 (0)