dioxus_fullstack_hooks/hooks/
server_cached.rs1use dioxus_core::use_hook;
2use dioxus_fullstack_protocol::SerializeContextEntry;
3use serde::{de::DeserializeOwned, Serialize};
4
5#[track_caller]
24pub fn use_server_cached<O: 'static + Clone + Serialize + DeserializeOwned>(
25 server_fn: impl Fn() -> O,
26) -> O {
27 let location = std::panic::Location::caller();
28 use_hook(|| server_cached(server_fn, location))
29}
30
31pub(crate) fn server_cached<O: 'static + Clone + Serialize + DeserializeOwned>(
32 value: impl FnOnce() -> O,
33 #[allow(unused)] location: &'static std::panic::Location<'static>,
34) -> O {
35 let serialize = dioxus_fullstack_protocol::serialize_context();
36 #[allow(unused)]
37 let entry: SerializeContextEntry<O> = serialize.create_entry();
38 #[cfg(feature = "server")]
39 {
40 let data = value();
41 entry.insert(&data, location);
42 data
43 }
44 #[cfg(all(not(feature = "server"), feature = "web"))]
45 {
46 match entry.get() {
47 Ok(value) => value,
48 Err(_) => value(),
49 }
50 }
51 #[cfg(not(any(feature = "server", feature = "web")))]
52 {
53 value()
54 }
55}