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
@@ -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<TableState<ChampionTable>>,
|
||||
table_in_season: bool,
|
||||
_subscriptions: Vec<Subscription>,
|
||||
_listener: Task<()>,
|
||||
}
|
||||
|
||||
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
|
||||
// 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
|
||||
|
||||
Reference in new issue
Block a user