diff --git a/apps/vectorcraft-web/src/demand.rs b/apps/vectorcraft-web/src/demand.rs index cbab6014bf..d2a2317bd3 100644 --- a/apps/vectorcraft-web/src/demand.rs +++ b/apps/vectorcraft-web/src/demand.rs @@ -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"); + } } diff --git a/apps/vectorcraft-web/src/fonts.rs b/apps/vectorcraft-web/src/fonts.rs index d2403e8084..873474f6c3 100644 --- a/apps/vectorcraft-web/src/fonts.rs +++ b/apps/vectorcraft-web/src/fonts.rs @@ -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(); }