Files
Kymo/kymo/Core/Services/MetricsStore.swift
T
wangairnan 631b412d15 feat(monitors): 实现 CPU 内存采样诊断与菜单栏实时指标
接入 Mach CPU 差分、内存与负载采样、暂停恢复及按需诊断,合并菜单栏单一原生标题的显示修复。
保留核心检查和本机交互验收记录;长期资源预算仍待验收。
2026-10-30 00:45:34 +08:00

41 lines
1.1 KiB
Swift

import Foundation
import Observation
@MainActor @Observable
final class MetricsStore {
private(set) var snapshot = MetricsSnapshot()
private(set) var isPaused = false
@ObservationIgnored private var task: Task<Void, Never>?
@ObservationIgnored private let sampler = SystemSampler()
init() { start() }
func start() {
guard task == nil else { return }
isPaused = false
task = Task { [weak self, sampler] in
await sampler.reset()
while !Task.isCancelled {
let next = await sampler.sample()
guard !Task.isCancelled else { return }
self?.snapshot = next
SamplingDiagnostics.record(next)
do { try await Task.sleep(for: .seconds(2)) }
catch { return }
}
}
}
func togglePause() {
if isPaused { start() }
else {
task?.cancel()
task = nil
isPaused = true
SamplingDiagnostics.record(snapshot, event: "pause")
}
}
deinit { task?.cancel() }
}