fix use_server_future
This commit is contained in:
1 parent
e3a759ba38
commit
8e54a89a74
9 files changed
+141
-65
No files matched your search
@@ -18,6 +18,7 @@ tower-http = { version = "0.4.1", features = ["auth"] }
|
||||
simple_logger = "4.2.0"
|
||||
wasm-logger = "0.2.0"
|
||||
log.workspace = true
|
||||
reqwest = "0.11.18"
|
||||
|
||||
[features]
|
||||
default = []
|
||||
|
||||
@@ -23,16 +23,13 @@ fn app(cx: Scope<AppProps>) -> Element {
|
||||
|
||||
fn Child(cx: Scope) -> Element {
|
||||
let state = use_server_future(cx, (), |()| async move {
|
||||
#[cfg(not(feature = "ssr"))]
|
||||
panic!();
|
||||
#[cfg(feature = "ssr")]
|
||||
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
return 1;
|
||||
})?;
|
||||
|
||||
log::info!("running child");
|
||||
let state = state.value();
|
||||
log::info!("child state: {:?}", state);
|
||||
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());
|
||||
@@ -72,12 +69,12 @@ async fn post_server_data(data: String) -> Result<(), ServerFnError> {
|
||||
|
||||
#[server(GetServerData)]
|
||||
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::default());
|
||||
wasm_logger::init(wasm_logger::Config::new(log::Level::Trace));
|
||||
#[cfg(feature = "ssr")]
|
||||
simple_logger::SimpleLogger::new().init().unwrap();
|
||||
|
||||
|
||||
@@ -14,6 +14,10 @@ tokio = { workspace = true, features = ["full"], optional = true }
|
||||
serde = "1.0.159"
|
||||
salvo = { version = "0.37.9", optional = true }
|
||||
execute = "0.2.12"
|
||||
reqwest = "0.11.18"
|
||||
simple_logger = "4.2.0"
|
||||
log.workspace = true
|
||||
wasm-logger = "0.2.0"
|
||||
|
||||
[features]
|
||||
default = []
|
||||
|
||||
@@ -7,25 +7,37 @@
|
||||
|
||||
#![allow(non_snake_case, unused)]
|
||||
use dioxus::prelude::*;
|
||||
use dioxus_fullstack::prelude::*;
|
||||
use dioxus_fullstack::{launch, prelude::*};
|
||||
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)]
|
||||
struct AppProps {
|
||||
count: i32,
|
||||
}
|
||||
|
||||
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());
|
||||
|
||||
cx.render(rsx! {
|
||||
div {
|
||||
"Server state: {state}"
|
||||
}
|
||||
h1 { "High-Five counter: {count}" }
|
||||
button { onclick: move |_| count += 1, "Up high!" }
|
||||
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}"
|
||||
})
|
||||
@@ -48,15 +60,23 @@ fn app(cx: Scope<AppProps>) -> Element {
|
||||
|
||||
#[server(PostServerData)]
|
||||
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!("Request parts are {:?}", cx.request_parts());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[server(GetServerData)]
|
||||
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"
|
||||
warp = { version = "0.3.3", optional = true }
|
||||
execute = "0.2.12"
|
||||
reqwest = "0.11.18"
|
||||
simple_logger = "4.2.0"
|
||||
log.workspace = true
|
||||
wasm-logger = "0.2.0"
|
||||
|
||||
[features]
|
||||
default = []
|
||||
|
||||
@@ -7,25 +7,37 @@
|
||||
|
||||
#![allow(non_snake_case, unused)]
|
||||
use dioxus::prelude::*;
|
||||
use dioxus_fullstack::prelude::*;
|
||||
use dioxus_fullstack::{launch, prelude::*};
|
||||
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)]
|
||||
struct AppProps {
|
||||
count: i32,
|
||||
}
|
||||
|
||||
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());
|
||||
|
||||
cx.render(rsx! {
|
||||
div {
|
||||
"Server state: {state}"
|
||||
}
|
||||
h1 { "High-Five counter: {count}" }
|
||||
button { onclick: move |_| count += 1, "Up high!" }
|
||||
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}"
|
||||
})
|
||||
@@ -48,15 +60,23 @@ fn app(cx: Scope<AppProps>) -> Element {
|
||||
|
||||
#[server(PostServerData)]
|
||||
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!("Request parts are {:?}", cx.request_parts());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[server(GetServerData)]
|
||||
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 }),
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user