Skip to content

Commit

Permalink
improved documentation and refactored naming to be more self-explanatory
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbehmenburg committed Dec 7, 2023
1 parent e4d60c8 commit adfbb6c
Show file tree
Hide file tree
Showing 91 changed files with 1,670 additions and 1,560 deletions.
17 changes: 9 additions & 8 deletions examples/all_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ const RECT_STYLE: &str = r#"
fn app(cx: Scope) -> Element {
let events = use_ref(cx, std::collections::VecDeque::new);

let log_event = move |event: Event| {
let mut events = events.write();

if events.len() >= MAX_EVENTS {
events.pop_front();
}
events.push_back(event);
};
let log_event =
move |event: Event| {
let mut events = events.write();

if events.len() >= MAX_EVENTS {
events.pop_front();
}
events.push_back(event);
};

cx.render(rsx! (
div {
Expand Down
19 changes: 10 additions & 9 deletions examples/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ fn main() {
}

fn app(cx: Scope) -> Element {
let login = use_callback!(cx, move |_| async move {
let res = reqwest::get("https://dog.ceo/api/breeds/list/all")
.await
.unwrap()
.text()
.await
.unwrap();
let login =
use_callback!(cx, move |_| async move {
let res = reqwest::get("https://dog.ceo/api/breeds/list/all")
.await
.unwrap()
.text()
.await
.unwrap();

println!("{res:#?}, ");
});
println!("{res:#?}, ");
});

cx.render(rsx! {
button { onclick: login, "Click me!" }
Expand Down
15 changes: 8 additions & 7 deletions examples/compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ fn app(cx: Scope) -> Element {
let window = use_window(cx);
let emails_sent = use_ref(cx, Vec::new);

let tx = use_coroutine(cx, |mut rx: UnboundedReceiver<String>| {
to_owned![emails_sent];
async move {
while let Some(message) = rx.next().await {
emails_sent.write().push(message);
let tx =
use_coroutine(cx, |mut rx: UnboundedReceiver<String>| {
to_owned![emails_sent];
async move {
while let Some(message) = rx.next().await {
emails_sent.write().push(message);
}
}
}
});
});

cx.render(rsx! {
div {
Expand Down
9 changes: 5 additions & 4 deletions examples/error_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ fn main() {
fn App(cx: Scope) -> Element {
let val = use_state(cx, || "0.0001");

let num = match val.parse::<f32>() {
Err(_) => return cx.render(rsx!("Parsing failed")),
Ok(num) => num,
};
let num =
match val.parse::<f32>() {
Err(_) => return cx.render(rsx!("Parsing failed")),
Ok(num) => num,
};

cx.render(rsx! {
h1 { "The parsed value is {num}" }
Expand Down
27 changes: 14 additions & 13 deletions examples/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@ fn main() {
fn app(cx: Scope) -> Element {
let eval_provider = use_eval(cx);

let future = use_future(cx, (), |_| {
to_owned![eval_provider];
async move {
let eval = eval_provider(
r#"
let future =
use_future(cx, (), |_| {
to_owned![eval_provider];
async move {
let eval = eval_provider(
r#"
dioxus.send("Hi from JS!");
let msg = await dioxus.recv();
console.log(msg);
return "hello world";
"#,
)
.unwrap();
)
.unwrap();

eval.send("Hi from Rust!".into()).unwrap();
let res = eval.recv().await.unwrap();
println!("{:?}", eval.await);
res
}
});
eval.send("Hi from Rust!".into()).unwrap();
let res = eval.recv().await.unwrap();
println!("{:?}", eval.await);
res
}
});

match future.value() {
Some(v) => cx.render(rsx!(
Expand Down
11 changes: 6 additions & 5 deletions examples/file_explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ struct Files {

impl Files {
fn new() -> Self {
let mut files = Self {
path_stack: vec!["./".to_string()],
path_names: vec![],
err: None,
};
let mut files =
Self {
path_stack: vec!["./".to_string()],
path_names: vec![],
err: None,
};

files.reload_path_list();

Expand Down
55 changes: 28 additions & 27 deletions examples/framework_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,33 +112,34 @@ fn ActionButton<'a>(cx: Scope<'a, ActionButtonProps<'a>>) -> Element {
})
}

static ADJECTIVES: &[&str] = &[
"pretty",
"large",
"big",
"small",
"tall",
"short",
"long",
"handsome",
"plain",
"quaint",
"clean",
"elegant",
"easy",
"angry",
"crazy",
"helpful",
"mushy",
"odd",
"unsightly",
"adorable",
"important",
"inexpensive",
"cheap",
"expensive",
"fancy",
];
static ADJECTIVES: &[&str] =
&[
"pretty",
"large",
"big",
"small",
"tall",
"short",
"long",
"handsome",
"plain",
"quaint",
"clean",
"elegant",
"easy",
"angry",
"crazy",
"helpful",
"mushy",
"odd",
"unsightly",
"adorable",
"important",
"inexpensive",
"cheap",
"expensive",
"fancy",
];

static COLOURS: &[&str] = &[
"red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black",
Expand Down
42 changes: 23 additions & 19 deletions examples/login_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,32 @@ fn main() {
}

fn app(cx: Scope) -> Element {
let onsubmit = move |evt: FormEvent| {
cx.spawn(async move {
let resp = reqwest::Client::new()
.post("http://localhost:8080/login")
.form(&[
("username", &evt.values["username"]),
("password", &evt.values["password"]),
])
.send()
.await;
let onsubmit =
move |evt: FormEvent| {
cx.spawn(async move {
let resp =
reqwest::Client::new()
.post("http://localhost:8080/login")
.form(&[
("username", &evt.values["username"]),
("password", &evt.values["password"]),
])
.send()
.await;

match resp {
// Parse data from here, such as storing a response token
Ok(_data) => println!("Login successful!"),
match resp {
// Parse data from here, such as storing a response token
Ok(_data) => println!("Login successful!"),

//Handle any errors from the fetch here
Err(_err) => {
println!("Login failed - you need a login server running on localhost:8080.")
//Handle any errors from the fetch here
Err(_err) => {
println!(
"Login failed - you need a login server running on localhost:8080."
)
}
}
}
});
};
});
};

cx.render(rsx! {
h1 { "Login" }
Expand Down
11 changes: 6 additions & 5 deletions examples/openid_connect_demo/src/oidc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,12 @@ pub async fn exchange_refresh_token(

pub async fn log_out_url(id_token_hint: CoreIdToken) -> Result<Url, crate::errors::Error> {
let provider_metadata = init_provider_metadata().await?;
let end_session_url = provider_metadata
.additional_metadata()
.clone()
.end_session_endpoint
.unwrap();
let end_session_url =
provider_metadata
.additional_metadata()
.clone()
.end_session_endpoint
.unwrap();
let logout_request: LogoutRequest = LogoutRequest::from(end_session_url);
Ok(logout_request
.set_client_id(ClientId::new(DIOXUS_FRONT_CLIENT_ID.to_string()))
Expand Down
40 changes: 21 additions & 19 deletions examples/openid_connect_demo/src/views/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,31 @@ pub fn LogOut(cx: Scope<ClientProps>) -> Element {
Some(fermi_auth_token_read) => match fermi_auth_token_read.id_token.clone() {
Some(id_token) => match log_out_url_state.get() {
Some(log_out_url_result) => match log_out_url_result {
Some(uri) => match uri {
Ok(uri) => {
rsx! {
Link {
onclick: move |_| {
{
AuthTokenState::persistent_set(
fermi_auth_token,
Some(AuthTokenState::default()),
);
}
},
to: uri.to_string(),
"Log out"
Some(uri) => {
match uri {
Ok(uri) => {
rsx! {
Link {
onclick: move |_| {
{
AuthTokenState::persistent_set(
fermi_auth_token,
Some(AuthTokenState::default()),
);
}
},
to: uri.to_string(),
"Log out"
}
}
}
}
Err(error) => {
rsx! {
div { format!{"Failed to load disconnection url: {:?}", error} }
Err(error) => {
rsx! {
div { format!{"Failed to load disconnection url: {:?}", error} }
}
}
}
},
}
None => {
rsx! { div { "Loading... Please wait" } }
}
Expand Down
28 changes: 15 additions & 13 deletions examples/rsx_compile_fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ fn main() {
}

fn example(cx: Scope) -> Element {
let items = use_state(cx, || {
vec![Thing {
a: "asd".to_string(),
b: 10,
}]
});

let things = use_ref(cx, || {
vec![Thing {
a: "asd".to_string(),
b: 10,
}]
});
let items =
use_state(cx, || {
vec![Thing {
a: "asd".to_string(),
b: 10,
}]
});

let things =
use_ref(cx, || {
vec![Thing {
a: "asd".to_string(),
b: 10,
}]
});
let things_list = things.read();

let mything = use_ref(cx, || Some(String::from("asd")));
Expand Down
Loading

0 comments on commit adfbb6c

Please sign in to comment.