Skip to content

Commit 835ec09

Browse files
committed
perf: align schedule polling to minute boundaries (xx:01) instead of every 30s
1 parent e11cff6 commit 835ec09

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

Tome/Managers/ScheduleManager.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,22 @@ class ScheduleManager: ObservableObject {
5656
}
5757

5858
private func startTimer() {
59-
timer = Timer.scheduledTimer(withTimeInterval: 30, repeats: true) { [weak self] _ in
59+
// fire at XX:01 of every minute — schedules always start/end on minute boundaries
60+
// so this gives ~1s response time with only 1 poll per minute
61+
let now = Date()
62+
let cal = Calendar.current
63+
let nextMinute = cal.nextDate(after: now, matching: DateComponents(second: 1), matchingPolicy: .nextTime)!
64+
let delay = nextMinute.timeIntervalSince(now)
65+
66+
let oneShot = Timer.scheduledTimer(withTimeInterval: delay, repeats: false) { [weak self] _ in
67+
self?.evaluate()
68+
self?.startRepeatingTimer()
69+
}
70+
RunLoop.main.add(oneShot, forMode: .common)
71+
}
72+
73+
private func startRepeatingTimer() {
74+
timer = Timer.scheduledTimer(withTimeInterval: 60, repeats: true) { [weak self] _ in
6075
self?.evaluate()
6176
}
6277
RunLoop.main.add(timer!, forMode: .common)

0 commit comments

Comments
 (0)