yew_api_hook/
lib.rs

1mod hooks;
2
3use async_trait::async_trait;
4
5#[cfg(feature = "cache")]
6use std::rc::Rc;
7#[cfg(feature = "cache")]
8use yewdux::store::Store;
9
10pub use hooks::*;
11
12pub mod prelude {
13    pub use crate::{hooks::*, Request};
14    pub use async_trait::async_trait;
15
16    #[cfg(feature = "cache")]
17    pub use crate::CachableRequest;
18    #[cfg(feature = "cache")]
19    pub use std::rc::Rc;
20    #[cfg(feature = "cache")]
21    pub use yewdux::store::Store;
22}
23
24/// The core request trait which has to be implemented for all handler
25/// which can be executed through the use api hook.
26#[async_trait(?Send)]
27pub trait Request: std::fmt::Debug + PartialEq + Clone {
28    /// The error which can occur on request failure
29    type Error: Clone + std::fmt::Debug + PartialEq + 'static;
30
31    /// The output type of a succesful request
32    type Output: Clone + std::fmt::Debug + PartialEq + 'static;
33
34    /// Run the asynchronous operation responsible for fetching or
35    /// computing the requested data
36    async fn run(&self) -> Result<Self::Output, Self::Error>;
37
38    /// Store or otherwise handle the data of a succesful run of the request
39    /// E.g. Store the requested data in a yewdux store
40    fn store(_: Self::Output) {}
41}
42
43/// A cachable request which may load a cached result from a yewdux store
44#[cfg(feature = "cache")]
45pub trait CachableRequest: Request {
46    /// The yewdux store used to load the cached result
47    type Store: Store + PartialEq;
48
49    /// Optionally extract the requested entity from the yewdux store
50    fn load(&self, store: Rc<Self::Store>) -> Option<Self::Output>;
51}