Skip to content

Commit c71434f

Browse files
committed
refactore rest of getUpdatedSummary for allowed properties
1 parent 1d5f5e7 commit c71434f

File tree

2 files changed

+348
-148
lines changed

2 files changed

+348
-148
lines changed

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

Lines changed: 48 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -38,170 +38,70 @@ extension ProgressManager {
3838
}
3939

4040
internal func getUpdatedDoubleSummary(property: MetatypeWrapper<Double, Double>) -> Double {
41+
// Collect information from state
42+
let updateInfo = state.withLock { state in
43+
state.getDoubleSummaryUpdateInfo(property: property)
44+
}
45+
46+
// Get updated summary for each dirty child
47+
let updatedSummaries = updateInfo.dirtyChildren.map { (index, child) in
48+
State.DoubleSummaryUpdate(index: index, updatedSummary: child.getUpdatedDoubleSummary(property: property))
49+
}
50+
51+
// Consolidate updated values
4152
return state.withLock { state in
42-
43-
var value: Double = property.defaultSummary
44-
property.reduce(&value, state.propertiesDouble[property] ?? property.defaultValue)
45-
46-
guard !state.children.isEmpty else {
47-
return value
48-
}
49-
50-
for (idx, childState) in state.children.enumerated() {
51-
if let childPropertyState = childState.childPropertiesDouble[property] {
52-
if childPropertyState.isDirty {
53-
// Update dirty path
54-
if let child = childState.child {
55-
let updatedSummary = child.getUpdatedDoubleSummary(property: property)
56-
let newChildPropertyState = PropertyStateDouble(value: updatedSummary, isDirty: false)
57-
state.children[idx].childPropertiesDouble[property] = newChildPropertyState
58-
value = property.merge(value, updatedSummary)
59-
}
60-
} else {
61-
if let _ = childState.child {
62-
// Merge non-dirty, updated value
63-
value = property.merge(value, childPropertyState.value)
64-
} else {
65-
value = property.finalSummary(value, childPropertyState.value)
66-
}
67-
}
68-
} else {
69-
// First fetch of value
70-
if let child = childState.child {
71-
let childSummary = child.getUpdatedDoubleSummary(property: property)
72-
let newChildPropertyState = PropertyStateDouble(value: childSummary, isDirty: false)
73-
state.children[idx].childPropertiesDouble[property] = newChildPropertyState
74-
value = property.merge(value, childSummary)
75-
}
76-
}
77-
}
78-
return value
53+
state.updateDoubleSummary(updateInfo, updatedSummaries)
7954
}
8055
}
8156

8257
internal func getUpdatedStringSummary(property: MetatypeWrapper<String?, [String?]>) -> [String?] {
58+
// Collect information from state
59+
let updateInfo = state.withLock { state in
60+
state.getStringSummaryUpdateInfo(property: property)
61+
}
62+
63+
// Get updated summary for each dirty child
64+
let updatedSummaries = updateInfo.dirtyChildren.map { (index, child) in
65+
State.StringSummaryUpdate(index: index, updatedSummary: child.getUpdatedStringSummary(property: property))
66+
}
67+
68+
// Consolidate updated values
8369
return state.withLock { state in
84-
85-
var value: [String?] = property.defaultSummary
86-
property.reduce(&value, state.propertiesString[property] ?? property.defaultValue)
87-
88-
guard !state.children.isEmpty else {
89-
return value
90-
}
91-
92-
for (idx, childState) in state.children.enumerated() {
93-
if let childPropertyState = childState.childPropertiesString[property] {
94-
if childPropertyState.isDirty {
95-
// Update dirty path
96-
if let child = childState.child {
97-
let updatedSummary = child.getUpdatedStringSummary(property: property)
98-
let newChildPropertyState = PropertyStateString(value: updatedSummary, isDirty: false)
99-
state.children[idx].childPropertiesString[property] = newChildPropertyState
100-
value = property.merge(value, updatedSummary)
101-
}
102-
} else {
103-
if let _ = childState.child {
104-
// Merge non-dirty, updated value
105-
value = property.merge(value, childPropertyState.value)
106-
} else {
107-
value = property.finalSummary(value, childPropertyState.value)
108-
}
109-
}
110-
} else {
111-
// First fetch of value
112-
if let child = childState.child {
113-
let childSummary = child.getUpdatedStringSummary(property: property)
114-
let newChildPropertyState = PropertyStateString(value: childSummary, isDirty: false)
115-
state.children[idx].childPropertiesString[property] = newChildPropertyState
116-
value = property.merge(value, childSummary)
117-
}
118-
}
119-
}
120-
return value
70+
state.updateStringSummary(updateInfo, updatedSummaries)
12171
}
12272
}
12373

12474
internal func getUpdatedURLSummary(property: MetatypeWrapper<URL?, [URL?]>) -> [URL?] {
75+
// Collect information from state
76+
let updateInfo = state.withLock { state in
77+
state.getURLSummaryUpdateInfo(property: property)
78+
}
79+
80+
// Get updated summary for each dirty child
81+
let updatedSummaries = updateInfo.dirtyChildren.map { (index, child) in
82+
State.URLSummaryUpdate(index: index, updatedSummary: child.getUpdatedURLSummary(property: property))
83+
}
84+
85+
// Consolidate updated values
12586
return state.withLock { state in
126-
127-
var value: [URL?] = property.defaultSummary
128-
property.reduce(&value, state.propertiesURL[property] ?? property.defaultValue)
129-
130-
guard !state.children.isEmpty else {
131-
return value
132-
}
133-
134-
for (idx, childState) in state.children.enumerated() {
135-
if let childPropertyState = childState.childPropertiesURL[property] {
136-
if childPropertyState.isDirty {
137-
// Update dirty path
138-
if let child = childState.child {
139-
let updatedSummary = child.getUpdatedURLSummary(property: property)
140-
let newChildPropertyState = PropertyStateURL(value: updatedSummary, isDirty: false)
141-
state.children[idx].childPropertiesURL[property] = newChildPropertyState
142-
value = property.merge(value, updatedSummary)
143-
}
144-
} else {
145-
if let _ = childState.child {
146-
// Merge non-dirty, updated value
147-
value = property.merge(value, childPropertyState.value)
148-
} else {
149-
value = property.finalSummary(value, childPropertyState.value)
150-
}
151-
}
152-
} else {
153-
// First fetch of value
154-
if let child = childState.child {
155-
let childSummary = child.getUpdatedURLSummary(property: property)
156-
let newChildPropertyState = PropertyStateURL(value: childSummary, isDirty: false)
157-
state.children[idx].childPropertiesURL[property] = newChildPropertyState
158-
value = property.merge(value, childSummary)
159-
}
160-
}
161-
}
162-
return value
87+
state.updateURLSummary(updateInfo, updatedSummaries)
16388
}
16489
}
16590

16691
internal func getUpdatedUInt64Summary(property: MetatypeWrapper<UInt64, [UInt64]>) -> [UInt64] {
92+
// Collect information from state
93+
let updateInfo = state.withLock { state in
94+
state.getUInt64SummaryUpdateInfo(property: property)
95+
}
96+
97+
// Get updated summary for each dirty child
98+
let updatedSummaries = updateInfo.dirtyChildren.map { (index, child) in
99+
State.UInt64SummaryUpdate(index: index, updatedSummary: child.getUpdatedUInt64Summary(property: property))
100+
}
101+
102+
// Consolidate updated values
167103
return state.withLock { state in
168-
169-
var value: [UInt64] = property.defaultSummary
170-
property.reduce(&value, state.propertiesUInt64[property] ?? property.defaultValue)
171-
172-
guard !state.children.isEmpty else {
173-
return value
174-
}
175-
176-
for (idx, childState) in state.children.enumerated() {
177-
if let childPropertyState = childState.childPropertiesUInt64[property] {
178-
if childPropertyState.isDirty {
179-
// Update dirty path
180-
if let child = childState.child {
181-
let updatedSummary = child.getUpdatedUInt64Summary(property: property)
182-
let newChildPropertyState = PropertyStateThroughput(value: updatedSummary, isDirty: false)
183-
state.children[idx].childPropertiesUInt64[property] = newChildPropertyState
184-
value = property.merge(value, updatedSummary)
185-
}
186-
} else {
187-
if let _ = childState.child {
188-
// Merge non-dirty, updated value
189-
value = property.merge(value, childPropertyState.value)
190-
} else {
191-
value = property.finalSummary(value, childPropertyState.value)
192-
}
193-
}
194-
} else {
195-
// First fetch of value
196-
if let child = childState.child {
197-
let childSummary = child.getUpdatedUInt64Summary(property: property)
198-
let newChildPropertyState = PropertyStateThroughput(value: childSummary, isDirty: false)
199-
state.children[idx].childPropertiesUInt64[property] = newChildPropertyState
200-
value = property.merge(value, childSummary)
201-
}
202-
}
203-
}
204-
return value
104+
state.updateUInt64Summary(updateInfo, updatedSummaries)
205105
}
206106
}
207107

0 commit comments

Comments
 (0)