use crate::{transport::SerializeContextEntry, Transportable};
use dioxus_core::use_hook;
#[track_caller]
pub fn use_server_cached<O, M>(server_fn: impl Fn() -> O) -> O
where
O: Transportable<M> + Clone,
M: 'static,
{
let location = std::panic::Location::caller();
use_hook(|| server_cached(server_fn, location))
}
pub(crate) fn server_cached<O, M>(
value: impl FnOnce() -> O,
#[allow(unused)] location: &'static std::panic::Location<'static>,
) -> O
where
O: Transportable<M> + Clone,
M: 'static,
{
let serialize = crate::transport::serialize_context();
#[allow(unused)]
let entry: SerializeContextEntry<O> = serialize.create_entry();
#[cfg(feature = "server")]
{
let data = value();
entry.insert(&data, location);
data
}
#[cfg(all(not(feature = "server"), feature = "web"))]
{
match entry.get() {
Ok(value) => value,
Err(_) => value(),
}
}
#[cfg(not(any(feature = "server", feature = "web")))]
{
value()
}
}