Skip to content

Commit 4c86d15

Browse files
committed
add code documentation
1 parent 649f824 commit 4c86d15

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

Sources/FoundationEssentials/ProgressManager/ProgressManager.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ internal struct AnyMetatypeWrapper: Hashable, Equatable, Sendable {
118118
}
119119
}
120120

121+
122+
/// A `ProgressReporter` instance, used for providing read-only observation of progress updates or composing into other `ProgressManager`s.
121123
public var reporter: ProgressReporter {
122124
return .init(manager: self)
123125
}
@@ -127,6 +129,7 @@ internal struct AnyMetatypeWrapper: Hashable, Equatable, Sendable {
127129

128130
associatedtype Value: Sendable, Hashable, Equatable
129131

132+
/// The default value to return when property is not set to a specific value.
130133
static var defaultValue: Value { get }
131134
}
132135

@@ -252,6 +255,9 @@ internal struct AnyMetatypeWrapper: Hashable, Equatable, Sendable {
252255

253256
/// Returns a `Subprogress` representing a portion of `self` which can be passed to any method that reports progress.
254257
///
258+
/// If the `Subprogress` is not converted into a `ProgressManager` (for example, due to an error or early return),
259+
/// then the assigned count is marked as completed in the parent `ProgressManager`.
260+
///
255261
/// - Parameter count: Units, which is a portion of `totalCount`delegated to an instance of `Subprogress`.
256262
/// - Returns: A `Subprogress` instance.
257263
public func subprogress(assigningCount portionOfParent: Int) -> Subprogress {
@@ -264,16 +270,16 @@ internal struct AnyMetatypeWrapper: Hashable, Equatable, Sendable {
264270
/// Adds a `ProgressReporter` as a child, with its progress representing a portion of `self`'s progress.
265271
/// - Parameters:
266272
/// - reporter: A `ProgressReporter` instance.
267-
/// - portionOfParent: Units, which is a portion of `totalCount`delegated to an instance of `Subprogress`.
268-
public func assign(count portionOfParent: Int, to reporter: ProgressReporter) {
273+
/// - count: Units, which is a portion of `totalCount`delegated to an instance of `Subprogress`.
274+
public func assign(count: Int, to reporter: ProgressReporter) {
269275
precondition(isCycle(reporter: reporter) == false, "Creating a cycle is not allowed.")
270276

271277
// get the actual progress from within the reporter, then add as children
272278
let actualManager = reporter.manager
273279

274280
// Add reporter as child + Add self as parent
275281
self.addToChildren(childManager: actualManager)
276-
actualManager.addParent(parentReporter: self, portionOfParent: portionOfParent)
282+
actualManager.addParent(parentReporter: self, portionOfParent: count)
277283
}
278284

279285
/// Increases `completedCount` by `count`.

Sources/FoundationEssentials/ProgressManager/ProgressReporter.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,40 @@ import Observation
1818
/// It is read-only and can be added as a child of another ProgressManager.
1919
@Observable public final class ProgressReporter: Sendable {
2020

21+
/// The total units of work.
2122
var totalCount: Int? {
2223
manager.totalCount
2324
}
2425

26+
/// The completed units of work.
27+
/// If `self` is indeterminate, the value will be 0.
2528
var completedCount: Int {
2629
manager.completedCount
2730
}
2831

32+
/// The proportion of work completed.
33+
/// This takes into account the fraction completed in its children instances if children are present.
34+
/// If `self` is indeterminate, the value will be 0.
2935
var fractionCompleted: Double {
3036
manager.fractionCompleted
3137
}
3238

39+
/// The state of initialization of `totalCount`.
40+
/// If `totalCount` is `nil`, the value will be `true`.
3341
var isIndeterminate: Bool {
3442
manager.isIndeterminate
3543
}
3644

45+
/// The state of completion of work.
46+
/// If `completedCount` >= `totalCount`, the value will be `true`.
3747
var isFinished: Bool {
3848
manager.isFinished
3949
}
4050

4151
/// Reads properties that convey additional information about progress.
42-
public func withProperties<T>(_ closure: @Sendable (ProgressManager.Values) throws -> T) rethrows -> T {
52+
public func withProperties<T, E: Error>(
53+
_ closure: (sending ProgressManager.Values) throws(E) -> sending T
54+
) throws(E) -> T {
4355
return try manager.getAdditionalProperties(closure)
4456
}
4557

Tests/FoundationEssentialsTests/ProgressManager/ProgressManagerTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,16 @@ class TestProgressReporter: XCTestCase {
607607

608608
manager.complete(count: 1)
609609
XCTAssertEqual(reporter.completedCount, 3)
610+
611+
let fileCount = reporter.withProperties { properties in
612+
properties.totalFileCount
613+
}
614+
XCTAssertEqual(fileCount, 0)
615+
616+
manager.withProperties { properties in
617+
properties.totalFileCount = 6
618+
}
619+
XCTAssertEqual(reporter.withProperties(\.totalFileCount), 6)
610620
}
611621

612622
func testAddProgressReporterAsChild() {

0 commit comments

Comments
 (0)