Web: a clock set back after a failed font fetch no longer stops its retry

The demand-font retry deadlines were read from the wall clock. If the
system clock went back after a fetch failed, the retry timer fired but
the deadline still lay ahead, so the face was never fetched again that
session. The deadlines now use the monotonic clock (now_ms), and a retry
also starts when the clock reads earlier than the failure.
This commit is contained in:
dotCooCoo
2026-11-13 07:41:08 -08:00
parent 3b072ad078
commit d491249329
2 changed files with 19 additions and 5 deletions
+17 -3
View File
@@ -15,9 +15,11 @@ enum State {
tries: u32,
},
Loaded,
/// `tries` fetches failed; the next may start at `retry_at` (ms).
/// `tries` fetches failed, the last at `failed_at` (ms); the next may start at `retry_at`, or
/// at once when the clock reads earlier than `failed_at`.
Failed {
tries: u32,
failed_at: f64,
retry_at: f64,
},
}
@@ -42,7 +44,7 @@ impl Demand {
let tries = match *state {
State::Idle => Some(0),
State::Loading { .. } | State::Loaded => None,
State::Failed { tries, retry_at } => (tries < MAX_TRIES && now >= retry_at).then_some(tries),
State::Failed { tries, failed_at, retry_at } => (tries < MAX_TRIES && (now >= retry_at || now < failed_at)).then_some(tries),
};
if let Some(tries) = tries {
*state = State::Loading { tries };
@@ -64,7 +66,7 @@ impl Demand {
}
let tries = tries + 1;
let wait = RETRY_MS << (tries - 1).min(4);
*state = State::Failed { tries, retry_at: now + f64::from(wait) };
*state = State::Failed { tries, failed_at: now, retry_at: now + f64::from(wait) };
(tries < MAX_TRIES).then_some(wait)
}
}
@@ -107,4 +109,16 @@ mod tests {
assert_eq!(d.finished(1, true, f64::from(RETRY_MS)), None);
assert!(d.start(&[1], 1e12).is_empty());
}
/// A clock that reads earlier than the failure (one set back meanwhile) does not hold the
/// retry back: the timer that asks for it has waited already.
#[test]
fn a_clock_set_back_after_a_failure_does_not_stop_the_retry() {
let mut d = Demand::default();
let failed_at = 1e12;
d.start(&[0], failed_at);
assert_eq!(d.finished(0, false, failed_at), Some(RETRY_MS));
assert!(d.start(&[0], failed_at + 1.0).is_empty(), "still waiting");
assert_eq!(d.start(&[0], failed_at - 3_600_000.0), [0], "the clock went back an hour");
}
}
+2 -2
View File
@@ -49,11 +49,11 @@ pub fn want_chars(chars: &[char]) {
/// Fetch the `demand` faces `wanted` (indexes into `WEB_FONTS`) that [`Demand::start`] lets start.
/// A face whose fetch fails is fetched again after the wait [`Demand::finished`] gives.
fn fetch_demand(wanted: &[usize]) {
for i in DEMAND.with(|d| d.borrow_mut().start(wanted, js_sys::Date::now())) {
for i in DEMAND.with(|d| d.borrow_mut().start(wanted, vectorcraft_ui_egui::now_ms())) {
let Some(f) = WEB_FONTS.get(i) else { continue };
wasm_bindgen_futures::spawn_local(async move {
let ok = load(f, BACKGROUND_TIMEOUT_MS).await;
let retry = DEMAND.with(|d| d.borrow_mut().finished(i, ok, js_sys::Date::now()));
let retry = DEMAND.with(|d| d.borrow_mut().finished(i, ok, vectorcraft_ui_egui::now_ms()));
if ok && let Some(ctx) = REPAINT.with(|r| r.borrow().clone()) {
ctx.request_repaint();
}