Save splashes when loading art is missing and restore art notifications (#83)
This commit was merged in pull request #93.
This commit is contained in:
1 parent
e440d155fc
commit
cee9ff2989
5 files changed
+190
-86
No files matched your search
+60
-42
@@ -77,31 +77,46 @@ impl RefreshGate {
|
||||
}
|
||||
}
|
||||
|
||||
/// While champion art keeps arriving, how often the page is told: each time, it reloads its
|
||||
/// While cards and splashes keep arriving, how often the page is told: each time, it reloads its
|
||||
/// statistics to show them.
|
||||
const PORTRAITS_TOLD_EVERY: Duration = Duration::from_secs(1);
|
||||
const ART_TOLD_EVERY: Duration = Duration::from_secs(1);
|
||||
|
||||
/// Shares the catalog notification's deadline across the card and splash passes.
|
||||
/// Spaces out what saved images cause: the first save is told at once, the next ones when
|
||||
/// `every` has passed since the last message, and one still untold when the pass ends is told
|
||||
/// then. No timer: each attempt of the pass, saved or not, and its end are what tell.
|
||||
struct Throttle {
|
||||
last_told: Instant,
|
||||
every: Duration,
|
||||
last_told: Option<Instant>,
|
||||
untold: bool,
|
||||
}
|
||||
|
||||
impl Throttle {
|
||||
fn new(every: Duration) -> Throttle {
|
||||
Throttle {
|
||||
every,
|
||||
last_told: None,
|
||||
untold: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// An attempt ended at `now`, with an image saved or not: true when the page must be told
|
||||
/// now, as a save is untold and `every` has passed since the last message.
|
||||
fn attempted(&mut self, saved: bool, now: Instant) -> bool {
|
||||
self.untold |= saved;
|
||||
if self.untold && now.duration_since(self.last_told) >= PORTRAITS_TOLD_EVERY {
|
||||
self.last_told = now;
|
||||
let due = self
|
||||
.last_told
|
||||
.is_none_or(|last| now.duration_since(last) >= self.every);
|
||||
if self.untold && due {
|
||||
self.last_told = Some(now);
|
||||
self.untold = false;
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// A final batch needs one wake at its deadline, even when the pass ends early.
|
||||
fn final_delay(self, now: Instant) -> Option<Duration> {
|
||||
/// The pass ended: true when a save is still untold.
|
||||
fn finished(self) -> bool {
|
||||
self.untold
|
||||
.then(|| PORTRAITS_TOLD_EVERY.saturating_sub(now.duration_since(self.last_told)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,24 +171,17 @@ fn spawn_guarded<T: Send + 'static>(
|
||||
pub fn spawn_follower(tracker: Arc<Tracker>, fallback_riot_id: String, tx: Sender<Msg>) {
|
||||
let champions = tracker.clone();
|
||||
spawn_guarded(tx.clone(), Msg::Crashed, move |tx| {
|
||||
let refreshed = champions.refresh_champions();
|
||||
let _ = tx.try_send(Msg::Champions);
|
||||
// Offline, the catalog call failed and so would every portrait request.
|
||||
if refreshed.is_err() {
|
||||
return;
|
||||
}
|
||||
let mut throttle = Throttle {
|
||||
last_told: Instant::now(),
|
||||
untold: false,
|
||||
let mut throttle = Throttle::new(ART_TOLD_EVERY);
|
||||
let notify = || {
|
||||
let _ = tx.try_send(Msg::Champions);
|
||||
};
|
||||
let _ = champions.refresh_champion_art(&mut |saved| {
|
||||
let _ = champions.refresh_champion_art(notify, &mut |saved| {
|
||||
if throttle.attempted(saved, Instant::now()) {
|
||||
let _ = tx.try_send(Msg::Champions);
|
||||
notify();
|
||||
}
|
||||
});
|
||||
if let Some(delay) = throttle.final_delay(Instant::now()) {
|
||||
thread::sleep(delay);
|
||||
let _ = tx.try_send(Msg::Champions);
|
||||
if throttle.finished() {
|
||||
notify();
|
||||
}
|
||||
});
|
||||
// Coalesce history updates while Riot is busy; the WebSocket keeps delivering live picks.
|
||||
@@ -435,29 +443,39 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn saved_art_is_told_at_most_once_a_second_including_the_final_batch() {
|
||||
fn saved_art_is_told_once_a_second_after_any_attempt_and_the_last_ones_at_the_end() {
|
||||
let start = Instant::now();
|
||||
let at = |ms: u64| start + Duration::from_millis(ms);
|
||||
let mut throttle = Throttle {
|
||||
last_told: start,
|
||||
untold: false,
|
||||
};
|
||||
assert!(!throttle.attempted(true, at(0)));
|
||||
assert!(!throttle.attempted(true, at(999)));
|
||||
assert!(throttle.attempted(false, at(1000)));
|
||||
assert!(!throttle.attempted(false, at(2000)));
|
||||
assert!(throttle.attempted(true, at(2100)));
|
||||
assert!(!throttle.attempted(true, at(2110)));
|
||||
assert_eq!(
|
||||
throttle.final_delay(at(2300)),
|
||||
Some(Duration::from_millis(800))
|
||||
let mut throttle = Throttle::new(Duration::from_secs(1));
|
||||
assert!(
|
||||
throttle.attempted(true, at(0)),
|
||||
"the first save is told at once"
|
||||
);
|
||||
assert!(!throttle.attempted(true, at(300)));
|
||||
assert!(!throttle.attempted(true, at(999)));
|
||||
assert!(throttle.attempted(true, at(1000)));
|
||||
assert!(!throttle.attempted(true, at(1500)));
|
||||
// A failed attempt tells nothing before the second is over, then the save at 1.5 s is.
|
||||
assert!(!throttle.attempted(false, at(1900)));
|
||||
assert!(throttle.attempted(false, at(2000)));
|
||||
assert!(
|
||||
!throttle.attempted(false, at(5000)),
|
||||
"nothing is untold any more"
|
||||
);
|
||||
assert!(!throttle.finished());
|
||||
|
||||
let empty = Throttle {
|
||||
last_told: start,
|
||||
untold: false,
|
||||
};
|
||||
assert_eq!(empty.final_delay(at(100)), None);
|
||||
// The last save was told late or never: the end of the pass tells it.
|
||||
let mut throttle = Throttle::new(Duration::from_secs(1));
|
||||
assert!(throttle.attempted(true, at(0)));
|
||||
assert!(!throttle.attempted(true, at(10)));
|
||||
assert!(!throttle.attempted(false, at(20)));
|
||||
assert!(throttle.finished());
|
||||
|
||||
// Nothing saved, nothing told.
|
||||
let mut throttle = Throttle::new(Duration::from_secs(1));
|
||||
assert!(!throttle.attempted(false, at(0)));
|
||||
assert!(!throttle.attempted(false, at(5000)));
|
||||
assert!(!throttle.finished());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user