Skip to content

Commit 1d5f5e7

Browse files
committed
refactor getUpdatedIntSummary method
1 parent e016096 commit 1d5f5e7

File tree

2 files changed

+87
-39
lines changed

2 files changed

+87
-39
lines changed

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

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,19 @@ extension ProgressManager {
2121

2222
//MARK: Helper Methods for Updating Dirty Path
2323
internal func getUpdatedIntSummary(property: MetatypeWrapper<Int, Int>) -> Int {
24+
// Collect information from state
25+
let updateInfo = state.withLock { state in
26+
state.getIntSummaryUpdateInfo(property: property)
27+
}
28+
29+
// Get updated summary for each dirty child
30+
let updatedSummaries = updateInfo.dirtyChildren.map { (index, child) in
31+
State.IntSummaryUpdate(index: index, updatedSummary: child.getUpdatedIntSummary(property: property))
32+
}
33+
34+
// Consolidate updated values
2435
return state.withLock { state in
25-
26-
var value: Int = property.defaultSummary
27-
property.reduce(&value, state.propertiesInt[property] ?? property.defaultValue)
28-
29-
guard !state.children.isEmpty else {
30-
return value
31-
}
32-
33-
for (idx, childState) in state.children.enumerated() {
34-
if let childPropertyState = childState.childPropertiesInt[property] {
35-
if childPropertyState.isDirty {
36-
// Dirty, needs to fetch value
37-
if let child = childState.child {
38-
let updatedSummary = child.getUpdatedIntSummary(property: property)
39-
let newChildPropertyState = PropertyStateInt(value: updatedSummary, isDirty: false)
40-
state.children[idx].childPropertiesInt[property] = newChildPropertyState
41-
value = property.merge(value, updatedSummary)
42-
}
43-
} else {
44-
// Not dirty, use value directly
45-
if let _ = childState.child {
46-
value = property.merge(value, childPropertyState.value)
47-
} else {
48-
// TODO: What to do after terminate? Set to nil?
49-
value = property.finalSummary(value, childPropertyState.value)
50-
}
51-
}
52-
} else {
53-
// Said property doesn't even get cached yet, but children might have been set
54-
if let child = childState.child {
55-
// If there is a child
56-
let childSummary = child.getUpdatedIntSummary(property: property)
57-
let newChildPropertyState = PropertyStateInt(value: childSummary, isDirty: false)
58-
state.children[idx].childPropertiesInt[property] = newChildPropertyState
59-
value = property.merge(value, childSummary)
60-
}
61-
}
62-
}
63-
return value
36+
state.updateIntSummary(updateInfo, updatedSummaries)
6437
}
6538
}
6639

Sources/FoundationEssentials/ProgressManager/ProgressManager+State.swift

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,18 @@ extension ProgressManager {
301301
}
302302

303303
// MARK: Clean up dirty paths
304+
internal struct IntSummaryUpdateInfo {
305+
let currentSummary: Int
306+
let dirtyChildren: [(index: Int, manager: ProgressManager)]
307+
let nonDirtySummaries: [(index: Int, summary: Int, isAlive: Bool)]
308+
let property: MetatypeWrapper<Int, Int>
309+
}
310+
311+
internal struct IntSummaryUpdate {
312+
let index: Int
313+
let updatedSummary: Int
314+
}
315+
304316
internal struct FileCountUpdateInfo {
305317
let currentSummary: Int
306318
let dirtyChildren: [(index: Int, manager: ProgressManager)]
@@ -347,6 +359,69 @@ extension ProgressManager {
347359
let updatedSummary: Duration
348360
}
349361

362+
internal mutating func getIntSummaryUpdateInfo(property: MetatypeWrapper<Int, Int>) -> IntSummaryUpdateInfo {
363+
var currentSummary: Int = property.defaultSummary
364+
property.reduce(&currentSummary, propertiesInt[property] ?? property.defaultValue)
365+
366+
guard !children.isEmpty else {
367+
return IntSummaryUpdateInfo(
368+
currentSummary: currentSummary,
369+
dirtyChildren: [],
370+
nonDirtySummaries: [],
371+
property: property
372+
)
373+
}
374+
375+
var dirtyChildren: [(index: Int, manager: ProgressManager)] = []
376+
var nonDirtySummaries: [(index: Int, summary: Int, isAlive: Bool)] = []
377+
378+
for (idx, childState) in children.enumerated() {
379+
if let childPropertyState = childState.childPropertiesInt[property] {
380+
if childPropertyState.isDirty {
381+
if let child = childState.child {
382+
dirtyChildren.append((idx, child))
383+
}
384+
} else {
385+
let isAlive = childState.child != nil
386+
nonDirtySummaries.append((idx, childPropertyState.value, isAlive))
387+
}
388+
} else {
389+
// Property doesn't exist yet in child - need to fetch it
390+
if let child = childState.child {
391+
dirtyChildren.append((idx, child))
392+
}
393+
}
394+
}
395+
396+
return IntSummaryUpdateInfo(
397+
currentSummary: currentSummary,
398+
dirtyChildren: dirtyChildren,
399+
nonDirtySummaries: nonDirtySummaries,
400+
property: property
401+
)
402+
}
403+
404+
internal mutating func updateIntSummary(_ updateInfo: IntSummaryUpdateInfo, _ childUpdates: [IntSummaryUpdate]) -> Int {
405+
var value = updateInfo.currentSummary
406+
407+
// Apply updates from children that were dirty
408+
for update in childUpdates {
409+
children[update.index].childPropertiesInt[updateInfo.property] = PropertyStateInt(value: update.updatedSummary, isDirty: false)
410+
value = updateInfo.property.merge(value, update.updatedSummary)
411+
}
412+
413+
// Apply values from non-dirty children
414+
for (_, childSummary, isAlive) in updateInfo.nonDirtySummaries {
415+
if isAlive {
416+
value = updateInfo.property.merge(value, childSummary)
417+
} else {
418+
value = updateInfo.property.finalSummary(value, childSummary)
419+
}
420+
}
421+
422+
return value
423+
}
424+
350425
internal mutating func getFileCountUpdateInfo(type: CountType) -> FileCountUpdateInfo {
351426
let currentSummary: Int
352427
var dirtyChildren: [(index: Int, manager: ProgressManager)] = []

0 commit comments

Comments
 (0)