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
Tell an enabled job left the list whichever read finds it gone
Țugui Dragoș45fa4d9521cc
diff --git a/Peel/Model/BackgroundItemLibrary.swift b/Peel/Model/BackgroundItemLibrary.swiftindex ba7a2494..9360aad7 100644--- a/Peel/Model/BackgroundItemLibrary.swift+++ b/Peel/Model/BackgroundItemLibrary.swift@@ -27,6 +27,9 @@ final class BackgroundItemLibrary {/// could be traced to the app that owns it.private var installedApps: [InstalledApp] = []private let scan: @MainActor ([InstalledApp], Exclusions) async -> BackgroundItems.Scan+ /// The reads of the list started so far, so that a job just enabled is judged by a read started after it.+ private var readsStarted = 0+ private var enabledJobs: [(job: BackgroundItem, readsBefore: Int)] = []init(scan: @escaping @MainActor ([InstalledApp], Exclusions) async -> BackgroundItems.Scan = {@@ -38,33 +41,50 @@ final class BackgroundItemLibrary {func refresh(installedApps: [InstalledApp]) async {self.installedApps = installedApps+ readsStarted += 1+ let read = readsStartedlet scan = scanguardlet result = await scanRun.run({ await scan(installedApps, ExclusionsStore.shared.exclusions) })else { return }- show(result)+ show(result, read: read)}/// 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.+ /// launchd runs. Never while an action runs, whose own scan follows it.func readAgain(installedApps: [InstalledApp]) async {guard items != nil, runningActionItemIDs.isEmpty else { return }self.installedApps = installedApps+ readsStarted += 1+ let read = readsStartedlet scan = scanguardlet result = await scanRun.runQuietly({ await scan(installedApps, ExclusionsStore.shared.exclusions) }),runningActionItemIDs.isEmptyelse { return }- show(result)+ show(result, read: read)}- private func show(_ result: BackgroundItems.Scan) {+ /// Notes a job just enabled. The first read of the list started after now says whether it left the list, whoever+ /// asked for that read: the action's own scan may be overtaken by another.+ func noteEnabled(_ item: BackgroundItem) {+ enabledJobs.append((item, readsStarted))+ }++ private func show(_ result: BackgroundItems.Scan, read: Int) {items = result.itemsunanswered = result.unansweredscans += 1if let selection, items?.contains(where: { $0.id == selection }) != true {self.selection = nil}+ let judged = enabledJobs.filter { $0.readsBefore < read }.map(\.job)+ enabledJobs.removeAll { $0.readsBefore < read }+ for job in judged {+ if let alert = Self.alertAfterEnabling(job, listed: items) {+ alerts.add(alert)+ }+ }}enum Action {@@ -106,14 +126,13 @@ final class BackgroundItemLibrary {) async {runningActionItemIDs.insert(item.id)defer { runningActionItemIDs.remove(item.id) }- var isEnabled = falsedo {switch action {case .start: try await BackgroundItemActions.start(item)case .stop: try await BackgroundItemActions.stop(item)case .enable:try await BackgroundItemActions.setEnabled(true, for: item)- isEnabled = true+ noteEnabled(item)case .disable: try await BackgroundItemActions.setEnabled(false, for: item)case .moveToTrash:let exclusions = ExclusionsStore.shared.exclusions@@ -136,9 +155,6 @@ final class BackgroundItemLibrary {alerts.add(.failed(ActionFailure(label: item.label, action: action, reason: error)))}await refresh(installedApps: installedApps)- if isEnabled, let alert = Self.alertAfterEnabling(item, listed: items) {- alerts.add(alert)- }}}diff --git a/PeelTests/BackgroundItemLibraryTests.swift b/PeelTests/BackgroundItemLibraryTests.swiftindex 5581e6ba..8dbdd5b2 100644--- a/PeelTests/BackgroundItemLibraryTests.swift+++ b/PeelTests/BackgroundItemLibraryTests.swift@@ -40,6 +40,75 @@ struct BackgroundItemLibraryTests {#expect(reads.wereShownAsAScan == [true, false], "a read again is announced to nobody")}+ /// A scan whose answers the test gives, call by call, and which holds the calls in `held` until it lets them go.+ private final class Script {+ var calls = 0+ var answers: [Int: [BackgroundItem]] = [:]+ var held: Set<Int> = []+ private var waiting: [CheckedContinuation<Void, Never>] = []+ let (started, starting) = AsyncStream<Int>.makeStream()++ func scan() async -> BackgroundItems.Scan {+ calls += 1+ let call = calls+ starting.yield(call)+ if held.contains(call) {+ await withCheckedContinuation { waiting.append($0) }+ }+ return BackgroundItems.Scan(items: answers[call] ?? [])+ }++ func release() {+ waiting.forEach { $0.resume() }+ waiting = []+ }+ }++ @Test func anEnabledJobThatLeftIsToldWhenAnotherReadOvertakesTheActionsOwn() async {+ let script = Script()+ script.answers = [1: [job]]+ script.held = [2]+ let library = BackgroundItemLibrary { _, _ in await script.scan() }+ var started = script.started.makeAsyncIterator()+ await library.refresh(installedApps: [])+ _ = await started.next(isolation: #isolation)++ library.noteEnabled(job)+ let actionsOwn = Task { await library.refresh(installedApps: []) }+ _ = await started.next(isolation: #isolation)+ await library.refresh(installedApps: [])+ script.release()+ await actionsOwn.value++ guard case .enabledUntilLoaded(let label, _)? = library.alerts.current else {+ Issue.record("the job left the list without a word")+ return+ }+ #expect(label == job.label)+ }++ @Test func aReadStartedBeforeTheEnableDoesNotJudgeIt() async {+ let script = Script()+ script.answers = [1: [job]]+ script.held = [2]+ let library = BackgroundItemLibrary { _, _ in await script.scan() }+ var started = script.started.makeAsyncIterator()+ await library.refresh(installedApps: [])+ _ = await started.next(isolation: #isolation)++ let earlier = Task { await library.refresh(installedApps: []) }+ _ = await started.next(isolation: #isolation)+ library.noteEnabled(job)+ script.release()+ await earlier.value+ let afterTheEarlierRead = library.alerts.current++ await library.refresh(installedApps: [])++ #expect(afterTheEarlierRead == nil, "a list read before the enable says nothing about it")+ #expect(library.alerts.current != nil)+ }+@Test func anEnabledJobThatLeftTheListSaysWhenItComesBack() {let left = BackgroundItemLibrary.alertAfterEnabling(job, listed: [])guard case .enabledUntilLoaded(let label, let kind)? = left else {