Skip to content

Commit 8240116

Browse files
committed
totalCount + completedCount updates swap to isDirty
1 parent 61b3945 commit 8240116

File tree

1 file changed

+55
-33
lines changed

1 file changed

+55
-33
lines changed

Sources/FoundationEssentials/ProgressManager/ProgressManager.swift

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ internal struct AnyMetatypeWrapper: Hashable, Equatable, Sendable {
145145
}
146146

147147
set {
148-
let previous = state.fractionState.overallFraction
148+
// let previous = state.fractionState.overallFraction
149149
if state.fractionState.selfFraction.total != newValue && state.fractionState.selfFraction.total > 0 {
150150
state.fractionState.childFraction = state.fractionState.childFraction * _ProgressFraction(completed: state.fractionState.selfFraction.total, total: newValue ?? 1)
151151
}
@@ -158,7 +158,10 @@ internal struct AnyMetatypeWrapper: Hashable, Equatable, Sendable {
158158
state.fractionState.indeterminate = true
159159
}
160160
//TODO: rdar://149015734 Check throttling
161-
manager.updateFractionCompleted(from: previous, to: state.fractionState.overallFraction)
161+
manager.markDirty()
162+
// manager.updateFractionCompleted(from: previous, to: state.fractionState.overallFraction)
163+
164+
// Interop updates stuff
162165
manager.ghostReporter?.notifyObservers(with: .totalCountUpdated)
163166
manager.monitorInterop.withLock { [manager] interop in
164167
if interop == true {
@@ -176,9 +179,10 @@ internal struct AnyMetatypeWrapper: Hashable, Equatable, Sendable {
176179
}
177180

178181
set {
179-
let prev = state.fractionState.overallFraction
182+
// let prev = state.fractionState.overallFraction
180183
state.fractionState.selfFraction.completed = newValue
181-
manager.updateFractionCompleted(from: prev, to: state.fractionState.overallFraction)
184+
manager.markDirty()
185+
// manager.updateFractionCompleted(from: prev, to: state.fractionState.overallFraction)
182186
manager.ghostReporter?.notifyObservers(with: .fractionUpdated)
183187

184188
manager.monitorInterop.withLock { [manager] interop in
@@ -251,33 +255,33 @@ internal struct AnyMetatypeWrapper: Hashable, Equatable, Sendable {
251255
self.init(total: totalCount, ghostReporter: nil, interopObservation: nil)
252256
}
253257

254-
/// Sets `totalCount`.
255-
/// - Parameter newTotal: Total units of work.
256-
public func setTotalCount(_ newTotal: Int?) {
257-
state.withLock { state in
258-
let previous = state.fractionState.overallFraction
259-
if state.fractionState.selfFraction.total != newTotal && state.fractionState.selfFraction.total > 0 {
260-
state.fractionState.childFraction = state.fractionState.childFraction * _ProgressFraction(completed: state.fractionState.selfFraction.total, total: newTotal ?? 1)
261-
}
262-
state.fractionState.selfFraction.total = newTotal ?? 0
263-
264-
// if newValue is nil, reset indeterminate to true
265-
if newTotal != nil {
266-
state.fractionState.indeterminate = false
267-
} else {
268-
state.fractionState.indeterminate = true
269-
}
270-
updateFractionCompleted(from: previous, to: state.fractionState.overallFraction)
271-
272-
ghostReporter?.notifyObservers(with: .totalCountUpdated)
273-
274-
monitorInterop.withLock { [self] interop in
275-
if interop == true {
276-
notifyObservers(with: .totalCountUpdated)
277-
}
278-
}
279-
}
280-
}
258+
// /// Sets `totalCount`.
259+
// /// - Parameter newTotal: Total units of work.
260+
// public func setTotalCount(_ newTotal: Int?) {
261+
// state.withLock { state in
262+
// let previous = state.fractionState.overallFraction
263+
// if state.fractionState.selfFraction.total != newTotal && state.fractionState.selfFraction.total > 0 {
264+
// state.fractionState.childFraction = state.fractionState.childFraction * _ProgressFraction(completed: state.fractionState.selfFraction.total, total: newTotal ?? 1)
265+
// }
266+
// state.fractionState.selfFraction.total = newTotal ?? 0
267+
//
268+
// // if newValue is nil, reset indeterminate to true
269+
// if newTotal != nil {
270+
// state.fractionState.indeterminate = false
271+
// } else {
272+
// state.fractionState.indeterminate = true
273+
// }
274+
// updateFractionCompleted(from: previous, to: state.fractionState.overallFraction)
275+
//
276+
// ghostReporter?.notifyObservers(with: .totalCountUpdated)
277+
//
278+
// monitorInterop.withLock { [self] interop in
279+
// if interop == true {
280+
// notifyObservers(with: .totalCountUpdated)
281+
// }
282+
// }
283+
// }
284+
// }
281285

282286
/// Returns a `Subprogress` representing a portion of `self` which can be passed to any method that reports progress.
283287
///
@@ -308,8 +312,12 @@ internal struct AnyMetatypeWrapper: Hashable, Equatable, Sendable {
308312
/// Increases `completedCount` by `count`.
309313
/// - Parameter count: Units of work.
310314
public func complete(count: Int) {
311-
let updateState = updateCompletedCount(count: count)
312-
updateFractionCompleted(from: updateState.previous, to: updateState.current)
315+
state.withLock { state in
316+
state.fractionState.selfFraction.completed += count
317+
}
318+
markDirty()
319+
// let updateState = updateCompletedCount(count: count)
320+
// updateFractionCompleted(from: updateState.previous, to: updateState.current)
313321

314322
// Interop updates stuff
315323
ghostReporter?.notifyObservers(with: .fractionUpdated)
@@ -485,6 +493,20 @@ internal struct AnyMetatypeWrapper: Hashable, Equatable, Sendable {
485493
updateFractionCompleted(from: updateState.previous, to: updateState.current)
486494
}
487495

496+
497+
/// Update completedCount and mark all ancestors as dirty.
498+
private func markDirty() {
499+
state.withLock { state in
500+
state.isDirty = true
501+
}
502+
// recursively mark all ancestors as dirty
503+
parents.withLock { parents in
504+
for (parent, _) in parents {
505+
parent.markDirty()
506+
}
507+
}
508+
}
509+
488510
//MARK: Interop-related internal methods
489511
/// Adds `observer` to list of `_observers` in `self`.
490512
internal func addObserver(observer: @escaping @Sendable (ObserverState) -> Void) {

0 commit comments

Comments
 (0)