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