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.

Peel keeps where its moves landed only while the Trash is watched

Țugui Dragoș7e80638b608b
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/OwnTrashMoves.swift b/Packages/PeelCore/Sources/PeelCore/Removal/OwnTrashMoves.swift
index e785460b..8a21eff4 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/OwnTrashMoves.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/OwnTrashMoves.swift
@@ -22,9 +22,14 @@ public final class OwnTrashMoves: Sendable {
public init() {}
- /// `action` runs whenever the last move under way ends, so the watch can look at the Trash again.
- public func whenSettled(_ action: @escaping @Sendable () -> Void) {
+ /// `action` runs whenever the last move under way ends, so the watch can look at the Trash again. Where moves
+ /// land is kept only while a watch is set, which it is before its first listing, and nil when the watch stops:
+ /// what landed before a watch began is in that listing already.
+ public func whenSettled(_ action: (@Sendable () -> Void)?) {
settled.withLock { $0 = action }
+ if action == nil {
+ state.withLock { $0.landed = [:] }
+ }
}
func began() {
@@ -32,16 +37,19 @@ public final class OwnTrashMoves: Sendable {
}
func ended(landedAt urls: [URL]) {
+ let action = settled.withLock { $0 }
let isSettled = state.withLock { state in
state.ended += 1
- for url in urls {
- state.landed[PathPattern.comparablePath(of: url)] = state.ended
+ if action != nil {
+ for url in urls {
+ state.landed[PathPattern.comparablePath(of: url)] = state.ended
+ }
}
state.underWay -= 1
return state.underWay == 0
}
if isSettled {
- settled.withLock { $0 }?()
+ action?()
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/OwnTrashMovesTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/OwnTrashMovesTests.swift
index cf502a51..bd92931d 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/OwnTrashMovesTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/OwnTrashMovesTests.swift
@@ -8,10 +8,16 @@ import Testing
struct OwnTrashMovesTests {
private let trash = URL(filePath: "/Users/x/.Trash", directoryHint: .isDirectory)
+ private func watched() -> OwnTrashMoves {
+ let moves = OwnTrashMoves()
+ moves.whenSettled {}
+ return moves
+ }
+
/// The Trash renames an item whose name is taken, so Peel's own move can land as `Foo 2.app` beside the
/// `Foo.app` already there. What counts is where it landed, not the name it had.
@Test func anAppPeelMovedIsNotToldUnderTheNameItLandedWith() {
- let moves = OwnTrashMoves()
+ let moves = watched()
let look = moves.look()
moves.began()
moves.ended(landedAt: [trash.appending(path: "Foo 2.app")])
@@ -49,7 +55,7 @@ struct OwnTrashMovesTests {
/// What Peel landed is forgotten once a look that began after it has run: the item was seen, or it has left
/// the Trash since. The same name moved there later by the user is then told.
@Test func forgetsWhatItLandedOnceALookHasSeenIt() {
- let moves = OwnTrashMoves()
+ let moves = watched()
moves.began()
moves.ended(landedAt: [trash.appending(path: "Foo.app")])
let first = moves.look()
@@ -62,7 +68,7 @@ struct OwnTrashMovesTests {
/// Each Trash is looked at on its own, so a look at one keeps what landed in another until that one is seen.
@Test func aLookAtOneTrashKeepsWhatLandedInAnother() {
- let moves = OwnTrashMoves()
+ let moves = watched()
let volume = URL(filePath: "/Volumes/Disk/.Trashes/501", directoryHint: .isDirectory)
moves.began()
moves.ended(landedAt: [volume.appending(path: "Foo.app")])
@@ -75,7 +81,7 @@ struct OwnTrashMovesTests {
/// A listing that began before Peel's move landed may not show what it landed, so what landed after the
/// look began is kept for the next one.
@Test func keepsWhatLandedAfterTheLookBegan() {
- let moves = OwnTrashMoves()
+ let moves = watched()
let look = moves.look()
moves.began()
moves.ended(landedAt: [trash.appending(path: "Foo.app")])
@@ -86,7 +92,7 @@ struct OwnTrashMovesTests {
/// Every removal goes through `TrashService`, so every tool's moves are Peel's own, whichever page made them.
@Test func everyMoveThroughTheTrashServiceIsPeelsOwn() async {
- let moves = OwnTrashMoves()
+ let moves = watched()
let environment = SearchEnvironment(homeDirectory: URL(filePath: "/Users/x"), rootDirectory: URL(filePath: "/"))
let service = TrashService(environment: environment, ownMoves: moves) { [trash] _ in trash.appending(path: "Install macOS 2.app") }
@@ -94,4 +100,18 @@ struct OwnTrashMovesTests {
#expect(moves.arrivals(["Install macOS 2.app"], known: [], in: trash, since: 0) == [])
}
+
+ @Test func keepsNothingWhileNobodyWatches() {
+ let moves = OwnTrashMoves()
+ moves.began()
+ moves.ended(landedAt: [trash.appending(path: "Foo.app")])
+ moves.whenSettled {}
+ #expect(moves.arrivals(["Foo.app"], known: [], in: trash, since: moves.look()) == ["Foo.app"])
+
+ moves.began()
+ moves.ended(landedAt: [trash.appending(path: "Bar.app")])
+ moves.whenSettled(nil)
+ moves.whenSettled {}
+ #expect(moves.arrivals(["Bar.app"], known: [], in: trash, since: moves.look()) == ["Bar.app"])
+ }
}
diff --git a/Peel/Model/TrashMonitor.swift b/Peel/Model/TrashMonitor.swift
index aa98c993..206cc72d 100644
--- a/Peel/Model/TrashMonitor.swift
+++ b/Peel/Model/TrashMonitor.swift
@@ -25,6 +25,14 @@ final class TrashMonitor {
func start() {
guard home == nil else { return }
+ // The setting is about apps the user moves to the Trash, so Peel's own moves are told apart, from
+ // before the first listing, and every Trash is looked at again once one ends.
+ OwnTrashMoves.shared.whenSettled { [weak self] in
+ Task { @MainActor in
+ self?.home?.changed()
+ self?.volumes.values.forEach { $0.changed() }
+ }
+ }
let trash = URL.homeDirectory.appending(path: ".Trash", directoryHint: .isDirectory)
// Finder makes the home's Trash again only when something is next thrown away, so while it is missing the
// home folder is watched for it.
@@ -34,9 +42,11 @@ final class TrashMonitor {
home = watch
status = .watching
case .refused:
+ OwnTrashMoves.shared.whenSettled(nil)
status = .needsFullDiskAccess
return
case .unavailable:
+ OwnTrashMoves.shared.whenSettled(nil)
status = .off
return
}
@@ -45,18 +55,11 @@ final class TrashMonitor {
self?.stop()
self?.status = result == .refused ? .needsFullDiskAccess : .off
}
- // The setting is about apps the user moves to the Trash, so Peel's own moves are told apart, and every
- // Trash is looked at again once one ends.
- OwnTrashMoves.shared.whenSettled { [weak self] in
- Task { @MainActor in
- self?.home?.changed()
- self?.volumes.values.forEach { $0.changed() }
- }
- }
startVolumes()
}
func stop() {
+ OwnTrashMoves.shared.whenSettled(nil)
home?.stop()
home = nil
volumes.values.forEach { $0.stop() }