Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ foreground. Three things make that work and they are easy to undo by accident:
way to keep the tick alive is the floating meter, which runs through a locked screen.

`SessionStore` encodes and writes on its own serial queue, coalescing bursts, and the
load in `PowerMonitor.init` is a `Task`. At the ceiling — 60 sessions × 1,500 samples —
load starts once in `PowerMonitor.start()` as a `Task`, so a background-only Shortcut
does not load unused history. At the ceiling — 60 sessions × 1,500 samples —
the file is several megabytes, and doing that inline was a stall before the first frame
and again every thirty seconds during a charge. `persist()` is a no-op until `isLoaded`,
or a save landing before the load would truncate the history to nothing.
Expand Down
8 changes: 7 additions & 1 deletion MiniWatts/Core/PowerMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ final class PowerMonitor {
private let store = SessionStore()

private var task: Task<Void, Never>?
private var hasRequestedStoredSessions = false
private var tick = 0
private var lastSampleWrite: Date = .distantPast
private var lastPersist: Date = .distantPast
Expand Down Expand Up @@ -154,7 +155,6 @@ final class PowerMonitor {
.flatMap(LiveActivityMetric.init(rawValue:)) ?? .chargingPower
sensorServiceCount = probeService.initialServiceCount
collectDiagnostics()
Task { await loadStoredSessions() }
}

/// Reads the session file off the main thread and merges it in.
Expand Down Expand Up @@ -189,6 +189,12 @@ final class PowerMonitor {

func start() {
guard task == nil else { return }
// A background Shortcut needs only a fresh reading, not session history.
// Remember the request across pauses, even while the file is still loading.
if !hasRequestedStoredSessions {
hasRequestedStoredSessions = true
Task { await loadStoredSessions() }
}
task = Task { [weak self] in
while let self, !Task.isCancelled {
await self.refresh()
Expand Down