use crate::{QueryError, QueryKey, QueryOptions, RetryConfig};
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
pub type QueryFn<T> =
Arc<dyn Fn() -> Pin<Box<dyn Future<Output = Result<T, QueryError>> + Send>> + Send + Sync>;
pub struct Query<T: Clone + Send + Sync + 'static> {
pub key: QueryKey,
pub fetch_fn: QueryFn<T>,
pub options: QueryOptions,
}
impl<T: Clone + Send + Sync + 'static> Query<T> {
pub fn new<F, Fut>(key: QueryKey, fetch_fn: F) -> Self
where
F: Fn() -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<T, QueryError>> + Send + 'static,
{
Self {
key,
fetch_fn: Arc::new(move || Box::pin(fetch_fn())),
options: QueryOptions::default(),
}
}
pub fn stale_time(mut self, duration: std::time::Duration) -> Self {
self.options.stale_time = duration;
self
}
pub fn gc_time(mut self, duration: std::time::Duration) -> Self {
self.options.gc_time = duration;
self
}
pub fn retry(mut self, config: RetryConfig) -> Self {
self.options.retry = config;
self
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.options.enabled = enabled;
self
}
}