mirror of
https://github.com/DioxusLabs/dioxus
synced 2025-02-18 14:48:26 +00:00
fix use_server_future
This commit is contained in:
parent
e3a759ba38
commit
8e54a89a74
9 changed files with 141 additions and 65 deletions
|
@ -18,6 +18,7 @@ tower-http = { version = "0.4.1", features = ["auth"] }
|
||||||
simple_logger = "4.2.0"
|
simple_logger = "4.2.0"
|
||||||
wasm-logger = "0.2.0"
|
wasm-logger = "0.2.0"
|
||||||
log.workspace = true
|
log.workspace = true
|
||||||
|
reqwest = "0.11.18"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
|
|
|
@ -23,16 +23,13 @@ fn app(cx: Scope<AppProps>) -> Element {
|
||||||
|
|
||||||
fn Child(cx: Scope) -> Element {
|
fn Child(cx: Scope) -> Element {
|
||||||
let state = use_server_future(cx, (), |()| async move {
|
let state = use_server_future(cx, (), |()| async move {
|
||||||
#[cfg(not(feature = "ssr"))]
|
loop {
|
||||||
panic!();
|
if let Ok(res) = get_server_data().await {
|
||||||
#[cfg(feature = "ssr")]
|
break res;
|
||||||
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
}
|
||||||
return 1;
|
}
|
||||||
})?;
|
})?
|
||||||
|
.value();
|
||||||
log::info!("running child");
|
|
||||||
let state = state.value();
|
|
||||||
log::info!("child state: {:?}", state);
|
|
||||||
|
|
||||||
let mut count = use_state(cx, || 0);
|
let mut count = use_state(cx, || 0);
|
||||||
let text = use_state(cx, || "...".to_string());
|
let text = use_state(cx, || "...".to_string());
|
||||||
|
@ -72,12 +69,12 @@ async fn post_server_data(data: String) -> Result<(), ServerFnError> {
|
||||||
|
|
||||||
#[server(GetServerData)]
|
#[server(GetServerData)]
|
||||||
async fn get_server_data() -> Result<String, ServerFnError> {
|
async fn get_server_data() -> Result<String, ServerFnError> {
|
||||||
Ok("Hello from the server!".to_string())
|
Ok(reqwest::get("https://httpbin.org/ip").await?.text().await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
#[cfg(feature = "web")]
|
#[cfg(feature = "web")]
|
||||||
wasm_logger::init(wasm_logger::Config::default());
|
wasm_logger::init(wasm_logger::Config::new(log::Level::Trace));
|
||||||
#[cfg(feature = "ssr")]
|
#[cfg(feature = "ssr")]
|
||||||
simple_logger::SimpleLogger::new().init().unwrap();
|
simple_logger::SimpleLogger::new().init().unwrap();
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,10 @@ tokio = { workspace = true, features = ["full"], optional = true }
|
||||||
serde = "1.0.159"
|
serde = "1.0.159"
|
||||||
salvo = { version = "0.37.9", optional = true }
|
salvo = { version = "0.37.9", optional = true }
|
||||||
execute = "0.2.12"
|
execute = "0.2.12"
|
||||||
|
reqwest = "0.11.18"
|
||||||
|
simple_logger = "4.2.0"
|
||||||
|
log.workspace = true
|
||||||
|
wasm-logger = "0.2.0"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
|
|
|
@ -7,25 +7,37 @@
|
||||||
|
|
||||||
#![allow(non_snake_case, unused)]
|
#![allow(non_snake_case, unused)]
|
||||||
use dioxus::prelude::*;
|
use dioxus::prelude::*;
|
||||||
use dioxus_fullstack::prelude::*;
|
use dioxus_fullstack::{launch, prelude::*};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
fn main() {
|
|
||||||
launch!(@([127, 0, 0, 1], 8080), app, (AppProps { count: 5 }), {
|
|
||||||
incremental: IncrementalRendererConfig::default().invalidate_after(std::time::Duration::from_secs(120)),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Props, PartialEq, Debug, Default, Serialize, Deserialize, Clone)]
|
#[derive(Props, PartialEq, Debug, Default, Serialize, Deserialize, Clone)]
|
||||||
struct AppProps {
|
struct AppProps {
|
||||||
count: i32,
|
count: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn app(cx: Scope<AppProps>) -> Element {
|
fn app(cx: Scope<AppProps>) -> Element {
|
||||||
let mut count = use_state(cx, || cx.props.count);
|
render! {
|
||||||
|
Child {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn Child(cx: Scope) -> Element {
|
||||||
|
let state = use_server_future(cx, (), |()| async move {
|
||||||
|
loop {
|
||||||
|
if let Ok(res) = get_server_data().await {
|
||||||
|
break res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})?
|
||||||
|
.value();
|
||||||
|
|
||||||
|
let mut count = use_state(cx, || 0);
|
||||||
let text = use_state(cx, || "...".to_string());
|
let text = use_state(cx, || "...".to_string());
|
||||||
|
|
||||||
cx.render(rsx! {
|
cx.render(rsx! {
|
||||||
|
div {
|
||||||
|
"Server state: {state}"
|
||||||
|
}
|
||||||
h1 { "High-Five counter: {count}" }
|
h1 { "High-Five counter: {count}" }
|
||||||
button { onclick: move |_| count += 1, "Up high!" }
|
button { onclick: move |_| count += 1, "Up high!" }
|
||||||
button { onclick: move |_| count -= 1, "Down low!" }
|
button { onclick: move |_| count -= 1, "Down low!" }
|
||||||
|
@ -40,7 +52,7 @@ fn app(cx: Scope<AppProps>) -> Element {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Run a server function"
|
"Run a server function!"
|
||||||
}
|
}
|
||||||
"Server said: {text}"
|
"Server said: {text}"
|
||||||
})
|
})
|
||||||
|
@ -48,15 +60,23 @@ fn app(cx: Scope<AppProps>) -> Element {
|
||||||
|
|
||||||
#[server(PostServerData)]
|
#[server(PostServerData)]
|
||||||
async fn post_server_data(data: String) -> Result<(), ServerFnError> {
|
async fn post_server_data(data: String) -> Result<(), ServerFnError> {
|
||||||
// The server context contains information about the current request and allows you to modify the response.
|
|
||||||
let cx = server_context();
|
|
||||||
println!("Server received: {}", data);
|
println!("Server received: {}", data);
|
||||||
println!("Request parts are {:?}", cx.request_parts());
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[server(GetServerData)]
|
#[server(GetServerData)]
|
||||||
async fn get_server_data() -> Result<String, ServerFnError> {
|
async fn get_server_data() -> Result<String, ServerFnError> {
|
||||||
Ok("Hello from the server!".to_string())
|
Ok(reqwest::get("https://httpbin.org/ip").await?.text().await?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
#[cfg(feature = "web")]
|
||||||
|
wasm_logger::init(wasm_logger::Config::new(log::Level::Trace));
|
||||||
|
#[cfg(feature = "ssr")]
|
||||||
|
simple_logger::SimpleLogger::new().init().unwrap();
|
||||||
|
|
||||||
|
launch!(@([127, 0, 0, 1], 8080), app, {
|
||||||
|
serve_cfg: ServeConfigBuilder::new(app, AppProps { count: 0 }),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,10 @@ tokio = { workspace = true, features = ["full"], optional = true }
|
||||||
serde = "1.0.159"
|
serde = "1.0.159"
|
||||||
warp = { version = "0.3.3", optional = true }
|
warp = { version = "0.3.3", optional = true }
|
||||||
execute = "0.2.12"
|
execute = "0.2.12"
|
||||||
|
reqwest = "0.11.18"
|
||||||
|
simple_logger = "4.2.0"
|
||||||
|
log.workspace = true
|
||||||
|
wasm-logger = "0.2.0"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
|
|
|
@ -7,25 +7,37 @@
|
||||||
|
|
||||||
#![allow(non_snake_case, unused)]
|
#![allow(non_snake_case, unused)]
|
||||||
use dioxus::prelude::*;
|
use dioxus::prelude::*;
|
||||||
use dioxus_fullstack::prelude::*;
|
use dioxus_fullstack::{launch, prelude::*};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
fn main() {
|
|
||||||
launch!(@([127, 0, 0, 1], 8080), app, (AppProps { count: 5 }), {
|
|
||||||
incremental: IncrementalRendererConfig::default().invalidate_after(std::time::Duration::from_secs(120)),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Props, PartialEq, Debug, Default, Serialize, Deserialize, Clone)]
|
#[derive(Props, PartialEq, Debug, Default, Serialize, Deserialize, Clone)]
|
||||||
struct AppProps {
|
struct AppProps {
|
||||||
count: i32,
|
count: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn app(cx: Scope<AppProps>) -> Element {
|
fn app(cx: Scope<AppProps>) -> Element {
|
||||||
let mut count = use_state(cx, || cx.props.count);
|
render! {
|
||||||
|
Child {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn Child(cx: Scope) -> Element {
|
||||||
|
let state = use_server_future(cx, (), |()| async move {
|
||||||
|
loop {
|
||||||
|
if let Ok(res) = get_server_data().await {
|
||||||
|
break res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})?
|
||||||
|
.value();
|
||||||
|
|
||||||
|
let mut count = use_state(cx, || 0);
|
||||||
let text = use_state(cx, || "...".to_string());
|
let text = use_state(cx, || "...".to_string());
|
||||||
|
|
||||||
cx.render(rsx! {
|
cx.render(rsx! {
|
||||||
|
div {
|
||||||
|
"Server state: {state}"
|
||||||
|
}
|
||||||
h1 { "High-Five counter: {count}" }
|
h1 { "High-Five counter: {count}" }
|
||||||
button { onclick: move |_| count += 1, "Up high!" }
|
button { onclick: move |_| count += 1, "Up high!" }
|
||||||
button { onclick: move |_| count -= 1, "Down low!" }
|
button { onclick: move |_| count -= 1, "Down low!" }
|
||||||
|
@ -40,7 +52,7 @@ fn app(cx: Scope<AppProps>) -> Element {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Run a server function"
|
"Run a server function!"
|
||||||
}
|
}
|
||||||
"Server said: {text}"
|
"Server said: {text}"
|
||||||
})
|
})
|
||||||
|
@ -48,15 +60,23 @@ fn app(cx: Scope<AppProps>) -> Element {
|
||||||
|
|
||||||
#[server(PostServerData)]
|
#[server(PostServerData)]
|
||||||
async fn post_server_data(data: String) -> Result<(), ServerFnError> {
|
async fn post_server_data(data: String) -> Result<(), ServerFnError> {
|
||||||
// The server context contains information about the current request and allows you to modify the response.
|
|
||||||
let cx = server_context();
|
|
||||||
println!("Server received: {}", data);
|
println!("Server received: {}", data);
|
||||||
println!("Request parts are {:?}", cx.request_parts());
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[server(GetServerData)]
|
#[server(GetServerData)]
|
||||||
async fn get_server_data() -> Result<String, ServerFnError> {
|
async fn get_server_data() -> Result<String, ServerFnError> {
|
||||||
Ok("Hello from the server!".to_string())
|
Ok(reqwest::get("https://httpbin.org/ip").await?.text().await?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
#[cfg(feature = "web")]
|
||||||
|
wasm_logger::init(wasm_logger::Config::new(log::Level::Trace));
|
||||||
|
#[cfg(feature = "ssr")]
|
||||||
|
simple_logger::SimpleLogger::new().init().unwrap();
|
||||||
|
|
||||||
|
launch!(@([127, 0, 0, 1], 8080), app, {
|
||||||
|
serve_cfg: ServeConfigBuilder::new(app, AppProps { count: 0 }),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ use std::any::Any;
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::cell::Ref;
|
use std::cell::Ref;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
use std::fmt::Debug;
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
@ -27,7 +28,7 @@ pub fn use_server_future<T, F, D>(
|
||||||
future: impl FnOnce(D::Out) -> F,
|
future: impl FnOnce(D::Out) -> F,
|
||||||
) -> Option<&UseServerFuture<T>>
|
) -> Option<&UseServerFuture<T>>
|
||||||
where
|
where
|
||||||
T: 'static + Serialize + DeserializeOwned,
|
T: 'static + Serialize + DeserializeOwned + Debug,
|
||||||
F: Future<Output = T> + 'static,
|
F: Future<Output = T> + 'static,
|
||||||
D: UseFutureDep,
|
D: UseFutureDep,
|
||||||
{
|
{
|
||||||
|
@ -39,7 +40,24 @@ where
|
||||||
dependencies: Vec::new(),
|
dependencies: Vec::new(),
|
||||||
});
|
});
|
||||||
|
|
||||||
let first_run = { state.value.borrow().as_ref().is_none() };
|
let first_run = { state.value.borrow().as_ref().is_none() && state.task.get().is_none() };
|
||||||
|
|
||||||
|
#[cfg(not(feature = "ssr"))]
|
||||||
|
{
|
||||||
|
if first_run {
|
||||||
|
match crate::html_storage::deserialize::take_server_data() {
|
||||||
|
Some(data) => {
|
||||||
|
log::trace!("Loaded {data:?} from server");
|
||||||
|
*state.value.borrow_mut() = Some(Box::new(data));
|
||||||
|
state.needs_regen.set(false);
|
||||||
|
return Some(state);
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
log::trace!("Failed to load from server... running future");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if dependencies.clone().apply(&mut state.dependencies) || state.needs_regen.get() {
|
if dependencies.clone().apply(&mut state.dependencies) || state.needs_regen.get() {
|
||||||
// We don't need regen anymore
|
// We don't need regen anymore
|
||||||
|
@ -70,10 +88,7 @@ where
|
||||||
}
|
}
|
||||||
#[cfg(not(feature = "ssr"))]
|
#[cfg(not(feature = "ssr"))]
|
||||||
{
|
{
|
||||||
data = match crate::html_storage::deserialize::take_server_data() {
|
data = fut.await;
|
||||||
Some(data) => data,
|
|
||||||
None => fut.await,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
*value.borrow_mut() = Some(Box::new(data));
|
*value.borrow_mut() = Some(Box::new(data));
|
||||||
|
|
||||||
|
@ -82,20 +97,17 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
if first_run {
|
if first_run {
|
||||||
|
#[cfg(feature = "ssr")]
|
||||||
|
{
|
||||||
log::trace!("Suspending first run of use_server_future");
|
log::trace!("Suspending first run of use_server_future");
|
||||||
cx.suspend();
|
cx.suspend();
|
||||||
|
}
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(state)
|
Some(state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum FutureState<'a, T> {
|
|
||||||
Pending,
|
|
||||||
Complete(&'a T),
|
|
||||||
Regenerating(&'a T), // the old value
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct UseServerFuture<T> {
|
pub struct UseServerFuture<T> {
|
||||||
update: Arc<dyn Fn()>,
|
update: Arc<dyn Fn()>,
|
||||||
needs_regen: Cell<bool>,
|
needs_regen: Cell<bool>,
|
||||||
|
@ -104,12 +116,6 @@ pub struct UseServerFuture<T> {
|
||||||
value: Rc<RefCell<Option<Box<T>>>>,
|
value: Rc<RefCell<Option<Box<T>>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum UseFutureState<'a, T> {
|
|
||||||
Pending,
|
|
||||||
Complete(&'a T),
|
|
||||||
Reloading(&'a T),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> UseServerFuture<T> {
|
impl<T> UseServerFuture<T> {
|
||||||
/// Restart the future with new dependencies.
|
/// Restart the future with new dependencies.
|
||||||
///
|
///
|
||||||
|
|
|
@ -7,19 +7,42 @@ use super::HTMLDataCursor;
|
||||||
|
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub(crate) fn serde_from_bytes<T: DeserializeOwned>(string: &[u8]) -> Option<T> {
|
pub(crate) fn serde_from_bytes<T: DeserializeOwned>(string: &[u8]) -> Option<T> {
|
||||||
let decompressed = STANDARD.decode(string).ok()?;
|
let decompressed = match STANDARD.decode(string) {
|
||||||
|
Ok(bytes) => bytes,
|
||||||
|
Err(err) => {
|
||||||
|
log::error!("Failed to decode base64: {}", err);
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
postcard::from_bytes(&decompressed).ok()
|
match postcard::from_bytes(&decompressed) {
|
||||||
|
Ok(data) => Some(data),
|
||||||
|
Err(err) => {
|
||||||
|
log::error!("Failed to deserialize: {}", err);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static SERVER_DATA: once_cell::sync::Lazy<Option<HTMLDataCursor>> =
|
static SERVER_DATA: once_cell::sync::Lazy<Option<HTMLDataCursor>> =
|
||||||
once_cell::sync::Lazy::new(|| {
|
once_cell::sync::Lazy::new(|| {
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
{
|
{
|
||||||
let attribute = web_sys::window()?
|
let window = web_sys::window()?.document()?;
|
||||||
.document()?
|
let element = match window.get_element_by_id("dioxus-storage-data") {
|
||||||
.get_element_by_id("dioxus-storage-data")?
|
Some(element) => element,
|
||||||
.get_attribute("data-serialized")?;
|
None => {
|
||||||
|
log::error!("Failed to get element by id: dioxus-storage-data");
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let attribute = match element.get_attribute("data-serialized") {
|
||||||
|
Some(attribute) => attribute,
|
||||||
|
None => {
|
||||||
|
log::error!("Failed to get attribute: data-serialized");
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let data: super::HTMLData = serde_from_bytes(attribute.as_bytes())?;
|
let data: super::HTMLData = serde_from_bytes(attribute.as_bytes())?;
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ use serde::Serialize;
|
||||||
use std::sync::RwLock;
|
use std::sync::RwLock;
|
||||||
use tokio::task::spawn_blocking;
|
use tokio::task::spawn_blocking;
|
||||||
|
|
||||||
use crate::{prelude::*, server_context::with_server_context};
|
use crate::prelude::*;
|
||||||
use dioxus::prelude::*;
|
use dioxus::prelude::*;
|
||||||
|
|
||||||
enum SsrRendererPool {
|
enum SsrRendererPool {
|
||||||
|
@ -300,6 +300,7 @@ impl<P: Clone + Serialize + Send + Sync + 'static> dioxus_ssr::incremental::Wrap
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A rendered response from the server.
|
/// A rendered response from the server.
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct RenderResponse {
|
pub struct RenderResponse {
|
||||||
pub(crate) html: String,
|
pub(crate) html: String,
|
||||||
pub(crate) freshness: RenderFreshness,
|
pub(crate) freshness: RenderFreshness,
|
||||||
|
|
Loading…
Add table
Reference in a new issue