Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace functions with Signal type #20

Merged
merged 4 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions examples/components/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn MyComponent(num: StateHandle<i32>) -> TemplateResult {
# "My component"
p {
# "Value: "
# num()
# num.get()
}
}
}
Expand All @@ -18,13 +18,12 @@ fn main() {
console_error_panic_hook::set_once();
console_log::init_with_level(log::Level::Debug).unwrap();

let (state, set_state) = create_signal(1);
let state = Signal::new(1);

let increment = {
let state = state.clone();
let set_state = set_state.clone();
move |_| {
set_state(*state() + 1);
state.set(*state.get() + 1);
}
};

Expand All @@ -34,8 +33,8 @@ fn main() {
# "Component demo"
}

MyComponent(state.clone())
MyComponent(state.clone())
MyComponent(state.handle())
MyComponent(state.handle())

button(on:click=increment) {
# "Increment"
Expand Down
15 changes: 6 additions & 9 deletions examples/counter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,31 @@ fn main() {
console_error_panic_hook::set_once();
console_log::init_with_level(log::Level::Debug).unwrap();

let (counter, set_counter) = create_signal(0);
let counter = Signal::new(0);

create_effect({
let counter = counter.clone();
move || {
log::info!("Counter value: {}", *counter());
log::info!("Counter value: {}", *counter.get());
}
});

let increment = {
let counter = counter.clone();
let set_counter = set_counter.clone();

move |_| set_counter(*counter() + 1)
move |_| counter.set(*counter.get() + 1)
};

let reset = {
let set_counter = set_counter.clone();

move |_| set_counter(0)
let counter = counter.clone();
move |_| counter.set(0)
};

let root = template! {
div {
# "Counter demo"
p(class="value") {
# "Value: "
# counter()
# counter.get()
}
button(class="increment", on:click=increment) {
# "Increment"
Expand Down
21 changes: 12 additions & 9 deletions examples/hello/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ fn main() {
console_error_panic_hook::set_once();
console_log::init_with_level(log::Level::Debug).unwrap();

let (name, set_name) = create_signal(String::new());

let displayed_name = create_memo(move || {
if *name() == "" {
"World".to_string()
} else {
name().as_ref().clone()
let name = Signal::new(String::new());

let displayed_name = create_memo({
let name = name.clone();
move || {
if name.get().is_empty() {
"World".to_string()
} else {
name.get().as_ref().clone()
}
}
});

let handle_change = move |event: Event| {
set_name(
name.set(
event
.target()
.unwrap()
Expand All @@ -33,7 +36,7 @@ fn main() {
div {
h1 {
# "Hello "
# displayed_name()
# displayed_name.get()
# "!"
}

Expand Down
5 changes: 1 addition & 4 deletions maple-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ impl TemplateResult {

/// The maple prelude.
pub mod prelude {
pub use crate::reactive::{
create_effect, create_memo, create_selector, create_signal, untracked, SetStateHandle,
StateHandle,
};
pub use crate::reactive::{create_effect, create_memo, create_selector, Signal, StateHandle};
pub use crate::{render, TemplateResult};

pub use maple_core_macro::template;
Expand Down
Loading