use core::any::{Any, TypeId};
use core::cell::{Cell, Ref, RefCell};
use core::fmt;
use core::future::Future;
use core::time::Duration;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use egui::{AsId, Id};
use web_time::Instant;
use super::runtime::{self, Abort, BoxFuture, MaybeSend};
use super::{CACHE_FOR, Entry, context, lock, with_registry};
use crate::load::Invalidation;
type Slot<T, E> = Arc<Mutex<Option<Result<T, E>>>>;
struct Run<T, E> {
slot: Slot<T, E>,
_task: Abort,
}
struct State<T, E> {
value: Option<Rc<T>>,
error: Option<Rc<E>>,
version: u64,
fetched_at: Option<Instant>,
run: Option<Run<T, E>>,
stale: bool,
depends: Vec<String>,
read_at: Instant,
cache_for: Duration,
}
impl<T: 'static, E: 'static> Entry for RefCell<State<T, E>> {
fn invalidate(&self, invalidation: &Invalidation) {
let mut state = self.borrow_mut();
if invalidation.hits(&state.depends) {
state.stale = true;
state.run = None;
}
}
fn expired(&self, now: Instant) -> bool {
let state = self.borrow();
now.saturating_duration_since(state.read_at) >= state.cache_for
}
fn as_any(self: Rc<Self>) -> Rc<dyn Any> {
self
}
}
#[derive(Debug)]
pub enum QueryState<T, E> {
Loading,
Ready(Rc<T>),
Failed(Rc<E>),
}
pub struct Query<T, E> {
state: Rc<RefCell<State<T, E>>>,
fetch: Rc<dyn Fn() -> BoxFuture<Result<T, E>>>,
refetch_every: Option<Duration>,
stale_after: Duration,
cache_for: Option<Duration>,
read: Cell<bool>,
}
pub fn query<T, E, F, Fut>(fetch: F) -> Query<T, E>
where
T: MaybeSend + 'static,
E: MaybeSend + 'static,
F: Fn() -> Fut + 'static,
Fut: Future<Output = Result<T, E>> + MaybeSend + 'static,
{
Query::new(fetch)
}
impl<T: MaybeSend + 'static, E: MaybeSend + 'static> Query<T, E> {
pub fn new<F, Fut>(fetch: F) -> Self
where
F: Fn() -> Fut + 'static,
Fut: Future<Output = Result<T, E>> + MaybeSend + 'static,
{
let state = Rc::new(RefCell::new(State {
value: None,
error: None,
version: 0,
fetched_at: None,
run: None,
stale: false,
depends: Vec::new(),
read_at: Instant::now(),
cache_for: CACHE_FOR,
}));
let entry: Rc<dyn Entry> = Rc::clone(&state) as _;
with_registry(|registry| registry.entries.push(Rc::downgrade(&entry)));
Self {
state,
fetch: Rc::new(move || Box::pin(fetch())),
refetch_every: None,
stale_after: Duration::ZERO,
cache_for: None,
read: Cell::new(false),
}
}
#[must_use]
pub fn key(mut self, key: impl AsId) -> Self {
let id = (TypeId::of::<(T, E)>(), Id::new(key));
let shared = with_registry(|registry| {
if let Some(entry) = registry.cache.get(&id) {
return Rc::clone(entry).as_any().downcast().ok();
}
registry
.cache
.insert(id, Rc::clone(&self.state) as Rc<dyn Entry>);
None
});
if let Some(shared) = shared {
let depends = core::mem::take(&mut self.state.borrow_mut().depends);
self.state = shared;
for key in depends {
self = self.depends(key);
}
}
self.apply_cache_for();
self
}
#[must_use]
pub fn depends(self, key: impl Into<String>) -> Self {
let key = key.into();
let mut state = self.state.borrow_mut();
if !state.depends.contains(&key) {
state.depends.push(key);
}
drop(state);
self
}
#[must_use]
pub fn refetch_every(mut self, every: Duration) -> Self {
self.refetch_every = Some(every);
self
}
#[must_use]
pub fn stale_after(mut self, duration: Duration) -> Self {
self.stale_after = duration;
self
}
#[must_use]
pub fn cache_for(mut self, duration: Duration) -> Self {
self.cache_for = Some(duration);
self.apply_cache_for();
self
}
fn apply_cache_for(&self) {
if let Some(duration) = self.cache_for {
self.state.borrow_mut().cache_for = duration;
}
}
pub fn state(&self) -> QueryState<T, E> {
let state = self.read();
if let Some(error) = &state.error {
QueryState::Failed(Rc::clone(error))
} else if let Some(value) = &state.value {
QueryState::Ready(Rc::clone(value))
} else {
QueryState::Loading
}
}
pub fn data(&self) -> Option<Rc<T>> {
self.read().value.clone()
}
pub fn error(&self) -> Option<Rc<E>> {
self.read().error.clone()
}
pub fn is_loading(&self) -> bool {
let state = self.read();
state.value.is_none() && state.run.is_some()
}
pub fn is_fetching(&self) -> bool {
self.read().run.is_some()
}
pub fn version(&self) -> u64 {
self.read().version
}
pub fn refresh(&self) {
let mut state = self.state.borrow_mut();
state.stale = true;
state.run = None;
drop(state);
if let Some(ctx) = context() {
ctx.request_repaint();
}
}
pub fn set_data(&self, value: T) {
let mut state = self.state.borrow_mut();
state.value = Some(Rc::new(value));
state.error = None;
state.version += 1;
}
fn read(&self) -> Ref<'_, State<T, E>> {
let now = Instant::now();
let mut state = self.state.borrow_mut();
state.read_at = now;
if let Some(result) = state.run.as_ref().and_then(|run| lock(&run.slot).take()) {
state.run = None;
state.fetched_at = Some(now);
match result {
Ok(value) => {
state.value = Some(Rc::new(value));
state.error = None;
state.version += 1;
}
Err(error) => state.error = Some(Rc::new(error)),
}
}
if state.run.is_none() && self.due(&state, now) {
state.stale = false;
state.run = Some(self.start());
}
drop(state);
self.read.set(true);
self.state.borrow()
}
fn due(&self, state: &State<T, E>, now: Instant) -> bool {
let Some(fetched_at) = state.fetched_at else {
return true;
};
let age = now.saturating_duration_since(fetched_at);
if state.stale || (!self.read.get() && age >= self.stale_after) {
return true;
}
match self.refetch_every {
Some(every) if age >= every => true,
Some(every) => {
if let Some(ctx) = context() {
ctx.request_repaint_after(every.saturating_sub(age));
}
false
}
None => false,
}
}
fn start(&self) -> Run<T, E> {
let slot: Slot<T, E> = Arc::default();
let future = (self.fetch)();
let ctx = context();
let task = runtime::spawn({
let slot = Arc::clone(&slot);
async move {
let result = future.await;
*lock(&slot) = Some(result);
if let Some(ctx) = ctx {
ctx.request_repaint();
}
}
});
Run { slot, _task: task }
}
}
impl<T, E> Clone for Query<T, E> {
fn clone(&self) -> Self {
Self {
state: Rc::clone(&self.state),
fetch: Rc::clone(&self.fetch),
refetch_every: self.refetch_every,
stale_after: self.stale_after,
cache_for: self.cache_for,
read: self.read.clone(),
}
}
}
impl<T: fmt::Debug, E: fmt::Debug> fmt::Debug for Query<T, E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let state = self.state.borrow();
f.debug_struct("Query")
.field("value", &state.value)
.field("error", &state.error)
.field("version", &state.version)
.field("fetching", &state.run.is_some())
.finish_non_exhaustive()
}
}