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.

Move a row to Updates Available once a round of checks ends

Țugui Dragoș29b884849c1b
diff --git a/Peel/Model/AppLibrary.swift b/Peel/Model/AppLibrary.swift
index 977d8b13..d6947811 100644
--- a/Peel/Model/AppLibrary.swift
+++ b/Peel/Model/AppLibrary.swift
@@ -94,8 +94,10 @@ final class AppLibrary {
}
var appsCheckingForUpdates: Set<InstalledApp.ID> { updateRounds.checking }
/// The apps with an update when the current round of checks began. The menu bar shows them until the round
- /// ends, rather than a count that climbs as answers arrive seconds apart.
- private var heldUpdates: Set<InstalledApp.ID>?
+ /// ends, rather than a count that climbs as answers arrive seconds apart, and the list draws only them first.
+ private var heldUpdates: Set<InstalledApp.ID>? {
+ didSet { updatesRevision += 1 }
+ }
private(set) var sizes: [InstalledApp.ID: Int64] = [:] {
didSet { sizesRevision += 1 }
}
@@ -501,7 +503,8 @@ final class AppLibrary {
var waiting: [InstalledApp] = []
var rest: [InstalledApp] = []
for app in sorted(result) {
- if hasUpdate(app) {
+ // An update found while a round runs moves its row up once the round ends, not under the pointer.
+ if hasUpdate(app), heldUpdates?.contains(app.id) ?? true {
waiting.append(app)
} else {
rest.append(app)
diff --git a/PeelTests/AppLibraryTests.swift b/PeelTests/AppLibraryTests.swift
index ae1165c6..859f8863 100644
--- a/PeelTests/AppLibraryTests.swift
+++ b/PeelTests/AppLibraryTests.swift
@@ -1,4 +1,5 @@
import Foundation
+import Observation
@testable import Peel
import PeelCore
import Synchronization
@@ -89,6 +90,52 @@ private nonisolated final class NotesProtocol: URLProtocol, @unchecked Sendable
}
}
+private nonisolated final class TwoFeedsProtocol: URLProtocol, @unchecked Sendable {
+ static let arrived = Mutex<AsyncStream<Void>.Continuation?>(nil)
+ /// A request to the held feed, with the thread it started on, where its answer has to be given.
+ private struct Held: @unchecked Sendable {
+ let load: TwoFeedsProtocol
+ let thread: Thread
+ }
+
+ private static let held = Mutex<[Held]>([])
+
+ override class func canInit(with request: URLRequest) -> Bool { true }
+ override class func canonicalRequest(for request: URLRequest) -> URLRequest { request }
+ override func stopLoading() {}
+
+ override func startLoading() {
+ guard request.url?.host() == "held.example.com" else { return answer() }
+ let request = Held(load: self, thread: .current)
+ Self.held.withLock { $0.append(request) }
+ _ = Self.arrived.withLock { $0?.yield() }
+ }
+
+ static func answerHeld() {
+ let loads = held.withLock { loads in
+ defer { loads = [] }
+ return loads
+ }
+ for request in loads {
+ request.load.perform(#selector(answer), on: request.thread, with: nil, waitUntilDone: false)
+ }
+ }
+
+ @objc private func answer() {
+ guard let url = request.url, let host = url.host(),
+ let response = HTTPURLResponse(url: url, statusCode: 200, httpVersion: "HTTP/1.1", headerFields: nil)
+ else { return }
+ let (build, version) = host == "held.example.com" ? ("100", "1.0") : ("101", "1.1")
+ client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
+ client?.urlProtocol(self, didLoad: Data("""
+ <rss xmlns:sparkle="http://www.andymatuschak.org/xml-namespaces/sparkle"><channel><item>\
+ <enclosure url="https://\(host)/u.zip" sparkle:version="\(build)" \
+ sparkle:shortVersionString="\(version)" /></item></channel></rss>
+ """.utf8))
+ client?.urlProtocolDidFinishLoading(self)
+ }
+}
+
private nonisolated final class TestClock: Sendable {
private let moment: Mutex<Date>
@@ -551,6 +598,31 @@ private nonisolated final class TestClock: Sendable {
#expect(library.releaseNotes.asks("org.example.fed", version: "1.1"))
}
+ @Test func theListMovesAnUpdateFoundDuringARoundOnceTheRoundEnds() async throws {
+ let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
+ let quick = try caskApp("Quick", in: folder, feed: "https://quick.example.com/appcast.xml")
+ let held = try caskApp("Held", in: folder, feed: "https://held.example.com/appcast.xml")
+ defer { try? FileManager.default.removeItem(at: folder) }
+ let made = try MadeUpDefaults()
+ defer { made.forget() }
+ let (arrivals, arrival) = AsyncStream.makeStream(of: Void.self)
+ TwoFeedsProtocol.arrived.withLock { $0 = arrival }
+ defer { TwoFeedsProtocol.arrived.withLock { $0 = nil } }
+ let library = library(with: made.defaults, notesIn: folder, answeredBy: TwoFeedsProtocol.self)
+ #expect(await library.reveal([quick, held]))
+ let apps = library.apps.filter { ["org.example.quick", "org.example.held"].contains($0.bundleIdentifier) }
+ let found = try #require(apps.first { $0.bundleIdentifier == "org.example.quick" })
+
+ let round = Task { await library.checkForUpdates(apps, force: true) }
+ for await _ in arrivals { break }
+ for await hasIt in Observations({ library.hasUpdate(found) }) where hasIt { break }
+ #expect(library.visibleApps(matching: "").waiting.isEmpty, "rows stay put while the round runs")
+
+ TwoFeedsProtocol.answerHeld()
+ await round.value
+ #expect(library.visibleApps(matching: "").waiting.map(\.id) == [found.id])
+ }
+
@Test func anAppFromACaskThatInstallsTheNewestCanNotBeChecked() async throws {
let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
let bundle = try caskApp("Tool", in: folder)