Skip to content

Commit

Permalink
Create effects within a reactivity root (#37)
Browse files Browse the repository at this point in the history
* cargo fmt

* Show error when creating an effect outside of a root

* Make render accept a FnOnce() -> TemplateResult

* create_root function (placeholder)

* Move signals into sub-module

* Move effects into sub-module

* Make Dependency a weak backlink

* Clear dependencies in Running::drop

* Make CONTEXTS use Weak references

* Make running owned by Owner

* Update examples to render from a component

* Destroy effects when Owner is dropped

* Add test for dropping effects when owner is dropped

* Change criterion noise threshold
  • Loading branch information
lukechu10 authored Mar 12, 2021
1 parent 5325c62 commit be748ed
Show file tree
Hide file tree
Showing 11 changed files with 804 additions and 610 deletions.
2 changes: 1 addition & 1 deletion docs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ fn main() {
}
};

render(root);
render(|| root);
}
18 changes: 10 additions & 8 deletions examples/components/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use maple_core::prelude::*;

pub fn MyComponent(num: StateHandle<i32>) -> TemplateResult {
fn MyComponent(num: StateHandle<i32>) -> TemplateResult {
template! {
div(class="my-component") {
# "My component"
Expand All @@ -14,17 +14,14 @@ pub fn MyComponent(num: StateHandle<i32>) -> TemplateResult {
}
}

fn main() {
console_error_panic_hook::set_once();
console_log::init_with_level(log::Level::Debug).unwrap();

fn App() -> TemplateResult {
let state = Signal::new(1);

let increment = cloned!((state) => move |_| {
state.set(*state.get() + 1);
});

let root = template! {
template! {
div {
h1 {
# "Component demo"
Expand All @@ -37,7 +34,12 @@ fn main() {
# "Increment"
}
}
};
}
}

fn main() {
console_error_panic_hook::set_once();
console_log::init_with_level(log::Level::Debug).unwrap();

render(root);
render(|| template! { App() });
}
18 changes: 11 additions & 7 deletions examples/counter/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use maple_core::prelude::*;
#![allow(non_snake_case)]

fn main() {
console_error_panic_hook::set_once();
console_log::init_with_level(log::Level::Debug).unwrap();
use maple_core::prelude::*;

fn App() -> TemplateResult {
let counter = Signal::new(0);

create_effect(cloned!((counter) => move || {
Expand All @@ -14,7 +13,7 @@ fn main() {

let reset = cloned!((counter) => move |_| counter.set(0));

let root = template! {
template! {
div {
# "Counter demo"
p(class="value") {
Expand All @@ -28,7 +27,12 @@ fn main() {
# "Reset"
}
}
};
}
}

fn main() {
console_error_panic_hook::set_once();
console_log::init_with_level(log::Level::Debug).unwrap();

render(root);
render(|| template! { App() });
}
16 changes: 9 additions & 7 deletions examples/hello/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use maple_core::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::{Event, HtmlInputElement};

fn main() {
console_error_panic_hook::set_once();
console_log::init_with_level(log::Level::Debug).unwrap();

fn App() -> TemplateResult {
let name = Signal::new(String::new());

let displayed_name = create_memo(cloned!((name) => move || {
Expand All @@ -29,7 +26,7 @@ fn main() {
);
};

let root = template! {
template! {
div {
h1 {
# "Hello "
Expand All @@ -39,7 +36,12 @@ fn main() {

input(on:input=handle_change)
}
};
}
}

fn main() {
console_error_panic_hook::set_once();
console_log::init_with_level(log::Level::Debug).unwrap();

render(root);
render(|| template! { App() });
}
1 change: 1 addition & 0 deletions maple-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ wasm-bindgen = "0.2.71"

[dependencies.web-sys]
features = [
"console",
"Document",
"Element",
"Event",
Expand Down
8 changes: 6 additions & 2 deletions maple-core/benches/reactivity.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use maple_core::prelude::*;

pub fn criterion_benchmark(c: &mut Criterion) {
pub fn bench(c: &mut Criterion) {
c.bench_function("reactivity signals run get/set 1000x", |b| {
b.iter(|| {
let state = Signal::new(black_box(0));
Expand All @@ -27,5 +27,9 @@ pub fn criterion_benchmark(c: &mut Criterion) {
});
}

criterion_group!(benches, criterion_benchmark);
criterion_group! {
name = benches;
config = Criterion::default().noise_threshold(0.05 /* noisy CI */);
targets = bench
}
criterion_main!(benches);
30 changes: 21 additions & 9 deletions maple-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,41 @@
//! ## Supported Targets
//! - `wasm32-unknown-unknown`
#[doc(hidden)]
pub mod macros;
#[doc(hidden)]
pub mod internal;
#[doc(hidden)]
pub mod macros;
pub mod reactive;

use web_sys::HtmlElement;

use std::cell::RefCell;
use std::rc::Rc;

/// The result of the `template!` macro. Should not be used directly.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TemplateResult {
element: HtmlElement,
}

/// Render an [`HtmlElement`] into the DOM.
pub fn render(template_result: TemplateResult) {
/// Render a [`TemplateResult`] into the DOM.
pub fn render(template_result: impl FnOnce() -> TemplateResult + 'static) {
let window = web_sys::window().unwrap();
let document = window.document().unwrap();
document
.body()
.unwrap()
.append_child(&template_result.element)
.unwrap();

let owner = reactive::create_root(move || {
document
.body()
.unwrap()
.append_child(&template_result().element)
.unwrap();
});

thread_local! {
static GLOBAL_OWNERS: RefCell<Vec<Rc<RefCell<reactive::Owner>>>> = RefCell::new(Vec::new());
}

GLOBAL_OWNERS.with(|global_owners| global_owners.borrow_mut().push(owner));
}

impl TemplateResult {
Expand Down
4 changes: 2 additions & 2 deletions maple-core/src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! Definition of `cloned!` macro. Proc-macros are defined in the separate `maple-core-macro` crate.
/// Utility macro for cloning all the arguments and expanding the expression.
///
///
/// Temporary workaround for [Rust RFC #2407](https://github.com/rust-lang/rfcs/issues/2407).
///
/// # Example
/// ```
/// use maple_core::prelude::*;
///
///
/// let state = Signal::new(0);
///
/// create_effect(cloned!((state) => move || {
Expand Down
Loading

1 comment on commit be748ed

@vercel
Copy link

@vercel vercel bot commented on be748ed Mar 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.