Met à jour les listes en direct via WebSocket

Le serveur prévient, sur /ws, le propriétaire et les membres d'une liste
dès qu'elle change ; leurs pages rechargent alors leurs données.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Romulus21andClaude Opus 5.5 committed 2026-10-11 13:32:37 +02:00
1 parent 9f3fd5701e
commit 2ade50465e
10 files changed
+359 -38

No files matched your search

+25 -9
View File
@@ -1,5 +1,6 @@
mod account;
mod icons;
mod live;
mod sheets;
mod tiles;
@@ -82,8 +83,9 @@ pub struct Ctx {
/// Bumped after every mutation so resources refetch.
refresh: RwSignal<u32>,
error: RwSignal<Option<String>>,
/// Checked state applied before the server confirms it.
optimistic: RwSignal<HashMap<i64, bool>>,
/// Checked state applied before fresh data shows it, with the number of
/// calls still in flight for that item.
optimistic: RwSignal<HashMap<i64, (bool, u32)>>,
item_editor: RwSignal<Option<ItemEditor>>,
list_editor: RwSignal<Option<ListEditor>>,
account_open: RwSignal<bool>,
@@ -121,14 +123,22 @@ impl Ctx {
fn toggle(self, item: &Item) {
let (id, checked) = (item.id, !item.checked);
self.optimistic.update(|map| {
map.insert(id, checked);
let in_flight = map.get(&id).map_or(0, |&(_, n)| n);
map.insert(id, (checked, in_flight + 1));
});
spawn_local(async move {
if let Err(err) = crate::api::set_checked(id, checked).await {
self.optimistic.update(|map| {
map.remove(&id);
});
self.report(err);
match crate::api::set_checked(id, checked).await {
Ok(()) => self.optimistic.update_untracked(|map| {
if let Some((_, in_flight)) = map.get_mut(&id) {
*in_flight -= 1;
}
}),
Err(err) => {
self.optimistic.update(|map| {
map.remove(&id);
});
self.report(err);
}
}
});
}
@@ -159,6 +169,7 @@ fn ListsView(user: User) -> impl IntoView {
auth: expect_context::<Auth>(),
};
provide_context(ctx);
live::subscribe(ctx.refresh);
let params = use_params_map();
let active_id = Memo::new(move |_| {
@@ -261,6 +272,11 @@ fn ListsContent(
.into_any();
};
// Fresh data includes every confirmed toggle: only keep pending ones, so
// changes made by others since are not masked.
ctx.optimistic
.update_untracked(|map| map.retain(|_, &mut (_, in_flight)| in_flight > 0));
// Items with pending checkbox changes applied, by category then name.
let mut items = items;
items.sort_by_cached_key(Item::sort_key);
@@ -272,7 +288,7 @@ fn ListsContent(
.iter()
.cloned()
.map(|mut item| {
if let Some(&checked) = optimistic.get(&item.id) {
if let Some(&(checked, _)) = optimistic.get(&item.id) {
item.checked = checked;
}
item