use std::{fmt, future::Future, pin::Pin, sync::Arc, time::Duration};
use super::{QueryError, QueryKey, TaskCx};
pub type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send + 'static>>;
pub(crate) type Fetcher<T> = Arc<dyn Fn(TaskCx) -> BoxFuture<Result<T, QueryError>> + Send + Sync>;
pub struct QueryOptions<T> {
pub(crate) key: QueryKey,
pub(crate) fetch: Fetcher<T>,
pub(crate) stale_time: Duration,
pub(crate) gc_time: Duration,
pub(crate) retry: u32,
pub(crate) retry_delay: Duration,
pub(crate) enabled: bool,
pub(crate) initial_data: Option<T>,
pub(crate) placeholder_data: Option<T>,
pub(crate) keep_previous_data: bool,
pub(crate) refetch_interval: Option<Duration>,
pub(crate) refetch_on_window_focus: bool,
}
impl<T: Send + 'static> QueryOptions<T> {
pub fn new<F, Fut, E>(key: QueryKey, fetch: F) -> Self
where
F: Fn(TaskCx) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<T, E>> + Send + 'static,
E: Into<QueryError>,
{
Self {
key,
fetch: Arc::new(move |cx| {
let future = fetch(cx);
Box::pin(async move { future.await.map_err(Into::into) })
}),
stale_time: Duration::ZERO,
gc_time: Duration::from_secs(5 * 60),
retry: 0,
retry_delay: Duration::from_millis(500),
enabled: true,
initial_data: None,
placeholder_data: None,
keep_previous_data: false,
refetch_interval: None,
refetch_on_window_focus: false,
}
}
}
impl<T> QueryOptions<T> {
#[must_use]
pub fn key(&self) -> &QueryKey {
&self.key
}
#[must_use]
pub fn stale_time(mut self, stale_time: Duration) -> Self {
self.stale_time = stale_time;
self
}
#[must_use]
pub fn gc_time(mut self, gc_time: Duration) -> Self {
self.gc_time = gc_time;
self
}
#[must_use]
pub fn retry(mut self, retries: u32) -> Self {
self.retry = retries;
self
}
#[must_use]
pub fn retry_delay(mut self, delay: Duration) -> Self {
self.retry_delay = delay;
self
}
#[must_use]
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
#[must_use]
pub fn initial_data(mut self, data: T) -> Self {
self.initial_data = Some(data);
self
}
#[must_use]
pub fn placeholder_data(mut self, data: T) -> Self {
self.placeholder_data = Some(data);
self
}
#[must_use]
pub fn keep_previous_data(mut self, keep: bool) -> Self {
self.keep_previous_data = keep;
self
}
#[must_use]
pub fn refetch_on_window_focus(mut self, refetch: bool) -> Self {
self.refetch_on_window_focus = refetch;
self
}
#[must_use]
pub fn refetch_interval(mut self, interval: Duration) -> Self {
self.refetch_interval = Some(interval);
self
}
}
impl<T: Clone> Clone for QueryOptions<T> {
fn clone(&self) -> Self {
Self {
key: self.key.clone(),
fetch: self.fetch.clone(),
stale_time: self.stale_time,
gc_time: self.gc_time,
retry: self.retry,
retry_delay: self.retry_delay,
enabled: self.enabled,
initial_data: self.initial_data.clone(),
placeholder_data: self.placeholder_data.clone(),
keep_previous_data: self.keep_previous_data,
refetch_interval: self.refetch_interval,
refetch_on_window_focus: self.refetch_on_window_focus,
}
}
}
impl<T> fmt::Debug for QueryOptions<T> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("QueryOptions")
.field("key", &self.key)
.field("stale_time", &self.stale_time)
.field("gc_time", &self.gc_time)
.field("retry", &self.retry)
.field("enabled", &self.enabled)
.finish_non_exhaustive()
}
}