From 75dd0ea643f413a3d610c1cbfc42acc627275167 Mon Sep 17 00:00:00 2001
From: Nessar
Date: Tue, 17 Nov 2026 18:46:16 +0100
Subject: [PATCH] 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
---
crates/app/src/main.rs | 4 ++--
crates/app/src/stats_page.rs | 36 ++++++++++++------------------------
crates/app/src/worker.rs | 29 ++++++++++++++++++++++-------
3 files changed, 36 insertions(+), 33 deletions(-)
diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs
index 55bbd46..80197c3 100644
--- a/crates/app/src/main.rs
+++ b/crates/app/src/main.rs
@@ -7,11 +7,11 @@ mod text;
mod worker;
use std::sync::Arc;
-use std::sync::mpsc::{self, Sender};
use arena_tracker_core::{
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::*;
@@ -49,7 +49,7 @@ impl RootView {
// 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.
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| {
StatsPage::new(
tracker.clone(),
diff --git a/crates/app/src/stats_page.rs b/crates/app/src/stats_page.rs
index 7bc52b9..529e108 100644
--- a/crates/app/src/stats_page.rs
+++ b/crates/app/src/stats_page.rs
@@ -1,13 +1,13 @@
//! The statistics page: banners, Challenge header, charts and champion table for one Account.
use std::sync::Arc;
-use std::sync::mpsc::{Receiver, Sender, TryRecvError};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use arena_tracker_core::{
Account, ChallengeSummary, Charts, Format, ProgressCheck, Scope, SeasonScope, StateFilter,
Statistics, Status, SyncProgress, Tracker, UpdateProblem,
};
+use gpui_kit::base::async_util::{Receiver, Sender};
use gpui_kit::component::StyledExt as _;
use gpui_kit::component::alert::Alert;
use gpui_kit::component::button::Button;
@@ -85,6 +85,7 @@ pub struct StatsPage {
table: Entity>,
table_in_season: bool,
_subscriptions: Vec,
+ _listener: Task<()>,
}
impl EventEmitter for StatsPage {}
@@ -190,31 +191,17 @@ impl StatsPage {
),
];
- // The workers' messages arrive on a plain channel; polling it keeps the UI free of
- // any extra dependency.
- cx.spawn_in(window, async move |this, cx| {
- loop {
- cx.background_executor()
- .timer(Duration::from_millis(100))
- .await;
- loop {
- 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,
- }
+ // gpui-kit's async channel wakes the page on sends, with no timer or extra dependency.
+ let listener = cx.spawn_in(window, async move |this, cx| {
+ while let Ok(msg) = rx.recv().await {
+ 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;
}
}
- })
- .detach();
+ });
let mut page = StatsPage {
status: tracker.status(),
@@ -245,6 +232,7 @@ impl StatsPage {
table,
table_in_season: false,
_subscriptions: subscriptions,
+ _listener: listener,
};
page.reload(window, cx);
page
diff --git a/crates/app/src/worker.rs b/crates/app/src/worker.rs
index 87e4db3..79bac16 100644
--- a/crates/app/src/worker.rs
+++ b/crates/app/src/worker.rs
@@ -3,11 +3,11 @@
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::sync::Arc;
-use std::sync::mpsc::Sender;
use std::thread;
use std::time::{SystemTime, UNIX_EPOCH};
use arena_tracker_core::{Status, SyncProgress, Tracker};
+use gpui_kit::base::async_util::Sender;
pub enum Msg {
/// A sync is fetching Matches.
@@ -67,7 +67,7 @@ fn now_ms() -> i64 {
fn spawn_guarded(tx: Sender, body: impl FnOnce(&Sender) + Send + 'static) {
thread::spawn(move || {
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, fallback_riot_id: String, tx: Sende
let champions = tracker.clone();
spawn_guarded(tx.clone(), move |tx| {
let _ = champions.refresh_champions();
- let _ = tx.send(Msg::Champions);
+ let _ = tx.try_send(Msg::Champions);
});
spawn_guarded(tx, move |tx| {
let fallback = Some(fallback_riot_id.as_str()).filter(|id| !id.trim().is_empty());
let mut first = true;
loop {
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 _ = tx.send(Msg::Round {
+ let _ = tx.try_send(Msg::Round {
status,
reload: changed || first,
});
@@ -105,16 +105,31 @@ pub fn spawn_refresh(tracker: Arc, tx: Sender) {
spawn_guarded(tx, move |tx| {
// A failure is recorded in the status, which the page shows as a banner.
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)]
mod tests {
+ use gpui_kit::base::async_util::unbounded;
+
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]
fn a_request_while_a_refresh_runs_waits_for_it_and_is_served_once() {
let mut gate = RefreshGate::default();