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.

Home reads again when asked while it is reading

Țugui Dragoșb14d196beea1
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/OneRunAtATime.swift b/Packages/PeelCore/Sources/PeelCore/Support/OneRunAtATime.swift
new file mode 100644
index 00000000..0de00796
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Support/OneRunAtATime.swift
@@ -0,0 +1,22 @@
+/// Runs a piece of work one call at a time. A call made while it runs is not dropped, since it may bring news the
+/// running one started too early to see: the work runs once more when it ends, however many calls came meanwhile.
+@MainActor
+public final class OneRunAtATime {
+ private var isRunning = false
+ private var isAskedAgain = false
+
+ public init() {}
+
+ public func run(_ work: () async -> Void) async {
+ guard !isRunning else {
+ isAskedAgain = true
+ return
+ }
+ isRunning = true
+ defer { isRunning = false }
+ repeat {
+ isAskedAgain = false
+ await work()
+ } while isAskedAgain
+ }
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/OneRunAtATimeTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/OneRunAtATimeTests.swift
new file mode 100644
index 00000000..5c1f21be
--- /dev/null
+++ b/Packages/PeelCore/Tests/PeelCoreTests/OneRunAtATimeTests.swift
@@ -0,0 +1,26 @@
+@testable import PeelCore
+import Testing
+
+@MainActor
+struct OneRunAtATimeTests {
+ @Test func runsOnceMoreForTheCallsMadeWhileItRan() async {
+ let runs = OneRunAtATime()
+ var count = 0
+ var release: CheckedContinuation<Void, Never>?
+
+ let first = Task {
+ await runs.run {
+ count += 1
+ if count == 1 { await withCheckedContinuation { release = $0 } }
+ }
+ }
+ while release == nil { await Task.yield() }
+ await runs.run { count += 1 }
+ await runs.run { count += 1 }
+ #expect(count == 1)
+ release?.resume()
+ await first.value
+
+ #expect(count == 2)
+ }
+}
diff --git a/Peel/Model/HomeModel.swift b/Peel/Model/HomeModel.swift
index 6f1759cf..a45460ea 100644
--- a/Peel/Model/HomeModel.swift
+++ b/Peel/Model/HomeModel.swift
@@ -82,7 +82,8 @@ final class HomeModel {
private(set) var isHelperFromAnotherCopy = false
/// The Finder extension is on, but macOS runs the one inside another copy of Peel, so this copy reads it as off.
private(set) var isFinderExtensionFromAnotherCopy = false
- private var isRefreshing = false
+ private let refreshes = OneRunAtATime()
+ private var isReadingModelName = false
private(set) var greeting = Greeting.at(.now)
private(set) var states: [Permission: State] = [:]
private(set) var helperStatus: PrivilegedHelper.Status = .notRegistered
@@ -124,12 +125,16 @@ final class HomeModel {
}
/// Reads again what Home shows: the Mac's details and the state of every permission. One call runs at a
- /// time: the window's first task and the activation that follows both call this, and two overlapping
- /// calls would both find nothing read yet.
+ /// time, since two overlapping calls would both find nothing read yet, and a call made meanwhile, such as the
+ /// one after the helper is installed, runs once more when it ends.
func refresh(helper: HelperModel) async {
- guard !isRefreshing else { return }
- isRefreshing = true
- defer { isRefreshing = false }
+ await refreshes.run { await read(helper: helper) }
+ // The model's marketing name is the only slow part (a `system_profiler` process), so it is read outside
+ // the runs, which it would otherwise hold up.
+ await readModelName()
+ }
+
+ private func read(helper: HelperModel) async {
greeting = .at(.now)
// The card needs none of the checks below, so it is read first and arrives with the window.
let isFirstRead = device == nil
@@ -166,12 +171,8 @@ final class HomeModel {
]
hasChecked = true
- // The model's marketing name is the only slow part (a `system_profiler` process), so the card is drawn
- // without it and the name arrives after. Free space is read every time, since it is how the user sees
- // that a cleanup worked.
- if isFirstRead {
- await readModelName()
- } else if let device {
+ // Free space is read every time, since it is how the user sees that a cleanup worked.
+ if !isFirstRead, let device {
self.device = device.withStorageRead().named(modelName)
}
}
@@ -186,7 +187,10 @@ final class HomeModel {
/// Reads the marketing name with `system_profiler` and saves it, when none is saved for this model yet.
private func readModelName() async {
- guard let identifier = device?.modelIdentifier, !identifier.isEmpty, modelName == nil else { return }
+ guard !isReadingModelName, let identifier = device?.modelIdentifier, !identifier.isEmpty, modelName == nil
+ else { return }
+ isReadingModelName = true
+ defer { isReadingModelName = false }
guard let name = await DeviceInfo.marketingName() else { return }
UserDefaults.standard.set(name, forKey: Self.modelNameKey + identifier)
device = device?.named(name)