fix server_cached function

This commit is contained in:
Evan Almloff 2023-07-17 09:48:35 -07:00
parent 040b9d15a8
commit b05fb16155
4 changed files with 21 additions and 8 deletions

View file

@ -1,3 +1,5 @@
#![allow(unused)]
use std::sync::atomic::AtomicUsize;
use serde::{de::DeserializeOwned, Serialize};
@ -34,11 +36,22 @@ impl HTMLDataCursor {
pub fn take<T: DeserializeOwned>(&self) -> Option<T> {
let current = self.index.load(std::sync::atomic::Ordering::SeqCst);
if current >= self.data.len() {
log::error!(
"Tried to take more data than was available, len: {}, index: {}",
self.data.len(),
current
);
return None;
}
let mut cursor = &self.data[current];
self.index.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
Some(postcard::from_bytes(&mut cursor).unwrap())
match postcard::from_bytes(&mut cursor) {
Ok(x) => Some(x),
Err(e) => {
log::error!("Error deserializing data: {:?}", e);
None
}
}
}
}

View file

@ -154,7 +154,7 @@ impl<'a, P: Clone + Serialize + Send + Sync + 'static> dioxus_ssr::incremental::
crate::html_storage::serialize::encode_props_in_element(&self.cfg.props, to)?;
// serialize the server state
crate::html_storage::serialize::encode_in_element(
&*self.server_context.html_data().map_err(|err| {
&*self.server_context.html_data().map_err(|_| {
dioxus_ssr::incremental::IncrementalRendererError::Other(Box::new({
#[derive(Debug)]
struct HTMLDataReadError;

View file

@ -106,7 +106,7 @@ mod server_fn_impl {
}
/// Insert some data into the html data store
pub(crate) async fn push_html_data<T: serde::Serialize>(
pub(crate) fn push_html_data<T: serde::Serialize>(
&self,
value: &T,
) -> Result<(), PoisonError<RwLockWriteGuard<'_, HTMLData>>> {

View file

@ -20,15 +20,15 @@ use serde::{de::DeserializeOwned, Serialize};
pub fn from_server<O: 'static + Serialize + DeserializeOwned>(server_fn: impl Fn() -> O) -> O {
#[cfg(feature = "ssr")]
{
let data =
crate::html_storage::deserialize::take_server_data().unwrap_or_else(|| server_fn());
let data = server_fn();
let sc = crate::prelude::server_context();
sc.push_html_data(&data);
if let Err(err) = sc.push_html_data(&data) {
log::error!("Failed to push HTML data: {}", err);
}
data
}
#[cfg(not(feature = "ssr"))]
{
let data = server_fn();
data
crate::html_storage::deserialize::take_server_data().unwrap_or_else(|| server_fn())
}
}