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.

Changing the update source stops the round still running

Țugui Dragoș206b82116a0a
diff --git a/Packages/PeelCore/Sources/PeelCore/Updates/UpdateRounds.swift b/Packages/PeelCore/Sources/PeelCore/Updates/UpdateRounds.swift
new file mode 100644
index 00000000..c469bd14
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Updates/UpdateRounds.swift
@@ -0,0 +1,54 @@
+/// The rounds of update checks under way, which apps they are asking about, and whether their answers still count.
+///
+/// Rounds can overlap: a round started for a bundle that changed can ask about an app another round is still
+/// waiting on, so an app is being checked until every round asking about it has its answer or has ended. A round
+/// started before the update source last changed asked under the old source, and none of its answers counts.
+public struct UpdateRounds<ID: Hashable & Sendable>: Sendable {
+ public struct Round: Hashable, Sendable {
+ let number: Int
+ let source: Int
+ }
+
+ private var started = 0
+ private var source = 0
+ private var waiting: [Round: Set<ID>] = [:]
+ /// The apps at least one round is still waiting on.
+ public private(set) var checking: Set<ID> = []
+
+ public init() {}
+
+ public mutating func begin(_ ids: some Sequence<ID>) -> Round {
+ started += 1
+ let round = Round(number: started, source: source)
+ waiting[round] = Set(ids)
+ checking.formUnion(ids)
+ return round
+ }
+
+ /// Marks `id` answered in `round`, and tells whether the answer counts.
+ public mutating func answered(_ id: ID, in round: Round) -> Bool {
+ waiting[round]?.remove(id)
+ settle([id])
+ return isCurrent(round)
+ }
+
+ /// Whether `round` asked under the update source in use.
+ public func isCurrent(_ round: Round) -> Bool {
+ round.source == source
+ }
+
+ /// Ends `round`, answered or not.
+ public mutating func end(_ round: Round) {
+ settle(waiting.removeValue(forKey: round) ?? [])
+ }
+
+ public mutating func sourceChanged() {
+ source += 1
+ }
+
+ private mutating func settle(_ ids: Set<ID>) {
+ for id in ids where !waiting.values.contains(where: { $0.contains(id) }) {
+ checking.remove(id)
+ }
+ }
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/UpdateRoundsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/UpdateRoundsTests.swift
new file mode 100644
index 00000000..eb789e55
--- /dev/null
+++ b/Packages/PeelCore/Tests/PeelCoreTests/UpdateRoundsTests.swift
@@ -0,0 +1,38 @@
+@testable import PeelCore
+import Testing
+
+struct UpdateRoundsTests {
+ /// A round started under the old update source keeps running for a while after the source changes. Its
+ /// answers would overwrite what the new round learns, so none of them counts once the source has changed.
+ @Test func aRoundFromBeforeTheSourceChangedCountsNothing() {
+ var rounds = UpdateRounds<String>()
+ let old = rounds.begin(["editor", "notes"])
+ let before = rounds.answered("editor", in: old)
+ #expect(before)
+
+ rounds.sourceChanged()
+ let new = rounds.begin(["editor", "notes"])
+ let late = rounds.answered("notes", in: old)
+ let current = rounds.answered("notes", in: new)
+ #expect(!late)
+ #expect(current)
+ #expect(!rounds.isCurrent(old))
+ #expect(rounds.isCurrent(new))
+ }
+
+ /// Two rounds can ask about the same app, as when a round starts while another still runs. The app is being
+ /// checked until the last of them has its answer or ends.
+ @Test func anAppIsCheckedUntilEveryRoundAskingAboutItIsDone() {
+ var rounds = UpdateRounds<String>()
+ let first = rounds.begin(["editor", "notes"])
+ let second = rounds.begin(["editor"])
+ #expect(rounds.checking == ["editor", "notes"])
+
+ _ = rounds.answered("editor", in: first)
+ #expect(rounds.checking == ["editor", "notes"])
+ rounds.end(first)
+ #expect(rounds.checking == ["editor"])
+ rounds.end(second)
+ #expect(rounds.checking.isEmpty)
+ }
+}
diff --git a/Peel/Model/AppLibrary.swift b/Peel/Model/AppLibrary.swift
index 7f523f5a..6e6c4359 100644
--- a/Peel/Model/AppLibrary.swift
+++ b/Peel/Model/AppLibrary.swift
@@ -81,15 +81,16 @@ final class AppLibrary {
/// Incremented by anything that changes which apps have an update waiting, so the list redraws.
private(set) var updatesRevision = 0
- private(set) var appsCheckingForUpdates: Set<InstalledApp.ID> = [] {
+ private var updateRounds = UpdateRounds<InstalledApp.ID>() {
didSet {
- if appsCheckingForUpdates.isEmpty {
+ if updateRounds.checking.isEmpty {
heldUpdateCount = nil
- } else if oldValue.isEmpty {
+ } else if oldValue.checking.isEmpty {
heldUpdateCount = appsWithUpdates.count
}
}
}
+ var appsCheckingForUpdates: Set<InstalledApp.ID> { updateRounds.checking }
/// The update count when the current round of checks began. The menu bar shows it until the round ends,
/// rather than a count that climbs as answers arrive seconds apart.
private var heldUpdateCount: Int?
@@ -435,9 +436,9 @@ final class AppLibrary {
.map { app in apps.first { $0.id == app.id } ?? app }
guard !wanted.isEmpty else { return }
let asked = Dictionary(wanted.map { ($0.id, $0) }, uniquingKeysWith: { first, _ in first })
- appsCheckingForUpdates.formUnion(wanted.map(\.id))
- // However the round ends, no app is left marked as being checked.
- defer { appsCheckingForUpdates.subtract(wanted.map(\.id)) }
+ let round = updateRounds.begin(wanted.map(\.id))
+ // However the round ends, no app is left marked as being checked by it.
+ defer { updateRounds.end(round) }
let checker = updateChecker
let preference = updateSource
let casks = casks
@@ -450,7 +451,11 @@ final class AppLibrary {
}
while case let (id, answer)? = await group.next() {
let status = answer.status
- appsCheckingForUpdates.remove(id)
+ // The update source changed since this round began, and the round that change started answers now.
+ guard updateRounds.answered(id, in: round) else {
+ group.cancelAll()
+ continue
+ }
// In a canceled round every pending request ends as a failure. Recording those would erase
// what was known and put off the next check for hours.
guard !Task.isCancelled else { continue }
@@ -474,7 +479,7 @@ final class AppLibrary {
}
}
}
- guard !Task.isCancelled else { return }
+ guard !Task.isCancelled, updateRounds.isCurrent(round) else { return }
UpdateMemoryStore.save(memory)
}
@@ -530,6 +535,7 @@ final class AppLibrary {
/// Forgets every update answer and schedule, then checks every app again. The answers shown were found
/// under the old source, and the badge would explain them in terms of the new one.
func updateSourceChanged() async {
+ updateRounds.sourceChanged()
updateStatuses = [:]
lastUpdateChecks = [:]
memory = [:]