Skip to main content

pre_arm_deadlines

Function pre_arm_deadlines 

Source
pub fn pre_arm_deadlines(
    input: &NotifyInput,
    now: Timestamp,
) -> BTreeMap<AlertClass, Timestamp>
Expand description

The instant an OS timer must be armed for, per class — the arm half of the arm / re-arm / cancel rule.

A class present in the map should have exactly one live timer, set to the returned instant. A class absent from it should have no timer: cancel whatever it holds. Because the answer is a pure function of state, the whole client-side protocol is “recompute after any state change, then reconcile your timers against this map” — and one entry per class from one function is why two live timers for one class is structurally impossible.

It is keyed per class rather than collapsed to a single instant because the timers are independent: with a staleness deadline two weeks out and a recovery check ninety days out, arming only the earlier one loses the later alert entirely on a device the app never runs on again — which is the exact case pre-arming exists for. A client also needs the class to pick its notification.* catalog key for the notification it is arming.

This is deliberately narrower than evaluate(). An armed notification fires from the OS’s own timer with the app not running, so it cannot be re-checked when it arrives: an instant is returned only when the alert is certain to be true on arrival. Three things therefore withhold one:

  • the class is not pre-armable — its condition is server-held, so the device cannot compute a deadline for it at all;
  • the resulting instant is not strictly after now (it has already passed, so there is nothing left to schedule) — including a class the alert has already fired for;
  • the class would arrive as something other than a notification: sync_stale with nothing un-synced (only a sync can change that, and a sync re-arms), recovery_check_due with its snooze budget spent (a badge, which is in-app), and any class the user has disabled.

A snooze defers the armed instant rather than cancelling it: a class snoozed after it fired must re-fire when the snooze ends, and the snooze end is a deadline the device can compute.