Compute statistics on a background worker instead of deferring them (#29)
Reloads triggered by the workers no longer wait for the window to be shown: the statistics and Match count are computed on a spawn_guarded thread (below normal priority) and applied as soon as they arrive, visible or not, so a restored window is up to date at once and the UI thread computes nothing during an import. The Accounts are still read on the UI thread; the old statistics stay shown until the new ones land. One job runs at a time (RefreshGate); a generation number, bumped when statistics are computed or the viewed Account changes, drops results overtaken by a newer request or a user's selection. The job reports its own panic as an error result, so another worker's crash never discards it. User selections still recompute synchronously. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit was merged in pull request #36.
This commit is contained in:
1 parent
33343546a5
commit
c9f5b264e6
2 files changed
+191
-34
No files matched your search
@@ -20,7 +20,7 @@ use gpui_kit::*;
|
||||
|
||||
use crate::champion_table::ChampionTable;
|
||||
use crate::text::{CLIENT_ABSENT, decimal, official_differs, official_note, riot_id, sync_line};
|
||||
use crate::worker::{Msg, RefreshGate, spawn_refresh};
|
||||
use crate::worker::{Msg, RefreshGate, spawn_refresh, spawn_statistics};
|
||||
|
||||
const ALL_SEASONS: &str = "Toutes les Saisons";
|
||||
const ALL_FORMATS: &str = "Tous les Formats";
|
||||
@@ -62,15 +62,16 @@ pub struct StatsPage {
|
||||
last_active: Option<Account>,
|
||||
/// The Account the user picked instead of following the one being updated.
|
||||
pinned: Option<String>,
|
||||
/// The Account whose statistics are in `stats`.
|
||||
/// The Account of the last requested statistics.
|
||||
shown: Option<String>,
|
||||
progress: Option<SyncProgress>,
|
||||
refresh: RefreshGate,
|
||||
stats_job: RefreshGate,
|
||||
stats_generation: u64,
|
||||
/// Until the follower's first round ends.
|
||||
pending_round: bool,
|
||||
crashed: bool,
|
||||
last_reload: Instant,
|
||||
reload_when_shown: bool,
|
||||
current_season: Option<String>,
|
||||
season: SeasonScope,
|
||||
format: Option<Format>,
|
||||
@@ -146,13 +147,6 @@ impl StatsPage {
|
||||
cx.new(|cx| TableState::new(ChampionTable::new(), window, cx).col_movable(false));
|
||||
|
||||
let subscriptions = vec![
|
||||
cx.observe_window_visibility(window, |this, visibility, window, cx| {
|
||||
if visibility.is_visible() && this.reload_when_shown {
|
||||
this.reload_when_shown = false;
|
||||
this.reload(window, cx);
|
||||
cx.notify();
|
||||
}
|
||||
}),
|
||||
cx.subscribe_in(
|
||||
&account_select,
|
||||
window,
|
||||
@@ -222,10 +216,11 @@ impl StatsPage {
|
||||
shown: None,
|
||||
progress: None,
|
||||
refresh: RefreshGate::default(),
|
||||
stats_job: RefreshGate::default(),
|
||||
stats_generation: 0,
|
||||
pending_round: following,
|
||||
crashed: false,
|
||||
last_reload: Instant::now(),
|
||||
reload_when_shown: false,
|
||||
current_season,
|
||||
season,
|
||||
format: None,
|
||||
@@ -299,10 +294,10 @@ impl StatsPage {
|
||||
Msg::Progress(progress) => {
|
||||
self.progress = Some(progress);
|
||||
if self.last_reload.elapsed() >= RELOAD_EVERY {
|
||||
self.reload_if_shown(window, cx);
|
||||
self.reload_in_background(window, cx);
|
||||
}
|
||||
}
|
||||
Msg::Champions => self.reload_if_shown(window, cx),
|
||||
Msg::Champions => self.reload_in_background(window, cx),
|
||||
Msg::Round { status, reload } => {
|
||||
// A follower round that changes nothing shown must not rebuild the page (they keep coming).
|
||||
let unchanged = status == self.status
|
||||
@@ -318,19 +313,34 @@ impl StatsPage {
|
||||
self.progress = None;
|
||||
self.crashed = false;
|
||||
if reload {
|
||||
self.reload_if_shown(window, cx);
|
||||
self.reload_in_background(window, cx);
|
||||
}
|
||||
}
|
||||
Msg::Refreshed(status) => {
|
||||
self.status = status;
|
||||
self.progress = None;
|
||||
self.crashed = false;
|
||||
self.reload_if_shown(window, cx);
|
||||
self.reload_in_background(window, cx);
|
||||
// A request made while this one ran (a key saved meanwhile) is served now.
|
||||
if self.refresh.finished() {
|
||||
spawn_refresh(self.tracker.clone(), self.tx.clone());
|
||||
}
|
||||
}
|
||||
Msg::Statistics {
|
||||
generation,
|
||||
stats,
|
||||
stored,
|
||||
} => {
|
||||
if generation == self.stats_generation {
|
||||
self.stats_error = stats.as_ref().err().cloned();
|
||||
self.stats = stats.ok();
|
||||
self.stored = stored;
|
||||
self.show_statistics(window, cx);
|
||||
}
|
||||
if self.stats_job.finished() {
|
||||
self.recompute_in_background(true, window, cx);
|
||||
}
|
||||
}
|
||||
Msg::Crashed => {
|
||||
self.refresh.reset();
|
||||
self.pending_round = false;
|
||||
@@ -341,19 +351,22 @@ impl StatsPage {
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// A worker's reload, deferred while GPUI reports the window hidden (minimised or not shown;
|
||||
/// on Windows a covered window still counts as visible), then run as soon as it is shown again
|
||||
/// (rule #1, budget 3).
|
||||
fn reload_if_shown(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
if window.is_visible() {
|
||||
self.reload(window, cx);
|
||||
} else {
|
||||
self.reload_when_shown = true;
|
||||
}
|
||||
/// A worker's reload: the Accounts now, the statistics on a below-normal worker, so neither a
|
||||
/// visible nor a hidden window waits for them (rule #1).
|
||||
fn reload_in_background(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
self.reload_accounts(window, cx);
|
||||
let start_job = self.stats_job.request();
|
||||
self.recompute_in_background(start_job, window, cx);
|
||||
}
|
||||
|
||||
/// Re-reads the Accounts, then the statistics of the one shown.
|
||||
fn reload(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
self.reload_accounts(window, cx);
|
||||
self.recompute(window, cx);
|
||||
}
|
||||
|
||||
/// Re-reads the Accounts and selects the one shown.
|
||||
fn reload_accounts(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
self.accounts = self.tracker.accounts().unwrap_or_default();
|
||||
self.last_active = self.tracker.last_active_account().ok().flatten();
|
||||
if self.pinned.as_ref() == self.status.updating.as_ref().map(|a| &a.puuid) {
|
||||
@@ -380,11 +393,17 @@ impl StatsPage {
|
||||
select.set_selected_value(puuid, window, cx);
|
||||
}
|
||||
});
|
||||
self.recompute(window, cx);
|
||||
}
|
||||
|
||||
/// Recomputes the statistics for the current scope; never called while rendering.
|
||||
fn recompute(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
/// Settles the scope of the next statistics. Earlier results become stale when they are
|
||||
/// computed again (`computing`) or when the viewed Account changed; a request merely queued
|
||||
/// behind a running job keeps that job's result for the same scope.
|
||||
fn prepare_statistics(
|
||||
&mut self,
|
||||
computing: bool,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Option<String> {
|
||||
self.last_reload = Instant::now();
|
||||
if matches!(self.season, SeasonScope::AllSeasons)
|
||||
&& !matches!(self.filter, StateFilter::All | StateFilter::NeverPlayed)
|
||||
@@ -392,6 +411,9 @@ impl StatsPage {
|
||||
self.filter = StateFilter::All;
|
||||
}
|
||||
let viewed = self.viewed().map(|a| a.puuid.clone());
|
||||
if computing || viewed != self.shown {
|
||||
self.stats_generation += 1;
|
||||
}
|
||||
if viewed != self.shown {
|
||||
// Another Account may not have played the selected Format.
|
||||
self.shown = viewed.clone();
|
||||
@@ -401,6 +423,47 @@ impl StatsPage {
|
||||
});
|
||||
}
|
||||
}
|
||||
viewed
|
||||
}
|
||||
|
||||
/// Requests the statistics from a worker (`start_job`: none is running), or shows none when no
|
||||
/// Account is viewed.
|
||||
fn recompute_in_background(
|
||||
&mut self,
|
||||
start_job: bool,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
let viewed = self.prepare_statistics(start_job, window, cx);
|
||||
if let Some(puuid) = viewed {
|
||||
if start_job {
|
||||
let scope = Scope {
|
||||
season: self.season.clone(),
|
||||
format: self.format,
|
||||
};
|
||||
// A below-normal worker keeps hidden and visible windows responsive (rule #1).
|
||||
spawn_statistics(
|
||||
self.tracker.clone(),
|
||||
puuid,
|
||||
scope,
|
||||
self.stats_generation,
|
||||
self.tx.clone(),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if start_job {
|
||||
self.stats_job.reset();
|
||||
}
|
||||
self.stats = None;
|
||||
self.stats_error = None;
|
||||
self.stored = None;
|
||||
self.show_statistics(window, cx);
|
||||
}
|
||||
}
|
||||
|
||||
/// Recomputes the statistics for the current scope; never called while rendering.
|
||||
fn recompute(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
let viewed = self.prepare_statistics(true, window, cx);
|
||||
self.stats = None;
|
||||
self.stats_error = None;
|
||||
self.stored = None;
|
||||
@@ -415,6 +478,11 @@ impl StatsPage {
|
||||
}
|
||||
self.stored = self.tracker.match_count(puuid).ok();
|
||||
}
|
||||
self.show_statistics(window, cx);
|
||||
}
|
||||
|
||||
/// Shows `stats`: the Format choices and the table.
|
||||
fn show_statistics(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
let mut names = vec![SharedString::from(ALL_FORMATS)];
|
||||
names.extend(
|
||||
self.stats
|
||||
|
||||
Reference in new issue
Block a user