feat(menubar): 支持固定居中面板与独立刷新频率及图表浮层
This commit is contained in:
@@ -7,30 +7,60 @@ final class MetricsStore {
|
||||
private(set) var history = MetricHistory()
|
||||
private(set) var isPaused = false
|
||||
private(set) var isPanelVisible = false
|
||||
let sampling: SamplingPreferences
|
||||
var effectiveMilliseconds: Int { isPanelVisible ? sampling.panelMilliseconds : sampling.backgroundMilliseconds }
|
||||
@ObservationIgnored private var generation = 0
|
||||
@ObservationIgnored private var visibilityGeneration = 0
|
||||
@ObservationIgnored private var task: Task<Void, Never>?
|
||||
@ObservationIgnored private let sampler = SystemSampler()
|
||||
@ObservationIgnored private var scheduledInterval = 0
|
||||
@ObservationIgnored private var needsHistoryBoundary = true
|
||||
|
||||
init() { start() }
|
||||
init(sampling: SamplingPreferences? = nil) {
|
||||
self.sampling = sampling ?? SamplingPreferences()
|
||||
self.sampling.onChange = { [weak self] in
|
||||
guard let self, !self.isPaused, self.effectiveMilliseconds != self.scheduledInterval else { return }
|
||||
self.reschedule(reset: false)
|
||||
}
|
||||
start()
|
||||
}
|
||||
|
||||
func start() {
|
||||
guard task == nil else { return }
|
||||
isPaused = false
|
||||
reschedule(reset: true)
|
||||
}
|
||||
|
||||
private func reschedule(reset: Bool) {
|
||||
generation += 1
|
||||
task?.cancel()
|
||||
needsHistoryBoundary = true
|
||||
scheduledInterval = effectiveMilliseconds
|
||||
let currentGeneration = generation
|
||||
task = Task { [weak self, sampler] in
|
||||
await sampler.reset()
|
||||
await sampler.setProcessVisible(self?.isPanelVisible == true)
|
||||
guard !Task.isCancelled else { return }
|
||||
if reset { await sampler.reset() }
|
||||
while !Task.isCancelled {
|
||||
let visibleGeneration = self?.visibilityGeneration
|
||||
var next = await sampler.sample(includeProcesses: self?.isPanelVisible == true)
|
||||
var next = await sampler.sample(includeProcesses: self?.isPanelVisible == true, processUpdate: { [weak self] processes in
|
||||
Task { @MainActor [weak self] in
|
||||
guard let self, self.generation == currentGeneration, self.visibilityGeneration == visibleGeneration,
|
||||
self.isPanelVisible, !self.isPaused else { return }
|
||||
self.snapshot.processes = processes
|
||||
}
|
||||
})
|
||||
guard !Task.isCancelled, self?.generation == currentGeneration else { return }
|
||||
if self?.isPanelVisible != true || self?.visibilityGeneration != visibleGeneration { next.processes = nil }
|
||||
else if let completed = self?.snapshot.processes, completed.date >= next.date {
|
||||
// A fast detached process result may reach MainActor before this quick snapshot.
|
||||
next.processes = completed
|
||||
}
|
||||
self?.snapshot = next
|
||||
self?.history.append(next)
|
||||
self?.history.append(next, breakBefore: self?.needsHistoryBoundary == true)
|
||||
self?.needsHistoryBoundary = false
|
||||
SamplingDiagnostics.record(next)
|
||||
do { try await Task.sleep(for: .seconds(2)) }
|
||||
let interval = self?.effectiveMilliseconds ?? 5_000
|
||||
do { try await Task.sleep(for: .milliseconds(interval)) }
|
||||
catch { return }
|
||||
}
|
||||
}
|
||||
@@ -55,6 +85,7 @@ final class MetricsStore {
|
||||
if !visible { snapshot.processes = nil }
|
||||
let active = visible && !isPaused
|
||||
Task { await sampler.setProcessVisible(active) }
|
||||
if !isPaused { reschedule(reset: false) }
|
||||
}
|
||||
|
||||
deinit { task?.cancel() }
|
||||
|
||||
Reference in New Issue
Block a user