2
0
Fork 0
mirror of https://github.com/DioxusLabs/dioxus synced 2024-12-26 20:43:08 +00:00
dioxus/packages/signals/examples/split_subscriptions.rs

143 lines
3 KiB
Rust
Raw Normal View History

2023-08-08 01:20:03 +00:00
#![allow(non_snake_case)]
use dioxus::prelude::*;
use dioxus_signals::Signal;
fn main() {
dioxus_desktop::launch(app);
}
#[derive(Clone, Copy, Default)]
struct ApplicationData {
first_data: Signal<i32>,
second_data: Signal<i32>,
many_signals: Signal<Vec<Signal<i32>>>,
}
fn use_app_data() -> ApplicationData {
2023-08-08 01:20:03 +00:00
*use_context(cx).unwrap()
}
fn app() -> Element {
use_context_provider(ApplicationData::default);
2023-08-08 01:20:03 +00:00
2024-01-16 19:18:46 +00:00
rsx! {
div { ReadsFirst {} }
div { ReadsSecond {} }
div { ReadsManySignals {} }
2023-08-08 01:20:03 +00:00
}
}
fn ReadsFirst() -> Element {
2023-08-08 01:20:03 +00:00
println!("running first");
let data = use_app_data(cx);
2024-01-16 19:18:46 +00:00
rsx! {
2023-08-08 01:20:03 +00:00
button {
onclick: move |_| {
*data.first_data.write() += 1;
},
"Increase"
}
button {
onclick: move |_| {
*data.first_data.write() -= 1;
},
"Decrease"
}
button {
onclick: move |_| {
*data.first_data.write() = 0;
},
"Reset"
}
"{data.first_data}"
}
}
fn ReadsSecond() -> Element {
2023-08-08 01:20:03 +00:00
println!("running second");
let data = use_app_data(cx);
2024-01-16 19:18:46 +00:00
rsx! {
2023-08-08 01:20:03 +00:00
button {
onclick: move |_| {
*data.second_data.write() += 1;
},
"Increase"
}
button {
onclick: move |_| {
*data.second_data.write() -= 1;
},
"Decrease"
}
button {
onclick: move |_| {
*data.second_data.write() = 0;
},
"Reset"
}
"{data.second_data}"
}
}
fn ReadsManySignals() -> Element {
2023-08-08 01:20:03 +00:00
println!("running many signals");
let data = use_app_data(cx);
2024-01-16 19:18:46 +00:00
rsx! {
2023-08-08 01:20:03 +00:00
button {
onclick: move |_| {
data.many_signals.write().push(Signal::new(0));
},
"Create"
}
button {
onclick: move |_| {
data.many_signals.write().pop();
},
"Destroy"
}
button {
onclick: move |_| {
if let Some(first) = data.many_signals.read().first() {
2023-08-08 01:20:03 +00:00
*first.write() += 1;
}
},
"Increase First Item"
}
for signal in data.many_signals {
2023-12-23 19:57:49 +00:00
Child { count: *signal }
2023-08-08 01:20:03 +00:00
}
}
}
#[derive(Props, PartialEq)]
struct ChildProps {
count: Signal<i32>,
}
fn Child(cx: Scope<ChildProps>) -> Element {
println!("running child");
let count = cx.props.count;
2024-01-16 19:18:46 +00:00
rsx! {
2023-08-08 01:20:03 +00:00
div {
"Child: {count}"
button {
onclick: move |_| {
*count.write() += 1;
},
"Increase"
}
button {
onclick: move |_| {
*count.write() -= 1;
},
"Decrease"
}
}
}
}