Wake the statistics page on worker messages instead of polling (#28)
The page polled a std channel on a 100 ms timer, waking the UI thread ten times per second with nothing to do. It now awaits gpui-kit's own async channel (gpui_kit::base::async_util, smol's on native), so it wakes only when a worker sends, with no timer and no new dependency. The listener task is owned by the page, so it stops when the window closes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit was merged in pull request #34.
This commit is contained in:
1 parent
e13db17bcb
commit
75dd0ea643
3 files changed
+36
-33
No files matched your search
@@ -7,11 +7,11 @@ mod text;
|
|||||||
mod worker;
|
mod worker;
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::mpsc::{self, Sender};
|
|
||||||
|
|
||||||
use arena_tracker_core::{
|
use arena_tracker_core::{
|
||||||
Config, DataDragon, LeagueClient, RiotApi, Tracker, data_dir, default_seasons, load_seasons,
|
Config, DataDragon, LeagueClient, RiotApi, Tracker, data_dir, default_seasons, load_seasons,
|
||||||
};
|
};
|
||||||
|
use gpui_kit::base::async_util::{Sender, unbounded};
|
||||||
use gpui_kit::component::*;
|
use gpui_kit::component::*;
|
||||||
use gpui_kit::*;
|
use gpui_kit::*;
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ impl RootView {
|
|||||||
// Without a key the page still shows what is stored; only a true first launch (no key
|
// Without a key the page still shows what is stored; only a true first launch (no key
|
||||||
// and nothing stored) has nothing to show, so the key form stands in for the page.
|
// and nothing stored) has nothing to show, so the key form stands in for the page.
|
||||||
let first_launch = !store.has_key() && tracker.accounts().unwrap_or_default().is_empty();
|
let first_launch = !store.has_key() && tracker.accounts().unwrap_or_default().is_empty();
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = unbounded();
|
||||||
let page = cx.new(|cx| {
|
let page = cx.new(|cx| {
|
||||||
StatsPage::new(
|
StatsPage::new(
|
||||||
tracker.clone(),
|
tracker.clone(),
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
//! The statistics page: banners, Challenge header, charts and champion table for one Account.
|
//! The statistics page: banners, Challenge header, charts and champion table for one Account.
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::mpsc::{Receiver, Sender, TryRecvError};
|
|
||||||
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
use arena_tracker_core::{
|
use arena_tracker_core::{
|
||||||
Account, ChallengeSummary, Charts, Format, ProgressCheck, Scope, SeasonScope, StateFilter,
|
Account, ChallengeSummary, Charts, Format, ProgressCheck, Scope, SeasonScope, StateFilter,
|
||||||
Statistics, Status, SyncProgress, Tracker, UpdateProblem,
|
Statistics, Status, SyncProgress, Tracker, UpdateProblem,
|
||||||
};
|
};
|
||||||
|
use gpui_kit::base::async_util::{Receiver, Sender};
|
||||||
use gpui_kit::component::StyledExt as _;
|
use gpui_kit::component::StyledExt as _;
|
||||||
use gpui_kit::component::alert::Alert;
|
use gpui_kit::component::alert::Alert;
|
||||||
use gpui_kit::component::button::Button;
|
use gpui_kit::component::button::Button;
|
||||||
@@ -85,6 +85,7 @@ pub struct StatsPage {
|
|||||||
table: Entity<TableState<ChampionTable>>,
|
table: Entity<TableState<ChampionTable>>,
|
||||||
table_in_season: bool,
|
table_in_season: bool,
|
||||||
_subscriptions: Vec<Subscription>,
|
_subscriptions: Vec<Subscription>,
|
||||||
|
_listener: Task<()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EventEmitter<PageEvent> for StatsPage {}
|
impl EventEmitter<PageEvent> for StatsPage {}
|
||||||
@@ -190,31 +191,17 @@ impl StatsPage {
|
|||||||
),
|
),
|
||||||
];
|
];
|
||||||
|
|
||||||
// The workers' messages arrive on a plain channel; polling it keeps the UI free of
|
// gpui-kit's async channel wakes the page on sends, with no timer or extra dependency.
|
||||||
// any extra dependency.
|
let listener = cx.spawn_in(window, async move |this, cx| {
|
||||||
cx.spawn_in(window, async move |this, cx| {
|
while let Ok(msg) = rx.recv().await {
|
||||||
loop {
|
let applied = cx
|
||||||
cx.background_executor()
|
.update(|window, app| this.update(app, |page, cx| page.apply(msg, window, cx)));
|
||||||
.timer(Duration::from_millis(100))
|
// The window or the page is gone: nothing left to update.
|
||||||
.await;
|
if !matches!(applied, Ok(Ok(()))) {
|
||||||
loop {
|
return;
|
||||||
match rx.try_recv() {
|
|
||||||
Ok(msg) => {
|
|
||||||
let applied = cx.update(|window, app| {
|
|
||||||
this.update(app, |page, cx| page.apply(msg, window, cx))
|
|
||||||
});
|
|
||||||
// The window or the page is gone: nothing left to update.
|
|
||||||
if !matches!(applied, Ok(Ok(()))) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(TryRecvError::Empty) => break,
|
|
||||||
Err(TryRecvError::Disconnected) => return,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
.detach();
|
|
||||||
|
|
||||||
let mut page = StatsPage {
|
let mut page = StatsPage {
|
||||||
status: tracker.status(),
|
status: tracker.status(),
|
||||||
@@ -245,6 +232,7 @@ impl StatsPage {
|
|||||||
table,
|
table,
|
||||||
table_in_season: false,
|
table_in_season: false,
|
||||||
_subscriptions: subscriptions,
|
_subscriptions: subscriptions,
|
||||||
|
_listener: listener,
|
||||||
};
|
};
|
||||||
page.reload(window, cx);
|
page.reload(window, cx);
|
||||||
page
|
page
|
||||||
|
|||||||
@@ -3,11 +3,11 @@
|
|||||||
|
|
||||||
use std::panic::{AssertUnwindSafe, catch_unwind};
|
use std::panic::{AssertUnwindSafe, catch_unwind};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::mpsc::Sender;
|
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
use arena_tracker_core::{Status, SyncProgress, Tracker};
|
use arena_tracker_core::{Status, SyncProgress, Tracker};
|
||||||
|
use gpui_kit::base::async_util::Sender;
|
||||||
|
|
||||||
pub enum Msg {
|
pub enum Msg {
|
||||||
/// A sync is fetching Matches.
|
/// A sync is fetching Matches.
|
||||||
@@ -67,7 +67,7 @@ fn now_ms() -> i64 {
|
|||||||
fn spawn_guarded(tx: Sender<Msg>, body: impl FnOnce(&Sender<Msg>) + Send + 'static) {
|
fn spawn_guarded(tx: Sender<Msg>, body: impl FnOnce(&Sender<Msg>) + Send + 'static) {
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
if catch_unwind(AssertUnwindSafe(|| body(&tx))).is_err() {
|
if catch_unwind(AssertUnwindSafe(|| body(&tx))).is_err() {
|
||||||
let _ = tx.send(Msg::Crashed);
|
let _ = tx.try_send(Msg::Crashed);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -80,17 +80,17 @@ pub fn spawn_follower(tracker: Arc<Tracker>, fallback_riot_id: String, tx: Sende
|
|||||||
let champions = tracker.clone();
|
let champions = tracker.clone();
|
||||||
spawn_guarded(tx.clone(), move |tx| {
|
spawn_guarded(tx.clone(), move |tx| {
|
||||||
let _ = champions.refresh_champions();
|
let _ = champions.refresh_champions();
|
||||||
let _ = tx.send(Msg::Champions);
|
let _ = tx.try_send(Msg::Champions);
|
||||||
});
|
});
|
||||||
spawn_guarded(tx, move |tx| {
|
spawn_guarded(tx, move |tx| {
|
||||||
let fallback = Some(fallback_riot_id.as_str()).filter(|id| !id.trim().is_empty());
|
let fallback = Some(fallback_riot_id.as_str()).filter(|id| !id.trim().is_empty());
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
loop {
|
loop {
|
||||||
let changed = tracker.follow_client(now_ms(), fallback, &mut |progress| {
|
let changed = tracker.follow_client(now_ms(), fallback, &mut |progress| {
|
||||||
let _ = tx.send(Msg::Progress(progress));
|
let _ = tx.try_send(Msg::Progress(progress));
|
||||||
});
|
});
|
||||||
let status = tracker.status();
|
let status = tracker.status();
|
||||||
let _ = tx.send(Msg::Round {
|
let _ = tx.try_send(Msg::Round {
|
||||||
status,
|
status,
|
||||||
reload: changed || first,
|
reload: changed || first,
|
||||||
});
|
});
|
||||||
@@ -105,16 +105,31 @@ pub fn spawn_refresh(tracker: Arc<Tracker>, tx: Sender<Msg>) {
|
|||||||
spawn_guarded(tx, move |tx| {
|
spawn_guarded(tx, move |tx| {
|
||||||
// A failure is recorded in the status, which the page shows as a banner.
|
// A failure is recorded in the status, which the page shows as a banner.
|
||||||
let _ = tracker.refresh(&mut |progress| {
|
let _ = tracker.refresh(&mut |progress| {
|
||||||
let _ = tx.send(Msg::Progress(progress));
|
let _ = tx.try_send(Msg::Progress(progress));
|
||||||
});
|
});
|
||||||
let _ = tx.send(Msg::Refreshed(tracker.status()));
|
let _ = tx.try_send(Msg::Refreshed(tracker.status()));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use gpui_kit::base::async_util::unbounded;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn guarded_workers_deliver_messages_and_report_panics() {
|
||||||
|
let (tx, rx) = unbounded();
|
||||||
|
spawn_guarded(tx.clone(), |tx| {
|
||||||
|
let _ = tx.try_send(Msg::Champions);
|
||||||
|
});
|
||||||
|
assert!(matches!(gpui_kit::block_on(rx.recv()), Ok(Msg::Champions)));
|
||||||
|
|
||||||
|
spawn_guarded(tx, |_| panic!("worker panic"));
|
||||||
|
assert!(matches!(gpui_kit::block_on(rx.recv()), Ok(Msg::Crashed)));
|
||||||
|
assert!(gpui_kit::block_on(rx.recv()).is_err());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn a_request_while_a_refresh_runs_waits_for_it_and_is_served_once() {
|
fn a_request_while_a_refresh_runs_waits_for_it_and_is_served_once() {
|
||||||
let mut gate = RefreshGate::default();
|
let mut gate = RefreshGate::default();
|
||||||
|
|||||||
Reference in new issue
Block a user