use core::fmt;
use core::hash::Hash;
use core::time::Duration;
use std::rc::Rc;
use web_time::Instant;
use super::query::{Query, QueryState};
use super::runtime::MaybeSend;
use super::{context, hash};
pub struct Live<T, E> {
current: Option<(u64, Query<T, E>)>,
previous: Option<Rc<T>>,
keep_previous: bool,
debounce: Option<Duration>,
wanted: Option<(u64, Instant)>,
}
pub fn live<T: MaybeSend + 'static, E: MaybeSend + 'static>() -> Live<T, E> {
Live::default()
}
impl<T, E> Default for Live<T, E> {
fn default() -> Self {
Self {
current: None,
previous: None,
keep_previous: false,
debounce: None,
wanted: None,
}
}
}
impl<T: MaybeSend + 'static, E: MaybeSend + 'static> Live<T, E> {
#[must_use]
pub fn keep_previous(mut self) -> Self {
self.keep_previous = true;
self
}
#[must_use]
pub fn debounce(mut self, after: Duration) -> Self {
self.debounce = Some(after);
self
}
pub fn update(&mut self, inputs: impl Hash, make: impl FnOnce() -> Query<T, E>) -> &Self {
let inputs = hash(inputs);
if let Some((_, query)) = &self.current
&& self.previous.is_some()
{
let (value, failed) = query.peek();
if value.is_some() || failed {
self.previous = None;
}
}
if self
.current
.as_ref()
.is_some_and(|(made, _)| *made == inputs)
{
self.wanted = None;
return self;
}
if let Some(after) = self.debounce {
let now = Instant::now();
let since = match self.wanted {
Some((wanted, since)) if wanted == inputs => since,
_ => {
self.wanted = Some((inputs, now));
now
}
};
let left = after.saturating_sub(now.saturating_duration_since(since));
if !left.is_zero() {
if let Some(ctx) = context() {
ctx.request_repaint_after(left);
}
return self;
}
}
self.wanted = None;
let query = make();
if let Some((_, old)) = self.current.replace((inputs, query))
&& self.keep_previous
&& let Some(value) = old.peek().0
{
self.previous = Some(value);
}
self
}
pub fn clear(&mut self) {
self.current = None;
self.previous = None;
self.wanted = None;
}
pub fn query(&self) -> Option<&Query<T, E>> {
self.current.as_ref().map(|(_, query)| query)
}
pub fn state(&self) -> QueryState<T, E> {
let Some(query) = self.query() else {
return QueryState::Loading;
};
match (query.state(), &self.previous) {
(QueryState::Loading, Some(previous)) => QueryState::Ready(Rc::clone(previous)),
(state, _) => state,
}
}
pub fn data(&self) -> Option<Rc<T>> {
let query = self.query()?;
query.data().or_else(|| {
query
.error()
.is_none()
.then(|| self.previous.clone())
.flatten()
})
}
pub fn error(&self) -> Option<Rc<E>> {
self.query()?.error()
}
pub fn is_fetching(&self) -> bool {
self.query().is_some_and(Query::is_fetching)
}
pub fn is_previous(&self) -> bool {
self.wanted.is_some()
|| self.previous.is_some()
&& self
.query()
.is_some_and(|query| query.data().is_none() && query.error().is_none())
}
}
impl<T: fmt::Debug, E: fmt::Debug> fmt::Debug for Live<T, E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Live")
.field("query", &self.current.as_ref().map(|(_, query)| query))
.field("previous", &self.previous)
.field("waiting", &self.wanted.is_some())
.finish_non_exhaustive()
}
}