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.

Make a tweak's switch change its setting one step at a time

Țugui Dragoșcd71a1a12f46
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/OneAtATime.swift b/Packages/PeelCore/Sources/PeelCore/Support/OneAtATime.swift
new file mode 100644
index 00000000..ddd52568
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Support/OneAtATime.swift
@@ -0,0 +1,35 @@
+/// One piece of work at a time for one thing, such as a setting. A request made while work runs waits, and a newer
+/// one takes its place, so only the latest runs next. The work learns whether another request waits, so it can leave
+/// what only the last needs, such as restarting the Dock, to that one.
+@MainActor
+public final class OneAtATime<Value> {
+ public typealias Work = @MainActor (Value, _ anotherWaits: () -> Bool) async -> Void
+
+ private var isRunning = false
+ private var waiting: (value: Value, work: Work)?
+ private var askers: [CheckedContinuation<Void, Never>] = []
+
+ public init() {}
+
+ /// Returns once `value` has run, or a newer request took its place and ran.
+ public func ask(_ value: Value, work: @escaping Work) async {
+ guard !isRunning else {
+ await withCheckedContinuation { asker in
+ waiting = (value, work)
+ askers.append(asker)
+ }
+ return
+ }
+ isRunning = true
+ var next: (value: Value, work: Work)? = (value, work)
+ while let current = next {
+ await current.work(current.value) { self.waiting != nil }
+ next = waiting
+ waiting = nil
+ }
+ isRunning = false
+ let done = askers
+ askers = []
+ done.forEach { $0.resume() }
+ }
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/OneAtATimeTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/OneAtATimeTests.swift
new file mode 100644
index 00000000..d65f444b
--- /dev/null
+++ b/Packages/PeelCore/Tests/PeelCoreTests/OneAtATimeTests.swift
@@ -0,0 +1,41 @@
+@testable import PeelCore
+import Testing
+
+@MainActor
+struct OneAtATimeTests {
+ @Test func aRequestMadeWhileOneRunsWaitsAndOnlyTheLatestRuns() async {
+ let line = OneAtATime<Bool>()
+ var runs: [(value: Bool, anotherWaited: Bool)] = []
+ var release: CheckedContinuation<Void, Never>?
+
+ let first = Task {
+ await line.ask(true) { value, anotherWaits in
+ await withCheckedContinuation { release = $0 }
+ runs.append((value, anotherWaits()))
+ }
+ }
+ while release == nil { await Task.yield() }
+ let second = Task { await line.ask(false) { value, anotherWaits in runs.append((value, anotherWaits())) } }
+ let third = Task { await line.ask(true) { value, anotherWaits in runs.append((value, anotherWaits())) } }
+ for _ in 0..<20 { await Task.yield() }
+ release?.resume()
+ await first.value
+ await second.value
+ await third.value
+
+ #expect(runs.map(\.value) == [true, true])
+ #expect(runs.map(\.anotherWaited) == [true, false])
+ }
+
+ @Test func aRequestAloneRunsAtOnce() async {
+ let line = OneAtATime<Int>()
+ var ran: [Int] = []
+
+ await line.ask(3) { value, anotherWaits in
+ ran.append(value)
+ #expect(!anotherWaits())
+ }
+
+ #expect(ran == [3])
+ }
+}
diff --git a/Peel/Model/TweakLibrary.swift b/Peel/Model/TweakLibrary.swift
index 271b63a5..9849bd70 100644
--- a/Peel/Model/TweakLibrary.swift
+++ b/Peel/Model/TweakLibrary.swift
@@ -77,9 +77,21 @@ final class TweakLibrary {
asked[tweak.id] ?? state.isOn
}
+ /// A switch's changes to one tweak, made one at a time: flipped quickly, only the latest waiting change runs next.
+ private var lines: [Tweak.ID: OneAtATime<Bool>] = [:]
+ /// The tweaks changed by a switch whose restart was left to a change waiting after it.
+ private var restartOwed: Set<Tweak.ID> = []
+
func set(_ tweak: Tweak, on isOn: Bool) {
asked[tweak.id] = isOn
- Task { isOn ? await turnOn(tweak) : await reset(tweak) }
+ let line = lines[tweak.id] ?? OneAtATime()
+ lines[tweak.id] = line
+ Task {
+ await line.ask(isOn) { [self] isOn, anotherWaits in
+ let outcome = isOn ? ledger.turnOn(tweak, in: store) : ledger.turnOff(tweak, in: store)
+ await finish(tweak, after: outcome, isLast: !anotherWaits())
+ }
+ }
}
func turnOn(_ tweak: Tweak, text: String? = nil) async {
@@ -156,10 +168,11 @@ final class TweakLibrary {
/// Saves the result of a change and restarts what the tweak needs. Nothing restarts when the setting was
/// already as asked.
- private func finish(_ tweak: Tweak, after outcome: TweakLedger.Outcome) async {
- defer { asked[tweak.id] = nil }
+ private func finish(_ tweak: Tweak, after outcome: TweakLedger.Outcome, isLast: Bool = true) async {
+ defer { if isLast { asked[tweak.id] = nil } }
refused = outcome == .refused ? [tweak.id] : []
guard outcome != .refused else {
+ if isLast, restartOwed.remove(tweak.id) != nil { await TweakRestart.run(tweak.restart) }
refresh()
return
}
@@ -169,8 +182,11 @@ final class TweakLibrary {
waitingForLogOut.formSymmetricDifference([tweak.id])
}
save()
- if outcome == .changed {
- await TweakRestart.run(tweak.restart)
+ if !isLast {
+ if outcome == .changed { restartOwed.insert(tweak.id) }
+ } else {
+ let wasOwed = restartOwed.remove(tweak.id) != nil
+ if outcome == .changed || wasOwed { await TweakRestart.run(tweak.restart) }
}
refresh()
}