use crate::cacheable::Cacheable;
use crate::error::FetchError;
#[cfg(not(target_arch = "wasm32"))]
use async_trait::async_trait;
use tokio::sync::{mpsc, oneshot, watch};
pub struct RefreshHandle {
pub(crate) cancel: watch::Sender<bool>,
pub(crate) trigger: mpsc::Sender<oneshot::Sender<Result<(), FetchError>>>,
}
impl RefreshHandle {
pub fn cancel(&self) {
let _ = self.cancel.send(true);
}
pub async fn refresh_now(&self) -> Result<(), FetchError> {
let (reply_tx, reply_rx) = oneshot::channel();
self.trigger
.send(reply_tx)
.await
.map_err(|_| FetchError::Serialization("refresh task stopped".to_owned()))?;
reply_rx.await.map_err(|_| {
FetchError::Serialization("refresh task stopped before replying".to_owned())
})?
}
}
impl Drop for RefreshHandle {
fn drop(&mut self) {
let _ = self.cancel.send(true);
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RefreshMode {
UpsertOnly,
Replace,
}
#[cfg(not(target_arch = "wasm32"))]
#[async_trait]
pub trait PunnuFetcher<T: Cacheable>: Send + Sync + 'static {
async fn fetch(&self) -> Result<Vec<T>, FetchError>;
}
#[cfg(target_arch = "wasm32")]
#[async_trait::async_trait(?Send)]
pub trait PunnuFetcher<T: Cacheable>: 'static {
async fn fetch(&self) -> Result<Vec<T>, FetchError>;
}