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.

Show the helper's answer as soon as Peel has it

Țugui Dragoșeab25ec9b23b
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/OverlappingChecks.swift b/Packages/PeelCore/Sources/PeelCore/Support/OverlappingChecks.swift
new file mode 100644
index 00000000..97eb52a6
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Support/OverlappingChecks.swift
@@ -0,0 +1,31 @@
+/// Which of several overlapping checks of one thing may show its answer: each one as soon as it ends, unless a check
+/// that started after it has shown its own, or the thing changed after it started. Waiting for the newest check alone
+/// shows nothing for as long as each check outlasts the start of the next.
+public struct OverlappingChecks: Sendable {
+ public struct Check: Sendable {
+ fileprivate let number: Int
+ fileprivate let change: Int
+ }
+
+ private var started = 0
+ private var shown = 0
+ private var changes = 0
+
+ public init() {}
+
+ public mutating func start() -> Check {
+ started += 1
+ return Check(number: started, change: changes)
+ }
+
+ /// The thing checked changed, so no check started before now may show its answer.
+ public mutating func changed() {
+ changes += 1
+ }
+
+ public mutating func mayShow(_ check: Check) -> Bool {
+ guard check.change == changes, check.number > shown else { return false }
+ shown = check.number
+ return true
+ }
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/OverlappingChecksTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/OverlappingChecksTests.swift
new file mode 100644
index 00000000..471db852
--- /dev/null
+++ b/Packages/PeelCore/Tests/PeelCoreTests/OverlappingChecksTests.swift
@@ -0,0 +1,59 @@
+@testable import PeelCore
+import Testing
+
+struct OverlappingChecksTests {
+ @Test func anAnswerShowsWhenItsCheckEndsThoughANewerCheckStarted() {
+ var checks = OverlappingChecks()
+ let first = checks.start()
+ _ = checks.start()
+
+ let shows = checks.mayShow(first)
+
+ #expect(shows)
+ }
+
+ @Test func aNewerAnswerReplacesAnOlderOne() {
+ var checks = OverlappingChecks()
+ let first = checks.start()
+ let second = checks.start()
+
+ let shows = [checks.mayShow(first), checks.mayShow(second)]
+
+ #expect(shows == [true, true])
+ }
+
+ @Test func anOlderAnswerNeverReplacesANewerOne() {
+ var checks = OverlappingChecks()
+ let first = checks.start()
+ let second = checks.start()
+
+ let shows = [checks.mayShow(second), checks.mayShow(first)]
+
+ #expect(shows == [true, false])
+ }
+
+ @Test func aChangeLeavesOutWhatChecksStartedBeforeItFound() {
+ var checks = OverlappingChecks()
+ let before = checks.start()
+ checks.changed()
+
+ var shows = [checks.mayShow(before)]
+ let after = checks.start()
+ shows.append(checks.mayShow(after))
+
+ #expect(shows == [false, true])
+ }
+
+ /// Each check outlasts the start of the next, as Home's and Settings' did while the helper didn't answer.
+ @Test func noAnswerWaitsForTheChecksToStopOverlapping() {
+ var checks = OverlappingChecks()
+ var running = [checks.start(), checks.start()]
+ var shows: [Bool] = []
+ for _ in 0..<4 {
+ running.append(checks.start())
+ shows.append(checks.mayShow(running.removeFirst()))
+ }
+
+ #expect(shows == [true, true, true, true])
+ }
+}
diff --git a/Peel/Model/HelperModel.swift b/Peel/Model/HelperModel.swift
index 5cb4223c..d7170c16 100644
--- a/Peel/Model/HelperModel.swift
+++ b/Peel/Model/HelperModel.swift
@@ -42,18 +42,16 @@ final class HelperModel {
status = PrivilegedHelper.status
}
- /// Counts the checks, so when two overlap the later one's answer stands, whichever returns last.
- private var generation = 0
+ private var checks = OverlappingChecks()
func checkConnection() async {
- generation += 1
- let current = generation
+ let check = checks.start()
let status = await PrivilegedHelper.currentStatus()
let isResponding = status == .enabled ? await PrivilegedHelper.isResponding() : nil
let isRegisteredByAnotherCopy = status == .notRegistered
? await PrivilegedHelper.isRegisteredByAnyCopy()
: false
- guard current == generation else { return }
+ guard checks.mayShow(check) else { return }
self.status = status
self.isResponding = isResponding
self.isRegisteredByAnotherCopy = isRegisteredByAnotherCopy
@@ -74,6 +72,7 @@ final class HelperModel {
failure = Failure(action: .install, reason: error.localizedDescription)
}
}
+ checks.changed()
refresh()
if status == .requiresApproval {
PrivilegedHelper.openLoginItemsSettings()
@@ -89,6 +88,7 @@ final class HelperModel {
} catch {
failure = Failure(action: .repair, reason: error.localizedDescription)
}
+ checks.changed()
refresh()
if status == .requiresApproval {
PrivilegedHelper.openLoginItemsSettings()
@@ -104,6 +104,7 @@ final class HelperModel {
} catch {
failure = Failure(action: .uninstall, reason: error.localizedDescription)
}
+ checks.changed()
refresh()
}
}