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.

Leave no old list behind a File Search stopped for new words

Țugui Dragoș00dd79a310b2
diff --git a/Peel/Model/FileSearchLibrary.swift b/Peel/Model/FileSearchLibrary.swift
index 3ebedc63..ab1d163c 100644
--- a/Peel/Model/FileSearchLibrary.swift
+++ b/Peel/Model/FileSearchLibrary.swift
@@ -7,6 +7,8 @@ final class FileSearchLibrary {
var criteria = FileSearchCriteria()
private(set) var results: FileSearchResults?
private(set) var searchedCriteria: FileSearchCriteria?
+ /// The criteria `results` answer.
+ private var answered: FileSearchCriteria?
let scanRun = ScanRun()
/// True while a search runs, and while one is due because the words changed since the last was asked, so the
/// page stays busy between two searches while someone types.
@@ -24,6 +26,15 @@ final class FileSearchLibrary {
private var generation = 0
/// The exclusions the list is filtered by: those the search ran under, or those it was narrowed to since.
private var filteredBy = Exclusions.none
+ private let run: @Sendable (FileSearchCriteria, Exclusions) async -> FileSearchResults
+
+ init(
+ run: @escaping @Sendable (FileSearchCriteria, Exclusions) async -> FileSearchResults = {
+ await FileSearch.run($0, exclusions: $1)
+ }
+ ) {
+ self.run = run
+ }
var chosenFile: FoundFile? {
results?.files.first { $0.url == chosen }
@@ -36,20 +47,21 @@ final class FileSearchLibrary {
searchedCriteria = criteria
guard criteria.isSearchable else {
scanRun.stop()
- results = nil
- selectedURLs = []
- recommendedURLs = []
- sizes = [:]
+ forgetResults()
return
}
let exclusions = ExclusionsStore.shared.exclusions
- guard let found = await scanRun.run({ await FileSearch.run(criteria, exclusions: exclusions) }) else {
+ let run = run
+ guard let found = await scanRun.run({ await run(criteria, exclusions) }) else {
// Canceled with the task that asked for it, as when the page goes away, the search answered nothing,
// so the page asks again when it comes back. Stopped or replaced by a newer search, it counts as asked.
if !scanRun.wasStopped, current == generation { searchedCriteria = nil }
+ // Stopped, it leaves no list that answers other words than those now asked.
+ if scanRun.wasStopped, current == generation, criteria != answered { forgetResults() }
return
}
results = found
+ answered = criteria
filteredBy = exclusions
recommendedURLs = Set(found.files.filter { !$0.requiresPrivileges && !$0.belongsToAnApp }.map(\.url))
sizes = Dictionary(found.files.map { ($0.url, $0.size) }, uniquingKeysWith: { first, _ in first })
@@ -58,6 +70,14 @@ final class FileSearchLibrary {
await leaveOut(ExclusionsStore.shared.exclusions)
}
+ private func forgetResults() {
+ results = nil
+ answered = nil
+ selectedURLs = []
+ recommendedURLs = []
+ sizes = [:]
+ }
+
/// Narrows the list to what `exclusions` leave. File Search runs only when asked, so without this a file
/// excluded from its own row would stay listed and selected until the move refused it.
func leaveOut(_ exclusions: Exclusions) async {
diff --git a/PeelTests/FileSearchLibraryTests.swift b/PeelTests/FileSearchLibraryTests.swift
new file mode 100644
index 00000000..05647f6e
--- /dev/null
+++ b/PeelTests/FileSearchLibraryTests.swift
@@ -0,0 +1,61 @@
+import Foundation
+import os
+@testable import Peel
+@testable import PeelCore
+import Testing
+
+@MainActor
+struct FileSearchLibraryTests {
+ private let invoice = URL(filePath: "/org.example/invoice.pdf")
+
+ /// A library whose first search finds `invoice` and whose later ones never answer until they are stopped, with
+ /// a stream that says when each search started.
+ private func library() -> (FileSearchLibrary, AsyncStream<Int>.Iterator) {
+ let (started, starting) = AsyncStream<Int>.makeStream()
+ let calls = OSAllocatedUnfairLock(initialState: 0)
+ let found = FileSearchResults(files: [
+ FoundFile(url: invoice, size: 4_096, modificationDate: .distantPast, requiresPrivileges: false)
+ ])
+ let library = FileSearchLibrary { _, _ in
+ let call = calls.withLock { $0 += 1; return $0 }
+ starting.yield(call)
+ if call == 1 { return found }
+ try? await Task.sleep(for: .seconds(3_600))
+ return FileSearchResults()
+ }
+ return (library, started.makeAsyncIterator())
+ }
+
+ @Test func aSearchStoppedForOtherWordsLeavesNothingOfTheOldOnes() async {
+ var (library, started) = library()
+ library.criteria.name = "invoice"
+ await library.search()
+ library.selectedURLs = [invoice]
+
+ library.criteria.name = "budget"
+ let searching = Task { await library.search() }
+ _ = await started.next(isolation: #isolation)
+ _ = await started.next(isolation: #isolation)
+ library.scanRun.stop()
+ await searching.value
+
+ #expect(library.results == nil)
+ #expect(library.selectedURLs.isEmpty)
+ }
+
+ @Test func aRescanStoppedForTheSameWordsKeepsTheList() async {
+ var (library, started) = library()
+ library.criteria.name = "invoice"
+ await library.search()
+ library.selectedURLs = [invoice]
+
+ let searching = Task { await library.search() }
+ _ = await started.next(isolation: #isolation)
+ _ = await started.next(isolation: #isolation)
+ library.scanRun.stop()
+ await searching.value
+
+ #expect(library.results?.files.map(\.url) == [invoice])
+ #expect(library.selectedURLs == [invoice])
+ }
+}