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

Remove render functions from prelude #140

Merged
merged 4 commits into from
Jul 2, 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
2 changes: 1 addition & 1 deletion .github/workflows/build_website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
cd "$path"
dist_dir="$output/example/$example"
mkdir "$dist_dir"
trunk build --release --dist "$dist_dir" --public-url "/example/$example"
trunk build --release --dist "$dist_dir" --public-url "/examples/$example"
)
done

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build_website_prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
cd "$path"
dist_dir="$output/example/$example"
mkdir "$dist_dir"
trunk build --release --dist "$dist_dir" --public-url "/example/$example"
trunk build --release --dist "$dist_dir" --public-url "/examples/$example"
)
done

Expand Down
4 changes: 2 additions & 2 deletions docs/markdown/getting_started/hello_world.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Here it is:
use sycamore::prelude::*;

fn main() {
render(|| template! {
sycamore::render(|| template! {
p { "Hello, World!" }
});
}
Expand All @@ -25,7 +25,7 @@ fn main() {
Nothing really special here. Trunk automatically uses `fn main` as your project's entrypoint. No need for any `#[wasm_bindgen(start)]` here.

```rust
render(...)
sycamore::render(...)
```

This function is provided by Sycamore and is used to render your app to the DOM (browser window). `render` accepts a closure (aka. lambda function) which should return a template to be rendered.
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ fn main() {
console_error_panic_hook::set_once();
console_log::init_with_level(log::Level::Debug).unwrap();

render(|| template! { App() });
sycamore::render(|| template! { App() });
}
2 changes: 1 addition & 1 deletion examples/components/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ fn main() {
console_error_panic_hook::set_once();
console_log::init_with_level(log::Level::Debug).unwrap();

render(|| template! { App() });
sycamore::render(|| template! { App() });
}
2 changes: 1 addition & 1 deletion examples/counter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ fn main() {
console_error_panic_hook::set_once();
console_log::init_with_level(log::Level::Debug).unwrap();

render(|| template! { App() });
sycamore::render(|| template! { App() });
}
2 changes: 1 addition & 1 deletion examples/hello/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ fn main() {
console_error_panic_hook::set_once();
console_log::init_with_level(log::Level::Debug).unwrap();

render(|| template! { App() });
sycamore::render(|| template! { App() });
}
2 changes: 1 addition & 1 deletion examples/iteration/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ fn main() {
console_error_panic_hook::set_once();
console_log::init_with_level(log::Level::Debug).unwrap();

render(|| template! { App() });
sycamore::render(|| template! { App() });
}
2 changes: 1 addition & 1 deletion examples/ssr/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ fn app() -> Template<G> {
}

fn main() {
let s = render_to_string(|| template! { App() });
let s = sycamore::render_to_string(|| template! { App() });
println!("{}", s);
}
2 changes: 1 addition & 1 deletion examples/todomvc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,5 @@ fn main() {
console_error_panic_hook::set_once();
console_log::init_with_level(log::Level::Debug).unwrap();

render(|| template! { App() });
sycamore::render(|| template! { App() });
}
2 changes: 1 addition & 1 deletion examples/tweened/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ fn main() {
console_error_panic_hook::set_once();
console_log::init_with_level(log::Level::Debug).unwrap();

render(|| template! { App() });
sycamore::render(|| template! { App() });
}
2 changes: 1 addition & 1 deletion packages/sycamore-macro/src/template/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl ToTokens for Element {
::sycamore::generic_node::render::insert(
&__el,
::sycamore::template::Template::new_lazy(move ||
::sycamore::render::IntoTemplate::create(&#text)
::sycamore::template::IntoTemplate::create(&#text)
),
None, __marker,
);
Expand Down
4 changes: 2 additions & 2 deletions packages/sycamore/benches/ssr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn bench(c: &mut Criterion) {
}
}

let _ssr = render_to_string(|| template! { App() });
let _ssr = sycamore::render_to_string(|| template! { App() });
})
});

Expand Down Expand Up @@ -48,7 +48,7 @@ pub fn bench(c: &mut Criterion) {
}
}

let _ssr = render_to_string(|| template! { App() });
let _ssr = sycamore::render_to_string(|| template! { App() });
})
});
}
Expand Down
17 changes: 9 additions & 8 deletions packages/sycamore/src/generic_node/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,17 @@ fn insert_expression<G: GenericNode>(
pub fn clean_children<G: GenericNode>(
parent: &G,
current: Vec<G>,
marker: Option<&G>,
_marker: Option<&G>,
replacement: Option<&G>,
) {
if marker == None {
parent.update_inner_text("");
if let Some(replacement) = replacement {
parent.append_child(replacement);
}
return;
}
// TODO: hot path for removing all children
// if marker == None {
// parent.update_inner_text("");
// if let Some(replacement) = replacement {
// parent.append_child(replacement);
// }
// return;
// }

for node in current {
if node.parent_node().as_ref() == Some(&parent) {
Expand Down
15 changes: 9 additions & 6 deletions packages/sycamore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,35 @@ pub mod flow;
pub mod generic_node;
pub mod macros;
pub mod noderef;
pub mod render;
pub mod rx;
pub mod template;
pub mod utils;

/// Alias self to sycamore for proc-macros.
extern crate self as sycamore;

#[cfg(feature = "dom")]
pub use crate::generic_node::{hydrate, hydrate_to, render, render_to, DomNode};
#[cfg(feature = "ssr")]
pub use crate::generic_node::{render_to_string, SsrNode};

/// The sycamore prelude.
pub mod prelude {
pub use sycamore_macro::{component, template};

pub use crate::cloned;
pub use crate::flow::{Indexed, IndexedProps, Keyed, KeyedProps};
pub use crate::generic_node::GenericNode;
#[cfg(feature = "dom")]
pub use crate::generic_node::{hydrate, hydrate_to, render, render_to, DomNode};
pub use crate::generic_node::DomNode;
pub use crate::generic_node::GenericNode;
#[cfg(feature = "ssr")]
pub use crate::generic_node::{render_to_string, SsrNode};
pub use crate::generic_node::SsrNode;
pub use crate::noderef::NodeRef;
pub use crate::render::IntoTemplate;
pub use crate::rx::{
create_effect, create_effect_initial, create_memo, create_root, create_selector,
create_selector_with, on_cleanup, untrack, Signal, StateHandle,
};
pub use crate::template::Template;
pub use crate::template::{IntoTemplate, Template};
}

/// Re-exports for use by `sycamore-macro`. Not intended for use by end-users.
Expand Down
25 changes: 0 additions & 25 deletions packages/sycamore/src/render.rs

This file was deleted.

4 changes: 2 additions & 2 deletions packages/sycamore/src/rx/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,10 +653,10 @@ mod tests {
});
}));

assert_eq!(*cleanup_called.get(), false);
assert!(!*cleanup_called.get());

drop(scope);
assert_eq!(*cleanup_called.get(), true);
assert!(*cleanup_called.get());
}

#[test]
Expand Down
19 changes: 19 additions & 0 deletions packages/sycamore/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,22 @@ impl<G: GenericNode> fmt::Debug for Template<G> {
}
}
}

/// Trait for describing how something should be rendered into DOM nodes.
pub trait IntoTemplate<G: GenericNode> {
/// Called during the initial render when creating the DOM nodes. Should return a
/// `Vec` of [`GenericNode`]s.
fn create(&self) -> Template<G>;
}

impl<G: GenericNode> IntoTemplate<G> for Template<G> {
fn create(&self) -> Template<G> {
self.clone()
}
}

impl<T: fmt::Display + ?Sized, G: GenericNode> IntoTemplate<G> for T {
fn create(&self) -> Template<G> {
Template::new_node(G::text_node(&format!("{}", self)))
}
}
8 changes: 4 additions & 4 deletions packages/sycamore/tests/ssr/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn hello_world() {
p { "Hello World!" }
};

assert_eq!(render_to_string(|| node), "<p>Hello World!</p>");
assert_eq!(sycamore::render_to_string(|| node), "<p>Hello World!</p>");
}

#[test]
Expand All @@ -18,12 +18,12 @@ fn reactive_text() {
});

assert_eq!(
render_to_string(cloned!((node) => move || node)),
sycamore::render_to_string(cloned!((node) => move || node)),
"<p>0</p>"
);

count.set(1);
assert_eq!(render_to_string(|| node), "<p>1</p>");
assert_eq!(sycamore::render_to_string(|| node), "<p>1</p>");
}

#[test]
Expand All @@ -36,7 +36,7 @@ fn self_closing_tag() {
};

assert_eq!(
render_to_string(|| node),
sycamore::render_to_string(|| node),
"<div><input /><input value=\"a\" /></div>"
)
}
18 changes: 9 additions & 9 deletions packages/sycamore/tests/web/keyed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn append() {
}
});

render_to(|| node, &test_container());
sycamore::render_to(|| node, &test_container());

let p = document().query_selector("ul").unwrap().unwrap();

Expand Down Expand Up @@ -51,7 +51,7 @@ fn swap_rows() {
}
});

render_to(|| node, &test_container());
sycamore::render_to(|| node, &test_container());

let p = document().query_selector("ul").unwrap().unwrap();
assert_eq!(p.text_content().unwrap(), "123");
Expand Down Expand Up @@ -87,7 +87,7 @@ fn delete_row() {
}
});

render_to(|| node, &test_container());
sycamore::render_to(|| node, &test_container());

let p = document().query_selector("ul").unwrap().unwrap();
assert_eq!(p.text_content().unwrap(), "123");
Expand Down Expand Up @@ -116,7 +116,7 @@ fn clear() {
}
});

render_to(|| node, &test_container());
sycamore::render_to(|| node, &test_container());

let p = document().query_selector("ul").unwrap().unwrap();
assert_eq!(p.text_content().unwrap(), "123");
Expand All @@ -141,7 +141,7 @@ fn insert_front() {
}
});

render_to(|| node, &test_container());
sycamore::render_to(|| node, &test_container());

let p = document().query_selector("ul").unwrap().unwrap();
assert_eq!(p.text_content().unwrap(), "123");
Expand Down Expand Up @@ -170,7 +170,7 @@ fn nested_reactivity() {
}
});

render_to(|| node, &test_container());
sycamore::render_to(|| node, &test_container());

let p = document().query_selector("ul").unwrap().unwrap();
assert_eq!(p.text_content().unwrap(), "123");
Expand Down Expand Up @@ -203,7 +203,7 @@ fn fragment_template() {
}
});

render_to(|| node, &test_container());
sycamore::render_to(|| node, &test_container());

let p = document().query_selector("div").unwrap().unwrap();

Expand Down Expand Up @@ -250,7 +250,7 @@ fn template_top_level() {
})
});

render_to(|| node, &test_container());
sycamore::render_to(|| node, &test_container());

let p = document()
.query_selector("#test-container")
Expand Down Expand Up @@ -296,7 +296,7 @@ fn template_with_other_nodes_at_same_level() {
}
});

render_to(|| node, &test_container());
sycamore::render_to(|| node, &test_container());

let elem = document().query_selector("ul").unwrap().unwrap();

Expand Down
Loading