Skip to content

Commit 89aeff6

Browse files
committed
move deinit cleanup to State
1 parent 0ed3183 commit 89aeff6

File tree

2 files changed

+273
-270
lines changed

2 files changed

+273
-270
lines changed

Sources/FoundationEssentials/ProgressManager/ProgressManager+State.swift

Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,5 +1264,277 @@ extension ProgressManager {
12641264

12651265
return value
12661266
}
1267+
1268+
struct FinalSummary {
1269+
var totalFileCountSummary: Int
1270+
var completedFileCountSummary: Int
1271+
var totalByteCountSummary: UInt64
1272+
var completedByteCountSummary: UInt64
1273+
var throughputSummary: [UInt64]
1274+
var estimatedTimeRemainingSummary: Duration
1275+
var customPropertiesIntSummary: [MetatypeWrapper<Int, Int>: Int]
1276+
var customPropertiesUInt64Summary: [MetatypeWrapper<UInt64, UInt64>: UInt64]
1277+
var customPropertiesDoubleSummary: [MetatypeWrapper<Double, Double>: Double]
1278+
var customPropertiesStringSummary: [MetatypeWrapper<String?, [String?]>: [String?]]
1279+
var customPropertiesURLSummary: [MetatypeWrapper<URL?, [URL?]>: [URL?]]
1280+
var customPropertiesUInt64ArraySummary: [MetatypeWrapper<UInt64, [UInt64]>: [UInt64]]
1281+
var customPropertiesDurationSummary: [MetatypeWrapper<Duration, Duration>: Duration]
1282+
}
1283+
1284+
func customPropertiesCleanup() -> (FinalSummary, [Parent]) {
1285+
// Set up default summaries
1286+
var totalFileCount = Properties.TotalFileCount.defaultSummary
1287+
var completedFileCount = Properties.CompletedFileCount.defaultSummary
1288+
var totalByteCount = Properties.TotalByteCount.defaultSummary
1289+
var completedByteCount = Properties.CompletedByteCount.defaultSummary
1290+
var throughput = Properties.Throughput.defaultSummary
1291+
var estimatedTimeRemaining = Properties.EstimatedTimeRemaining.defaultSummary
1292+
var customPropertiesIntSummary: [MetatypeWrapper<Int, Int>: Int] = [:]
1293+
var customPropertiesUInt64Summary: [MetatypeWrapper<UInt64, UInt64>: UInt64] = [:]
1294+
var customPropertiesDoubleSummary: [MetatypeWrapper<Double, Double>: Double] = [:]
1295+
var customPropertiesStringSummary: [MetatypeWrapper<String?, [String?]>: [String?]] = [:]
1296+
var customPropertiesURLSummary: [MetatypeWrapper<URL?, [URL?]>: [URL?]] = [:]
1297+
var customPropertiesUInt64ArraySummary: [MetatypeWrapper<UInt64, [UInt64]>: [UInt64]] = [:]
1298+
var customPropertiesDurationSummary: [MetatypeWrapper<Duration, Duration>: Duration] = [:]
1299+
1300+
// Include self's custom properties values
1301+
Properties.TotalFileCount.reduce(into: &totalFileCount, value: self.totalFileCount)
1302+
Properties.CompletedFileCount.reduce(into: &completedFileCount, value: self.completedFileCount)
1303+
Properties.TotalByteCount.reduce(into: &totalByteCount, value: self.totalByteCount)
1304+
Properties.CompletedByteCount.reduce(into: &completedByteCount, value: self.completedByteCount)
1305+
Properties.Throughput.reduce(into: &throughput, value: self.throughput)
1306+
Properties.EstimatedTimeRemaining.reduce(into: &estimatedTimeRemaining, value: self.estimatedTimeRemaining)
1307+
1308+
// MARK: Custom Properties (Int, Int)
1309+
// Aggregate information using self's custom property keys
1310+
for (key, value) in customPropertiesInt {
1311+
// Set up overall summary
1312+
var summary = key.defaultSummary
1313+
1314+
// Include self's value into summary
1315+
key.reduce(&summary, value)
1316+
1317+
// Save summary to dictionary
1318+
customPropertiesIntSummary[key] = summary
1319+
}
1320+
1321+
// MARK: Custom Properties (UInt64, UInt64)
1322+
// Aggregate information using self's custom property keys
1323+
for (key, value) in customPropertiesUInt64 {
1324+
// Set up overall summary
1325+
var summary = key.defaultSummary
1326+
1327+
// Include self's value into summary
1328+
key.reduce(&summary, value)
1329+
1330+
// Save summary to dictionary
1331+
customPropertiesUInt64Summary[key] = summary
1332+
}
1333+
1334+
// MARK: Custom Properties (UInt64, [UInt64])
1335+
// Aggregate information using self's custom property keys
1336+
for (key, value) in customPropertiesUInt64Array {
1337+
// Set up overall summary
1338+
var summary = key.defaultSummary
1339+
1340+
// Include self's value into summary
1341+
key.reduce(&summary, value)
1342+
1343+
// Save summary to dictionary
1344+
customPropertiesUInt64ArraySummary[key] = summary
1345+
}
1346+
1347+
// MARK: Custom Properties (Double, Double)
1348+
// Aggregate information using self's custom property keys
1349+
for (key, value) in customPropertiesDouble {
1350+
// Set up overall summary
1351+
var summary = key.defaultSummary
1352+
1353+
// Include self's value into summary
1354+
key.reduce(&summary, value)
1355+
1356+
// Save summary to dictionary
1357+
customPropertiesDoubleSummary[key] = summary
1358+
}
1359+
1360+
// MARK: Custom Properties (String?, [String?])
1361+
// Aggregate information using self's custom property keys
1362+
for (key, value) in customPropertiesString {
1363+
// Set up overall summary
1364+
var summary = key.defaultSummary
1365+
1366+
// Include self's value into summary
1367+
key.reduce(&summary, value)
1368+
1369+
// Save summary to dictionary
1370+
customPropertiesStringSummary[key] = summary
1371+
}
1372+
1373+
// MARK: Custom Properties (URL?, [URL?])
1374+
// Aggregate information using self's custom property keys
1375+
for (key, value) in customPropertiesURL {
1376+
// Set up overall summary
1377+
var summary = key.defaultSummary
1378+
1379+
// Include self's value into summary
1380+
key.reduce(&summary, value)
1381+
1382+
// Save summary to dictionary
1383+
customPropertiesURLSummary[key] = summary
1384+
}
1385+
1386+
// MARK: Custom Properties (Duration, Duration)
1387+
// Aggregate information using self's custom property keys
1388+
for (key, value) in customPropertiesDuration {
1389+
// Set up overall summary
1390+
var summary = key.defaultSummary
1391+
1392+
// Include self's value into summary
1393+
key.reduce(&summary, value)
1394+
1395+
// Save summary to dictionary
1396+
customPropertiesDurationSummary[key] = summary
1397+
}
1398+
1399+
// Include child's custom properties summaries, we need to take into account the fact that some of the children's custom properties may not be in self, so we need to check that too. As for the ones that are in self, we need to call finalSummary.
1400+
for child in children {
1401+
totalFileCount = Properties.TotalFileCount.finalSummary(totalFileCount, child.totalFileCountSummary.value)
1402+
completedFileCount = Properties.CompletedFileCount.finalSummary(completedFileCount, child.completedFileCountSummary.value)
1403+
totalByteCount = Properties.TotalByteCount.finalSummary(totalByteCount, child.totalByteCountSummary.value)
1404+
completedByteCount = Properties.CompletedByteCount.finalSummary(completedByteCount, child.completedByteCountSummary.value)
1405+
throughput = Properties.Throughput.finalSummary(throughput, child.throughputSummary.value)
1406+
estimatedTimeRemaining = Properties.EstimatedTimeRemaining.finalSummary(estimatedTimeRemaining, child.estimatedTimeRemainingSummary.value)
1407+
1408+
for (key, _) in customPropertiesInt {
1409+
customPropertiesIntSummary[key] = key.finalSummary(customPropertiesIntSummary[key] ?? key.defaultSummary, child.customPropertiesIntSummary[key]?.value ?? key.defaultSummary)
1410+
}
1411+
1412+
// Aggregate information using child's custom property keys that may be absent from self's custom property keys
1413+
for (key, value) in child.customPropertiesIntSummary {
1414+
if !customPropertiesInt.keys.contains(key) {
1415+
// Set up default summary
1416+
var summary = key.defaultSummary
1417+
// Include child's value
1418+
summary = key.finalSummary(summary, value.value)
1419+
// Save summary value to dictionary
1420+
customPropertiesIntSummary[key] = summary
1421+
}
1422+
}
1423+
1424+
1425+
for (key, _) in customPropertiesUInt64 {
1426+
customPropertiesUInt64Summary[key] = key.finalSummary(customPropertiesUInt64Summary[key] ?? key.defaultSummary, child.customPropertiesUInt64Summary[key]?.value ?? key.defaultSummary)
1427+
}
1428+
1429+
// Aggregate information using child's custom property keys that may be absent from self's custom property keys
1430+
for (key, value) in child.customPropertiesUInt64Summary {
1431+
if !customPropertiesUInt64.keys.contains(key) {
1432+
// Set up default summary
1433+
var summary = key.defaultSummary
1434+
// Include child's value
1435+
summary = key.finalSummary(summary, value.value)
1436+
// Save summary value to dictionary
1437+
customPropertiesUInt64Summary[key] = summary
1438+
}
1439+
}
1440+
1441+
1442+
for (key, _) in customPropertiesUInt64Array {
1443+
customPropertiesUInt64ArraySummary[key] = key.finalSummary(customPropertiesUInt64ArraySummary[key] ?? key.defaultSummary, child.customPropertiesUInt64ArraySummary[key]?.value ?? key.defaultSummary)
1444+
}
1445+
1446+
// Aggregate information using child's custom property keys that may be absent from self's custom property keys
1447+
for (key, value) in child.customPropertiesUInt64ArraySummary {
1448+
if !customPropertiesUInt64Array.keys.contains(key) {
1449+
// Set up default summary
1450+
var summary = key.defaultSummary
1451+
// Include child's value
1452+
summary = key.finalSummary(summary, value.value)
1453+
// Save summary value to dictionary
1454+
customPropertiesUInt64ArraySummary[key] = summary
1455+
}
1456+
}
1457+
1458+
for (key, _) in customPropertiesDouble {
1459+
customPropertiesDoubleSummary[key] = key.finalSummary(customPropertiesDoubleSummary[key] ?? key.defaultSummary, child.customPropertiesDoubleSummary[key]?.value ?? key.defaultSummary)
1460+
}
1461+
1462+
// Aggregate information using child's custom property keys that may be absent from self's custom property keys
1463+
for (key, value) in child.customPropertiesDoubleSummary {
1464+
if !customPropertiesDouble.keys.contains(key) {
1465+
// Set up default summary
1466+
var summary = key.defaultSummary
1467+
// Include child's value
1468+
summary = key.finalSummary(summary, value.value)
1469+
// Save summary value to dictionary
1470+
customPropertiesDoubleSummary[key] = summary
1471+
}
1472+
}
1473+
1474+
for (key, _) in customPropertiesString {
1475+
customPropertiesStringSummary[key] = key.finalSummary(customPropertiesStringSummary[key] ?? key.defaultSummary, child.customPropertiesStringSummary[key]?.value ?? key.defaultSummary)
1476+
}
1477+
1478+
// Aggregate information using child's custom property keys that may be absent from self's custom property keys
1479+
for (key, value) in child.customPropertiesStringSummary {
1480+
if !customPropertiesString.keys.contains(key) {
1481+
// Set up default summary
1482+
var summary = key.defaultSummary
1483+
// Include child's value
1484+
summary = key.finalSummary(summary, value.value)
1485+
// Save summary value to dictionary
1486+
customPropertiesStringSummary[key] = summary
1487+
}
1488+
}
1489+
1490+
for (key, _) in customPropertiesURL {
1491+
customPropertiesURLSummary[key] = key.finalSummary(customPropertiesURLSummary[key] ?? key.defaultSummary, child.customPropertiesURLSummary[key]?.value ?? key.defaultSummary)
1492+
}
1493+
1494+
// Aggregate information using child's custom property keys that may be absent from self's custom property keys
1495+
for (key, value) in child.customPropertiesURLSummary {
1496+
if !customPropertiesURL.keys.contains(key) {
1497+
// Set up default summary
1498+
var summary = key.defaultSummary
1499+
// Include child's value
1500+
summary = key.finalSummary(summary, value.value)
1501+
// Save summary value to dictionary
1502+
customPropertiesURLSummary[key] = summary
1503+
}
1504+
}
1505+
1506+
for (key, _) in customPropertiesDuration {
1507+
customPropertiesDurationSummary[key] = key.finalSummary(customPropertiesDurationSummary[key] ?? key.defaultSummary, child.customPropertiesDurationSummary[key]?.value ?? key.defaultSummary)
1508+
}
1509+
1510+
// Aggregate information using child's custom property keys that may be absent from self's custom property keys
1511+
for (key, value) in child.customPropertiesDurationSummary {
1512+
if !customPropertiesDuration.keys.contains(key) {
1513+
// Set up default summary
1514+
var summary = key.defaultSummary
1515+
// Include child's value
1516+
summary = key.finalSummary(summary, value.value)
1517+
// Save summary value to dictionary
1518+
customPropertiesDurationSummary[key] = summary
1519+
}
1520+
}
1521+
}
1522+
1523+
return (FinalSummary(totalFileCountSummary: totalFileCount,
1524+
completedFileCountSummary: completedFileCount,
1525+
totalByteCountSummary: totalByteCount,
1526+
completedByteCountSummary: completedByteCount,
1527+
throughputSummary: throughput,
1528+
estimatedTimeRemainingSummary: estimatedTimeRemaining,
1529+
customPropertiesIntSummary: customPropertiesIntSummary,
1530+
customPropertiesUInt64Summary: customPropertiesUInt64Summary,
1531+
customPropertiesDoubleSummary: customPropertiesDoubleSummary,
1532+
customPropertiesStringSummary: customPropertiesStringSummary,
1533+
customPropertiesURLSummary: customPropertiesURLSummary,
1534+
customPropertiesUInt64ArraySummary: customPropertiesUInt64ArraySummary,
1535+
customPropertiesDurationSummary: customPropertiesDurationSummary
1536+
),
1537+
parents)
1538+
}
12671539
}
12681540
}

0 commit comments

Comments
 (0)