Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Let a page that comes back start its scan while the one it left still waits

Țugui Dragoșf3f248072c59
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/ScanRun.swift b/Packages/PeelCore/Sources/PeelCore/Support/ScanRun.swift
index 5b429b11..f6fb46ba 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/ScanRun.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/ScanRun.swift
@@ -13,6 +13,11 @@ public final class ScanRun {
/// True when the last scan was stopped before it finished. The page then shows what the scan before it
/// found, or says that nothing was read.
public private(set) var wasStopped = false
+ /// True when the page that asked for the running scan went away. The scan may still be waiting on macOS, and
+ /// its answer will be dropped, so a page that comes back starts its own.
+ public private(set) var isAbandoned = false
+ /// True while a scan runs for a page that still waits for its answer.
+ public var isWaitedFor: Bool { isRunning && !isAbandoned }
/// What the scan has read so far (`ScanCount`), brought up to date ten times a second while it runs.
public private(set) var itemsRead = 0
@ObservationIgnored private var newest = 0
@@ -30,6 +35,7 @@ public final class ScanRun {
let task = Task { await ScanCount.$current.withValue(count) { await work() } }
stopNewest = { task.cancel() }
wasStopped = false
+ isAbandoned = false
isRunning = true
itemsRead = 0
let reading = Task { [weak self] in
@@ -43,11 +49,16 @@ public final class ScanRun {
await task.value
} onCancel: {
task.cancel()
+ Task { @MainActor [weak self] in
+ guard let self, asked == self.newest else { return }
+ self.isAbandoned = true
+ }
}
reading.cancel()
guard asked == newest else { return nil }
stopNewest = nil
isRunning = false
+ isAbandoned = false
return task.isCancelled ? nil : answer
}
@@ -59,6 +70,7 @@ public final class ScanRun {
stopNewest = nil
newest += 1
isRunning = false
+ isAbandoned = false
wasStopped = true
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ScanRunTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ScanRunTests.swift
index 16e12c82..b2bc91d2 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ScanRunTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ScanRunTests.swift
@@ -26,6 +26,29 @@ struct ScanRunTests {
}
}
+ /// Work that waits, whatever cancels it, until the test lets it go, as a read waits while macOS asks.
+ private final class Stuck: Sendable {
+ private let state = Mutex<(started: Bool, waiter: CheckedContinuation<Void, Never>?)>((false, nil))
+
+ func run() async -> Int {
+ await withCheckedContinuation { waiter in state.withLock { $0 = (true, waiter) } }
+ return 1
+ }
+
+ func release() {
+ state.withLock {
+ $0.waiter?.resume()
+ $0.waiter = nil
+ }
+ }
+
+ func untilStarted() async {
+ while !state.withLock({ $0.started }) {
+ try? await Task.sleep(for: .milliseconds(1))
+ }
+ }
+ }
+
@Test func aStoppedScanAnswersNothingAndSaysSo() async {
let scan = ScanRun()
let waiting = Waiting()
@@ -72,6 +95,25 @@ struct ScanRunTests {
#expect(!scan.wasStopped)
}
+ @Test func aScanItsPageLeftIsNoLongerWaitedFor() async {
+ let scan = ScanRun()
+ let stuck = Stuck()
+ let asking = Task { await scan.run { await stuck.run() } }
+ await stuck.untilStarted()
+ let wasWaitedFor = scan.isWaitedFor
+
+ asking.cancel()
+ await Task.yield()
+ let isWaitedFor = scan.isWaitedFor
+ let next = await scan.run { 2 }
+ stuck.release()
+
+ #expect(wasWaitedFor && !isWaitedFor)
+ #expect(next == 2)
+ #expect(await asking.value == nil)
+ #expect(!scan.isRunning && !scan.wasStopped && !scan.isAbandoned)
+ }
+
/// While a scan runs, the page shows how many items it has read, a number that moves even while a folder
/// does not answer.
@Test func aScanSaysHowMuchItHasRead() async {
diff --git a/Peel/Model/PackageLibrary.swift b/Peel/Model/PackageLibrary.swift
index f1747f85..5067f175 100644
--- a/Peel/Model/PackageLibrary.swift
+++ b/Peel/Model/PackageLibrary.swift
@@ -49,7 +49,7 @@ final class PackageLibrary {
/// Reads the receipts again when one changed since the last scan.
func refreshIfReceiptsChanged() async {
- guard receipts != nil, !isScanning, !isWorking else { return }
+ guard receipts != nil, !scanRun.isWaitedFor, !isWorking else { return }
guard PackageActions.receiptsChanged() != scannedChange else { return }
await refresh()
}
diff --git a/Peel/Model/ProjectLibrary.swift b/Peel/Model/ProjectLibrary.swift
index d5b50b2b..c4b6ad51 100644
--- a/Peel/Model/ProjectLibrary.swift
+++ b/Peel/Model/ProjectLibrary.swift
@@ -60,7 +60,7 @@ final class ProjectLibrary {
/// Scans unless the last scan ended less than `age` ago, one or a removal runs, or the last scan was stopped: a
/// project changed in the last 7 days is held back, so what a scan saw ages.
func refreshUnlessScanned(within age: Duration) async {
- guard !isScanning, !isRemoving, !scanRun.wasStopped else { return }
+ guard !scanRun.isWaitedFor, !isRemoving, !scanRun.wasStopped else { return }
if let scannedAt, ContinuousClock.now - scannedAt < age { return }
await refresh()
}
diff --git a/Peel/Views/BackgroundItems/BackgroundItemList.swift b/Peel/Views/BackgroundItems/BackgroundItemList.swift
index 089cb8f5..34c1c17f 100644
--- a/Peel/Views/BackgroundItems/BackgroundItemList.swift
+++ b/Peel/Views/BackgroundItems/BackgroundItemList.swift
@@ -61,7 +61,7 @@ struct BackgroundItemList: View {
}
// The first scan waits for the installed apps, which it needs to trace each item to the app that owns it.
.task(id: library.hasLoaded) {
- guard library.hasLoaded, backgroundItems.items == nil, !backgroundItems.isScanning,
+ guard library.hasLoaded, backgroundItems.items == nil, !backgroundItems.scanRun.isWaitedFor,
!backgroundItems.scanRun.wasStopped
else { return }
await backgroundItems.refresh(installedApps: library.apps)
diff --git a/Peel/Views/Cloud/CloudView.swift b/Peel/Views/Cloud/CloudView.swift
index f4626ccd..6efa6c2b 100644
--- a/Peel/Views/Cloud/CloudView.swift
+++ b/Peel/Views/Cloud/CloudView.swift
@@ -219,7 +219,7 @@ struct CloudView: View {
Text(verbatim: leftAlone.message)
}
.task {
- guard cloud.files == nil, !cloud.isScanning, !cloud.scanRun.wasStopped else { return }
+ guard cloud.files == nil, !cloud.scanRun.isWaitedFor, !cloud.scanRun.wasStopped else { return }
await cloud.refresh()
}
.rescanOnExclusionChange("CloudView") { await cloud.refresh() }
diff --git a/Peel/Views/Developer/DeveloperList.swift b/Peel/Views/Developer/DeveloperList.swift
index 22939a6c..8b423068 100644
--- a/Peel/Views/Developer/DeveloperList.swift
+++ b/Peel/Views/Developer/DeveloperList.swift
@@ -53,7 +53,9 @@ struct DeveloperList: View {
}
}
.task {
- guard developer.environments == nil, !developer.isScanning, !developer.scanRun.wasStopped else { return }
+ guard developer.environments == nil, !developer.scanRun.isWaitedFor, !developer.scanRun.wasStopped else {
+ return
+ }
await developer.refresh()
}
.rescanOnExclusionChange("DeveloperList") { await developer.refresh() }
diff --git a/Peel/Views/Extensions/ExtensionList.swift b/Peel/Views/Extensions/ExtensionList.swift
index 369226cd..fdb5ae53 100644
--- a/Peel/Views/Extensions/ExtensionList.swift
+++ b/Peel/Views/Extensions/ExtensionList.swift
@@ -67,7 +67,9 @@ struct ExtensionList: View {
}
}
.task {
- guard extensions.extensions == nil, !extensions.isScanning, !extensions.scanRun.wasStopped else { return }
+ guard extensions.extensions == nil, !extensions.scanRun.isWaitedFor, !extensions.scanRun.wasStopped else {
+ return
+ }
await extensions.refresh()
}
.rescanOnExclusionChange("ExtensionList") { await extensions.refresh() }
diff --git a/Peel/Views/Homebrew/HomebrewList.swift b/Peel/Views/Homebrew/HomebrewList.swift
index e966a81d..81f0e0ed 100644
--- a/Peel/Views/Homebrew/HomebrewList.swift
+++ b/Peel/Views/Homebrew/HomebrewList.swift
@@ -126,7 +126,7 @@ struct HomebrewList: View {
.task {
// Only this page shows what Clean Up would free, so the figure is asked for here, and only while
// it's unknown: `brew cleanup --dry-run` has to walk the cache and the Cellar to work it out.
- guard !homebrew.isScanning, !homebrew.scanRun.wasStopped,
+ guard !homebrew.scanRun.isWaitedFor, !homebrew.scanRun.wasStopped,
homebrew.packages == nil || homebrew.reclaimable == nil
else { return }
await homebrew.refresh(includingReclaimable: true)
diff --git a/Peel/Views/Installers/InstallerList.swift b/Peel/Views/Installers/InstallerList.swift
index 69fc81cd..757d9103 100644
--- a/Peel/Views/Installers/InstallerList.swift
+++ b/Peel/Views/Installers/InstallerList.swift
@@ -89,7 +89,8 @@ struct InstallerList: View {
}
}
.task(id: library.hasLoaded) {
- guard library.hasLoaded, installers.scan == nil, !installers.isScanning, !installers.scanRun.wasStopped
+ guard library.hasLoaded, installers.scan == nil, !installers.scanRun.isWaitedFor,
+ !installers.scanRun.wasStopped
else { return }
await installers.refresh(installedApps: library.apps)
}
diff --git a/Peel/Views/Packages/PackageList.swift b/Peel/Views/Packages/PackageList.swift
index 7a8d5e2c..9b01d2bb 100644
--- a/Peel/Views/Packages/PackageList.swift
+++ b/Peel/Views/Packages/PackageList.swift
@@ -79,7 +79,7 @@ struct PackageList: View {
await packages.refreshIfReceiptsChanged()
return
}
- guard !packages.isScanning, !packages.scanRun.wasStopped else { return }
+ guard !packages.scanRun.isWaitedFor, !packages.scanRun.wasStopped else { return }
await packages.refresh()
}
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
diff --git a/Peel/Views/Plugins/PluginList.swift b/Peel/Views/Plugins/PluginList.swift
index 238ef092..83cd362d 100644
--- a/Peel/Views/Plugins/PluginList.swift
+++ b/Peel/Views/Plugins/PluginList.swift
@@ -99,7 +99,7 @@ struct PluginList: View {
}
}
.task {
- guard plugins.plugins == nil, !plugins.isScanning, !plugins.scanRun.wasStopped else { return }
+ guard plugins.plugins == nil, !plugins.scanRun.isWaitedFor, !plugins.scanRun.wasStopped else { return }
await plugins.refresh()
}
.rescanOnExclusionChange("PluginList") { await plugins.refresh() }
diff --git a/Peel/Views/Space/SpaceList.swift b/Peel/Views/Space/SpaceList.swift
index 8a42b78f..6029b2a8 100644
--- a/Peel/Views/Space/SpaceList.swift
+++ b/Peel/Views/Space/SpaceList.swift
@@ -96,7 +96,7 @@ struct SpaceList: View {
}
.task {
if space.report != nil { await space.readDisk() }
- guard space.report == nil, !space.isScanning, !space.scanRun.wasStopped else { return }
+ guard space.report == nil, !space.scanRun.isWaitedFor, !space.scanRun.wasStopped else { return }
await space.refresh()
}
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in