//! Item changes are shown at once and saved in the background: they wait in a //! queue and leave in batches, one request at a time, so a slow mobile //! connection never holds the interface back. use std::time::Duration; use leptos::ev; use leptos::prelude::*; use leptos::task::spawn_local; use super::Ctx; use crate::model::{Item, ItemOp, SyncReport}; /// Changes made within this delay leave together. const DEBOUNCE: Duration = Duration::from_millis(400); const MAX_RETRY_DELAY: Duration = Duration::from_secs(30); #[derive(Clone, Copy)] pub struct Sync(RwSignal); #[derive(Default)] struct State { /// Changes not sent yet. queue: Vec, /// Sent changes, shown until data fetched after they were saved has them. sent: Vec, /// Batches saved so far. saved: u32, /// Real ids of items added here, by temporary id. ids: Vec<(i64, i64)>, /// Last temporary id handed out. last_temp: i64, /// A flush is planned. scheduled: bool, /// A batch is on its way; the next one waits for its answer. sending: bool, /// Failed attempts since the last batch got through. retries: u32, } struct Batch { ops: Vec, /// Value of `saved` once this batch was saved. saved_as: Option, } enum Outcome { Saved(SyncReport), Refused(ServerFnError), Retry(Duration), } impl Sync { pub fn new() -> Self { Self(RwSignal::new(State::default())) } /// Sends what is left when the page is hidden: the phone may freeze it next. pub fn flush_on_hide(self, ctx: Ctx) { let handle = window_event_listener(ev::visibilitychange, move |_| { if document().hidden() { self.flush(ctx); } }); on_cleanup(move || handle.remove()); } pub fn push(self, ctx: Ctx, op: ItemOp) { let schedule = self.0.try_update(|state| { state.push(op); !std::mem::replace(&mut state.scheduled, true) }); if schedule == Some(true) { set_timeout(move || self.flush(ctx), DEBOUNCE); } } /// Saved batches so far: data fetched from now on includes them. pub fn saved(self) -> u32 { self.0.try_with_untracked(|state| state.saved).unwrap_or_default() } /// Forgets saved changes that data fetched when `saved` batches were /// saved already shows, so changes made by others since are not masked. pub fn settle(self, saved: u32) { self.0.try_update_untracked(|state| { state.sent.retain(|batch| batch.saved_as.is_none_or(|n| n > saved)); }); } /// The list's items with the changes not saved or not fetched yet. pub fn apply(self, list_id: i64, items: &[Item]) -> Vec { let mut items = items.to_vec(); self.0.with(|state| { let ops = state.sent.iter().flat_map(|batch| &batch.ops).chain(&state.queue); for op in ops { apply(&mut items, list_id, op); } }); items } /// The last attempt failed for lack of network; changes are kept. pub fn offline(self) -> bool { self.0.with(|state| state.retries > 0) } fn flush(self, ctx: Ctx) { let ops = self.0.try_update(|state| { state.scheduled = false; if state.sending || state.queue.is_empty() { return None; } let ops = std::mem::take(&mut state.queue); state.sent.push(Batch { ops: ops.clone(), saved_as: None }); state.sending = true; Some(ops) }); let Some(Some(ops)) = ops else { return }; spawn_local(async move { let result = crate::api::sync_items(ops).await; self.answered(ctx, result); }); } fn answered(self, ctx: Ctx, result: Result) { let outcome = self.0.try_update(|state| { state.sending = false; let mut batch = state.sent.pop().expect("the batch sent is the last one"); match result { Ok(report) => { state.saved += 1; batch.saved_as = Some(state.saved); state.sent.push(batch); state.resolve(&report.created); state.retries = 0; Outcome::Saved(report) } // Refused by the server: retrying would not help. Err(err @ ServerFnError::ServerError(_)) => { state.retries = 0; Outcome::Refused(err) } // Not delivered, or the answer was lost: send it again later. Err(_) => { state.queue.splice(0..0, batch.ops); state.retries += 1; let delay = Duration::from_secs(1 << state.retries.min(5)).min(MAX_RETRY_DELAY); Outcome::Retry(delay) } } }); match outcome { None => {} Some(Outcome::Saved(report)) => { if let Some(msg) = report.rejected { ctx.error.set(Some(msg)); } ctx.refresh.update(|n| *n += 1); self.flush(ctx); } Some(Outcome::Refused(err)) => { ctx.report(err); ctx.refresh.update(|n| *n += 1); self.flush(ctx); } Some(Outcome::Retry(delay)) => set_timeout(move || self.flush(ctx), delay), } } } impl State { /// Queues the change, merged with the queued ones for the same item. fn push(&mut self, mut op: ItemOp) { if let ItemOp::Add { id, .. } = &mut op { self.last_temp -= 1; *id = self.last_temp; } else if let Some(&(_, real)) = self.ids.iter().find(|&&(temp, _)| temp == op.id()) { // Still open in an editor from before the item was saved. *op.id_mut() = real; } let id = op.id(); let queued_add = self.queue.iter().position(|q| matches!(q, ItemOp::Add { id: added, .. } if *added == id)); match (op, queued_add) { (ItemOp::Delete { .. }, Some(_)) => self.queue.retain(|q| q.id() != id), (ItemOp::Update { input, .. }, Some(index)) => { if let ItemOp::Add { input: added, .. } = &mut self.queue[index] { *added = input; } } (op @ ItemOp::Delete { .. }, None) => { self.queue.retain(|q| q.id() != id); self.queue.push(op); } (op @ (ItemOp::Update { .. } | ItemOp::SetChecked { .. }), _) => { let same_kind = std::mem::discriminant(&op); self.queue.retain(|q| q.id() != id || std::mem::discriminant(q) != same_kind); self.queue.push(op); } (op, _) => self.queue.push(op), } } /// Swaps temporary ids for the ones the server gave. fn resolve(&mut self, created: &[(i64, i64)]) { let ops = self.queue.iter_mut().chain(self.sent.iter_mut().flat_map(|batch| &mut batch.ops)); for op in ops { if let Some(&(_, real)) = created.iter().find(|&&(temp, _)| temp == op.id()) { *op.id_mut() = real; } } self.ids.extend_from_slice(created); } } fn apply(items: &mut Vec, list_id: i64, op: &ItemOp) { fn find(items: &mut [Item], id: i64) -> Option<&mut Item> { items.iter_mut().find(|item| item.id == id) } match op { ItemOp::Add { id, list_id: list, input } => { if *list == list_id && !items.iter().any(|item| item.id == *id) { items.push(Item { id: *id, list_id, name: input.name.clone(), quantity: input.quantity, icon: input.icon, category: input.category, checked: false, }); } } ItemOp::Update { id, input } => { if let Some(item) = find(items, *id) { item.name = input.name.clone(); item.quantity = input.quantity; item.icon = input.icon; item.category = input.category; } } ItemOp::Delete { id } => items.retain(|item| item.id != *id), ItemOp::SetChecked { id, checked } => { if let Some(item) = find(items, *id) { item.checked = *checked; } } } } #[cfg(test)] mod tests { use super::*; use crate::model::{Category, Icon, ItemInput}; fn input(name: &str) -> ItemInput { ItemInput { name: name.into(), quantity: None, icon: Icon::Basket, category: Category::Autre } } fn add(name: &str) -> ItemOp { ItemOp::Add { id: 0, list_id: 1, input: input(name) } } #[test] fn merges_queued_changes() { let mut state = State::default(); state.push(ItemOp::SetChecked { id: 5, checked: true }); state.push(ItemOp::SetChecked { id: 5, checked: false }); state.push(add("Pain")); state.push(ItemOp::Update { id: -1, input: input("Pain complet") }); assert_eq!( state.queue, [ ItemOp::SetChecked { id: 5, checked: false }, ItemOp::Add { id: -1, list_id: 1, input: input("Pain complet") }, ] ); state.push(ItemOp::Delete { id: -1 }); state.push(ItemOp::Delete { id: 5 }); assert_eq!(state.queue, [ItemOp::Delete { id: 5 }]); } #[test] fn resolves_temporary_ids() { let mut state = State::default(); state.push(add("Pain")); state.sent.push(Batch { ops: std::mem::take(&mut state.queue), saved_as: None }); state.push(ItemOp::SetChecked { id: -1, checked: true }); state.resolve(&[(-1, 42)]); assert_eq!(state.queue, [ItemOp::SetChecked { id: 42, checked: true }]); assert_eq!(state.sent[0].ops[0].id(), 42); // An editor opened on the temporary item. state.push(ItemOp::Delete { id: -1 }); assert_eq!(state.queue, [ItemOp::Delete { id: 42 }]); } #[test] fn applies_changes_to_fetched_items() { let item = Item { id: 7, list_id: 1, name: "Lait".into(), quantity: None, icon: Icon::Milk, category: Category::Frais, checked: false, }; let mut items = vec![item]; apply(&mut items, 1, &ItemOp::SetChecked { id: 7, checked: true }); apply(&mut items, 1, &ItemOp::Add { id: -1, list_id: 1, input: input("Pain") }); apply(&mut items, 1, &ItemOp::Add { id: -2, list_id: 2, input: input("Ailleurs") }); assert!(items[0].checked); assert_eq!(items.len(), 2); // Already in the fetched data. apply(&mut items, 1, &ItemOp::Add { id: 7, list_id: 1, input: input("Lait") }); apply(&mut items, 1, &ItemOp::Delete { id: -1 }); assert_eq!(items.len(), 1); } }