( expected: Date, now: Date, toleranceMs: number, getNextMatch: (date: Date) => Date )
| 22 | * cron's interval can never run the same slot twice. |
| 23 | */ |
| 24 | export function planBeat( |
| 25 | expected: Date, |
| 26 | now: Date, |
| 27 | toleranceMs: number, |
| 28 | getNextMatch: (date: Date) => Date |
| 29 | ): BeatPlan { |
| 30 | const missed: Date[] = []; |
| 31 | let slot = expected; |
| 32 | |
| 33 | while (true) { |
| 34 | const nowMs = now.getTime(); |
| 35 | const slotMs = slot.getTime(); |
| 36 | |
| 37 | // Timer woke before this slot is due (e.g. the clock moved back). Nothing |
| 38 | // to run yet; keep waiting for it. |
| 39 | if (nowMs < slotMs) { |
| 40 | return { missed, next: slot }; |
| 41 | } |
| 42 | |
| 43 | const next = getNextMatch(slot); |
| 44 | |
| 45 | // Defense in depth: getNextMatch must always advance. If it ever does not |
| 46 | // (e.g. a bug around a DST boundary), re-base from now to avoid looping |
| 47 | // forever. |
| 48 | if (next.getTime() <= slotMs) { |
| 49 | return { missed, next: getNextMatch(now) }; |
| 50 | } |
| 51 | |
| 52 | const gap = next.getTime() - slotMs; |
| 53 | const lateBy = nowMs - slotMs; |
| 54 | |
| 55 | // Runnable while we are within tolerance AND the next slot has not arrived |
| 56 | // yet. The gap bound means a late run can never bleed into the following |
| 57 | // slot, so a tolerance larger than the interval can never run a slot twice. |
| 58 | if (lateBy <= toleranceMs && lateBy < gap) { |
| 59 | return { missed, run: slot, next }; |
| 60 | } |
| 61 | |
| 62 | // Too late for this slot. Report it missed and consider the next one. |
| 63 | missed.push(slot); |
| 64 | slot = next; |
| 65 | } |
| 66 | } |
no outgoing calls
no test coverage detected