wip: more refactor for async

This commit is contained in:
Jonathan Kelley committed 2021-07-09 12:47:41 -04:00
1 parent 8cfc437bfe
commit 975fa566f9
18 files changed
+110 -122

No files matched your search

+13 -38
View File
@@ -7,7 +7,9 @@ use std::pin::Pin;
use dioxus::prelude::*;
use futures::Future;
fn main() {
dioxus::web::launch(App)
env_logger::init();
log::info!("hello world");
dioxus::desktop::launch(App, |c| c).expect("faield to launch");
}
#[derive(serde::Deserialize)]
@@ -17,58 +19,31 @@ struct DogApi {
const ENDPOINT: &str = "https://dog.ceo/api/breeds/image/random";
struct Ex(Pin<Box<dyn Future<Output = ()> + 'static>>);
static App: FC<()> = |cx| {
// let mut count = use_state(cx, || 0);
let mut fut = cx.use_hook(
move || {
Box::pin(async {
Ex(Box::pin(async {
//
loop {
// repeatadly get new doggos
match surf::get(ENDPOINT).recv_json::<DogApi>().await {
Ok(_) => (),
Err(_) => (),
// Ok(res) => rsx!(in cx, img { src: "{res.message}" }),
// Err(_) => rsx!(in cx, div { "No doggos for you :(" }),
}
// wait one seconds
}
}) as Pin<Box<dyn Future<Output = ()> + 'static>>
})
as Pin<Box<dyn Future<Output = ()> + 'static>>)
},
|h| h,
|h| &mut h.0,
|_| {},
);
cx.submit_task(fut);
todo!()
// cx.render(rsx! {
// div {
// h1 { "Hifive counter: {count}" }
// button { onclick: move |_| count += 1, "Up high!" }
// button { onclick: move |_| count -= 1, "Down low!" }
// }
// })
cx.render(rsx! {
div {
}
})
};
// #[derive(serde::Deserialize)]
// struct DogApi {
// message: String,
// }
// const ENDPOINT: &str = "https://dog.ceo/api/breeds/image/random";
// pub static App: FC<()> = |cx| {
// let doggo = use_future_effect(&cx, move || async move {
// match surf::get(ENDPOINT).recv_json::<DogApi>().await {
// Ok(res) => rsx!(in cx, img { src: "{res.message}" }),
// Err(_) => rsx!(in cx, div { "No doggos for you :(" }),
// }
// });
// cx.render(rsx!(
// div {
// h1 {"Waiting for a doggo..."}
// {doggo}
// }
// ))
// };
+11 -12
View File
@@ -26,19 +26,18 @@ static App: FC<()> = |cx| {
let clear_display = display_value.eq("0");
let clear_text = if clear_display { "C" } else { "AC" };
let input_digit =
move |num: u8| display_value.modify(move |f| f.push_str(num.to_string().as_str()));
let input_digit = move |num: u8| display_value.get_mut().push_str(num.to_string().as_str());
let input_dot = move || display_value.modify(move |f| f.push_str("."));
let input_dot = move || display_value.get_mut().push_str(".");
let perform_operation = move || {
if let Some(op) = operator.as_ref() {
let rhs = display_value.parse::<f64>().unwrap();
let new_val = match op {
Operator::Add => **cur_val + rhs,
Operator::Sub => **cur_val - rhs,
Operator::Mul => **cur_val * rhs,
Operator::Div => **cur_val / rhs,
Operator::Add => *cur_val + rhs,
Operator::Sub => *cur_val - rhs,
Operator::Mul => *cur_val * rhs,
Operator::Div => *cur_val / rhs,
};
cur_val.set(new_val);
display_value.set(new_val.to_string());
@@ -50,7 +49,7 @@ static App: FC<()> = |cx| {
if display_value.starts_with("-") {
display_value.set(display_value.trim_start_matches("-").to_string())
} else {
display_value.set(format!("-{}", **display_value))
display_value.set(format!("-{}", *display_value))
}
};
let toggle_percent = move |_| todo!();
@@ -64,11 +63,11 @@ static App: FC<()> = |cx| {
};
let keydownhandler = move |evt: KeyboardEvent| match evt.key_code() {
KeyCode::Backspace => display_value.modify(|f| {
if !f.as_str().eq("0") {
f.pop();
KeyCode::Backspace => {
if !display_value.as_str().eq("0") {
display_value.get_mut().pop();
}
}),
}
KeyCode::_0 => input_digit(0),
KeyCode::_1 => input_digit(1),
KeyCode::_2 => input_digit(2),
+1 -1
View File
@@ -2,7 +2,7 @@ use dioxus::prelude::*;
fn main() {}
static Example: FC<()> = |cx| {
let (g, set_g) = use_state_classic(cx, || 0);
let (g, set_g) = use_state(cx, || 0).classic();
let v = (0..10).map(move |f| {
rsx!(li {
onclick: move |_| set_g(10)
+3 -3
View File
@@ -36,9 +36,9 @@ enum Operator {
}
static App: FC<()> = |cx| {
let (cur_val, set_cur_val) = use_state_classic(cx, || 0.0_f64);
let (operator, set_operator) = use_state_classic(cx, || None as Option<Operator>);
let (display_value, set_display_value) = use_state_classic(cx, || "0".to_string());
let (cur_val, set_cur_val) = use_state(cx, || 0.0_f64).classic();
let (operator, set_operator) = use_state(cx, || None as Option<Operator>).classic();
let (display_value, set_display_value) = use_state(cx, || "0".to_string()).classic();
let clear_display = display_value.eq("0");
let clear_text = if clear_display { "C" } else { "AC" };
+5 -3
View File
@@ -1,5 +1,6 @@
//! Example: Reducer Pattern
//! -----------------
//!
//! This example shows how to encapsulate sate in dioxus components with the reducer pattern.
//! This pattern is very useful when a single component can handle many types of input that can
//! be represented by an enum.
@@ -10,7 +11,7 @@ fn main() {
}
pub static App: FC<()> = |cx| {
let (state, reduce) = use_reducer(cx, PlayerState::new, PlayerState::reduce);
let state = use_state(cx, PlayerState::new);
let is_playing = state.is_playing();
@@ -20,11 +21,11 @@ pub static App: FC<()> = |cx| {
h3 {"The radio is... {is_playing}!"}
button {
"Pause"
onclick: move |_| reduce(PlayerAction::Pause)
onclick: move |_| state.get_mut().reduce(PlayerAction::Pause)
}
button {
"Play"
onclick: move |_| reduce(PlayerAction::Play)
onclick: move |_| state.get_mut().reduce(PlayerAction::Play)
}
}
})
@@ -35,6 +36,7 @@ enum PlayerAction {
Play,
}
#[derive(Clone)]
struct PlayerState {
is_playing: bool,
}