Skip to content

Commit f24b8b1

Browse files
committed
replace redundant deinit complete(count:) call with mark dirty + add tests for deinit bahavior
1 parent 374ec32 commit f24b8b1

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

Sources/FoundationEssentials/ProgressManager/ProgressManager+State.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,13 @@ extension ProgressManager {
179179
return selfFraction.isIndeterminate
180180
}
181181

182-
internal func getIsFinished() -> Bool {
182+
internal mutating func getIsFinished() -> Bool {
183183
#if FOUNDATION_FRAMEWORK
184184
if let interopIsFinished = interopType?.isFinished {
185185
return interopIsFinished
186186
}
187187
#endif
188+
updateChildrenProgressFraction()
188189
return selfFraction.isFinished
189190
}
190191

Sources/FoundationEssentials/ProgressManager/ProgressManager.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,7 @@ internal import _FoundationCollections
297297
}
298298

299299
deinit {
300-
if let totalCount = self.totalCount {
301-
let diff = totalCount - self.completedCount
302-
self.complete(count: diff)
303-
}
304-
300+
305301
let (propertiesInt, propertiesDouble, propertiesString, propertiesURL, propertiesUInt64, parents) = state.withLock { state in
306302
return (state.propertiesInt, state.propertiesDouble, state.propertiesString, state.propertiesURL, state.propertiesUInt64, state.parents)
307303
}
@@ -344,6 +340,10 @@ internal import _FoundationCollections
344340
let estimatedTimeRemaining = self.getUpdatedEstimatedTimeRemaining()
345341
let fileURL = self.getUpdatedFileURL()
346342

343+
if !isFinished {
344+
markSelfDirty(parents: parents)
345+
}
346+
347347
for parentState in parents {
348348
parentState.parent.setChildDeclaredAdditionalProperties(
349349
at: parentState.positionInParent,

Tests/FoundationEssentialsTests/ProgressManager/ProgressManagerTests.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ extension Tag {
330330
}
331331
}
332332

333+
// MARK: Test deinit behavior
333334
func makeUnfinishedChild(subprogress: consuming Subprogress) async {
334335
let manager = subprogress.start(totalCount: 3)
335336
manager.complete(count: 2)
@@ -345,4 +346,31 @@ extension Tag {
345346
await makeUnfinishedChild(subprogress: manager.subprogress(assigningCount: 1))
346347
#expect(manager.fractionCompleted == 1.0)
347348
}
349+
350+
func makeFinishedChild(subprogress: consuming Subprogress) async {
351+
let manager = subprogress.start(totalCount: 2)
352+
manager.complete(count: 2)
353+
}
354+
355+
@Test func finishedChild() async throws {
356+
let manager = ProgressManager(totalCount: 2)
357+
358+
manager.complete(count: 1)
359+
#expect(manager.fractionCompleted == 0.5)
360+
361+
await makeFinishedChild(subprogress: manager.subprogress(assigningCount: 1))
362+
#expect(manager.fractionCompleted == 1.0)
363+
}
364+
365+
@Test func uninitializedSubprogress() async throws {
366+
let manager = ProgressManager(totalCount: 2)
367+
368+
manager.complete(count: 1)
369+
370+
var subprogress: Subprogress? = manager.subprogress(assigningCount: 1)
371+
#expect(manager.fractionCompleted == 0.5)
372+
373+
subprogress = nil
374+
#expect(manager.fractionCompleted == 1.0)
375+
}
348376
}

0 commit comments

Comments
 (0)