Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
Read Background Items again when it is shown and when Peel comes forward
Țugui Dragoșf18fae545f38
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/ScanRun.swift b/Packages/PeelCore/Sources/PeelCore/Support/ScanRun.swiftindex f6fb46ba..49a14112 100644--- a/Packages/PeelCore/Sources/PeelCore/Support/ScanRun.swift+++ b/Packages/PeelCore/Sources/PeelCore/Support/ScanRun.swift@@ -62,6 +62,21 @@ public final class ScanRun {return task.isCancelled ? nil : answer}+ /// Runs `work` as a read the page doesn't show, as when it reads itself again on its own: nothing turns busy, so+ /// nothing is announced. Nil, without running, while a scan runs, and when a scan starts before it ends.+ public func runQuietly<Answer: Sendable>(_ work: @escaping @MainActor () async -> Answer) async -> Answer? {+ guard !isRunning else { return nil }+ stopNewest?()+ newest += 1+ let asked = newest+ let task = Task { await work() }+ stopNewest = { task.cancel() }+ let answer = await withTaskCancellationHandler { await task.value } onCancel: { task.cancel() }+ guard asked == newest else { return nil }+ stopNewest = nil+ return task.isCancelled ? nil : answer+ }+/// Stops the running scan. `isRunning` turns false at once, so the Rescan button comes back; the scan/// itself ends a moment later, and what it found is dropped.public func stop() {diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ScanRunTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ScanRunTests.swiftindex b2bc91d2..4912d34e 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/ScanRunTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/ScanRunTests.swift@@ -136,6 +136,43 @@ struct ScanRunTests {_ = await asking.value}+ @Test func aQuietReadShowsNothing() async {+ let scan = ScanRun()++ let wasRunning = await scan.runQuietly { scan.isRunning }++ #expect(wasRunning == false, "nothing turns busy, so the page announces nothing and keeps its Rescan button")+ #expect(!scan.isRunning && !scan.wasStopped)+ }++ @Test func aQuietReadNeverRunsOverAScan() async {+ let scan = ScanRun()+ let waiting = Waiting()+ let asking = Task { await scan.run { await waiting.run(answering: 1) } }+ await waiting.untilStarted()+ let quiet = Waiting()++ let answer = await scan.runQuietly { await quiet.run(answering: 2) }++ #expect(answer == nil)+ #expect(!quiet.started)+ #expect(scan.isRunning && !waiting.stopped, "the scan goes on")+ scan.stop()+ _ = await asking.value+ }++ @Test func aScanOvertakesAQuietRead() async {+ let scan = ScanRun()+ let quiet = Waiting()+ let asking = Task { await scan.runQuietly { await quiet.run(answering: 1) } }+ await quiet.untilStarted()++ #expect(await scan.run { 2 } == 2)+ #expect(await asking.value == nil)+ #expect(quiet.stopped)+ #expect(!scan.isRunning && !scan.wasStopped)+ }+@Test func aScanAfterAStopIsNoLongerStopped() async {let scan = ScanRun()let waiting = Waiting()diff --git a/Peel/Model/BackgroundItemLibrary.swift b/Peel/Model/BackgroundItemLibrary.swiftindex 8c747805..ba7a2494 100644--- a/Peel/Model/BackgroundItemLibrary.swift+++ b/Peel/Model/BackgroundItemLibrary.swift@@ -26,14 +26,39 @@ final class BackgroundItemLibrary {/// The installed apps passed to the last scan, reused by the rescan after an action. Without them no item/// could be traced to the app that owns it.private var installedApps: [InstalledApp] = []+ private let scan: @MainActor ([InstalledApp], Exclusions) async -> BackgroundItems.Scan++ init(+ scan: @escaping @MainActor ([InstalledApp], Exclusions) async -> BackgroundItems.Scan = {+ await BackgroundItems.scan(installedApps: $0, exclusions: $1)+ }+ ) {+ self.scan = scan+ }func refresh(installedApps: [InstalledApp]) async {self.installedApps = installedApps+ let scan = scan+ guard+ let result = await scanRun.run({ await scan(installedApps, ExclusionsStore.shared.exclusions) })+ else { return }+ show(result)+ }++ /// Reads the list again without showing a scan, once it has been read: System Settings and other apps change what+ /// launchd runs. Never while an action runs, since the scan after it decides what the list says.+ func readAgain(installedApps: [InstalledApp]) async {+ guard items != nil, runningActionItemIDs.isEmpty else { return }+ self.installedApps = installedApps+ let scan = scanguard- let result = await scanRun.run({- await BackgroundItems.scan(installedApps: installedApps, exclusions: ExclusionsStore.shared.exclusions)- })+ let result = await scanRun.runQuietly({ await scan(installedApps, ExclusionsStore.shared.exclusions) }),+ runningActionItemIDs.isEmptyelse { return }+ show(result)+ }++ private func show(_ result: BackgroundItems.Scan) {items = result.itemsunanswered = result.unansweredscans += 1diff --git a/Peel/Views/BackgroundItems/BackgroundItemList.swift b/Peel/Views/BackgroundItems/BackgroundItemList.swiftindex 88ba8920..10132edb 100644--- a/Peel/Views/BackgroundItems/BackgroundItemList.swift+++ b/Peel/Views/BackgroundItems/BackgroundItemList.swift@@ -60,12 +60,20 @@ struct BackgroundItemList: View {}}// The first scan waits for the installed apps, which it needs to trace each item to the app that owns it.+ // After it, the list is read again each time it is shown and each time Peel comes forward..task(id: library.hasLoaded) {- guard library.hasLoaded, backgroundItems.items == nil, !backgroundItems.scanRun.isWaitedFor,- !backgroundItems.scanRun.wasStopped- else { return }+ guard library.hasLoaded else { return }+ guard backgroundItems.items == nil else {+ await backgroundItems.readAgain(installedApps: library.apps)+ return+ }+ guard !backgroundItems.scanRun.isWaitedFor, !backgroundItems.scanRun.wasStopped else { return }await backgroundItems.refresh(installedApps: library.apps)}+ .onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in+ guard library.hasLoaded else { return }+ Task { await backgroundItems.readAgain(installedApps: library.apps) }+ }.rescanOnExclusionChange("BackgroundItemList") { await backgroundItems.refresh(installedApps: library.apps) }.rescanAfterPutBack(.backgroundItems) { await backgroundItems.refresh(installedApps: library.apps) }// Shown from the list, which stays on screen. An alert on the item's page would vanish with the page ifdiff --git a/PeelTests/BackgroundItemLibraryTests.swift b/PeelTests/BackgroundItemLibraryTests.swiftindex 619921a0..5581e6ba 100644--- a/PeelTests/BackgroundItemLibraryTests.swift+++ b/PeelTests/BackgroundItemLibraryTests.swift@@ -3,15 +3,44 @@ import Foundation@testable import PeelCoreimport Testing+@MainActorstruct BackgroundItemLibraryTests {- @Test func anEnabledJobThatLeftTheListSaysWhenItComesBack() {- let job = BackgroundItem(- label: "org.example.agent", kind: .agent, source: .app, plistURL: nil, program: nil, runsAtLoad: nil,- keepsAlive: nil, ownerBundleIdentifier: "org.example.app", ownerName: "App",- ownerURL: URL(filePath: "/Applications/App.app", directoryHint: .isDirectory), isOrphan: false,- state: .notLoaded, isDisabled: true- )+ private let job = BackgroundItem(+ label: "org.example.agent", kind: .agent, source: .app, plistURL: nil, program: nil, runsAtLoad: nil,+ keepsAlive: nil, ownerBundleIdentifier: "org.example.app", ownerName: "App",+ ownerURL: URL(filePath: "/Applications/App.app", directoryHint: .isDirectory), isOrphan: false,+ state: .notLoaded, isDisabled: true+ )++ private final class Reads {+ var count = 0+ var library: BackgroundItemLibrary?+ var wereShownAsAScan: [Bool] = []+ }++ @Test func readsTheListAgainWithoutShowingAScan() async {+ let reads = Reads()+ let job = job+ let library = BackgroundItemLibrary { _, _ in+ reads.count += 1+ reads.wereShownAsAScan.append(reads.library?.isScanning == true)+ return BackgroundItems.Scan(items: reads.count > 1 ? [job] : [])+ }+ reads.library = library+ await library.readAgain(installedApps: [])+ #expect(reads.count == 0, "nothing is read again before the list was read once")++ await library.refresh(installedApps: [])+ let scans = library.scans+ await library.readAgain(installedApps: [])++ #expect(library.items?.map(\.label) == [job.label])+ #expect(library.scans == scans + 1, "the item's page asks macOS again")+ #expect(reads.wereShownAsAScan == [true, false], "a read again is announced to nobody")+ }++ @Test func anEnabledJobThatLeftTheListSaysWhenItComesBack() {let left = BackgroundItemLibrary.alertAfterEnabling(job, listed: [])guard case .enabledUntilLoaded(let label, let kind)? = left else {Issue.record("no alert for a job that left the list")