Compare commits

..

3 Commits

Author SHA1 Message Date
Greg Johnston
faf0cb3378 fix: correct Owner restoration in FlatRoutes (closes #3014) 2024-09-24 19:35:19 -04:00
Greg Johnston
bdc2285658 change: remove generic rendering (#3015) 2024-09-24 17:01:57 -04:00
zakstucke
9d4ce6e526 Fix custom root when not using islands. (#3016) 2024-09-24 11:42:54 -07:00
81 changed files with 2734 additions and 4434 deletions

View File

@@ -1,18 +0,0 @@
[package]
name = "gtk"
version = "0.1.0"
edition = "2021"
[dependencies]
leptos = { path = "../../leptos" }
throw_error = { path = "../../any_error/" }
# these are used to build the integration
gtk = { version = "0.9.0", package = "gtk4" }
next_tuple = { path = "../../next_tuple/" }
paste = "1.0"
# we want to support using glib for the reactive runtime event loop
any_spawner = { path = "../../any_spawner/", features = ["glib"] }
# yes, we want effects to run: this is a "frontend," not a backend
reactive_graph = { path = "../../reactive_graph", features = ["effects"] }

View File

@@ -1 +0,0 @@
extend = [{ path = "../cargo-make/main.toml" }]

View File

@@ -1,8 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta name="color-scheme" content="dark">
<link rel="css" href="style.css" data-trunk>
</head>
<body></body>
</html>

View File

@@ -1,645 +0,0 @@
use self::properties::Connect;
use gtk::{
glib::{
object::{IsA, IsClass, ObjectExt},
Object, Value,
},
prelude::{Cast, WidgetExt},
Label, Orientation, Widget,
};
use leptos::{
reactive_graph::effect::RenderEffect,
tachys::{
renderer::{CastFrom, Renderer},
view::{Mountable, Render},
},
};
use next_tuple::NextTuple;
use std::marker::PhantomData;
#[derive(Debug)]
pub struct LeptosGtk;
#[derive(Debug, Clone)]
pub struct Element(pub Widget);
impl Element {
pub fn remove(&self) {
self.0.unparent();
}
}
#[derive(Debug, Clone)]
pub struct Text(pub Element);
impl<T> From<T> for Element
where
T: Into<Widget>,
{
fn from(value: T) -> Self {
Element(value.into())
}
}
impl Mountable<LeptosGtk> for Element {
fn unmount(&mut self) {
self.remove()
}
fn mount(
&mut self,
parent: &<LeptosGtk as Renderer>::Element,
marker: Option<&<LeptosGtk as Renderer>::Node>,
) {
self.0
.insert_before(&parent.0, marker.as_ref().map(|m| &m.0));
}
fn insert_before_this(&self, child: &mut dyn Mountable<LeptosGtk>) -> bool {
if let Some(parent) = self.0.parent() {
child.mount(&Element(parent), Some(self));
return true;
}
false
}
}
impl Mountable<LeptosGtk> for Text {
fn unmount(&mut self) {
self.0.remove()
}
fn mount(
&mut self,
parent: &<LeptosGtk as Renderer>::Element,
marker: Option<&<LeptosGtk as Renderer>::Node>,
) {
self.0
.0
.insert_before(&parent.0, marker.as_ref().map(|m| &m.0));
}
fn insert_before_this(&self, child: &mut dyn Mountable<LeptosGtk>) -> bool {
self.0.insert_before_this(child)
}
}
impl CastFrom<Element> for Element {
fn cast_from(source: Element) -> Option<Self> {
Some(source)
}
}
impl CastFrom<Element> for Text {
fn cast_from(source: Element) -> Option<Self> {
source
.0
.downcast::<Label>()
.ok()
.map(|n| Text(Element::from(n)))
}
}
impl AsRef<Element> for Element {
fn as_ref(&self) -> &Element {
self
}
}
impl AsRef<Element> for Text {
fn as_ref(&self) -> &Element {
&self.0
}
}
impl Renderer for LeptosGtk {
type Node = Element;
type Element = Element;
type Text = Text;
type Placeholder = Element;
fn intern(text: &str) -> &str {
text
}
fn create_text_node(text: &str) -> Self::Text {
Text(Element::from(Label::new(Some(text))))
}
fn create_placeholder() -> Self::Placeholder {
let label = Label::new(None);
label.set_visible(false);
Element::from(label)
}
fn set_text(node: &Self::Text, text: &str) {
let node_as_text = node.0 .0.downcast_ref::<Label>().unwrap();
node_as_text.set_label(text);
}
fn set_attribute(node: &Self::Element, name: &str, value: &str) {
node.0.set_property(name, value);
}
fn remove_attribute(node: &Self::Element, name: &str) {
node.0.set_property(name, None::<&str>);
}
fn insert_node(
parent: &Self::Element,
new_child: &Self::Node,
marker: Option<&Self::Node>,
) {
new_child
.0
.insert_before(&parent.0, marker.as_ref().map(|n| &n.0));
}
fn remove_node(
_parent: &Self::Element,
_child: &Self::Node,
) -> Option<Self::Node> {
todo!()
}
fn remove(_node: &Self::Node) {
todo!()
}
fn get_parent(node: &Self::Node) -> Option<Self::Node> {
node.0.parent().map(Element::from)
}
fn first_child(_node: &Self::Node) -> Option<Self::Node> {
todo!()
}
fn next_sibling(_node: &Self::Node) -> Option<Self::Node> {
todo!()
}
fn log_node(node: &Self::Node) {
println!("{node:?}");
}
fn clear_children(_parent: &Self::Element) {
todo!()
}
}
pub fn root<Chil>(children: Chil) -> (Widget, impl Mountable<LeptosGtk>)
where
Chil: Render<LeptosGtk>,
{
let state = r#box()
.orientation(Orientation::Vertical)
.spacing(12)
.child(children)
.build();
(state.as_widget().clone(), state)
}
pub trait WidgetClass {
type Widget: Into<Widget> + IsA<Object> + IsClass;
}
pub struct LGtkWidget<Widg, Props, Chil> {
widget: PhantomData<Widg>,
properties: Props,
children: Chil,
}
impl<Widg, Props, Chil> LGtkWidget<Widg, Props, Chil>
where
Widg: WidgetClass,
Chil: NextTuple,
{
pub fn child<T>(
self,
child: T,
) -> LGtkWidget<Widg, Props, Chil::Output<T>> {
let LGtkWidget {
widget,
properties,
children,
} = self;
LGtkWidget {
widget,
properties,
children: children.next_tuple(child),
}
}
}
impl<Widg, Props, Chil> LGtkWidget<Widg, Props, Chil>
where
Widg: WidgetClass,
Props: NextTuple,
Chil: Render<LeptosGtk>,
{
pub fn connect<F>(
self,
signal_name: &'static str,
callback: F,
) -> LGtkWidget<Widg, Props::Output<Connect<F>>, Chil>
where
F: Fn(&[Value]) -> Option<Value> + Send + Sync + 'static,
{
let LGtkWidget {
widget,
properties,
children,
} = self;
LGtkWidget {
widget,
properties: properties.next_tuple(Connect {
signal_name,
callback,
}),
children,
}
}
}
pub struct LGtkWidgetState<Widg, Props, Chil>
where
Chil: Render<LeptosGtk>,
Props: Property,
Widg: WidgetClass,
{
ty: PhantomData<Widg>,
widget: Element,
properties: Props::State,
children: Chil::State,
}
impl<Widg, Props, Chil> LGtkWidgetState<Widg, Props, Chil>
where
Chil: Render<LeptosGtk>,
Props: Property,
Widg: WidgetClass,
{
pub fn as_widget(&self) -> &Widget {
&self.widget.0
}
}
impl<Widg, Props, Chil> Render<LeptosGtk> for LGtkWidget<Widg, Props, Chil>
where
Widg: WidgetClass,
Props: Property,
Chil: Render<LeptosGtk>,
{
type State = LGtkWidgetState<Widg, Props, Chil>;
fn build(self) -> Self::State {
let widget = Object::new::<Widg::Widget>();
let widget = Element::from(widget);
let properties = self.properties.build(&widget);
let mut children = self.children.build();
children.mount(&widget, None);
LGtkWidgetState {
ty: PhantomData,
widget,
properties,
children,
}
}
fn rebuild(self, state: &mut Self::State) {
self.properties
.rebuild(&state.widget, &mut state.properties);
self.children.rebuild(&mut state.children);
}
}
impl<Widg, Props, Chil> Mountable<LeptosGtk>
for LGtkWidgetState<Widg, Props, Chil>
where
Widg: WidgetClass,
Props: Property,
Chil: Render<LeptosGtk>,
{
fn unmount(&mut self) {
self.children.unmount();
self.widget.remove();
}
fn mount(
&mut self,
parent: &<LeptosGtk as Renderer>::Element,
marker: Option<&<LeptosGtk as Renderer>::Node>,
) {
self.children.mount(&self.widget, None);
LeptosGtk::insert_node(parent, &self.widget, marker);
}
fn insert_before_this(&self, child: &mut dyn Mountable<LeptosGtk>) -> bool {
self.widget.insert_before_this(child)
}
}
pub trait Property {
type State;
fn build(self, element: &Element) -> Self::State;
fn rebuild(self, element: &Element, state: &mut Self::State);
}
impl<T, F> Property for F
where
T: Property,
T::State: 'static,
F: Fn() -> T + 'static,
{
type State = RenderEffect<T::State>;
fn build(self, widget: &Element) -> Self::State {
let widget = widget.clone();
RenderEffect::new(move |prev| {
let value = self();
if let Some(mut prev) = prev {
value.rebuild(&widget, &mut prev);
prev
} else {
value.build(&widget)
}
})
}
fn rebuild(self, widget: &Element, state: &mut Self::State) {
let prev_value = state.take_value();
let widget = widget.to_owned();
*state = RenderEffect::new_with_value(
move |prev| {
let value = self();
if let Some(mut state) = prev {
value.rebuild(&widget, &mut state);
state
} else {
unreachable!()
}
},
prev_value,
);
}
}
pub fn button() -> LGtkWidget<gtk::Button, (), ()> {
LGtkWidget {
widget: PhantomData,
properties: (),
children: (),
}
}
pub fn r#box() -> LGtkWidget<gtk::Box, (), ()> {
LGtkWidget {
widget: PhantomData,
properties: (),
children: (),
}
}
mod widgets {
use super::WidgetClass;
impl WidgetClass for gtk::Button {
type Widget = Self;
}
impl WidgetClass for gtk::Box {
type Widget = Self;
}
}
pub mod properties {
#![allow(dead_code)]
use super::{Element, LGtkWidget, LeptosGtk, Property, WidgetClass};
use gtk::glib::{object::ObjectExt, Value};
use leptos::tachys::{renderer::Renderer, view::Render};
use next_tuple::NextTuple;
pub struct Connect<F>
where
F: Fn(&[Value]) -> Option<Value> + Send + Sync + 'static,
{
pub signal_name: &'static str,
pub callback: F,
}
impl<F> Property for Connect<F>
where
F: Fn(&[Value]) -> Option<Value> + Send + Sync + 'static,
{
type State = ();
fn build(self, element: &Element) -> Self::State {
element.0.connect(self.signal_name, false, self.callback);
}
fn rebuild(self, _element: &Element, _state: &mut Self::State) {
// TODO we want to *remove* the previous listener, and reconnect with this new one
}
}
/* examples for macro */
pub struct Orientation {
value: gtk::Orientation,
}
pub struct OrientationState {
value: gtk::Orientation,
}
impl Property for Orientation {
type State = OrientationState;
fn build(self, element: &Element) -> Self::State {
element.0.set_property("orientation", self.value);
OrientationState { value: self.value }
}
fn rebuild(self, element: &Element, state: &mut Self::State) {
if self.value != state.value {
element.0.set_property("orientation", self.value);
state.value = self.value;
}
}
}
impl<Widg, Props, Chil> LGtkWidget<Widg, Props, Chil>
where
Widg: WidgetClass,
Props: NextTuple,
Chil: Render<LeptosGtk>,
{
pub fn orientation(
self,
value: impl Into<gtk::Orientation>,
) -> LGtkWidget<Widg, Props::Output<Orientation>, Chil> {
let LGtkWidget {
widget,
properties,
children,
} = self;
LGtkWidget {
widget,
properties: properties.next_tuple(Orientation {
value: value.into(),
}),
children,
}
}
}
pub struct Spacing {
value: i32,
}
pub struct SpacingState {
value: i32,
}
impl Property for Spacing {
type State = SpacingState;
fn build(self, element: &Element) -> Self::State {
element.0.set_property("spacing", self.value);
SpacingState { value: self.value }
}
fn rebuild(self, element: &Element, state: &mut Self::State) {
if self.value != state.value {
element.0.set_property("spacing", self.value);
state.value = self.value;
}
}
}
impl<Widg, Props, Chil> LGtkWidget<Widg, Props, Chil>
where
Widg: WidgetClass,
Props: NextTuple,
Chil: Render<LeptosGtk>,
{
pub fn spacing(
self,
value: impl Into<i32>,
) -> LGtkWidget<Widg, Props::Output<Spacing>, Chil> {
let LGtkWidget {
widget,
properties,
children,
} = self;
LGtkWidget {
widget,
properties: properties.next_tuple(Spacing {
value: value.into(),
}),
children,
}
}
}
/* end examples for properties macro */
#[derive(Debug)]
pub struct Label {
value: String,
}
impl Label {
pub fn new(value: impl Into<String>) -> Self {
Self {
value: value.into(),
}
}
}
pub struct LabelState {
value: String,
}
impl Property for Label {
type State = LabelState;
fn build(self, element: &Element) -> Self::State {
LeptosGtk::set_attribute(element, "label", &self.value);
LabelState { value: self.value }
}
fn rebuild(self, element: &Element, state: &mut Self::State) {
if self.value != state.value {
LeptosGtk::set_attribute(element, "label", &self.value);
}
}
}
impl Property for () {
type State = ();
fn build(self, _element: &Element) -> Self::State {}
fn rebuild(self, _element: &Element, _state: &mut Self::State) {}
}
macro_rules! tuples {
($($ty:ident),* $(,)?) => {
impl<$($ty,)*> Property for ($($ty,)*)
where $($ty: Property,)*
{
type State = ($($ty::State,)*);
fn build(self, element: &Element) -> Self::State {
#[allow(non_snake_case)]
let ($($ty,)*) = self;
($($ty.build(element),)*)
}
fn rebuild(self, element: &Element, state: &mut Self::State) {
paste::paste! {
#[allow(non_snake_case)]
let ($($ty,)*) = self;
#[allow(non_snake_case)]
let ($([<state_ $ty:lower>],)*) = state;
$($ty.rebuild(element, [<state_ $ty:lower>]));*
}
}
}
}
}
tuples!(A);
tuples!(A, B);
tuples!(A, B, C);
tuples!(A, B, C, D);
tuples!(A, B, C, D, E);
tuples!(A, B, C, D, E, F);
tuples!(A, B, C, D, E, F, G);
tuples!(A, B, C, D, E, F, G, H);
tuples!(A, B, C, D, E, F, G, H, I);
tuples!(A, B, C, D, E, F, G, H, I, J);
tuples!(A, B, C, D, E, F, G, H, I, J, K);
tuples!(A, B, C, D, E, F, G, H, I, J, K, L);
tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M);
tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, N);
tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O);
tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P);
tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q);
tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R);
tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S);
tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T);
tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U);
tuples!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V);
tuples!(
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W
);
tuples!(
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X
);
tuples!(
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X,
Y
);
}

View File

@@ -1,107 +0,0 @@
use any_spawner::Executor;
use gtk::{prelude::*, Application, ApplicationWindow, Orientation};
use leptos::prelude::*;
use leptos_gtk::LeptosGtk;
use std::{mem, thread, time::Duration};
mod leptos_gtk;
const APP_ID: &str = "dev.leptos.Counter";
// Basic GTK app setup from https://gtk-rs.org/gtk4-rs/stable/latest/book/hello_world.html
fn main() {
// use the glib event loop to power the reactive system
_ = Executor::init_glib();
let app = Application::builder().application_id(APP_ID).build();
app.connect_startup(|_| load_css());
app.connect_activate(|app| {
// Connect to "activate" signal of `app`
let owner = Owner::new();
let view = owner.with(ui);
let (root, state) = leptos_gtk::root(view);
let window = ApplicationWindow::builder()
.application(app)
.title("TachyGTK")
.child(&root)
.build();
// Present window
window.present();
mem::forget((owner, state));
});
app.run();
}
fn ui() -> impl Render<LeptosGtk> {
let value = RwSignal::new(0);
let rows = RwSignal::new(vec![1, 2, 3, 4, 5]);
Effect::new(move |_| {
println!("value = {}", value.get());
});
// just an example of multithreaded reactivity
thread::spawn(move || loop {
thread::sleep(Duration::from_millis(250));
value.update(|n| *n += 1);
});
vstack((
hstack((
button("-1", move || {
println!("clicked -1");
value.update(|n| *n -= 1);
}),
move || value.get().to_string(),
button("+1", move || value.update(|n| *n += 1)),
)),
button("Swap", move || {
rows.update(|items| {
items.swap(1, 3);
})
}),
hstack(rows),
))
}
fn button(
label: impl Render<LeptosGtk>,
callback: impl Fn() + Send + Sync + 'static,
) -> impl Render<LeptosGtk> {
leptos_gtk::button()
.child(label)
.connect("clicked", move |_| {
callback();
None
})
}
fn vstack(children: impl Render<LeptosGtk>) -> impl Render<LeptosGtk> {
leptos_gtk::r#box()
.orientation(Orientation::Vertical)
.spacing(12)
.child(children)
}
fn hstack(children: impl Render<LeptosGtk>) -> impl Render<LeptosGtk> {
leptos_gtk::r#box()
.orientation(Orientation::Horizontal)
.spacing(12)
.child(children)
}
fn load_css() {
use gtk::{gdk::Display, CssProvider};
let provider = CssProvider::new();
provider.load_from_path("style.css");
// Add the provider to the default screen
gtk::style_context_add_provider_for_display(
&Display::default().expect("Could not connect to a display."),
&provider,
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
);
}

View File

View File

@@ -65,7 +65,7 @@ pub fn RouterExample() -> impl IntoView {
// You can define other routes in their own component.
// Routes implement the MatchNestedRoutes
#[component]
pub fn ContactRoutes() -> impl MatchNestedRoutes<Dom> + Clone {
pub fn ContactRoutes() -> impl MatchNestedRoutes + Clone {
view! {
<ParentRoute path=path!("") view=ContactList>
<Route path=path!("/") view=|| "Select a contact."/>

View File

@@ -3,13 +3,10 @@ use std::{
fmt::{self, Debug},
sync::Arc,
};
use tachys::{
renderer::dom::Dom,
view::{
any_view::{AnyView, IntoAny},
fragment::{Fragment, IntoFragment},
RenderHtml,
},
use tachys::view::{
any_view::{AnyView, IntoAny},
fragment::{Fragment, IntoFragment},
RenderHtml,
};
/// The most common type for the `children` property on components,
@@ -17,31 +14,31 @@ use tachys::{
///
/// This does not support iterating over individual nodes within the children.
/// To iterate over children, use [`ChildrenFragment`].
pub type Children = Box<dyn FnOnce() -> AnyView<Dom> + Send>;
pub type Children = Box<dyn FnOnce() -> AnyView + Send>;
/// A type for the `children` property on components that can be called only once,
/// and provides a collection of all the children passed to this component.
pub type ChildrenFragment = Box<dyn FnOnce() -> Fragment<Dom> + Send>;
pub type ChildrenFragment = Box<dyn FnOnce() -> Fragment + Send>;
/// A type for the `children` property on components that can be called
/// more than once.
pub type ChildrenFn = Arc<dyn Fn() -> AnyView<Dom> + Send + Sync>;
pub type ChildrenFn = Arc<dyn Fn() -> AnyView + Send + Sync>;
/// A type for the `children` property on components that can be called more than once,
/// and provides a collection of all the children passed to this component.
pub type ChildrenFragmentFn = Arc<dyn Fn() -> Fragment<Dom> + Send>;
pub type ChildrenFragmentFn = Arc<dyn Fn() -> Fragment + Send>;
/// A type for the `children` property on components that can be called
/// more than once, but may mutate the children.
pub type ChildrenFnMut = Box<dyn FnMut() -> AnyView<Dom> + Send>;
pub type ChildrenFnMut = Box<dyn FnMut() -> AnyView + Send>;
/// A type for the `children` property on components that can be called more than once,
/// but may mutate the children, and provides a collection of all the children
/// passed to this component.
pub type ChildrenFragmentMut = Box<dyn FnMut() -> Fragment<Dom> + Send>;
pub type ChildrenFragmentMut = Box<dyn FnMut() -> Fragment + Send>;
// This is to still support components that accept `Box<dyn Fn() -> AnyView>` as a children.
type BoxedChildrenFn = Box<dyn Fn() -> AnyView<Dom> + Send>;
type BoxedChildrenFn = Box<dyn Fn() -> AnyView + Send>;
/// This trait can be used when constructing a component that takes children without needing
/// to know exactly what children type the component expects. This is used internally by the
@@ -97,7 +94,7 @@ pub trait ToChildren<F> {
impl<F, C> ToChildren<F> for Children
where
F: FnOnce() -> C + Send + 'static,
C: RenderHtml<Dom> + Send + 'static,
C: RenderHtml + Send + 'static,
{
#[inline]
fn to_children(f: F) -> Self {
@@ -108,7 +105,7 @@ where
impl<F, C> ToChildren<F> for ChildrenFn
where
F: Fn() -> C + Send + Sync + 'static,
C: RenderHtml<Dom> + Send + 'static,
C: RenderHtml + Send + 'static,
{
#[inline]
fn to_children(f: F) -> Self {
@@ -119,7 +116,7 @@ where
impl<F, C> ToChildren<F> for ChildrenFnMut
where
F: Fn() -> C + Send + 'static,
C: RenderHtml<Dom> + Send + 'static,
C: RenderHtml + Send + 'static,
{
#[inline]
fn to_children(f: F) -> Self {
@@ -130,7 +127,7 @@ where
impl<F, C> ToChildren<F> for BoxedChildrenFn
where
F: Fn() -> C + Send + 'static,
C: RenderHtml<Dom> + Send + 'static,
C: RenderHtml + Send + 'static,
{
#[inline]
fn to_children(f: F) -> Self {
@@ -141,7 +138,7 @@ where
impl<F, C> ToChildren<F> for ChildrenFragment
where
F: FnOnce() -> C + Send + 'static,
C: IntoFragment<Dom>,
C: IntoFragment,
{
#[inline]
fn to_children(f: F) -> Self {
@@ -152,7 +149,7 @@ where
impl<F, C> ToChildren<F> for ChildrenFragmentFn
where
F: Fn() -> C + Send + 'static,
C: IntoFragment<Dom>,
C: IntoFragment,
{
#[inline]
fn to_children(f: F) -> Self {
@@ -163,7 +160,7 @@ where
impl<F, C> ToChildren<F> for ChildrenFragmentMut
where
F: FnMut() -> C + Send + 'static,
C: IntoFragment<Dom>,
C: IntoFragment,
{
#[inline]
fn to_children(mut f: F) -> Self {
@@ -174,7 +171,7 @@ where
/// New-type wrapper for a function that returns a view with `From` and `Default` traits implemented
/// to enable optional props in for example `<Show>` and `<Suspense>`.
#[derive(Clone)]
pub struct ViewFn(Arc<dyn Fn() -> AnyView<Dom> + Send + Sync + 'static>);
pub struct ViewFn(Arc<dyn Fn() -> AnyView + Send + Sync + 'static>);
impl Default for ViewFn {
fn default() -> Self {
@@ -185,7 +182,7 @@ impl Default for ViewFn {
impl<F, C> From<F> for ViewFn
where
F: Fn() -> C + Send + Sync + 'static,
C: RenderHtml<Dom> + Send + 'static,
C: RenderHtml + Send + 'static,
{
fn from(value: F) -> Self {
Self(Arc::new(move || value().into_any()))
@@ -194,14 +191,14 @@ where
impl ViewFn {
/// Execute the wrapped function
pub fn run(&self) -> AnyView<Dom> {
pub fn run(&self) -> AnyView {
(self.0)()
}
}
/// New-type wrapper for a function, which will only be called once and returns a view with `From` and
/// `Default` traits implemented to enable optional props in for example `<Show>` and `<Suspense>`.
pub struct ViewFnOnce(Box<dyn FnOnce() -> AnyView<Dom> + Send + 'static>);
pub struct ViewFnOnce(Box<dyn FnOnce() -> AnyView + Send + 'static>);
impl Default for ViewFnOnce {
fn default() -> Self {
@@ -212,7 +209,7 @@ impl Default for ViewFnOnce {
impl<F, C> From<F> for ViewFnOnce
where
F: FnOnce() -> C + Send + 'static,
C: RenderHtml<Dom> + Send + 'static,
C: RenderHtml + Send + 'static,
{
fn from(value: F) -> Self {
Self(Box::new(move || value().into_any()))
@@ -221,7 +218,7 @@ where
impl ViewFnOnce {
/// Execute the wrapped function
pub fn run(self) -> AnyView<Dom> {
pub fn run(self) -> AnyView {
(self.0)()
}
}

View File

@@ -9,12 +9,11 @@ use reactive_graph::{
traits::{Get, Update, With, WithUntracked},
};
use rustc_hash::FxHashMap;
use std::{fmt::Debug, marker::PhantomData, sync::Arc};
use std::{fmt::Debug, sync::Arc};
use tachys::{
html::attribute::Attribute,
hydration::Cursor,
reactive_graph::OwnedView,
renderer::Renderer,
ssr::StreamBuilder,
view::{
add_attr::AddAnyAttr, Mountable, Position, PositionState, Render,
@@ -112,20 +111,18 @@ where
children,
errors,
fallback,
rndr: PhantomData,
},
owner,
)
}
struct ErrorBoundaryView<Chil, FalFn, Rndr> {
struct ErrorBoundaryView<Chil, FalFn> {
hook: Arc<dyn ErrorHook>,
boundary_id: SerializedDataId,
errors_empty: ArcMemo<bool>,
children: Chil,
fallback: FalFn,
errors: ArcRwSignal<Errors>,
rndr: PhantomData<Rndr>,
}
struct ErrorBoundaryViewState<Chil, Fal> {
@@ -134,11 +131,10 @@ struct ErrorBoundaryViewState<Chil, Fal> {
fallback: Option<Fal>,
}
impl<Chil, Fal, Rndr> Mountable<Rndr> for ErrorBoundaryViewState<Chil, Fal>
impl<Chil, Fal> Mountable for ErrorBoundaryViewState<Chil, Fal>
where
Chil: Mountable<Rndr>,
Fal: Mountable<Rndr>,
Rndr: Renderer,
Chil: Mountable,
Fal: Mountable,
{
fn unmount(&mut self) {
if let Some(fallback) = &mut self.fallback {
@@ -148,7 +144,11 @@ where
}
}
fn mount(&mut self, parent: &Rndr::Element, marker: Option<&Rndr::Node>) {
fn mount(
&mut self,
parent: &tachys::renderer::types::Element,
marker: Option<&tachys::renderer::types::Node>,
) {
if let Some(fallback) = &mut self.fallback {
fallback.mount(parent, marker);
} else {
@@ -156,7 +156,7 @@ where
}
}
fn insert_before_this(&self, child: &mut dyn Mountable<Rndr>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
if let Some(fallback) = &self.fallback {
fallback.insert_before_this(child)
} else {
@@ -165,13 +165,11 @@ where
}
}
impl<Chil, FalFn, Fal, Rndr> Render<Rndr>
for ErrorBoundaryView<Chil, FalFn, Rndr>
impl<Chil, FalFn, Fal> Render for ErrorBoundaryView<Chil, FalFn>
where
Chil: Render<Rndr> + 'static,
Chil: Render + 'static,
FalFn: FnMut(ArcRwSignal<Errors>) -> Fal + Send + 'static,
Fal: Render<Rndr> + 'static,
Rndr: Renderer,
Fal: Render + 'static,
{
type State = RenderEffect<ErrorBoundaryViewState<Chil::State, Fal::State>>;
@@ -228,26 +226,21 @@ where
}
}
impl<Chil, FalFn, Fal, Rndr> AddAnyAttr<Rndr>
for ErrorBoundaryView<Chil, FalFn, Rndr>
impl<Chil, FalFn, Fal> AddAnyAttr for ErrorBoundaryView<Chil, FalFn>
where
Chil: RenderHtml<Rndr> + 'static,
Chil: RenderHtml + 'static,
FalFn: FnMut(ArcRwSignal<Errors>) -> Fal + Send + 'static,
Fal: RenderHtml<Rndr> + Send + 'static,
Rndr: Renderer,
Fal: RenderHtml + Send + 'static,
{
type Output<SomeNewAttr: Attribute<Rndr>> = ErrorBoundaryView<
Chil::Output<SomeNewAttr::CloneableOwned>,
FalFn,
Rndr,
>;
type Output<SomeNewAttr: Attribute> =
ErrorBoundaryView<Chil::Output<SomeNewAttr::CloneableOwned>, FalFn>;
fn add_any_attr<NewAttr: Attribute<Rndr>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Rndr>,
Self::Output<NewAttr>: RenderHtml,
{
let ErrorBoundaryView {
hook,
@@ -256,7 +249,6 @@ where
children,
fallback,
errors,
rndr,
} = self;
ErrorBoundaryView {
hook,
@@ -265,20 +257,17 @@ where
children: children.add_any_attr(attr.into_cloneable_owned()),
fallback,
errors,
rndr,
}
}
}
impl<Chil, FalFn, Fal, Rndr> RenderHtml<Rndr>
for ErrorBoundaryView<Chil, FalFn, Rndr>
impl<Chil, FalFn, Fal> RenderHtml for ErrorBoundaryView<Chil, FalFn>
where
Chil: RenderHtml<Rndr> + Send + 'static,
Chil: RenderHtml + Send + 'static,
FalFn: FnMut(ArcRwSignal<Errors>) -> Fal + Send + 'static,
Fal: RenderHtml<Rndr> + Send + 'static,
Rndr: Renderer,
Fal: RenderHtml + Send + 'static,
{
type AsyncOutput = ErrorBoundaryView<Chil::AsyncOutput, FalFn, Rndr>;
type AsyncOutput = ErrorBoundaryView<Chil::AsyncOutput, FalFn>;
const MIN_LENGTH: usize = Chil::MIN_LENGTH;
@@ -303,7 +292,6 @@ where
children: children.resolve().await,
fallback,
errors,
rndr: PhantomData,
}
}
@@ -377,7 +365,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
mut self,
cursor: &Cursor<Rndr>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
let mut children = Some(self.children);

View File

@@ -157,7 +157,7 @@ where
};
move || keyed(each(), key.clone(), children.clone())
}
/*
#[cfg(test)]
mod tests {
use crate::prelude::*;
@@ -168,7 +168,7 @@ mod tests {
fn creates_list() {
Owner::new().with(|| {
let values = RwSignal::new(vec![1, 2, 3, 4, 5]);
let list: View<HtmlElement<_, _, _, Dom>> = view! {
let list: View<HtmlElement<_, _, _>> = view! {
<ol>
<For each=move || values.get() key=|i| *i let:i>
<li>{i}</li>
@@ -187,7 +187,7 @@ mod tests {
fn creates_list_enumerate() {
Owner::new().with(|| {
let values = RwSignal::new(vec![1, 2, 3, 4, 5]);
let list: View<HtmlElement<_, _, _, Dom>> = view! {
let list: View<HtmlElement<_, _, _>> = view! {
<ol>
<ForEnumerate each=move || values.get() key=|i| *i let(index, i)>
<li>{move || index.get()}"-"{i}</li>
@@ -200,7 +200,7 @@ mod tests {
<!>-<!>4</li><li>4<!>-<!>5</li><!></ol>"
);
let list: View<HtmlElement<_, _, _, Dom>> = view! {
let list: View<HtmlElement<_, _, _>> = view! {
<ol>
<ForEnumerate each=move || values.get() key=|i| *i let(index, i)>
<li>{move || index.get()}"-"{i}</li>
@@ -216,3 +216,4 @@ mod tests {
});
}
}
*/

View File

@@ -1,8 +1,8 @@
(function (root, pkg_path, output_name, wasm_output_name) {
import(`${root}/${pkg_path}/${output_name}.js`)
.then(mod => {
mod.default(`/${pkg_path}/${wasm_output_name}.wasm`).then(() => {
mod.default(`${root}/${pkg_path}/${wasm_output_name}.wasm`).then(() => {
mod.hydrate();
});
})
})
})

View File

@@ -2,7 +2,6 @@ use std::borrow::Cow;
use tachys::{
html::attribute::Attribute,
hydration::Cursor,
renderer::{dom::Dom, Renderer},
ssr::StreamBuilder,
view::{
add_attr::AddAnyAttr, Position, PositionState, Render, RenderHtml,
@@ -50,14 +49,14 @@ impl<T> View<T> {
pub trait IntoView
where
Self: Sized + Render<Dom> + RenderHtml<Dom> + Send,
Self: Sized + Render + RenderHtml + Send,
{
fn into_view(self) -> View<Self>;
}
impl<T> IntoView for T
where
T: Sized + Render<Dom> + RenderHtml<Dom> + Send, //+ AddAnyAttr<Dom>,
T: Sized + Render + RenderHtml + Send, //+ AddAnyAttr,
{
fn into_view(self) -> View<Self> {
View {
@@ -68,7 +67,7 @@ where
}
}
impl<T: Render<Rndr>, Rndr: Renderer> Render<Rndr> for View<T> {
impl<T: Render> Render for View<T> {
type State = T::State;
fn build(self) -> Self::State {
@@ -80,10 +79,10 @@ impl<T: Render<Rndr>, Rndr: Renderer> Render<Rndr> for View<T> {
}
}
impl<T: RenderHtml<Rndr>, Rndr: Renderer> RenderHtml<Rndr> for View<T> {
impl<T: RenderHtml> RenderHtml for View<T> {
type AsyncOutput = T::AsyncOutput;
const MIN_LENGTH: usize = <T as RenderHtml<Rndr>>::MIN_LENGTH;
const MIN_LENGTH: usize = <T as RenderHtml>::MIN_LENGTH;
async fn resolve(self) -> Self::AsyncOutput {
self.inner.resolve().await
@@ -147,7 +146,7 @@ impl<T: RenderHtml<Rndr>, Rndr: Renderer> RenderHtml<Rndr> for View<T> {
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<Rndr>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
self.inner.hydrate::<FROM_SERVER>(cursor, position)
@@ -166,18 +165,15 @@ impl<T: ToTemplate> ToTemplate for View<T> {
}
}
impl<T: AddAnyAttr<Rndr>, Rndr> AddAnyAttr<Rndr> for View<T>
where
Rndr: Renderer,
{
type Output<SomeNewAttr: Attribute<Rndr>> = View<T::Output<SomeNewAttr>>;
impl<T: AddAnyAttr> AddAnyAttr for View<T> {
type Output<SomeNewAttr: Attribute> = View<T::Output<SomeNewAttr>>;
fn add_any_attr<NewAttr: Attribute<Rndr>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Rndr>,
Self::Output<NewAttr>: RenderHtml,
{
let View {
inner,

View File

@@ -5,10 +5,8 @@ use any_spawner::Executor;
use reactive_graph::owner::Owner;
#[cfg(debug_assertions)]
use std::cell::Cell;
use std::marker::PhantomData;
use tachys::{
dom::body,
renderer::{dom::Dom, Renderer},
view::{Mountable, Render},
};
#[cfg(feature = "hydrate")]
@@ -38,10 +36,7 @@ thread_local! {
#[cfg(feature = "hydrate")]
/// Runs the provided closure and mounts the result to the provided element.
pub fn hydrate_from<F, N>(
parent: HtmlElement,
f: F,
) -> UnmountHandle<N::State, Dom>
pub fn hydrate_from<F, N>(parent: HtmlElement, f: F) -> UnmountHandle<N::State>
where
F: FnOnce() -> N + 'static,
N: IntoView,
@@ -85,11 +80,7 @@ where
// returns a handle that owns the owner
// when this is dropped, it will clean up the reactive system and unmount the view
UnmountHandle {
owner,
mountable,
rndr: PhantomData,
}
UnmountHandle { owner, mountable }
}
/// Runs the provided closure and mounts the result to the `<body>`.
@@ -103,7 +94,7 @@ where
}
/// Runs the provided closure and mounts the result to the provided element.
pub fn mount_to<F, N>(parent: HtmlElement, f: F) -> UnmountHandle<N::State, Dom>
pub fn mount_to<F, N>(parent: HtmlElement, f: F) -> UnmountHandle<N::State>
where
F: FnOnce() -> N + 'static,
N: IntoView,
@@ -140,22 +131,17 @@ where
// returns a handle that owns the owner
// when this is dropped, it will clean up the reactive system and unmount the view
UnmountHandle {
owner,
mountable,
rndr: PhantomData,
}
UnmountHandle { owner, mountable }
}
/// Runs the provided closure and mounts the result to the provided element.
pub fn mount_to_renderer<F, N, R>(
parent: &R::Element,
pub fn mount_to_renderer<F, N>(
parent: &tachys::renderer::types::Element,
f: F,
) -> UnmountHandle<N::State, R>
) -> UnmountHandle<N::State>
where
F: FnOnce() -> N + 'static,
N: Render<R>,
R: Renderer,
N: Render,
{
// use wasm-bindgen-futures to drive the reactive system
// we ignore the return value because an Err here just means the wasm-bindgen executor is
@@ -173,11 +159,7 @@ where
// returns a handle that owns the owner
// when this is dropped, it will clean up the reactive system and unmount the view
UnmountHandle {
owner,
mountable,
rndr: PhantomData,
}
UnmountHandle { owner, mountable }
}
/// Hydrates any islands that are currently present on the page.
@@ -211,21 +193,18 @@ pub fn hydrate_islands() {
reactive system. You should either call `.forget()` to keep the \
view permanently mounted, or store the `UnmountHandle` somewhere \
and drop it when you'd like to unmount the view."]
pub struct UnmountHandle<M, R>
pub struct UnmountHandle<M>
where
M: Mountable<R>,
R: Renderer,
M: Mountable,
{
#[allow(dead_code)]
owner: Owner,
mountable: M,
rndr: PhantomData<R>,
}
impl<M, R> UnmountHandle<M, R>
impl<M> UnmountHandle<M>
where
M: Mountable<R>,
R: Renderer,
M: Mountable,
{
/// Leaks the handle, preventing the reactive system from being cleaned up and the view from
/// being unmounted. This should always be called when [`mount_to`] is used for the root of an
@@ -235,10 +214,9 @@ where
}
}
impl<M, R> Drop for UnmountHandle<M, R>
impl<M> Drop for UnmountHandle<M>
where
M: Mountable<R>,
R: Renderer,
M: Mountable,
{
fn drop(&mut self) {
self.mountable.unmount();

View File

@@ -6,7 +6,7 @@ use base64::{
};
use rand::{thread_rng, RngCore};
use std::{fmt::Display, ops::Deref, sync::Arc};
use tachys::{html::attribute::AttributeValue, renderer::Renderer};
use tachys::html::attribute::AttributeValue;
/// A cryptographic nonce ("number used once") which can be
/// used by Content Security Policy to determine whether or not a given
@@ -65,12 +65,9 @@ impl Display for Nonce {
}
}
impl<R> AttributeValue<R> for Nonce
where
R: Renderer,
{
impl AttributeValue for Nonce {
type AsyncOutput = Self;
type State = <Arc<str> as AttributeValue<R>>::State;
type State = <Arc<str> as AttributeValue>::State;
type Cloneable = Self;
type CloneableOwned = Self;
@@ -79,7 +76,7 @@ where
}
fn to_html(self, key: &str, buf: &mut String) {
<Arc<str> as AttributeValue<R>>::to_html(self.0, key, buf)
<Arc<str> as AttributeValue>::to_html(self.0, key, buf)
}
fn to_template(_key: &str, _buf: &mut String) {}
@@ -87,17 +84,21 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
key: &str,
el: &<R as Renderer>::Element,
el: &tachys::renderer::types::Element,
) -> Self::State {
<Arc<str> as AttributeValue<R>>::hydrate::<FROM_SERVER>(self.0, key, el)
<Arc<str> as AttributeValue>::hydrate::<FROM_SERVER>(self.0, key, el)
}
fn build(self, el: &<R as Renderer>::Element, key: &str) -> Self::State {
<Arc<str> as AttributeValue<R>>::build(self.0, el, key)
fn build(
self,
el: &tachys::renderer::types::Element,
key: &str,
) -> Self::State {
<Arc<str> as AttributeValue>::build(self.0, el, key)
}
fn rebuild(self, key: &str, state: &mut Self::State) {
<Arc<str> as AttributeValue<R>>::rebuild(self.0, key, state)
<Arc<str> as AttributeValue>::rebuild(self.0, key, state)
}
fn into_cloneable(self) -> Self::Cloneable {

View File

@@ -21,7 +21,6 @@ use tachys::{
html::attribute::Attribute,
hydration::Cursor,
reactive_graph::{OwnedView, OwnedViewState},
renderer::Renderer,
ssr::StreamBuilder,
view::{
add_attr::AddAnyAttr,
@@ -135,15 +134,14 @@ pub(crate) struct SuspenseBoundary<const TRANSITION: bool, Fal, Chil> {
pub children: Chil,
}
impl<const TRANSITION: bool, Fal, Chil, Rndr> Render<Rndr>
impl<const TRANSITION: bool, Fal, Chil> Render
for SuspenseBoundary<TRANSITION, Fal, Chil>
where
Fal: Render<Rndr> + Send + 'static,
Chil: Render<Rndr> + Send + 'static,
Rndr: Renderer + 'static,
Fal: Render + Send + 'static,
Chil: Render + Send + 'static,
{
type State = RenderEffect<
OwnedViewState<EitherKeepAliveState<Chil::State, Fal::State>, Rndr>,
OwnedViewState<EitherKeepAliveState<Chil::State, Fal::State>>,
>;
fn build(self) -> Self::State {
@@ -187,25 +185,24 @@ where
}
}
impl<const TRANSITION: bool, Fal, Chil, Rndr> AddAnyAttr<Rndr>
impl<const TRANSITION: bool, Fal, Chil> AddAnyAttr
for SuspenseBoundary<TRANSITION, Fal, Chil>
where
Fal: RenderHtml<Rndr> + Send + 'static,
Chil: RenderHtml<Rndr> + Send + 'static,
Rndr: Renderer + 'static,
Fal: RenderHtml + Send + 'static,
Chil: RenderHtml + Send + 'static,
{
type Output<SomeNewAttr: Attribute<Rndr>> = SuspenseBoundary<
type Output<SomeNewAttr: Attribute> = SuspenseBoundary<
TRANSITION,
Fal,
Chil::Output<SomeNewAttr::CloneableOwned>,
>;
fn add_any_attr<NewAttr: Attribute<Rndr>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Rndr>,
Self::Output<NewAttr>: RenderHtml,
{
let attr = attr.into_cloneable_owned();
let SuspenseBoundary {
@@ -223,12 +220,11 @@ where
}
}
impl<const TRANSITION: bool, Fal, Chil, Rndr> RenderHtml<Rndr>
impl<const TRANSITION: bool, Fal, Chil> RenderHtml
for SuspenseBoundary<TRANSITION, Fal, Chil>
where
Fal: RenderHtml<Rndr> + Send + 'static,
Chil: RenderHtml<Rndr> + Send + 'static,
Rndr: Renderer + 'static,
Fal: RenderHtml + Send + 'static,
Chil: RenderHtml + Send + 'static,
{
// i.e., if this is the child of another Suspense during SSR, don't wait for it: it will handle
// itself
@@ -405,7 +401,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<Rndr>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
let cursor = cursor.to_owned();
@@ -455,10 +451,9 @@ impl<T> Unsuspend<T> {
}
}
impl<T, Rndr> Render<Rndr> for Unsuspend<T>
impl<T> Render for Unsuspend<T>
where
T: Render<Rndr>,
Rndr: Renderer,
T: Render,
{
type State = T::State;
@@ -471,30 +466,28 @@ where
}
}
impl<T, Rndr> AddAnyAttr<Rndr> for Unsuspend<T>
impl<T> AddAnyAttr for Unsuspend<T>
where
T: AddAnyAttr<Rndr> + 'static,
Rndr: Renderer,
T: AddAnyAttr + 'static,
{
type Output<SomeNewAttr: Attribute<Rndr>> =
type Output<SomeNewAttr: Attribute> =
Unsuspend<T::Output<SomeNewAttr::CloneableOwned>>;
fn add_any_attr<NewAttr: Attribute<Rndr>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Rndr>,
Self::Output<NewAttr>: RenderHtml,
{
let attr = attr.into_cloneable_owned();
Unsuspend::new(move || (self.0)().add_any_attr(attr))
}
}
impl<T, Rndr> RenderHtml<Rndr> for Unsuspend<T>
impl<T> RenderHtml for Unsuspend<T>
where
T: RenderHtml<Rndr> + 'static,
Rndr: Renderer,
T: RenderHtml + 'static,
{
type AsyncOutput = Self;
@@ -535,7 +528,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<Rndr>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
(self.0)().hydrate::<FROM_SERVER>(cursor, position)

View File

@@ -5,7 +5,7 @@ fn simple_ssr_test() {
use leptos::prelude::*;
let (value, set_value) = signal(0);
let rendered: View<HtmlElement<_, _, _, Dom>> = view! {
let rendered: View<HtmlElement<_, _, _>> = view! {
<div>
<button on:click=move |_| set_value.update(|value| *value -= 1)>"-1"</button>
<span>"Value: " {move || value.get().to_string()} "!"</span>
@@ -36,7 +36,7 @@ fn ssr_test_with_components() {
}
}
let rendered: View<HtmlElement<_, _, _, Dom>> = view! {
let rendered: View<HtmlElement<_, _, _>> = view! {
<div class="counters">
<Counter initial_value=1/>
<Counter initial_value=2/>
@@ -66,7 +66,7 @@ fn ssr_test_with_snake_case_components() {
</div>
}
}
let rendered: View<HtmlElement<_, _, _, Dom>> = view! {
let rendered: View<HtmlElement<_, _, _>> = view! {
<div class="counters">
<SnakeCaseCounter initial_value=1/>
<SnakeCaseCounter initial_value=2/>
@@ -86,7 +86,7 @@ fn test_classes() {
use leptos::prelude::*;
let (value, _set_value) = signal(5);
let rendered: View<HtmlElement<_, _, _, Dom>> = view! {
let rendered: View<HtmlElement<_, _, _>> = view! {
<div
class="my big"
class:a=move || { value.get() > 10 }
@@ -104,7 +104,7 @@ fn ssr_with_styles() {
let (_, set_value) = signal(0);
let styles = "myclass";
let rendered: View<HtmlElement<_, _, _, Dom>> = view! { class=styles,
let rendered: View<HtmlElement<_, _, _>> = view! { class=styles,
<div>
<button class="btn" on:click=move |_| set_value.update(|value| *value -= 1)>
"-1"
@@ -124,7 +124,7 @@ fn ssr_option() {
use leptos::prelude::*;
let (_, _) = signal(0);
let rendered: View<HtmlElement<_, _, _, Dom>> = view! { <option></option> };
let rendered: View<HtmlElement<_, _, _>> = view! { <option></option> };
assert_eq!(rendered.to_html(), "<option></option>");
}

View File

@@ -188,20 +188,18 @@ mod view_implementations {
html::attribute::Attribute,
hydration::Cursor,
reactive_graph::{RenderEffectState, Suspend, SuspendState},
renderer::Renderer,
ssr::StreamBuilder,
view::{
add_attr::AddAnyAttr, Position, PositionState, Render, RenderHtml,
},
};
impl<T, R, Ser> Render<R> for Resource<T, Ser>
impl<T, Ser> Render for Resource<T, Ser>
where
T: Render<R> + Send + Sync + Clone,
T: Render + Send + Sync + Clone,
Ser: Send + 'static,
R: Renderer,
{
type State = RenderEffectState<SuspendState<T, R>>;
type State = RenderEffectState<SuspendState<T>>;
fn build(self) -> Self::State {
(move || Suspend::new(async move { self.await })).build()
@@ -212,19 +210,18 @@ mod view_implementations {
}
}
impl<T, R, Ser> AddAnyAttr<R> for Resource<T, Ser>
impl<T, Ser> AddAnyAttr for Resource<T, Ser>
where
T: RenderHtml<R> + Send + Sync + Clone,
T: RenderHtml + Send + Sync + Clone,
Ser: Send + 'static,
R: Renderer,
{
type Output<SomeNewAttr: Attribute<R>> = Box<
type Output<SomeNewAttr: Attribute> = Box<
dyn FnMut() -> Suspend<
Pin<
Box<
dyn Future<
Output = <T as AddAnyAttr<R>>::Output<
<SomeNewAttr::CloneableOwned as Attribute<R>>::CloneableOwned,
Output = <T as AddAnyAttr>::Output<
<SomeNewAttr::CloneableOwned as Attribute>::CloneableOwned,
>,
> + Send,
>,
@@ -232,22 +229,21 @@ mod view_implementations {
> + Send,
>;
fn add_any_attr<NewAttr: Attribute<R>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<R>,
Self::Output<NewAttr>: RenderHtml,
{
(move || Suspend::new(async move { self.await })).add_any_attr(attr)
}
}
impl<T, R, Ser> RenderHtml<R> for Resource<T, Ser>
impl<T, Ser> RenderHtml for Resource<T, Ser>
where
T: RenderHtml<R> + Send + Sync + Clone,
T: RenderHtml + Send + Sync + Clone,
Ser: Send + 'static,
R: Renderer,
{
type AsyncOutput = Option<T>;
@@ -296,7 +292,7 @@ mod view_implementations {
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
(move || Suspend::new(async move { self.await }))

View File

@@ -7,7 +7,6 @@ use leptos::{
dom::document,
html::attribute::Attribute,
hydration::Cursor,
renderer::{dom::Dom, Renderer},
view::{
add_attr::AddAnyAttr, Mountable, Position, PositionState, Render,
RenderHtml,
@@ -56,14 +55,14 @@ struct BodyView<At> {
struct BodyViewState<At>
where
At: Attribute<Dom>,
At: Attribute,
{
attributes: At::State,
}
impl<At> Render<Dom> for BodyView<At>
impl<At> Render for BodyView<At>
where
At: Attribute<Dom>,
At: Attribute,
{
type State = BodyViewState<At>;
@@ -79,19 +78,19 @@ where
}
}
impl<At> AddAnyAttr<Dom> for BodyView<At>
impl<At> AddAnyAttr for BodyView<At>
where
At: Attribute<Dom>,
At: Attribute,
{
type Output<SomeNewAttr: Attribute<Dom>> =
BodyView<<At as NextAttribute<Dom>>::Output<SomeNewAttr>>;
type Output<SomeNewAttr: Attribute> =
BodyView<<At as NextAttribute>::Output<SomeNewAttr>>;
fn add_any_attr<NewAttr: Attribute<Dom>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Dom>,
Self::Output<NewAttr>: RenderHtml,
{
BodyView {
attributes: self.attributes.add_any_attr(attr),
@@ -99,9 +98,9 @@ where
}
}
impl<At> RenderHtml<Dom> for BodyView<At>
impl<At> RenderHtml for BodyView<At>
where
At: Attribute<Dom>,
At: Attribute,
{
type AsyncOutput = BodyView<At::AsyncOutput>;
@@ -135,7 +134,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
_cursor: &Cursor<Dom>,
_cursor: &Cursor,
_position: &PositionState,
) -> Self::State {
let el = document().body().expect("there to be a <body> element");
@@ -145,20 +144,20 @@ where
}
}
impl<At> Mountable<Dom> for BodyViewState<At>
impl<At> Mountable for BodyViewState<At>
where
At: Attribute<Dom>,
At: Attribute,
{
fn unmount(&mut self) {}
fn mount(
&mut self,
_parent: &<Dom as Renderer>::Element,
_marker: Option<&<Dom as Renderer>::Node>,
_parent: &leptos::tachys::renderer::types::Element,
_marker: Option<&leptos::tachys::renderer::types::Node>,
) {
}
fn insert_before_this(&self, _child: &mut dyn Mountable<Dom>) -> bool {
fn insert_before_this(&self, _child: &mut dyn Mountable) -> bool {
false
}
}

View File

@@ -7,7 +7,6 @@ use leptos::{
dom::document,
html::attribute::Attribute,
hydration::Cursor,
renderer::{dom::Dom, Renderer},
view::{
add_attr::AddAnyAttr, Mountable, Position, PositionState, Render,
RenderHtml,
@@ -53,14 +52,14 @@ struct HtmlView<At> {
struct HtmlViewState<At>
where
At: Attribute<Dom>,
At: Attribute,
{
attributes: At::State,
}
impl<At> Render<Dom> for HtmlView<At>
impl<At> Render for HtmlView<At>
where
At: Attribute<Dom>,
At: Attribute,
{
type State = HtmlViewState<At>;
@@ -79,19 +78,19 @@ where
}
}
impl<At> AddAnyAttr<Dom> for HtmlView<At>
impl<At> AddAnyAttr for HtmlView<At>
where
At: Attribute<Dom>,
At: Attribute,
{
type Output<SomeNewAttr: Attribute<Dom>> =
HtmlView<<At as NextAttribute<Dom>>::Output<SomeNewAttr>>;
type Output<SomeNewAttr: Attribute> =
HtmlView<<At as NextAttribute>::Output<SomeNewAttr>>;
fn add_any_attr<NewAttr: Attribute<Dom>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Dom>,
Self::Output<NewAttr>: RenderHtml,
{
HtmlView {
attributes: self.attributes.add_any_attr(attr),
@@ -99,9 +98,9 @@ where
}
}
impl<At> RenderHtml<Dom> for HtmlView<At>
impl<At> RenderHtml for HtmlView<At>
where
At: Attribute<Dom>,
At: Attribute,
{
type AsyncOutput = HtmlView<At::AsyncOutput>;
@@ -135,7 +134,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
_cursor: &Cursor<Dom>,
_cursor: &Cursor,
_position: &PositionState,
) -> Self::State {
let el = document()
@@ -148,22 +147,22 @@ where
}
}
impl<At> Mountable<Dom> for HtmlViewState<At>
impl<At> Mountable for HtmlViewState<At>
where
At: Attribute<Dom>,
At: Attribute,
{
fn unmount(&mut self) {}
fn mount(
&mut self,
_parent: &<Dom as Renderer>::Element,
_marker: Option<&<Dom as Renderer>::Node>,
_parent: &leptos::tachys::renderer::types::Element,
_marker: Option<&leptos::tachys::renderer::types::Node>,
) {
// <Html> only sets attributes
// the <html> tag doesn't need to be mounted anywhere, of course
}
fn insert_before_this(&self, _child: &mut dyn Mountable<Dom>) -> bool {
fn insert_before_this(&self, _child: &mut dyn Mountable) -> bool {
false
}
}

View File

@@ -57,10 +57,9 @@ use leptos::{
dom::document,
html::{
attribute::Attribute,
element::{CreateElement, ElementType, HtmlElement},
element::{ElementType, HtmlElement},
},
hydration::Cursor,
renderer::{dom::Dom, Renderer},
view::{
add_attr::AddAnyAttr, Mountable, Position, PositionState, Render,
RenderHtml,
@@ -106,7 +105,7 @@ pub struct MetaContext {
/// Metadata associated with the `<title>` element.
pub(crate) title: TitleContext,
/// The hydration cursor for the location in the `<head>` for arbitrary tags will be rendered.
pub(crate) cursor: Arc<Lazy<SendWrapper<Cursor<Dom>>>>,
pub(crate) cursor: Arc<Lazy<SendWrapper<Cursor>>>,
}
impl MetaContext {
@@ -123,7 +122,7 @@ const COMMENT_NODE: u16 = 8;
impl Default for MetaContext {
fn default() -> Self {
let build_cursor: fn() -> SendWrapper<Cursor<Dom>> = || {
let build_cursor: fn() -> SendWrapper<Cursor> = || {
let head = document().head().expect("missing <head> element");
let mut cursor = None;
let mut child = head.first_child();
@@ -323,10 +322,10 @@ pub fn use_head() -> MetaContext {
}
pub(crate) fn register<E, At, Ch>(
el: HtmlElement<E, At, Ch, Dom>,
el: HtmlElement<E, At, Ch>,
) -> RegisteredMetaTag<E, At, Ch>
where
HtmlElement<E, At, Ch, Dom>: RenderHtml<Dom>,
HtmlElement<E, At, Ch>: RenderHtml,
{
#[allow(unused_mut)] // used for `ssr`
let mut el = Some(el);
@@ -358,19 +357,19 @@ where
struct RegisteredMetaTag<E, At, Ch> {
// this is `None` if we've already taken it out to render to HTML on the server
// we don't render it in place in RenderHtml, so it's fine
el: Option<HtmlElement<E, At, Ch, Dom>>,
el: Option<HtmlElement<E, At, Ch>>,
}
struct RegisteredMetaTagState<E, At, Ch>
where
HtmlElement<E, At, Ch, Dom>: Render<Dom>,
HtmlElement<E, At, Ch>: Render,
{
state: <HtmlElement<E, At, Ch, Dom> as Render<Dom>>::State,
state: <HtmlElement<E, At, Ch> as Render>::State,
}
impl<E, At, Ch> Drop for RegisteredMetaTagState<E, At, Ch>
where
HtmlElement<E, At, Ch, Dom>: Render<Dom>,
HtmlElement<E, At, Ch>: Render,
{
fn drop(&mut self) {
self.state.unmount();
@@ -387,11 +386,11 @@ fn document_head() -> HtmlHeadElement {
})
}
impl<E, At, Ch> Render<Dom> for RegisteredMetaTag<E, At, Ch>
impl<E, At, Ch> Render for RegisteredMetaTag<E, At, Ch>
where
E: ElementType + CreateElement<Dom>,
At: Attribute<Dom>,
Ch: Render<Dom>,
E: ElementType,
At: Attribute,
Ch: Render,
{
type State = RegisteredMetaTagState<E, At, Ch>;
@@ -405,24 +404,21 @@ where
}
}
impl<E, At, Ch> AddAnyAttr<Dom> for RegisteredMetaTag<E, At, Ch>
impl<E, At, Ch> AddAnyAttr for RegisteredMetaTag<E, At, Ch>
where
E: ElementType + CreateElement<Dom> + Send,
At: Attribute<Dom> + Send,
Ch: RenderHtml<Dom> + Send,
E: ElementType + Send,
At: Attribute + Send,
Ch: RenderHtml + Send,
{
type Output<SomeNewAttr: Attribute<Dom>> = RegisteredMetaTag<
E,
<At as NextAttribute<Dom>>::Output<SomeNewAttr>,
Ch,
>;
type Output<SomeNewAttr: Attribute> =
RegisteredMetaTag<E, <At as NextAttribute>::Output<SomeNewAttr>, Ch>;
fn add_any_attr<NewAttr: Attribute<Dom>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Dom>,
Self::Output<NewAttr>: RenderHtml,
{
RegisteredMetaTag {
el: self.el.map(|inner| inner.add_any_attr(attr)),
@@ -430,11 +426,11 @@ where
}
}
impl<E, At, Ch> RenderHtml<Dom> for RegisteredMetaTag<E, At, Ch>
impl<E, At, Ch> RenderHtml for RegisteredMetaTag<E, At, Ch>
where
E: ElementType + CreateElement<Dom>,
At: Attribute<Dom>,
Ch: RenderHtml<Dom> + Send,
E: ElementType,
At: Attribute,
Ch: RenderHtml + Send,
{
type AsyncOutput = Self;
@@ -461,7 +457,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
_cursor: &Cursor<Dom>,
_cursor: &Cursor,
_position: &PositionState,
) -> Self::State {
let cursor = use_context::<MetaContext>()
@@ -471,18 +467,18 @@ where
)
.cursor;
let state = self.el.unwrap().hydrate::<FROM_SERVER>(
&*cursor,
&cursor,
&PositionState::new(Position::NextChild),
);
RegisteredMetaTagState { state }
}
}
impl<E, At, Ch> Mountable<Dom> for RegisteredMetaTagState<E, At, Ch>
impl<E, At, Ch> Mountable for RegisteredMetaTagState<E, At, Ch>
where
E: ElementType + CreateElement<Dom>,
At: Attribute<Dom>,
Ch: Render<Dom>,
E: ElementType,
At: Attribute,
Ch: Render,
{
fn unmount(&mut self) {
self.state.unmount();
@@ -490,8 +486,8 @@ where
fn mount(
&mut self,
_parent: &<Dom as Renderer>::Element,
_marker: Option<&<Dom as Renderer>::Node>,
_parent: &leptos::tachys::renderer::types::Element,
_marker: Option<&leptos::tachys::renderer::types::Node>,
) {
// we always mount this to the <head>, which is the whole point
// but this shouldn't warn about the parent being a regular element or being unused
@@ -500,7 +496,7 @@ where
self.state.mount(&document_head(), None);
}
fn insert_before_this(&self, _child: &mut dyn Mountable<Dom>) -> bool {
fn insert_before_this(&self, _child: &mut dyn Mountable) -> bool {
// Registered meta tags will be mounted in the <head>, but *seem* to be mounted somewhere
// else in the DOM. We should never tell the renderer that we have successfully mounted
// something before this, because if e.g., a <Meta/> is the first item in an Either, then
@@ -525,7 +521,7 @@ struct MetaTagsView;
// rendering HTML for all the tags that will be injected into the `<head>`
//
// client-side rendering is handled by the individual components
impl Render<Dom> for MetaTagsView {
impl Render for MetaTagsView {
type State = ();
fn build(self) -> Self::State {}
@@ -533,21 +529,21 @@ impl Render<Dom> for MetaTagsView {
fn rebuild(self, _state: &mut Self::State) {}
}
impl AddAnyAttr<Dom> for MetaTagsView {
type Output<SomeNewAttr: Attribute<Dom>> = MetaTagsView;
impl AddAnyAttr for MetaTagsView {
type Output<SomeNewAttr: Attribute> = MetaTagsView;
fn add_any_attr<NewAttr: Attribute<Dom>>(
fn add_any_attr<NewAttr: Attribute>(
self,
_attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Dom>,
Self::Output<NewAttr>: RenderHtml,
{
self
}
}
impl RenderHtml<Dom> for MetaTagsView {
impl RenderHtml for MetaTagsView {
type AsyncOutput = Self;
const MIN_LENGTH: usize = 0;
@@ -570,7 +566,7 @@ impl RenderHtml<Dom> for MetaTagsView {
fn hydrate<const FROM_SERVER: bool>(
self,
_cursor: &Cursor<Dom>,
_cursor: &Cursor,
_position: &PositionState,
) -> Self::State {
}

View File

@@ -10,7 +10,6 @@ use leptos::{
tachys::{
dom::document,
hydration::Cursor,
renderer::{dom::Dom, Renderer},
view::{
add_attr::AddAnyAttr, Mountable, Position, PositionState, Render,
RenderHtml,
@@ -187,7 +186,7 @@ struct TitleViewState {
effect: RenderEffect<Oco<'static, str>>,
}
impl Render<Dom> for TitleView {
impl Render for TitleView {
type State = TitleViewState;
fn build(mut self) -> Self::State {
@@ -219,21 +218,21 @@ impl Render<Dom> for TitleView {
}
}
impl AddAnyAttr<Dom> for TitleView {
type Output<SomeNewAttr: Attribute<Dom>> = TitleView;
impl AddAnyAttr for TitleView {
type Output<SomeNewAttr: Attribute> = TitleView;
fn add_any_attr<NewAttr: Attribute<Dom>>(
fn add_any_attr<NewAttr: Attribute>(
self,
_attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Dom>,
Self::Output<NewAttr>: RenderHtml,
{
self
}
}
impl RenderHtml<Dom> for TitleView {
impl RenderHtml for TitleView {
type AsyncOutput = Self;
const MIN_LENGTH: usize = 0;
@@ -257,7 +256,7 @@ impl RenderHtml<Dom> for TitleView {
fn hydrate<const FROM_SERVER: bool>(
mut self,
_cursor: &Cursor<Dom>,
_cursor: &Cursor,
_position: &PositionState,
) -> Self::State {
let el = self.el();
@@ -285,19 +284,19 @@ impl RenderHtml<Dom> for TitleView {
}
}
impl Mountable<Dom> for TitleViewState {
impl Mountable for TitleViewState {
fn unmount(&mut self) {}
fn mount(
&mut self,
_parent: &<Dom as Renderer>::Element,
_marker: Option<&<Dom as Renderer>::Node>,
_parent: &leptos::tachys::renderer::types::Element,
_marker: Option<&leptos::tachys::renderer::types::Node>,
) {
// <title> doesn't need to be mounted
// TitleView::el() guarantees that there is a <title> in the <head>
}
fn insert_before_this(&self, _child: &mut dyn Mountable<Dom>) -> bool {
fn insert_before_this(&self, _child: &mut dyn Mountable) -> bool {
false
}
}

View File

@@ -24,11 +24,10 @@ use reactive_graph::{
use std::{
borrow::Cow,
fmt::{Debug, Display},
marker::PhantomData,
sync::Arc,
time::Duration,
};
use tachys::{renderer::dom::Dom, view::any_view::AnyView};
use tachys::view::any_view::AnyView;
#[derive(Debug)]
pub struct RouteChildren<Children>(Children);
@@ -211,7 +210,7 @@ pub fn Routes<Defs, FallbackFn, Fallback>(
children: RouteChildren<Defs>,
) -> impl IntoView
where
Defs: MatchNestedRoutes<Dom> + Clone + Send + 'static,
Defs: MatchNestedRoutes + Clone + Send + 'static,
FallbackFn: FnOnce() -> Fallback + Clone + Send + 'static,
Fallback: IntoView + 'static,
{
@@ -243,7 +242,6 @@ where
current_url: current_url.clone(),
base: base.clone(),
fallback: fallback.clone(),
rndr: PhantomData,
set_is_routing,
}
}
@@ -255,7 +253,7 @@ pub fn FlatRoutes<Defs, FallbackFn, Fallback>(
children: RouteChildren<Defs>,
) -> impl IntoView
where
Defs: MatchNestedRoutes<Dom> + Clone + Send + 'static,
Defs: MatchNestedRoutes + Clone + Send + 'static,
FallbackFn: FnOnce() -> Fallback + Clone + Send + 'static,
Fallback: IntoView + 'static,
{
@@ -301,9 +299,9 @@ pub fn Route<Segments, View>(
path: Segments,
view: View,
#[prop(optional)] ssr: SsrMode,
) -> NestedRoute<Segments, (), (), View, Dom>
) -> NestedRoute<Segments, (), (), View>
where
View: ChooseView<Dom>,
View: ChooseView,
{
NestedRoute::new(path, view).ssr_mode(ssr)
}
@@ -314,9 +312,9 @@ pub fn ParentRoute<Segments, View, Children>(
view: View,
children: RouteChildren<Children>,
#[prop(optional)] ssr: SsrMode,
) -> NestedRoute<Segments, Children, (), View, Dom>
) -> NestedRoute<Segments, Children, (), View>
where
View: ChooseView<Dom>,
View: ChooseView,
{
let children = children.into_inner();
NestedRoute::new(path, view).ssr_mode(ssr).child(children)
@@ -329,7 +327,7 @@ pub fn ProtectedRoute<Segments, ViewFn, View, C, PathFn, P>(
condition: C,
redirect_path: PathFn,
#[prop(optional)] ssr: SsrMode,
) -> NestedRoute<Segments, (), (), impl Fn() -> AnyView<Dom> + Send + Clone, Dom>
) -> NestedRoute<Segments, (), (), impl Fn() -> AnyView + Send + Clone>
where
ViewFn: Fn() -> View + Send + Clone + 'static,
View: IntoView + 'static,
@@ -372,13 +370,7 @@ pub fn ProtectedParentRoute<Segments, ViewFn, View, C, PathFn, P, Children>(
redirect_path: PathFn,
children: RouteChildren<Children>,
#[prop(optional)] ssr: SsrMode,
) -> NestedRoute<
Segments,
Children,
(),
impl Fn() -> AnyView<Dom> + Send + Clone,
Dom,
>
) -> NestedRoute<Segments, Children, (), impl Fn() -> AnyView + Send + Clone>
where
ViewFn: Fn() -> View + Send + Clone + 'static,
View: IntoView + 'static,

View File

@@ -21,7 +21,6 @@ use std::{cell::RefCell, iter, mem, rc::Rc};
use tachys::{
hydration::Cursor,
reactive_graph::OwnedView,
renderer::Renderer,
ssr::StreamBuilder,
view::{
add_attr::AddAnyAttr, Mountable, Position, PositionState, Render,
@@ -29,23 +28,22 @@ use tachys::{
},
};
pub(crate) struct FlatRoutesView<Loc, Defs, FalFn, R> {
pub(crate) struct FlatRoutesView<Loc, Defs, FalFn> {
pub current_url: ArcRwSignal<Url>,
pub location: Option<Loc>,
pub routes: Routes<Defs, R>,
pub routes: Routes<Defs>,
pub fallback: FalFn,
pub outer_owner: Owner,
pub set_is_routing: Option<SignalSetter<bool>>,
}
pub struct FlatRoutesViewState<Defs, Fal, R>
pub struct FlatRoutesViewState<Defs, Fal>
where
Defs: MatchNestedRoutes<R> + 'static,
Fal: Render<R> + 'static,
R: Renderer + 'static
Defs: MatchNestedRoutes + 'static,
Fal: Render + 'static,
{
#[allow(clippy::type_complexity)]
view: <EitherOf3<(), Fal, <Defs::Match as MatchInterface<R>>::View> as Render<R>>::State,
view: <EitherOf3<(), Fal, OwnedView<<Defs::Match as MatchInterface>::View>> as Render>::State,
id: Option<RouteMatchId>,
owner: Owner,
params: ArcRwSignal<ParamsMap>,
@@ -54,11 +52,10 @@ where
matched: ArcRwSignal<String>
}
impl<Defs, Fal, R> Mountable<R> for FlatRoutesViewState<Defs, Fal, R>
impl<Defs, Fal> Mountable for FlatRoutesViewState<Defs, Fal>
where
Defs: MatchNestedRoutes<R> + 'static,
Fal: Render<R> + 'static,
R: Renderer + 'static,
Defs: MatchNestedRoutes + 'static,
Fal: Render + 'static,
{
fn unmount(&mut self) {
self.view.unmount();
@@ -66,26 +63,25 @@ where
fn mount(
&mut self,
parent: &<R as Renderer>::Element,
marker: Option<&<R as Renderer>::Node>,
parent: &leptos::tachys::renderer::types::Element,
marker: Option<&leptos::tachys::renderer::types::Node>,
) {
self.view.mount(parent, marker);
}
fn insert_before_this(&self, child: &mut dyn Mountable<R>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
self.view.insert_before_this(child)
}
}
impl<Loc, Defs, FalFn, Fal, R> Render<R> for FlatRoutesView<Loc, Defs, FalFn, R>
impl<Loc, Defs, FalFn, Fal> Render for FlatRoutesView<Loc, Defs, FalFn>
where
Loc: LocationProvider,
Defs: MatchNestedRoutes<R> + 'static,
Defs: MatchNestedRoutes + 'static,
FalFn: FnOnce() -> Fal + Send,
Fal: Render<R> + 'static,
R: Renderer + 'static,
Fal: Render + 'static,
{
type State = Rc<RefCell<FlatRoutesViewState<Defs, Fal, R>>>;
type State = Rc<RefCell<FlatRoutesViewState<Defs, Fal>>>;
fn build(self) -> Self::State {
let FlatRoutesView {
@@ -151,7 +147,7 @@ where
provide_context(params_memo);
provide_context(url);
provide_context(Matched(ArcMemo::from(matched)));
view.choose().await
OwnedView::new(view.choose().await)
}
})
}));
@@ -296,7 +292,7 @@ where
provide_context(Matched(ArcMemo::from(
new_matched,
)));
let view =
let view = OwnedView::new(
if let Some(set_is_routing) = set_is_routing {
set_is_routing.set(true);
let value =
@@ -306,7 +302,8 @@ where
value
} else {
view.choose().await
};
},
);
// only update the route if it's still the current path
// i.e., if we've navigated away before this has loaded, do nothing
@@ -332,41 +329,37 @@ where
}
}
impl<Loc, Defs, FalFn, Fal, R> AddAnyAttr<R>
for FlatRoutesView<Loc, Defs, FalFn, R>
impl<Loc, Defs, FalFn, Fal> AddAnyAttr for FlatRoutesView<Loc, Defs, FalFn>
where
Loc: LocationProvider + Send,
Defs: MatchNestedRoutes<R> + Send + 'static,
Defs: MatchNestedRoutes + Send + 'static,
FalFn: FnOnce() -> Fal + Send,
Fal: RenderHtml<R> + 'static,
R: Renderer + 'static,
Fal: RenderHtml + 'static,
{
type Output<SomeNewAttr: leptos::attr::Attribute<R>> =
FlatRoutesView<Loc, Defs, FalFn, R>;
type Output<SomeNewAttr: leptos::attr::Attribute> =
FlatRoutesView<Loc, Defs, FalFn>;
fn add_any_attr<NewAttr: leptos::attr::Attribute<R>>(
fn add_any_attr<NewAttr: leptos::attr::Attribute>(
self,
_attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<R>,
Self::Output<NewAttr>: RenderHtml,
{
todo!()
}
}
impl<Loc, Defs, FalFn, Fal, R> FlatRoutesView<Loc, Defs, FalFn, R>
impl<Loc, Defs, FalFn, Fal> FlatRoutesView<Loc, Defs, FalFn>
where
Loc: LocationProvider + Send,
Defs: MatchNestedRoutes<R> + Send + 'static,
Defs: MatchNestedRoutes + Send + 'static,
FalFn: FnOnce() -> Fal + Send,
Fal: RenderHtml<R> + 'static,
R: Renderer + 'static,
Fal: RenderHtml + 'static,
{
fn choose_ssr(
self,
) -> OwnedView<Either<Fal, <Defs::Match as MatchInterface<R>>::View>, R>
{
) -> OwnedView<Either<Fal, <Defs::Match as MatchInterface>::View>> {
let current_url = self.current_url.read_untracked();
let new_match = self.routes.match_route(current_url.path());
let owner = self.outer_owner.child();
@@ -411,21 +404,19 @@ where
}
}
impl<Loc, Defs, FalFn, Fal, R> RenderHtml<R>
for FlatRoutesView<Loc, Defs, FalFn, R>
impl<Loc, Defs, FalFn, Fal> RenderHtml for FlatRoutesView<Loc, Defs, FalFn>
where
Loc: LocationProvider + Send,
Defs: MatchNestedRoutes<R> + Send + 'static,
Defs: MatchNestedRoutes + Send + 'static,
FalFn: FnOnce() -> Fal + Send,
Fal: RenderHtml<R> + 'static,
R: Renderer + 'static,
Fal: RenderHtml + 'static,
{
type AsyncOutput = Self;
const MIN_LENGTH: usize = <Either<
Fal,
<Defs::Match as MatchInterface<R>>::View,
> as RenderHtml<R>>::MIN_LENGTH;
<Defs::Match as MatchInterface>::View,
> as RenderHtml>::MIN_LENGTH;
fn dry_resolve(&mut self) {}
@@ -509,7 +500,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
// this can be mostly the same as the build() implementation, but with hydrate()
@@ -582,7 +573,7 @@ where
provide_context(params_memo);
provide_context(url);
provide_context(Matched(ArcMemo::from(matched)));
view.choose().await
OwnedView::new(view.choose().await)
}
})
}));

View File

@@ -13,7 +13,7 @@ use std::{
future::Future,
mem,
};
use tachys::{renderer::Renderer, view::RenderHtml};
use tachys::view::RenderHtml;
#[derive(Clone, Debug, Default)]
/// A route that this application can serve.
@@ -220,12 +220,9 @@ impl RouteList {
static GENERATED: RefCell<Option<RouteList>> = const { RefCell::new(None) };
}
pub fn generate<T, Rndr>(app: impl FnOnce() -> T) -> Option<Self>
pub fn generate<T>(app: impl FnOnce() -> T) -> Option<Self>
where
T: RenderHtml<Rndr>,
Rndr: Renderer,
Rndr::Node: Clone,
Rndr::Element: Clone,
T: RenderHtml,
{
Self::IS_GENERATING.set(true);
// run the app once, but throw away the HTML

View File

@@ -1,14 +1,10 @@
use either_of::*;
use std::{future::Future, marker::PhantomData};
use tachys::{
renderer::Renderer,
view::{any_view::AnyView, Render},
};
use tachys::view::{any_view::AnyView, Render};
pub trait ChooseView<R>
pub trait ChooseView
where
Self: Send + Clone + 'static,
R: Renderer + 'static,
{
type Output;
@@ -17,11 +13,10 @@ where
fn preload(&self) -> impl Future<Output = ()>;
}
impl<F, View, R> ChooseView<R> for F
impl<F, View> ChooseView for F
where
F: Fn() -> View + Send + Clone + 'static,
View: Render<R> + Send,
R: Renderer + 'static,
View: Render + Send,
{
type Output = View;
@@ -32,12 +27,11 @@ where
async fn preload(&self) {}
}
impl<T, R> ChooseView<R> for Lazy<T>
impl<T> ChooseView for Lazy<T>
where
T: LazyRoute<R>,
R: Renderer + 'static,
T: LazyRoute,
{
type Output = AnyView<R>;
type Output = AnyView;
async fn choose(self) -> Self::Output {
T::data().view().await
@@ -48,13 +42,10 @@ where
}
}
pub trait LazyRoute<R>: Send + 'static
where
R: Renderer,
{
pub trait LazyRoute: Send + 'static {
fn data() -> Self;
fn view(self) -> impl Future<Output = AnyView<R>>;
fn view(self) -> impl Future<Output = AnyView>;
}
#[derive(Debug)]
@@ -82,10 +73,7 @@ impl<T> Default for Lazy<T> {
}
}
impl<R> ChooseView<R> for ()
where
R: Renderer + 'static,
{
impl ChooseView for () {
type Output = ();
async fn choose(self) -> Self::Output {}
@@ -93,11 +81,10 @@ where
async fn preload(&self) {}
}
impl<A, B, Rndr> ChooseView<Rndr> for Either<A, B>
impl<A, B> ChooseView for Either<A, B>
where
A: ChooseView<Rndr>,
B: ChooseView<Rndr>,
Rndr: Renderer + 'static,
A: ChooseView,
B: ChooseView,
{
type Output = Either<A::Output, B::Output>;
@@ -118,10 +105,9 @@ where
macro_rules! tuples {
($either:ident => $($ty:ident),*) => {
impl<$($ty,)* Rndr> ChooseView<Rndr> for $either<$($ty,)*>
impl<$($ty,)*> ChooseView for $either<$($ty,)*>
where
$($ty: ChooseView<Rndr>,)*
Rndr: Renderer + 'static,
$($ty: ChooseView,)*
{
type Output = $either<$($ty::Output,)*>;

View File

@@ -9,21 +9,17 @@ mod vertical;
use crate::{static_routes::RegenerationFn, Method, SsrMode};
pub use horizontal::*;
pub use nested::*;
use std::{borrow::Cow, collections::HashSet, marker::PhantomData};
use tachys::{
renderer::Renderer,
view::{Render, RenderHtml},
};
use std::{borrow::Cow, collections::HashSet};
use tachys::view::{Render, RenderHtml};
pub use vertical::*;
#[derive(Debug)]
pub struct Routes<Children, Rndr> {
pub struct Routes<Children> {
base: Option<Cow<'static, str>>,
children: Children,
ty: PhantomData<Rndr>,
}
impl<Children, Rndr> Clone for Routes<Children, Rndr>
impl<Children> Clone for Routes<Children>
where
Children: Clone,
{
@@ -31,17 +27,15 @@ where
Self {
base: self.base.clone(),
children: self.children.clone(),
ty: PhantomData,
}
}
}
impl<Children, Rndr> Routes<Children, Rndr> {
impl<Children> Routes<Children> {
pub fn new(children: Children) -> Self {
Self {
base: None,
children,
ty: PhantomData,
}
}
@@ -52,15 +46,13 @@ impl<Children, Rndr> Routes<Children, Rndr> {
Self {
base: Some(base.into()),
children,
ty: PhantomData,
}
}
}
impl<Children, Rndr> Routes<Children, Rndr>
impl<Children> Routes<Children>
where
Rndr: Renderer + 'static,
Children: MatchNestedRoutes<Rndr>,
Children: MatchNestedRoutes,
{
pub fn match_route(&self, path: &str) -> Option<Children::Match> {
let path = match &self.base {
@@ -101,12 +93,9 @@ where
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct RouteMatchId(pub(crate) u16);
pub trait MatchInterface<R>
where
R: Renderer + 'static,
{
type Child: MatchInterface<R> + MatchParams + 'static;
type View: Render<R> + RenderHtml<R> + Send + 'static;
pub trait MatchInterface {
type Child: MatchInterface + MatchParams + 'static;
type View: Render + RenderHtml + Send + 'static;
fn as_id(&self) -> RouteMatchId;
@@ -114,7 +103,7 @@ where
fn into_view_and_child(
self,
) -> (impl ChooseView<R, Output = Self::View>, Option<Self::Child>);
) -> (impl ChooseView<Output = Self::View>, Option<Self::Child>);
}
pub trait MatchParams {
@@ -123,13 +112,10 @@ pub trait MatchParams {
fn to_params(&self) -> Self::Params;
}
pub trait MatchNestedRoutes<R>
where
R: Renderer + 'static,
{
pub trait MatchNestedRoutes {
type Data;
type View;
type Match: MatchInterface<R> + MatchParams;
type Match: MatchInterface + MatchParams;
fn match_nested<'a>(
&'a self,
@@ -157,12 +143,11 @@ mod tests {
WildcardSegment,
};
use either_of::Either;
use tachys::renderer::dom::Dom;
#[test]
pub fn matches_single_root_route() {
let routes =
Routes::<_, Dom>::new(NestedRoute::new(StaticSegment("/"), || ()));
Routes::<_>::new(NestedRoute::new(StaticSegment("/"), || ()));
let matched = routes.match_route("/");
assert!(matched.is_some());
// this case seems like it should match, but implementing it interferes with
@@ -178,7 +163,7 @@ mod tests {
#[test]
pub fn matches_nested_route() {
let routes: Routes<_, Dom> =
let routes: Routes<_> =
Routes::new(NestedRoute::new(StaticSegment(""), || "Home").child(
NestedRoute::new(
(StaticSegment("author"), StaticSegment("contact")),
@@ -200,17 +185,17 @@ mod tests {
);
let matched = routes.match_route("/author/contact").unwrap();
assert_eq!(MatchInterface::<Dom>::as_matched(&matched), "");
let (_, child) = MatchInterface::<Dom>::into_view_and_child(matched);
assert_eq!(MatchInterface::as_matched(&matched), "");
let (_, child) = MatchInterface::into_view_and_child(matched);
assert_eq!(
MatchInterface::<Dom>::as_matched(&child.unwrap()),
MatchInterface::as_matched(&child.unwrap()),
"/author/contact"
);
}
#[test]
pub fn does_not_match_route_unless_full_param_matches() {
let routes = Routes::<_, Dom>::new((
let routes = Routes::<_>::new((
NestedRoute::new(StaticSegment("/property-api"), || ()),
NestedRoute::new(StaticSegment("/property"), || ()),
));
@@ -220,7 +205,7 @@ mod tests {
#[test]
pub fn does_not_match_incomplete_route() {
let routes: Routes<_, Dom> =
let routes: Routes<_> =
Routes::new(NestedRoute::new(StaticSegment(""), || "Home").child(
NestedRoute::new(
(StaticSegment("author"), StaticSegment("contact")),
@@ -233,7 +218,7 @@ mod tests {
#[test]
pub fn chooses_between_nested_routes() {
let routes: Routes<_, Dom> = Routes::new((
let routes: Routes<_> = Routes::new((
NestedRoute::new(StaticSegment("/"), || ()).child((
NestedRoute::new(StaticSegment(""), || ()),
NestedRoute::new(StaticSegment("about"), || ()),
@@ -287,7 +272,7 @@ mod tests {
#[test]
pub fn arbitrary_nested_routes() {
let routes: Routes<_, Dom> = Routes::new_with_base(
let routes: Routes<_> = Routes::new_with_base(
(
NestedRoute::new(StaticSegment("/"), || ()).child((
NestedRoute::new(StaticSegment("/"), || ()),

View File

@@ -8,32 +8,27 @@ use either_of::Either;
use std::{
borrow::Cow,
collections::HashSet,
marker::PhantomData,
sync::atomic::{AtomicU16, Ordering},
};
use tachys::{
renderer::Renderer,
view::{Render, RenderHtml},
};
use tachys::view::{Render, RenderHtml};
mod tuples;
static ROUTE_ID: AtomicU16 = AtomicU16::new(1);
#[derive(Debug, PartialEq, Eq)]
pub struct NestedRoute<Segments, Children, Data, View, R> {
pub struct NestedRoute<Segments, Children, Data, View> {
id: u16,
segments: Segments,
children: Option<Children>,
data: Data,
view: View,
rndr: PhantomData<R>,
methods: HashSet<Method>,
ssr_mode: SsrMode,
}
impl<Segments, Children, Data, View, R> Clone
for NestedRoute<Segments, Children, Data, View, R>
impl<Segments, Children, Data, View> Clone
for NestedRoute<Segments, Children, Data, View>
where
Segments: Clone,
Children: Clone,
@@ -47,18 +42,16 @@ where
children: self.children.clone(),
data: self.data.clone(),
view: self.view.clone(),
rndr: PhantomData,
methods: self.methods.clone(),
ssr_mode: self.ssr_mode.clone(),
}
}
}
impl<Segments, View, R> NestedRoute<Segments, (), (), View, R> {
impl<Segments, View> NestedRoute<Segments, (), (), View> {
pub fn new(path: Segments, view: View) -> Self
where
View: ChooseView<R>,
R: Renderer + 'static,
View: ChooseView,
{
Self {
id: ROUTE_ID.fetch_add(1, Ordering::Relaxed),
@@ -66,24 +59,22 @@ impl<Segments, View, R> NestedRoute<Segments, (), (), View, R> {
children: None,
data: (),
view,
rndr: PhantomData,
methods: [Method::Get].into(),
ssr_mode: Default::default(),
}
}
}
impl<Segments, Data, View, R> NestedRoute<Segments, (), Data, View, R> {
impl<Segments, Data, View> NestedRoute<Segments, (), Data, View> {
pub fn child<Children>(
self,
child: Children,
) -> NestedRoute<Segments, Children, Data, View, R> {
) -> NestedRoute<Segments, Children, Data, View> {
let Self {
id,
segments,
data,
view,
rndr,
ssr_mode,
methods,
..
@@ -96,7 +87,6 @@ impl<Segments, Data, View, R> NestedRoute<Segments, (), Data, View, R> {
view,
ssr_mode,
methods,
rndr,
}
}
@@ -146,13 +136,12 @@ where
}
}
impl<ParamsIter, Child, View, Rndr> MatchInterface<Rndr>
impl<ParamsIter, Child, View> MatchInterface
for NestedMatch<ParamsIter, Child, View>
where
Rndr: Renderer + 'static,
Child: MatchInterface<Rndr> + MatchParams + 'static,
View: ChooseView<Rndr>,
View::Output: Render<Rndr> + RenderHtml<Rndr> + Send + 'static,
Child: MatchInterface + MatchParams + 'static,
View: ChooseView,
View::Output: Render + RenderHtml + Send + 'static,
{
type Child = Child;
type View = View::Output;
@@ -167,28 +156,24 @@ where
fn into_view_and_child(
self,
) -> (
impl ChooseView<Rndr, Output = Self::View>,
Option<Self::Child>,
) {
) -> (impl ChooseView<Output = Self::View>, Option<Self::Child>) {
(self.view_fn, self.child)
}
}
impl<Segments, Children, Data, View, Rndr> MatchNestedRoutes<Rndr>
for NestedRoute<Segments, Children, Data, View, Rndr>
impl<Segments, Children, Data, View> MatchNestedRoutes
for NestedRoute<Segments, Children, Data, View>
where
Self: 'static,
Rndr: Renderer + 'static,
Segments: PossibleRouteMatch + std::fmt::Debug,
<<Segments as PossibleRouteMatch>::ParamsIter as IntoIterator>::IntoIter: Clone,
Children: MatchNestedRoutes<Rndr>,
<<<Children as MatchNestedRoutes<Rndr>>::Match as MatchParams>::Params as IntoIterator>::IntoIter: Clone,
Children: MatchNestedRoutes,
<<<Children as MatchNestedRoutes>::Match as MatchParams>::Params as IntoIterator>::IntoIter: Clone,
Children::Match: MatchParams,
Children: 'static,
<Children::Match as MatchParams>::Params: Clone,
View: ChooseView<Rndr> + Clone,
View::Output: Render<Rndr> + RenderHtml<Rndr> + Send + 'static,
View: ChooseView + Clone,
View::Output: Render + RenderHtml + Send + 'static,
{
type Data = Data;
type View = View::Output;

View File

@@ -3,7 +3,6 @@ use crate::{ChooseView, GeneratedRouteData, MatchParams};
use core::iter;
use either_of::*;
use std::borrow::Cow;
use tachys::renderer::Renderer;
impl MatchParams for () {
type Params = iter::Empty<(Cow<'static, str>, String)>;
@@ -13,10 +12,7 @@ impl MatchParams for () {
}
}
impl<Rndr> MatchInterface<Rndr> for ()
where
Rndr: Renderer + 'static,
{
impl MatchInterface for () {
type Child = ();
type View = ();
@@ -30,18 +26,12 @@ where
fn into_view_and_child(
self,
) -> (
impl ChooseView<Rndr, Output = Self::View>,
Option<Self::Child>,
) {
) -> (impl ChooseView<Output = Self::View>, Option<Self::Child>) {
((), None)
}
}
impl<Rndr> MatchNestedRoutes<Rndr> for ()
where
Rndr: Renderer + 'static,
{
impl MatchNestedRoutes for () {
type Data = ();
type View = ();
type Match = ();
@@ -74,10 +64,9 @@ where
}
}
impl<A, Rndr> MatchInterface<Rndr> for (A,)
impl<A> MatchInterface for (A,)
where
A: MatchInterface<Rndr> + 'static,
Rndr: Renderer + 'static,
A: MatchInterface + 'static,
{
type Child = A::Child;
type View = A::View;
@@ -92,18 +81,14 @@ where
fn into_view_and_child(
self,
) -> (
impl ChooseView<Rndr, Output = Self::View>,
Option<Self::Child>,
) {
) -> (impl ChooseView<Output = Self::View>, Option<Self::Child>) {
self.0.into_view_and_child()
}
}
impl<A, Rndr> MatchNestedRoutes<Rndr> for (A,)
impl<A> MatchNestedRoutes for (A,)
where
A: MatchNestedRoutes<Rndr> + 'static,
Rndr: Renderer + 'static,
A: MatchNestedRoutes + 'static,
{
type Data = A::Data;
type View = A::View;
@@ -141,11 +126,10 @@ where
}
}
impl<A, B, Rndr> MatchInterface<Rndr> for Either<A, B>
impl<A, B> MatchInterface for Either<A, B>
where
Rndr: Renderer + 'static,
A: MatchInterface<Rndr>,
B: MatchInterface<Rndr>,
A: MatchInterface,
B: MatchInterface,
{
type Child = Either<A::Child, B::Child>;
type View = Either<A::View, B::View>;
@@ -166,10 +150,7 @@ where
fn into_view_and_child(
self,
) -> (
impl ChooseView<Rndr, Output = Self::View>,
Option<Self::Child>,
) {
) -> (impl ChooseView<Output = Self::View>, Option<Self::Child>) {
match self {
Either::Left(i) => {
let (view, child) = i.into_view_and_child();
@@ -183,11 +164,10 @@ where
}
}
impl<A, B, Rndr> MatchNestedRoutes<Rndr> for (A, B)
impl<A, B> MatchNestedRoutes for (A, B)
where
A: MatchNestedRoutes<Rndr>,
B: MatchNestedRoutes<Rndr>,
Rndr: Renderer + 'static,
A: MatchNestedRoutes,
B: MatchNestedRoutes,
{
type Data = (A::Data, B::Data);
type View = Either<A::View, B::View>;
@@ -251,10 +231,9 @@ macro_rules! tuples {
}
}
impl<Rndr, $($ty,)*> MatchInterface<Rndr> for $either <$($ty,)*>
impl<$($ty,)*> MatchInterface for $either <$($ty,)*>
where
Rndr: Renderer + 'static,
$($ty: MatchInterface<Rndr> + 'static),*,
$($ty: MatchInterface + 'static),*,
{
type Child = $either<$($ty::Child,)*>;
type View = $either<$($ty::View,)*>;
@@ -274,7 +253,7 @@ macro_rules! tuples {
fn into_view_and_child(
self,
) -> (
impl ChooseView<Rndr, Output = Self::View>,
impl ChooseView<Output = Self::View>,
Option<Self::Child>,
) {
match self {
@@ -286,10 +265,9 @@ macro_rules! tuples {
}
}
impl<Rndr, $($ty),*> MatchNestedRoutes<Rndr> for ($($ty,)*)
impl<$($ty),*> MatchNestedRoutes for ($($ty,)*)
where
Rndr: Renderer + 'static,
$($ty: MatchNestedRoutes<Rndr> + 'static),*,
$($ty: MatchNestedRoutes + 'static),*,
{
type Data = ($($ty::Data,)*);
type View = $either<$($ty::View,)*>;

View File

@@ -23,9 +23,7 @@ use std::{
cell::RefCell,
fmt::Debug,
future::Future,
iter,
marker::PhantomData,
mem,
iter, mem,
pin::Pin,
rc::Rc,
sync::{Arc, Mutex},
@@ -33,7 +31,6 @@ use std::{
use tachys::{
hydration::Cursor,
reactive_graph::{OwnedView, Suspend},
renderer::Renderer,
ssr::StreamBuilder,
view::{
add_attr::AddAnyAttr,
@@ -43,42 +40,38 @@ use tachys::{
},
};
pub(crate) struct NestedRoutesView<Loc, Defs, FalFn, R> {
pub(crate) struct NestedRoutesView<Loc, Defs, FalFn> {
pub location: Option<Loc>,
pub routes: Routes<Defs, R>,
pub routes: Routes<Defs>,
pub outer_owner: Owner,
pub current_url: ArcRwSignal<Url>,
pub base: Option<Oco<'static, str>>,
pub fallback: FalFn,
#[allow(unused)] // TODO
pub set_is_routing: Option<SignalSetter<bool>>,
pub rndr: PhantomData<R>,
}
pub struct NestedRouteViewState<Fal, R>
pub struct NestedRouteViewState<Fal>
where
Fal: Render<R>,
R: Renderer + 'static,
Fal: Render,
{
path: String,
current_url: ArcRwSignal<Url>,
outlets: Vec<RouteContext<R>>,
outlets: Vec<RouteContext>,
// TODO loading fallback
#[allow(clippy::type_complexity)]
view: Rc<RefCell<EitherOf3State<(), Fal, AnyView<R>, R>>>,
view: Rc<RefCell<EitherOf3State<(), Fal, AnyView>>>,
}
impl<Loc, Defs, FalFn, Fal, R> Render<R>
for NestedRoutesView<Loc, Defs, FalFn, R>
impl<Loc, Defs, FalFn, Fal> Render for NestedRoutesView<Loc, Defs, FalFn>
where
Loc: LocationProvider,
Defs: MatchNestedRoutes<R>,
Defs: MatchNestedRoutes,
FalFn: FnOnce() -> Fal,
Fal: Render<R> + 'static,
R: Renderer + 'static,
Fal: Render + 'static,
{
// TODO support fallback while loading
type State = NestedRouteViewState<Fal, R>;
type State = NestedRouteViewState<Fal>;
fn build(self) -> Self::State {
let NestedRoutesView {
@@ -112,11 +105,7 @@ where
&outer_owner,
);
drop(url);
outer_owner.with(|| {
EitherOf3::C(
Outlet(OutletProps::builder().build()).into_any(),
)
})
outer_owner.with(|| EitherOf3::C(Outlet().into_any()))
}
};
@@ -161,7 +150,7 @@ where
match new_match {
None => {
EitherOf3::<(), Fal, AnyView<R>>::B((self.fallback)())
EitherOf3::<(), Fal, AnyView>::B((self.fallback)())
.rebuild(&mut state.view.borrow_mut());
state.outlets.clear();
}
@@ -191,10 +180,8 @@ where
// if it was on the fallback, show the view instead
if matches!(state.view.borrow().state, EitherOf3::B(_)) {
self.outer_owner.with(|| {
EitherOf3::<(), Fal, AnyView<R>>::C(
Outlet(OutletProps::builder().build()).into_any(),
)
.rebuild(&mut *state.view.borrow_mut());
EitherOf3::<(), Fal, AnyView>::C(Outlet().into_any())
.rebuild(&mut *state.view.borrow_mut());
})
}
}
@@ -206,37 +193,33 @@ where
}
}
impl<Loc, Defs, Fal, FalFn, R> AddAnyAttr<R>
for NestedRoutesView<Loc, Defs, FalFn, R>
impl<Loc, Defs, Fal, FalFn> AddAnyAttr for NestedRoutesView<Loc, Defs, FalFn>
where
Loc: LocationProvider + Send,
Defs: MatchNestedRoutes<R> + Send,
Defs: MatchNestedRoutes + Send,
FalFn: FnOnce() -> Fal + Send,
Fal: RenderHtml<R> + 'static,
R: Renderer + 'static,
Fal: RenderHtml + 'static,
{
type Output<SomeNewAttr: leptos::attr::Attribute<R>> =
NestedRoutesView<Loc, Defs, FalFn, R>;
type Output<SomeNewAttr: leptos::attr::Attribute> =
NestedRoutesView<Loc, Defs, FalFn>;
fn add_any_attr<NewAttr: leptos::attr::Attribute<R>>(
fn add_any_attr<NewAttr: leptos::attr::Attribute>(
self,
_attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<R>,
Self::Output<NewAttr>: RenderHtml,
{
todo!()
}
}
impl<Loc, Defs, FalFn, Fal, R> RenderHtml<R>
for NestedRoutesView<Loc, Defs, FalFn, R>
impl<Loc, Defs, FalFn, Fal> RenderHtml for NestedRoutesView<Loc, Defs, FalFn>
where
Loc: LocationProvider + Send,
Defs: MatchNestedRoutes<R> + Send,
Defs: MatchNestedRoutes + Send,
FalFn: FnOnce() -> Fal + Send,
Fal: RenderHtml<R> + 'static,
R: Renderer + 'static,
Fal: RenderHtml + 'static,
{
type AsyncOutput = Self;
@@ -330,11 +313,7 @@ where
.now_or_never()
.expect("async routes not supported in SSR");
outer_owner.with(|| {
Either::Right(
Outlet(OutletProps::builder().build()).into_any(),
)
})
outer_owner.with(|| Either::Right(Outlet().into_any()))
}
};
view.to_html_with_buf(buf, position, escape, mark_branches);
@@ -381,11 +360,7 @@ where
.now_or_never()
.expect("async routes not supported in SSR");
outer_owner.with(|| {
Either::Right(
Outlet(OutletProps::builder().build()).into_any(),
)
})
outer_owner.with(|| Either::Right(Outlet().into_any()))
}
};
view.to_html_async_with_buf::<OUT_OF_ORDER>(
@@ -398,7 +373,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
let NestedRoutesView {
@@ -436,11 +411,7 @@ where
join_all(mem::take(&mut loaders))
.now_or_never()
.expect("async routes not supported in SSR");
outer_owner.with(|| {
EitherOf3::C(
Outlet(OutletProps::builder().build()).into_any(),
)
})
outer_owner.with(|| EitherOf3::C(Outlet().into_any()))
}
}
.hydrate::<FROM_SERVER>(cursor, position),
@@ -455,15 +426,11 @@ where
}
}
type OutletViewFn<R> = Box<
dyn Fn() -> Suspend<Pin<Box<dyn Future<Output = AnyView<R>> + Send>>>
+ Send,
type OutletViewFn = Box<
dyn Fn() -> Suspend<Pin<Box<dyn Future<Output = AnyView> + Send>>> + Send,
>;
pub(crate) struct RouteContext<R>
where
R: Renderer,
{
pub(crate) struct RouteContext {
id: RouteMatchId,
trigger: ArcTrigger,
url: ArcRwSignal<Url>,
@@ -471,10 +438,10 @@ where
owner: Owner,
pub matched: ArcRwSignal<String>,
base: Option<Oco<'static, str>>,
view_fn: Arc<Mutex<OutletViewFn<R>>>,
view_fn: Arc<Mutex<OutletViewFn>>,
}
impl<R: Renderer> Debug for RouteContext<R> {
impl Debug for RouteContext {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RouteContext")
.field("id", &self.id)
@@ -488,19 +455,13 @@ impl<R: Renderer> Debug for RouteContext<R> {
}
}
impl<R> RouteContext<R>
where
R: Renderer + 'static,
{
impl RouteContext {
fn provide_contexts(&self) {
provide_context(self.clone());
}
}
impl<R> Clone for RouteContext<R>
where
R: Renderer,
{
impl Clone for RouteContext {
fn clone(&self) -> Self {
Self {
url: self.url.clone(),
@@ -515,16 +476,13 @@ where
}
}
trait AddNestedRoute<R>
where
R: Renderer,
{
trait AddNestedRoute {
fn build_nested_route(
self,
url: &Url,
base: Option<Oco<'static, str>>,
loaders: &mut Vec<Pin<Box<dyn Future<Output = ArcTrigger>>>>,
outlets: &mut Vec<RouteContext<R>>,
outlets: &mut Vec<RouteContext>,
parent: &Owner,
);
@@ -534,22 +492,21 @@ where
base: Option<Oco<'static, str>>,
items: &mut usize,
loaders: &mut Vec<Pin<Box<dyn Future<Output = ArcTrigger>>>>,
outlets: &mut Vec<RouteContext<R>>,
outlets: &mut Vec<RouteContext>,
parent: &Owner,
);
}
impl<Match, R> AddNestedRoute<R> for Match
impl<Match> AddNestedRoute for Match
where
Match: MatchInterface<R> + MatchParams,
R: Renderer + 'static,
Match: MatchInterface + MatchParams,
{
fn build_nested_route(
self,
url: &Url,
base: Option<Oco<'static, str>>,
loaders: &mut Vec<Pin<Box<dyn Future<Output = ArcTrigger>>>>,
outlets: &mut Vec<RouteContext<R>>,
outlets: &mut Vec<RouteContext>,
parent: &Owner,
) {
let orig_url = url;
@@ -650,7 +607,7 @@ where
OwnedView::new(view).into_any()
})
as Pin<
Box<dyn Future<Output = AnyView<R>> + Send>,
Box<dyn Future<Output = AnyView> + Send>,
>)
})
});
@@ -678,7 +635,7 @@ where
base: Option<Oco<'static, str>>,
items: &mut usize,
loaders: &mut Vec<Pin<Box<dyn Future<Output = ArcTrigger>>>>,
outlets: &mut Vec<RouteContext<R>>,
outlets: &mut Vec<RouteContext>,
parent: &Owner,
) {
let (parent_params, parent_matches): (Vec<_>, Vec<_>) = outlets
@@ -833,32 +790,33 @@ where
}
}
impl<Fal, R> Mountable<R> for NestedRouteViewState<Fal, R>
impl<Fal> Mountable for NestedRouteViewState<Fal>
where
Fal: Render<R>,
R: Renderer,
Fal: Render,
{
fn unmount(&mut self) {
self.view.unmount();
}
fn mount(&mut self, parent: &R::Element, marker: Option<&R::Node>) {
fn mount(
&mut self,
parent: &leptos::tachys::renderer::types::Element,
marker: Option<&leptos::tachys::renderer::types::Node>,
) {
self.view.mount(parent, marker);
}
fn insert_before_this(&self, child: &mut dyn Mountable<R>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
self.view.insert_before_this(child)
}
}
#[component]
pub fn Outlet<R>(#[prop(optional)] rndr: PhantomData<R>) -> impl RenderHtml<R>
pub fn Outlet() -> impl RenderHtml
where
R: Renderer + 'static,
{
_ = rndr;
move || {
let ctx = use_context::<RouteContext<R>>()
let ctx = use_context::<RouteContext>()
.expect("<Outlet/> used without RouteContext being provided.");
let RouteContext {
trigger, view_fn, ..

View File

@@ -26,7 +26,7 @@ pub fn ReactiveRouter<Rndr, Loc, DefFn, Defs, FallbackFn, Fallback>(
mut location: Loc,
routes: DefFn,
fallback: FallbackFn,
) -> impl RenderHtml<Rndr>
) -> impl RenderHtml
where
DefFn: Fn() -> Defs + 'static,
Defs: 'static,
@@ -35,10 +35,9 @@ where
Rndr::Element: Clone,
Rndr::Node: Clone,
FallbackFn: Fn() -> Fallback + Clone + 'static,
Fallback: Render<Rndr> + 'static,
Fallback: Render + 'static,
Router<Rndr, Loc, Defs, FallbackFn>: FallbackOrView,
<Router<Rndr, Loc, Defs, FallbackFn> as FallbackOrView>::Output:
RenderHtml<Rndr>,
<Router<Rndr, Loc, Defs, FallbackFn> as FallbackOrView>::Output: RenderHtml,
{
// create a reactive URL signal that will drive the router view
let url = ArcRwSignal::new(location.try_to_url().unwrap_or_default());
@@ -75,7 +74,7 @@ where
fal: PhantomData<Fallback>,
}
impl<Rndr, Loc, Defs, FallbackFn, Fallback> Render<Rndr>
impl<Rndr, Loc, Defs, FallbackFn, Fallback> Render
for ReactiveRouterInner<Rndr, Loc, Defs, FallbackFn, Fallback>
where
Loc: Location,
@@ -83,10 +82,9 @@ where
Rndr::Element: Clone,
Rndr::Node: Clone,
FallbackFn: Fn() -> Fallback,
Fallback: Render<Rndr>,
Fallback: Render,
Router<Rndr, Loc, Defs, FallbackFn>: FallbackOrView,
<Router<Rndr, Loc, Defs, FallbackFn> as FallbackOrView>::Output:
Render<Rndr>,
<Router<Rndr, Loc, Defs, FallbackFn> as FallbackOrView>::Output: Render,
{
type State =
ReactiveRouterInnerState<Rndr, Loc, Defs, FallbackFn, Fallback>;
@@ -112,7 +110,7 @@ where
}
}
impl<Rndr, Loc, Defs, FallbackFn, Fallback> RenderHtml<Rndr>
impl<Rndr, Loc, Defs, FallbackFn, Fallback> RenderHtml
for ReactiveRouterInner<Rndr, Loc, Defs, FallbackFn, Fallback>
where
Loc: Location,
@@ -120,18 +118,18 @@ where
Rndr::Element: Clone,
Rndr::Node: Clone,
FallbackFn: Fn() -> Fallback,
Fallback: Render<Rndr>,
Fallback: Render,
Router<Rndr, Loc, Defs, FallbackFn>: FallbackOrView,
<Router<Rndr, Loc, Defs, FallbackFn> as FallbackOrView>::Output:
RenderHtml<Rndr>,
<Router<Rndr, Loc, Defs, FallbackFn> as FallbackOrView>::Output: RenderHtml,
{
const MIN_LENGTH: usize = <<Router<Rndr, Loc, Defs, FallbackFn> as FallbackOrView>::Output as RenderHtml<Rndr>>::MIN_LENGTH;
const MIN_LENGTH: usize = <<Router<Rndr, Loc, Defs, FallbackFn> as FallbackOrView>::Output as RenderHtml>::MIN_LENGTH;
fn to_html_with_buf(
self,
buf: &mut String,
position: &mut Position,
escape: bool, mark_branches: bool
escape: bool,
mark_branches: bool,
) {
// if this is being run on the server for the first time, generating all possible routes
if RouteList::is_generating() {
@@ -156,7 +154,8 @@ where
self,
buf: &mut StreamBuilder,
position: &mut Position,
escape: bool, mark_branches: bool
escape: bool,
mark_branches: bool,
) where
Self: Sized,
{
@@ -168,7 +167,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<Rndr>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
let (prev_id, inner) = self.inner.fallback_or_view();
@@ -186,21 +185,20 @@ where
struct ReactiveRouterInnerState<Rndr, Loc, Defs, FallbackFn, Fallback>
where
Router<Rndr, Loc, Defs, FallbackFn>: FallbackOrView,
<Router<Rndr, Loc, Defs, FallbackFn> as FallbackOrView>::Output: Render<Rndr>,
<Router<Rndr, Loc, Defs, FallbackFn> as FallbackOrView>::Output: Render,
Rndr: Renderer,
{
owner: Owner,
prev_id: &'static str,
inner: <<Router<Rndr, Loc, Defs, FallbackFn> as FallbackOrView>::Output as Render<Rndr>>::State,
inner: <<Router<Rndr, Loc, Defs, FallbackFn> as FallbackOrView>::Output as Render>::State,
fal: PhantomData<Fallback>,
}
impl<Rndr, Loc, Defs, FallbackFn, Fallback> Mountable<Rndr>
impl<Rndr, Loc, Defs, FallbackFn, Fallback> Mountable
for ReactiveRouterInnerState<Rndr, Loc, Defs, FallbackFn, Fallback>
where
Router<Rndr, Loc, Defs, FallbackFn>: FallbackOrView,
<Router<Rndr, Loc, Defs, FallbackFn> as FallbackOrView>::Output:
Render<Rndr>,
<Router<Rndr, Loc, Defs, FallbackFn> as FallbackOrView>::Output: Render,
Rndr: Renderer,
{
fn unmount(&mut self) {
@@ -209,13 +207,13 @@ where
fn mount(
&mut self,
parent: &<Rndr as Renderer>::Element,
marker: Option<&<Rndr as Renderer>::Node>,
parent: &leptos::tachys::renderer::types::Element,
marker: Option<&leptos::tachys::renderer::types::Node>,
) {
self.inner.mount(parent, marker);
}
fn insert_before_this(&self, child: &mut dyn Mountable<Rndr>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
self.inner.insert_before_this(child)
}
}
@@ -248,12 +246,12 @@ impl ReactiveMatchedRoute {
}
}
pub fn reactive_route<ViewFn, View, Rndr>(
pub fn reactive_route<ViewFn, View>(
view_fn: ViewFn,
) -> impl Fn(MatchedRoute) -> ReactiveRoute<ViewFn, View, Rndr>
) -> impl Fn(MatchedRoute) -> ReactiveRoute<ViewFn, View>
where
ViewFn: Fn(&ReactiveMatchedRoute) -> View + Clone,
View: Render<Rndr>,
View: Render,
Rndr: Renderer,
{
move |matched| ReactiveRoute {
@@ -263,21 +261,21 @@ where
}
}
pub struct ReactiveRoute<ViewFn, View, Rndr>
pub struct ReactiveRoute<ViewFn, View>
where
ViewFn: Fn(&ReactiveMatchedRoute) -> View,
View: Render<Rndr>,
View: Render,
Rndr: Renderer,
{
view_fn: ViewFn,
matched: MatchedRoute,
ty: PhantomData<Rndr>,
ty: PhantomData,
}
impl<ViewFn, View, Rndr> Render<Rndr> for ReactiveRoute<ViewFn, View, Rndr>
impl<ViewFn, View> Render for ReactiveRoute<ViewFn, View>
where
ViewFn: Fn(&ReactiveMatchedRoute) -> View,
View: Render<Rndr>,
View: Render,
Rndr: Renderer,
{
type State = ReactiveRouteState<View::State>;
@@ -310,10 +308,10 @@ where
}
}
impl<ViewFn, View, Rndr> RenderHtml<Rndr> for ReactiveRoute<ViewFn, View, Rndr>
impl<ViewFn, View> RenderHtml for ReactiveRoute<ViewFn, View>
where
ViewFn: Fn(&ReactiveMatchedRoute) -> View,
View: RenderHtml<Rndr>,
View: RenderHtml,
Rndr: Renderer,
Rndr::Node: Clone,
Rndr::Element: Clone,
@@ -324,7 +322,8 @@ where
self,
buf: &mut String,
position: &mut Position,
escape: bool, mark_branches: bool
escape: bool,
mark_branches: bool,
) {
let MatchedRoute {
search_params,
@@ -345,7 +344,8 @@ where
self,
buf: &mut StreamBuilder,
position: &mut Position,
escape: bool, mark_branches: bool
escape: bool,
mark_branches: bool,
) where
Self: Sized,
{
@@ -367,7 +367,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<Rndr>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
let MatchedRoute {
@@ -401,10 +401,9 @@ impl<State> Drop for ReactiveRouteState<State> {
}
}
impl<T, R> Mountable<R> for ReactiveRouteState<T>
impl<T> Mountable for ReactiveRouteState<T>
where
T: Mountable<R>,
R: Renderer,
T: Mountable,
{
fn unmount(&mut self) {
self.view_state.unmount();
@@ -418,7 +417,7 @@ where
self.view_state.mount(parent, marker);
}
fn insert_before_this(&self, child: &mut dyn Mountable<R>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
self.view_state.insert_before_this(child)
}
}

View File

@@ -48,7 +48,7 @@ use tachys::{
pub struct Router<Rndr, Loc, Children, FallbackFn> {
base: Option<Cow<'static, str>>,
location: PhantomData<Loc>,
pub routes: Routes<Children, Rndr>,
pub routes: Routes<Children>,
fallback: FallbackFn,
}
@@ -60,7 +60,7 @@ where
FallbackFn: Fn() -> Fallback,
{
pub fn new(
routes: Routes<Children, Rndr>,
routes: Routes<Children>,
fallback: FallbackFn,
) -> Router<Rndr, Loc, Children, FallbackFn> {
Self {
@@ -73,7 +73,7 @@ where
pub fn new_with_base(
base: impl Into<Cow<'static, str>>,
routes: Routes<Children, Rndr>,
routes: Routes<Children>,
fallback: FallbackFn,
) -> Router<Rndr, Loc, Children, FallbackFn> {
Self {
@@ -98,28 +98,28 @@ where
pub struct
where
R: Renderer + 'static,
{
pub params: ArcMemo<Params>,
pub outlet: Outlet<R>,
pub outlet: Outlet,
}
impl<Rndr, Loc, FallbackFn, Fallback, Children> Render<Rndr>
impl<Rndr, Loc, FallbackFn, Fallback, Children> Render
for Router<Rndr, Loc, Children, FallbackFn>
where
Loc: Location,
FallbackFn: Fn() -> Fallback + 'static,
Fallback: Render<Rndr>,
Children: MatchNestedRoutes<Rndr> + 'static,
Fallback: Render,
Children: MatchNestedRoutes + 'static,
Fallback::State: 'static,
Rndr: Renderer + 'static,
Children::Match: std::fmt::Debug,
<Children::Match as MatchInterface<Rndr>>::Child: std::fmt::Debug,
<Children::Match as MatchInterface>::Child: std::fmt::Debug,
{
type State = RenderEffect<
EitherState<
<NestedRouteView<Children::Match, Rndr> as Render<Rndr>>::State,
<Fallback as Render<Rndr>>::State,
<NestedRouteView<Children::Match> as Render>::State,
<Fallback as Render>::State,
Rndr,
>,
>;
@@ -158,7 +158,7 @@ where
}
}
} else {
Either::<NestedRouteView<Children::Match, Rndr>, _>::Right(
Either::<NestedRouteView<Children::Match>, _>::Right(
(self.fallback)(),
)
.rebuild(&mut prev);
@@ -180,21 +180,21 @@ where
fn rebuild(self, state: &mut Self::State) {}
}
impl<Rndr, Loc, FallbackFn, Fallback, Children> RenderHtml<Rndr>
impl<Rndr, Loc, FallbackFn, Fallback, Children> RenderHtml
for Router<Rndr, Loc, Children, FallbackFn>
where
Loc: Location + Send,
FallbackFn: Fn() -> Fallback + Send + 'static,
Fallback: RenderHtml<Rndr>,
Children: MatchNestedRoutes<Rndr> + Send + 'static,
Children::View: RenderHtml<Rndr>,
/*View: Render<Rndr> + IntoAny<Rndr> + 'static,
Fallback: RenderHtml,
Children: MatchNestedRoutes + Send + 'static,
Children::View: RenderHtml,
/*View: Render + IntoAny + 'static,
View::State: 'static,*/
Fallback: RenderHtml<Rndr>,
Fallback: RenderHtml,
Fallback::State: 'static,
Rndr: Renderer + 'static,
Children::Match: std::fmt::Debug,
<Children::Match as MatchInterface<Rndr>>::Child: std::fmt::Debug,
<Children::Match as MatchInterface>::Child: std::fmt::Debug,
{
type AsyncOutput = Self;
@@ -294,7 +294,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<Rndr>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
let location = Loc::new().unwrap(); // TODO
@@ -332,7 +332,7 @@ where
}
}
} else {
Either::<NestedRouteView<Children::Match, Rndr>, _>::Right(
Either::<NestedRouteView<Children::Match>, _>::Right(
(self.fallback)(),
)
.rebuild(&mut prev);
@@ -359,20 +359,20 @@ where
pub struct NestedRouteView<Matcher, R>
where
Matcher: MatchInterface<R>,
R: Renderer + 'static,
Matcher: MatchInterface,
{
id: RouteMatchId,
owner: Owner,
params: ArcRwSignal<Params>,
outlets: VecDeque<Outlet<R>>,
outlets: VecDeque<Outlet>,
view: Matcher::View,
ty: PhantomData<(Matcher, R)>,
}
impl<Matcher, Rndr> NestedRouteView<Matcher, Rndr>
impl<Matcher> NestedRouteView<Matcher>
where
Matcher: MatchInterface<Rndr> + MatchParams,
Matcher: MatchInterface + MatchParams,
Matcher::Child: 'static,
Matcher::View: 'static,
Rndr: Renderer + 'static,
@@ -414,7 +414,7 @@ where
pub fn new_hydrate(
outer_owner: &Owner,
route_match: Matcher,
cursor: &Cursor<Rndr>,
cursor: &Cursor,
position: &PositionState,
) -> Self {
// keep track of all outlets, for diffing
@@ -459,26 +459,26 @@ where
}
}
pub struct NestedRouteState<Matcher, Rndr>
pub struct NestedRouteState<Matcher>
where
Matcher: MatchInterface<Rndr>,
Matcher: MatchInterface,
Rndr: Renderer + 'static,
{
id: RouteMatchId,
owner: Owner,
params: ArcRwSignal<Params>,
view: <Matcher::View as Render<Rndr>>::State,
outlets: VecDeque<Outlet<Rndr>>,
view: <Matcher::View as Render>::State,
outlets: VecDeque<Outlet>,
}
fn get_inner_view<Match, R>(
outlets: &mut VecDeque<Outlet<R>>,
outlets: &mut VecDeque<Outlet>,
parent: &Owner,
route_match: Match,
) -> Outlet<R>
) -> Outlet
where
Match: MatchInterface<R> + MatchParams,
R: Renderer + 'static,
Match: MatchInterface + MatchParams,
{
let owner = parent.child();
let id = route_match.as_id();
@@ -503,7 +503,7 @@ where
})
.into_any(),
))
}) as Box<dyn FnOnce() -> RwLock<Option<AnyView<R>>>>
}) as Box<dyn FnOnce() -> RwLock<Option<AnyView>>>
}));
let inner = Arc::new(RwLock::new(OutletStateInner {
html_len: {
@@ -525,15 +525,15 @@ where
}
fn get_inner_view_hydrate<Match, R>(
outlets: &mut VecDeque<Outlet<R>>,
outlets: &mut VecDeque<Outlet>,
parent: &Owner,
route_match: Match,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Outlet<R>
) -> Outlet
where
Match: MatchInterface<R> + MatchParams,
R: Renderer + 'static,
Match: MatchInterface + MatchParams,
{
let owner = parent.child();
let id = route_match.as_id();
@@ -558,7 +558,7 @@ where
})
.into_any(),
))
}) as Box<dyn FnOnce() -> RwLock<Option<AnyView<R>>>>
}) as Box<dyn FnOnce() -> RwLock<Option<AnyView>>>
}));
let inner = Arc::new(RwLock::new(OutletStateInner {
html_len: Box::new({
@@ -585,18 +585,18 @@ where
}
#[derive(Debug)]
pub struct Outlet<R>
pub struct Outlet
where
R: Renderer + Send + 'static,
{
id: RouteMatchId,
owner: Owner,
params: ArcRwSignal<Params>,
rndr: PhantomData<R>,
inner: OutletInner<R>,
rndr: PhantomData,
inner: OutletInner,
}
pub enum OutletInner<R> where R: Renderer + 'static {
pub enum OutletInner where R: Renderer + 'static {
Server {
html_len: Box<dyn Fn() -> usize + Send + Sync>,
view: Box<
@@ -608,30 +608,30 @@ pub enum OutletInner<R> where R: Renderer + 'static {
html_len: Box<dyn Fn() -> usize + Send + Sync>,
view: Arc<
Lazy<
RwLock<Option<AnyView<R>>>,
Box<dyn FnOnce() -> RwLock<Option<AnyView<R>>> + Send + Sync>,
RwLock<Option<AnyView>>,
Box<dyn FnOnce() -> RwLock<Option<AnyView>> + Send + Sync>,
>,
>,
state: Lazy<
SendWrapper<AnyViewState<R>>,
Box<dyn FnOnce() -> SendWrapper<AnyViewState<R>> + Send + Sync>,
SendWrapper<AnyViewState>,
Box<dyn FnOnce() -> SendWrapper<AnyViewState> + Send + Sync>,
>,
*/
impl<R: Renderer> Debug for OutletInner<R> {
impl<R: Renderer> Debug for OutletInner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("OutletInner").finish_non_exhaustive()
}
}
impl<R> Default for OutletInner<R>
impl Default for OutletInner
where
R: Renderer + 'static,
{
fn default() -> Self {
let view =
Arc::new(Lazy::new(Box::new(|| RwLock::new(Some(().into_any())))
as Box<dyn FnOnce() -> RwLock<Option<AnyView<R>>>>));
as Box<dyn FnOnce() -> RwLock<Option<AnyView>>>));
Self {
html_len: Box::new(|| 0),
view,
@@ -640,9 +640,9 @@ where
}
}
impl<R> Clone for Outlet<R>
impl Clone for Outlet
where
R: Renderer + 'static,
{
fn clone(&self) -> Self {
Self {
@@ -655,9 +655,9 @@ where
}
}
impl<R> Default for Outlet<R>
impl Default for Outlet
where
R: Renderer + 'static,
{
fn default() -> Self {
Self {
@@ -669,11 +669,11 @@ where
}
}
impl<R> Render<R> for Outlet<R>
impl Render for Outlet
where
R: Renderer + 'static,
{
type State = Outlet<R>;
type State = Outlet;
fn build(self) -> Self::State {
self
@@ -684,9 +684,9 @@ where
}
}
impl<R> RenderHtml<R> for Outlet<R>
impl RenderHtml for Outlet
where
R: Renderer + 'static,
{
type AsyncOutput = Self;
@@ -725,7 +725,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
todo!()
@@ -743,38 +743,38 @@ where
}
}
/*pub struct OutletStateInner<R>
/*pub struct OutletStateInner
where
R: Renderer + 'static,
{
html_len: Box<dyn Fn() -> usize + Send + Sync>,
view: Arc<
Lazy<
RwLock<Option<AnyView<R>>>,
Box<dyn FnOnce() -> RwLock<Option<AnyView<R>>> + Send + Sync>,
RwLock<Option<AnyView>>,
Box<dyn FnOnce() -> RwLock<Option<AnyView>> + Send + Sync>,
>,
>,
state: Lazy<
SendWrapper<AnyViewState<R>>,
Box<dyn FnOnce() -> SendWrapper<AnyViewState<R>> + Send + Sync>,
SendWrapper<AnyViewState>,
Box<dyn FnOnce() -> SendWrapper<AnyViewState> + Send + Sync>,
>,
}
impl<R: Renderer> Debug for OutletStateInner<R> {
impl<R: Renderer> Debug for OutletStateInner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("OutletStateInner").finish_non_exhaustive()
}
}
impl<R> Default for OutletStateInner<R>
impl Default for OutletStateInner
where
R: Renderer + 'static,
{
fn default() -> Self {
let view =
Arc::new(Lazy::new(Box::new(|| RwLock::new(Some(().into_any())))
as Box<dyn FnOnce() -> RwLock<Option<AnyView<R>>>>));
as Box<dyn FnOnce() -> RwLock<Option<AnyView>>>));
Self {
html_len: Box::new(|| 0),
view,
@@ -784,9 +784,9 @@ where
}
*/
impl<R> Mountable<R> for Outlet<R>
impl Mountable for Outlet
where
R: Renderer + 'static,
{
fn unmount(&mut self) {
todo!()
@@ -802,7 +802,7 @@ where
}
fn insert_before_this(&self,
child: &mut dyn Mountable<R>,
child: &mut dyn Mountable,
) -> bool {
/*self.inner
.write()
@@ -818,8 +818,8 @@ fn rebuild_nested<Match, R>(
prev: &mut NestedRouteState<Match, R>,
new_match: Match,
) where
Match: MatchInterface<R> + MatchParams + std::fmt::Debug,
R: Renderer + 'static,
Match: MatchInterface + MatchParams + std::fmt::Debug,
{
let mut items = 0;
let NestedRouteState {
@@ -846,11 +846,11 @@ fn rebuild_nested<Match, R>(
fn rebuild_inner<Match, R>(
items: &mut usize,
outlets: &mut VecDeque<Outlet<R>>,
outlets: &mut VecDeque<Outlet>,
route_match: Match,
) where
Match: MatchInterface<R> + MatchParams,
R: Renderer + 'static,
Match: MatchInterface + MatchParams,
{
*items += 1;
@@ -910,11 +910,11 @@ fn rebuild_inner<Match, R>(
}
}
impl<Matcher, R> Render<R> for NestedRouteView<Matcher, R>
impl<Matcher, R> Render for NestedRouteView<Matcher, R>
where
Matcher: MatchInterface<R>,
Matcher: MatchInterface,
Matcher::View: Sized + 'static,
R: Renderer + 'static,
{
type State = NestedRouteState<Matcher, R>;
@@ -953,11 +953,11 @@ where
}
}
impl<Matcher, R> RenderHtml<R> for NestedRouteView<Matcher, R>
impl<Matcher, R> RenderHtml for NestedRouteView<Matcher, R>
where
Matcher: MatchInterface<R> + Send,
Matcher: MatchInterface + Send,
Matcher::View: Sized + 'static,
R: Renderer + 'static,
{
type AsyncOutput = Self;
@@ -988,7 +988,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
let NestedRouteView {
@@ -1009,10 +1009,10 @@ where
}
}
impl<Matcher, R> Mountable<R> for NestedRouteState<Matcher, R>
impl<Matcher, R> Mountable for NestedRouteState<Matcher, R>
where
Matcher: MatchInterface<R>,
R: Renderer + 'static,
Matcher: MatchInterface,
{
fn unmount(&mut self) {
self.view.unmount();
@@ -1023,43 +1023,43 @@ where
}
fn insert_before_this(&self,
child: &mut dyn Mountable<R>,
child: &mut dyn Mountable,
) -> bool {
self.view.insert_before_this(child)
}
}
impl<Rndr, Loc, FallbackFn, Fallback, Children, View> AddAnyAttr<Rndr>
impl<Rndr, Loc, FallbackFn, Fallback, Children, View> AddAnyAttr
for Router<Rndr, Loc, Children, FallbackFn>
where
Loc: Location,
FallbackFn: Fn() -> Fallback,
Fallback: Render<Rndr>,
Children: MatchNestedRoutes<Rndr>,
<<Children as MatchNestedRoutes<Rndr>>::Match as MatchInterface<
Fallback: Render,
Children: MatchNestedRoutes,
<<Children as MatchNestedRoutes>::Match as MatchInterface<
Rndr,
>>::View: ChooseView<Rndr, Output = View>,
Rndr: Renderer + 'static,
Router<Rndr, Loc, Children, FallbackFn>: RenderHtml<Rndr>,
Router<Rndr, Loc, Children, FallbackFn>: RenderHtml,
{
type Output<SomeNewAttr: Attribute<Rndr>> = Self;
type Output<SomeNewAttr: Attribute> = Self;
fn add_any_attr<NewAttr: Attribute<Rndr>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Rndr>,
Self::Output<NewAttr>: RenderHtml,
{
self
}
fn add_any_attr_by_ref<NewAttr: Attribute<Rndr>>(
fn add_any_attr_by_ref<NewAttr: Attribute>(
self,
attr: &NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Rndr>,
Self::Output<NewAttr>: RenderHtml,
{
self
}
@@ -1069,7 +1069,7 @@ where
pub struct FlatRouter<Rndr, Loc, Children, FallbackFn> {
base: Option<Cow<'static, str>>,
location: PhantomData<Loc>,
pub routes: Routes<Children, Rndr>,
pub routes: Routes<Children>,
fallback: FallbackFn,
}
@@ -1081,7 +1081,7 @@ where
FallbackFn: Fn() -> Fallback,
{
pub fn new(
routes: Routes<Children, Rndr>,
routes: Routes<Children>,
fallback: FallbackFn,
) -> FlatRouter<Rndr, Loc, Children, FallbackFn> {
Self {
@@ -1094,7 +1094,7 @@ where
pub fn new_with_base(
base: impl Into<Cow<'static, str>>,
routes: Routes<Children, Rndr>,
routes: Routes<Children>,
fallback: FallbackFn,
) -> FlatRouter<Rndr, Loc, Children, FallbackFn> {
Self {
@@ -1105,23 +1105,23 @@ where
}
}
}
impl<Rndr, Loc, FallbackFn, Fallback, Children> Render<Rndr>
impl<Rndr, Loc, FallbackFn, Fallback, Children> Render
for FlatRouter<Rndr, Loc, Children, FallbackFn>
where
Loc: Location,
FallbackFn: Fn() -> Fallback + 'static,
Fallback: Render<Rndr>,
Children: MatchNestedRoutes<Rndr> + 'static,
Fallback: Render,
Children: MatchNestedRoutes + 'static,
Fallback::State: 'static,
Rndr: Renderer + 'static,
{
type State =
RenderEffect<
EitherState<
<<Children::Match as MatchInterface<Rndr>>::View as Render<
<<Children::Match as MatchInterface>::View as Render<
Rndr,
>>::State,
<Fallback as Render<Rndr>>::State,
<Fallback as Render>::State,
Rndr,
>,
>;
@@ -1170,7 +1170,7 @@ where
let view = outer_owner.with(|| view.choose());
Either::Left::<_, Fallback>(view).rebuild(&mut prev);
} else {
Either::<<Children::Match as MatchInterface<Rndr>>::View, _>::Right((self.fallback)()).rebuild(&mut prev);
Either::<<Children::Match as MatchInterface>::View, _>::Right((self.fallback)()).rebuild(&mut prev);
}
prev
} else {
@@ -1209,20 +1209,20 @@ where
fn rebuild(self, state: &mut Self::State) {}
}
impl<Rndr, Loc, FallbackFn, Fallback, Children> RenderHtml<Rndr>
impl<Rndr, Loc, FallbackFn, Fallback, Children> RenderHtml
for FlatRouter<Rndr, Loc, Children, FallbackFn>
where
Loc: Location + Send,
FallbackFn: Fn() -> Fallback + Send + 'static,
Fallback: RenderHtml<Rndr>,
Children: MatchNestedRoutes<Rndr> + Send + 'static,
Fallback: RenderHtml,
Children: MatchNestedRoutes + Send + 'static,
Fallback::State: 'static,
Rndr: Renderer + 'static,
{
type AsyncOutput = Self;
const MIN_LENGTH: usize =
<Children::Match as MatchInterface<Rndr>>::View::MIN_LENGTH;
<Children::Match as MatchInterface>::View::MIN_LENGTH;
async fn resolve(self) -> Self::AsyncOutput {
self
@@ -1357,7 +1357,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<Rndr>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
let location = Loc::new().unwrap(); // TODO
@@ -1405,7 +1405,7 @@ where
let view = outer_owner.with(|| view.choose());
Either::Left::<_, Fallback>(view).rebuild(&mut prev);
} else {
Either::<<Children::Match as MatchInterface<Rndr>>::View, _>::Right((self.fallback)()).rebuild(&mut prev);
Either::<<Children::Match as MatchInterface>::View, _>::Right((self.fallback)()).rebuild(&mut prev);
}
prev
} else {

View File

@@ -1,78 +1,68 @@
use super::{Attribute, NextAttribute};
use crate::renderer::Renderer;
use std::{
any::{Any, TypeId},
fmt::Debug,
marker::PhantomData,
};
#[cfg(feature = "ssr")]
use std::{future::Future, pin::Pin};
/// A type-erased container for any [`Attribute`].
pub struct AnyAttribute<R: Renderer> {
pub struct AnyAttribute {
type_id: TypeId,
html_len: usize,
value: Box<dyn Any + Send>,
#[cfg(feature = "ssr")]
to_html:
fn(Box<dyn Any>, &mut String, &mut String, &mut String, &mut String),
build: fn(Box<dyn Any>, el: &R::Element) -> AnyAttributeState<R>,
rebuild: fn(TypeId, Box<dyn Any>, &mut AnyAttributeState<R>),
build: fn(
Box<dyn Any>,
el: &crate::renderer::types::Element,
) -> AnyAttributeState,
rebuild: fn(TypeId, Box<dyn Any>, &mut AnyAttributeState),
#[cfg(feature = "hydrate")]
hydrate_from_server: fn(Box<dyn Any>, &R::Element) -> AnyAttributeState<R>,
hydrate_from_server:
fn(Box<dyn Any>, &crate::renderer::types::Element) -> AnyAttributeState,
#[cfg(feature = "hydrate")]
hydrate_from_template:
fn(Box<dyn Any>, &R::Element) -> AnyAttributeState<R>,
fn(Box<dyn Any>, &crate::renderer::types::Element) -> AnyAttributeState,
#[cfg(feature = "ssr")]
#[allow(clippy::type_complexity)]
resolve: fn(
Box<dyn Any>,
) -> Pin<Box<dyn Future<Output = AnyAttribute<R>> + Send>>,
resolve:
fn(Box<dyn Any>) -> Pin<Box<dyn Future<Output = AnyAttribute> + Send>>,
#[cfg(feature = "ssr")]
dry_resolve: fn(&mut Box<dyn Any + Send>),
}
impl<R> Debug for AnyAttribute<R>
where
R: Renderer,
{
impl Debug for AnyAttribute {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AnyAttribute").finish_non_exhaustive()
}
}
/// View state for [`AnyAttribute`].
pub struct AnyAttributeState<R>
where
R: Renderer,
{
pub struct AnyAttributeState {
type_id: TypeId,
state: Box<dyn Any>,
el: R::Element,
rndr: PhantomData<R>,
el: crate::renderer::types::Element,
}
/// Converts an [`Attribute`] into [`AnyAttribute`].
pub trait IntoAnyAttribute<R>
where
R: Renderer,
{
pub trait IntoAnyAttribute {
/// Wraps the given attribute.
fn into_any_attr(self) -> AnyAttribute<R>;
fn into_any_attr(self) -> AnyAttribute;
}
impl<T, R> IntoAnyAttribute<R> for T
impl<T> IntoAnyAttribute for T
where
Self: Send,
T: Attribute<R> + 'static,
T: Attribute + 'static,
T::State: 'static,
R: Renderer + 'static,
R::Element: Clone,
crate::renderer::types::Element: Clone,
{
// inlining allows the compiler to remove the unused functions
// i.e., doesn't ship HTML-generating code that isn't used
#[inline(always)]
fn into_any_attr(self) -> AnyAttribute<R> {
fn into_any_attr(self) -> AnyAttribute {
let html_len = self.html_len();
let value = Box::new(self) as Box<dyn Any + Send>;
@@ -88,7 +78,8 @@ where
.expect("AnyAttribute::to_html could not be downcast");
value.to_html(buf, class, style, inner_html);
};
let build = |value: Box<dyn Any>, el: &R::Element| {
let build = |value: Box<dyn Any>,
el: &crate::renderer::types::Element| {
let value = value
.downcast::<T>()
.expect("AnyAttribute::build couldn't downcast");
@@ -98,40 +89,39 @@ where
type_id: TypeId::of::<T>(),
state,
el: el.clone(),
rndr: PhantomData,
}
};
#[cfg(feature = "hydrate")]
let hydrate_from_server = |value: Box<dyn Any>, el: &R::Element| {
let value = value
.downcast::<T>()
.expect("AnyAttribute::hydrate_from_server couldn't downcast");
let state = Box::new(value.hydrate::<true>(el));
let hydrate_from_server =
|value: Box<dyn Any>, el: &crate::renderer::types::Element| {
let value = value.downcast::<T>().expect(
"AnyAttribute::hydrate_from_server couldn't downcast",
);
let state = Box::new(value.hydrate::<true>(el));
AnyAttributeState {
type_id: TypeId::of::<T>(),
state,
el: el.clone(),
rndr: PhantomData,
}
};
AnyAttributeState {
type_id: TypeId::of::<T>(),
state,
el: el.clone(),
}
};
#[cfg(feature = "hydrate")]
let hydrate_from_template = |value: Box<dyn Any>, el: &R::Element| {
let value = value
.downcast::<T>()
.expect("AnyAttribute::hydrate_from_server couldn't downcast");
let state = Box::new(value.hydrate::<true>(el));
let hydrate_from_template =
|value: Box<dyn Any>, el: &crate::renderer::types::Element| {
let value = value.downcast::<T>().expect(
"AnyAttribute::hydrate_from_server couldn't downcast",
);
let state = Box::new(value.hydrate::<true>(el));
AnyAttributeState {
type_id: TypeId::of::<T>(),
state,
el: el.clone(),
rndr: PhantomData,
}
};
AnyAttributeState {
type_id: TypeId::of::<T>(),
state,
el: el.clone(),
}
};
let rebuild = |new_type_id: TypeId,
value: Box<dyn Any>,
state: &mut AnyAttributeState<R>| {
state: &mut AnyAttributeState| {
let value = value
.downcast::<T>()
.expect("AnyAttribute::rebuild couldn't downcast value");
@@ -160,7 +150,7 @@ where
.downcast::<T>()
.expect("AnyView::resolve could not be downcast");
Box::pin(async move { value.resolve().await.into_any_attr() })
as Pin<Box<dyn Future<Output = AnyAttribute<R>> + Send>>
as Pin<Box<dyn Future<Output = AnyAttribute> + Send>>
};
AnyAttribute {
type_id: TypeId::of::<T>(),
@@ -182,13 +172,10 @@ where
}
}
impl<R> NextAttribute<R> for AnyAttribute<R>
where
R: Renderer,
{
type Output<NewAttr: Attribute<R>> = (Self, NewAttr);
impl NextAttribute for AnyAttribute {
type Output<NewAttr: Attribute> = (Self, NewAttr);
fn add_any_attr<NewAttr: Attribute<R>>(
fn add_any_attr<NewAttr: Attribute>(
self,
new_attr: NewAttr,
) -> Self::Output<NewAttr> {
@@ -196,14 +183,11 @@ where
}
}
impl<R> Attribute<R> for AnyAttribute<R>
where
R: Renderer,
{
impl Attribute for AnyAttribute {
const MIN_LENGTH: usize = 0;
type AsyncOutput = AnyAttribute<R>;
type State = AnyAttributeState<R>;
type AsyncOutput = AnyAttribute;
type State = AnyAttributeState;
type Cloneable = ();
type CloneableOwned = ();
@@ -232,7 +216,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
el: &<R as Renderer>::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
#[cfg(feature = "hydrate")]
if FROM_SERVER {
@@ -250,7 +234,7 @@ where
}
}
fn build(self, el: &<R as Renderer>::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
(self.build)(self.value, el)
}

View File

@@ -1,24 +1,23 @@
use crate::{
html::{
attribute::{Attr, *},
element::{CreateElement, ElementType, HtmlElement},
element::{ElementType, HtmlElement},
},
renderer::Rndr,
view::{add_attr::AddAnyAttr, RenderHtml},
};
/// Applies ARIA attributes to an HTML element.
pub trait AriaAttributes<Rndr, V>
where
Self: Sized + AddAnyAttr<Rndr>,
V: AttributeValue<Rndr>,
Rndr: Renderer,
Self: Sized + AddAnyAttr,
V: AttributeValue,
{
/// Identifies the currently active descendant of a composite widget.
fn aria_activedescendant(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaActivedescendant, V, Rndr>>
{
) -> <Self as AddAnyAttr>::Output<Attr<AriaActivedescendant, V>> {
self.add_any_attr(aria_activedescendant(value))
}
@@ -26,7 +25,7 @@ where
fn aria_atomic(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaAtomic, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaAtomic, V>> {
self.add_any_attr(aria_atomic(value))
}
@@ -34,8 +33,7 @@ where
fn aria_autocomplete(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaAutocomplete, V, Rndr>>
{
) -> <Self as AddAnyAttr>::Output<Attr<AriaAutocomplete, V>> {
self.add_any_attr(aria_autocomplete(value))
}
@@ -43,7 +41,7 @@ where
fn aria_busy(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaBusy, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaBusy, V>> {
self.add_any_attr(aria_busy(value))
}
@@ -51,7 +49,7 @@ where
fn aria_checked(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaChecked, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaChecked, V>> {
self.add_any_attr(aria_checked(value))
}
@@ -59,7 +57,7 @@ where
fn aria_colcount(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaColcount, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaColcount, V>> {
self.add_any_attr(aria_colcount(value))
}
@@ -67,7 +65,7 @@ where
fn aria_colindex(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaColindex, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaColindex, V>> {
self.add_any_attr(aria_colindex(value))
}
@@ -75,7 +73,7 @@ where
fn aria_colspan(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaColspan, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaColspan, V>> {
self.add_any_attr(aria_colspan(value))
}
@@ -83,7 +81,7 @@ where
fn aria_controls(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaControls, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaControls, V>> {
self.add_any_attr(aria_controls(value))
}
@@ -91,7 +89,7 @@ where
fn aria_current(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaCurrent, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaCurrent, V>> {
self.add_any_attr(aria_current(value))
}
@@ -99,8 +97,7 @@ where
fn aria_describedby(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaDescribedby, V, Rndr>>
{
) -> <Self as AddAnyAttr>::Output<Attr<AriaDescribedby, V>> {
self.add_any_attr(aria_describedby(value))
}
@@ -108,8 +105,7 @@ where
fn aria_description(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaDescription, V, Rndr>>
{
) -> <Self as AddAnyAttr>::Output<Attr<AriaDescription, V>> {
self.add_any_attr(aria_description(value))
}
@@ -117,7 +113,7 @@ where
fn aria_details(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaDetails, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaDetails, V>> {
self.add_any_attr(aria_details(value))
}
@@ -125,7 +121,7 @@ where
fn aria_disabled(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaDisabled, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaDisabled, V>> {
self.add_any_attr(aria_disabled(value))
}
@@ -133,7 +129,7 @@ where
fn aria_dropeffect(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaDropeffect, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaDropeffect, V>> {
self.add_any_attr(aria_dropeffect(value))
}
@@ -141,8 +137,7 @@ where
fn aria_errormessage(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaErrormessage, V, Rndr>>
{
) -> <Self as AddAnyAttr>::Output<Attr<AriaErrormessage, V>> {
self.add_any_attr(aria_errormessage(value))
}
@@ -150,7 +145,7 @@ where
fn aria_expanded(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaExpanded, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaExpanded, V>> {
self.add_any_attr(aria_expanded(value))
}
@@ -158,7 +153,7 @@ where
fn aria_flowto(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaFlowto, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaFlowto, V>> {
self.add_any_attr(aria_flowto(value))
}
@@ -166,7 +161,7 @@ where
fn aria_grabbed(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaGrabbed, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaGrabbed, V>> {
self.add_any_attr(aria_grabbed(value))
}
@@ -174,7 +169,7 @@ where
fn aria_haspopup(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaHaspopup, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaHaspopup, V>> {
self.add_any_attr(aria_haspopup(value))
}
@@ -182,7 +177,7 @@ where
fn aria_hidden(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaHidden, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaHidden, V>> {
self.add_any_attr(aria_hidden(value))
}
@@ -190,7 +185,7 @@ where
fn aria_invalid(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaInvalid, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaInvalid, V>> {
self.add_any_attr(aria_invalid(value))
}
@@ -198,8 +193,7 @@ where
fn aria_keyshortcuts(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaKeyshortcuts, V, Rndr>>
{
) -> <Self as AddAnyAttr>::Output<Attr<AriaKeyshortcuts, V>> {
self.add_any_attr(aria_keyshortcuts(value))
}
@@ -207,7 +201,7 @@ where
fn aria_label(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaLabel, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaLabel, V>> {
self.add_any_attr(aria_label(value))
}
@@ -215,7 +209,7 @@ where
fn aria_labelledby(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaLabelledby, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaLabelledby, V>> {
self.add_any_attr(aria_labelledby(value))
}
@@ -223,7 +217,7 @@ where
fn aria_live(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaLive, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaLive, V>> {
self.add_any_attr(aria_live(value))
}
@@ -231,7 +225,7 @@ where
fn aria_modal(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaModal, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaModal, V>> {
self.add_any_attr(aria_modal(value))
}
@@ -239,7 +233,7 @@ where
fn aria_multiline(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaMultiline, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaMultiline, V>> {
self.add_any_attr(aria_multiline(value))
}
@@ -247,8 +241,7 @@ where
fn aria_multiselectable(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaMultiselectable, V, Rndr>>
{
) -> <Self as AddAnyAttr>::Output<Attr<AriaMultiselectable, V>> {
self.add_any_attr(aria_multiselectable(value))
}
@@ -256,8 +249,7 @@ where
fn aria_orientation(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaOrientation, V, Rndr>>
{
) -> <Self as AddAnyAttr>::Output<Attr<AriaOrientation, V>> {
self.add_any_attr(aria_orientation(value))
}
@@ -265,7 +257,7 @@ where
fn aria_owns(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaOwns, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaOwns, V>> {
self.add_any_attr(aria_owns(value))
}
@@ -273,8 +265,7 @@ where
fn aria_placeholder(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaPlaceholder, V, Rndr>>
{
) -> <Self as AddAnyAttr>::Output<Attr<AriaPlaceholder, V>> {
self.add_any_attr(aria_placeholder(value))
}
@@ -282,7 +273,7 @@ where
fn aria_posinset(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaPosinset, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaPosinset, V>> {
self.add_any_attr(aria_posinset(value))
}
@@ -290,7 +281,7 @@ where
fn aria_pressed(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaPressed, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaPressed, V>> {
self.add_any_attr(aria_pressed(value))
}
@@ -298,7 +289,7 @@ where
fn aria_readonly(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaReadonly, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaReadonly, V>> {
self.add_any_attr(aria_readonly(value))
}
@@ -306,7 +297,7 @@ where
fn aria_relevant(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaRelevant, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaRelevant, V>> {
self.add_any_attr(aria_relevant(value))
}
@@ -314,7 +305,7 @@ where
fn aria_required(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaRequired, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaRequired, V>> {
self.add_any_attr(aria_required(value))
}
@@ -322,8 +313,7 @@ where
fn aria_roledescription(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaRoledescription, V, Rndr>>
{
) -> <Self as AddAnyAttr>::Output<Attr<AriaRoledescription, V>> {
self.add_any_attr(aria_roledescription(value))
}
@@ -331,7 +321,7 @@ where
fn aria_rowcount(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaRowcount, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaRowcount, V>> {
self.add_any_attr(aria_rowcount(value))
}
@@ -339,7 +329,7 @@ where
fn aria_rowindex(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaRowindex, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaRowindex, V>> {
self.add_any_attr(aria_rowindex(value))
}
@@ -347,7 +337,7 @@ where
fn aria_rowspan(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaRowspan, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaRowspan, V>> {
self.add_any_attr(aria_rowspan(value))
}
@@ -355,7 +345,7 @@ where
fn aria_selected(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaSelected, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaSelected, V>> {
self.add_any_attr(aria_selected(value))
}
@@ -363,7 +353,7 @@ where
fn aria_setsize(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaSetsize, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaSetsize, V>> {
self.add_any_attr(aria_setsize(value))
}
@@ -371,7 +361,7 @@ where
fn aria_sort(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaSort, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaSort, V>> {
self.add_any_attr(aria_sort(value))
}
@@ -379,7 +369,7 @@ where
fn aria_valuemax(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaValuemax, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaValuemax, V>> {
self.add_any_attr(aria_valuemax(value))
}
@@ -387,7 +377,7 @@ where
fn aria_valuemin(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaValuemin, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaValuemin, V>> {
self.add_any_attr(aria_valuemin(value))
}
@@ -395,7 +385,7 @@ where
fn aria_valuenow(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaValuenow, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaValuenow, V>> {
self.add_any_attr(aria_valuenow(value))
}
@@ -403,18 +393,16 @@ where
fn aria_valuetext(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<AriaValuetext, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<AriaValuetext, V>> {
self.add_any_attr(aria_valuetext(value))
}
}
impl<El, At, Ch, Rndr, V> AriaAttributes<Rndr, V>
for HtmlElement<El, At, Ch, Rndr>
impl<El, At, Ch, V> AriaAttributes<Rndr, V> for HtmlElement<El, At, Ch>
where
El: ElementType + CreateElement<Rndr> + Send,
At: Attribute<Rndr> + Send,
Ch: RenderHtml<Rndr> + Send,
V: AttributeValue<Rndr>,
Rndr: Renderer,
El: ElementType + Send,
At: Attribute + Send,
Ch: RenderHtml + Send,
V: AttributeValue,
{
}

View File

@@ -1,65 +1,54 @@
use super::NextAttribute;
use crate::{
html::attribute::{Attribute, AttributeValue},
renderer::DomRenderer,
view::{add_attr::AddAnyAttr, Position, ToTemplate},
};
use std::{borrow::Cow, marker::PhantomData, sync::Arc};
use std::{borrow::Cow, sync::Arc};
/// Adds a custom attribute with any key-value combintion.
#[inline(always)]
pub fn custom_attribute<K, V, R>(key: K, value: V) -> CustomAttr<K, V, R>
pub fn custom_attribute<K, V>(key: K, value: V) -> CustomAttr<K, V>
where
K: CustomAttributeKey,
V: AttributeValue<R>,
R: DomRenderer,
V: AttributeValue,
{
CustomAttr {
key,
value,
rndr: PhantomData,
}
CustomAttr { key, value }
}
/// A custom attribute with any key-value combination.
#[derive(Debug)]
pub struct CustomAttr<K, V, R>
pub struct CustomAttr<K, V>
where
K: CustomAttributeKey,
V: AttributeValue<R>,
R: DomRenderer,
V: AttributeValue,
{
key: K,
value: V,
rndr: PhantomData<R>,
}
impl<K, V, R> Clone for CustomAttr<K, V, R>
impl<K, V> Clone for CustomAttr<K, V>
where
K: CustomAttributeKey,
V: AttributeValue<R> + Clone,
R: DomRenderer,
V: AttributeValue + Clone,
{
fn clone(&self) -> Self {
Self {
key: self.key.clone(),
value: self.value.clone(),
rndr: self.rndr,
}
}
}
impl<K, V, R> Attribute<R> for CustomAttr<K, V, R>
impl<K, V> Attribute for CustomAttr<K, V>
where
K: CustomAttributeKey,
V: AttributeValue<R>,
R: DomRenderer,
V: AttributeValue,
{
const MIN_LENGTH: usize = 0;
type AsyncOutput = CustomAttr<K, V::AsyncOutput, R>;
type AsyncOutput = CustomAttr<K, V::AsyncOutput>;
type State = V::State;
type Cloneable = CustomAttr<K, V::Cloneable, R>;
type CloneableOwned = CustomAttr<K, V::CloneableOwned, R>;
type Cloneable = CustomAttr<K, V::Cloneable>;
type CloneableOwned = CustomAttr<K, V::CloneableOwned>;
fn html_len(&self) -> usize {
self.key.as_ref().len() + 3 + self.value.html_len()
@@ -75,7 +64,10 @@ where
self.value.to_html(self.key.as_ref(), buf);
}
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
if !K::KEY.is_empty() {
self.value.hydrate::<FROM_SERVER>(self.key.as_ref(), el)
} else {
@@ -83,7 +75,7 @@ where
}
}
fn build(self, el: &R::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
self.value.build(el, self.key.as_ref())
}
@@ -95,7 +87,6 @@ where
CustomAttr {
key: self.key,
value: self.value.into_cloneable(),
rndr: self.rndr,
}
}
@@ -103,7 +94,6 @@ where
CustomAttr {
key: self.key,
value: self.value.into_cloneable_owned(),
rndr: self.rndr,
}
}
@@ -115,20 +105,18 @@ where
CustomAttr {
key: self.key,
value: self.value.resolve().await,
rndr: self.rndr,
}
}
}
impl<K, V, R> NextAttribute<R> for CustomAttr<K, V, R>
impl<K, V> NextAttribute for CustomAttr<K, V>
where
K: CustomAttributeKey,
V: AttributeValue<R>,
R: DomRenderer,
V: AttributeValue,
{
type Output<NewAttr: Attribute<R>> = (Self, NewAttr);
type Output<NewAttr: Attribute> = (Self, NewAttr);
fn add_any_attr<NewAttr: Attribute<R>>(
fn add_any_attr<NewAttr: Attribute>(
self,
new_attr: NewAttr,
) -> Self::Output<NewAttr> {
@@ -136,11 +124,10 @@ where
}
}
impl<K, V, R> ToTemplate for CustomAttr<K, V, R>
impl<K, V> ToTemplate for CustomAttr<K, V>
where
K: CustomAttributeKey,
V: AttributeValue<R>,
R: DomRenderer,
V: AttributeValue,
{
fn to_template(
buf: &mut String,
@@ -186,28 +173,27 @@ impl<const K: &'static str> CustomAttributeKey
}
/// Adds a custom attribute to an element.
pub trait CustomAttribute<K, V, Rndr>
pub trait CustomAttribute<K, V>
where
K: CustomAttributeKey,
V: AttributeValue<Rndr>,
Rndr: DomRenderer,
Self: Sized + AddAnyAttr<Rndr>,
V: AttributeValue,
Self: Sized + AddAnyAttr,
{
/// Adds an HTML attribute by key and value.
fn attr(
self,
key: K,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<CustomAttr<K, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<CustomAttr<K, V>> {
self.add_any_attr(custom_attribute(key, value))
}
}
impl<T, K, V, Rndr> CustomAttribute<K, V, Rndr> for T
impl<T, K, V> CustomAttribute<K, V> for T
where
T: AddAnyAttr<Rndr>,
T: AddAnyAttr,
K: CustomAttributeKey,
V: AttributeValue<Rndr>,
Rndr: DomRenderer,
V: AttributeValue,
{
}

View File

@@ -3,22 +3,20 @@ use crate::{
html::{
attribute::*,
class::{class, Class, IntoClass},
element::{CreateElement, ElementType, HasElementType, HtmlElement},
element::{ElementType, HasElementType, HtmlElement},
event::{on, on_target, EventDescriptor, On, Targeted},
property::{prop, IntoProperty, Property},
style::{style, IntoStyle, Style},
},
prelude::RenderHtml,
renderer::DomRenderer,
view::add_attr::AddAnyAttr,
};
use core::convert::From;
/// Adds an attribute that modifies the `class`.
pub trait ClassAttribute<C, Rndr>
pub trait ClassAttribute<C>
where
C: IntoClass<Rndr>,
Rndr: DomRenderer,
C: IntoClass,
{
/// The type of the element with the new attribute added.
type Output;
@@ -27,16 +25,14 @@ where
fn class(self, value: C) -> Self::Output;
}
impl<E, At, Ch, C, Rndr> ClassAttribute<C, Rndr>
for HtmlElement<E, At, Ch, Rndr>
impl<E, At, Ch, C> ClassAttribute<C> for HtmlElement<E, At, Ch>
where
E: ElementType + CreateElement<Rndr> + Send,
At: Attribute<Rndr> + Send,
Ch: RenderHtml<Rndr> + Send,
C: IntoClass<Rndr>,
Rndr: DomRenderer,
E: ElementType + Send,
At: Attribute + Send,
Ch: RenderHtml + Send,
C: IntoClass,
{
type Output = <Self as AddAnyAttr<Rndr>>::Output<Class<C, Rndr>>;
type Output = <Self as AddAnyAttr>::Output<Class<C>>;
fn class(self, value: C) -> Self::Output {
self.add_any_attr(class(value))
@@ -44,10 +40,9 @@ where
}
/// Adds an attribute that modifies the DOM properties.
pub trait PropAttribute<K, P, Rndr>
pub trait PropAttribute<K, P>
where
P: IntoProperty<Rndr>,
Rndr: DomRenderer,
P: IntoProperty,
{
/// The type of the element with the new attribute added.
type Output;
@@ -56,17 +51,15 @@ where
fn prop(self, key: K, value: P) -> Self::Output;
}
impl<E, At, Ch, K, P, Rndr> PropAttribute<K, P, Rndr>
for HtmlElement<E, At, Ch, Rndr>
impl<E, At, Ch, K, P> PropAttribute<K, P> for HtmlElement<E, At, Ch>
where
E: ElementType + CreateElement<Rndr> + Send,
At: Attribute<Rndr> + Send,
Ch: RenderHtml<Rndr> + Send,
E: ElementType + Send,
At: Attribute + Send,
Ch: RenderHtml + Send,
K: AsRef<str> + Send,
P: IntoProperty<Rndr>,
Rndr: DomRenderer,
P: IntoProperty,
{
type Output = <Self as AddAnyAttr<Rndr>>::Output<Property<K, P, Rndr>>;
type Output = <Self as AddAnyAttr>::Output<Property<K, P>>;
fn prop(self, key: K, value: P) -> Self::Output {
self.add_any_attr(prop(key, value))
@@ -74,10 +67,9 @@ where
}
/// Adds an attribute that modifies the CSS styles.
pub trait StyleAttribute<S, Rndr>
pub trait StyleAttribute<S>
where
S: IntoStyle<Rndr>,
Rndr: DomRenderer,
S: IntoStyle,
{
/// The type of the element with the new attribute added.
type Output;
@@ -86,16 +78,14 @@ where
fn style(self, value: S) -> Self::Output;
}
impl<E, At, Ch, S, Rndr> StyleAttribute<S, Rndr>
for HtmlElement<E, At, Ch, Rndr>
impl<E, At, Ch, S> StyleAttribute<S> for HtmlElement<E, At, Ch>
where
E: ElementType + CreateElement<Rndr> + Send,
At: Attribute<Rndr> + Send,
Ch: RenderHtml<Rndr> + Send,
S: IntoStyle<Rndr>,
Rndr: DomRenderer,
E: ElementType + Send,
At: Attribute + Send,
Ch: RenderHtml + Send,
S: IntoStyle,
{
type Output = <Self as AddAnyAttr<Rndr>>::Output<Style<S, Rndr>>;
type Output = <Self as AddAnyAttr>::Output<Style<S>>;
fn style(self, value: S) -> Self::Output {
self.add_any_attr(style(value))
@@ -103,7 +93,7 @@ where
}
/// Adds an event listener to an element definition.
pub trait OnAttribute<E, F, Rndr> {
pub trait OnAttribute<E, F> {
/// The type of the element with the event listener added.
type Output;
@@ -111,19 +101,17 @@ pub trait OnAttribute<E, F, Rndr> {
fn on(self, event: E, cb: F) -> Self::Output;
}
impl<El, At, Ch, E, F, Rndr> OnAttribute<E, F, Rndr>
for HtmlElement<El, At, Ch, Rndr>
impl<El, At, Ch, E, F> OnAttribute<E, F> for HtmlElement<El, At, Ch>
where
El: ElementType + CreateElement<Rndr> + Send,
At: Attribute<Rndr> + Send,
Ch: RenderHtml<Rndr> + Send,
El: ElementType + Send,
At: Attribute + Send,
Ch: RenderHtml + Send,
E: EventDescriptor + Send + 'static,
E::EventType: 'static,
E::EventType: From<Rndr::Event>,
E::EventType: From<crate::renderer::types::Event>,
F: FnMut(E::EventType) + 'static,
Rndr: DomRenderer,
{
type Output = <Self as AddAnyAttr<Rndr>>::Output<On<E, F, Rndr>>;
type Output = <Self as AddAnyAttr>::Output<On<E, F>>;
fn on(self, event: E, cb: F) -> Self::Output {
self.add_any_attr(on(event, cb))
@@ -131,7 +119,7 @@ where
}
/// Adds an event listener with a typed target to an element definition.
pub trait OnTargetAttribute<E, F, T, Rndr> {
pub trait OnTargetAttribute<E, F, T> {
/// The type of the element with the new attribute added.
type Output;
@@ -139,43 +127,36 @@ pub trait OnTargetAttribute<E, F, T, Rndr> {
fn on_target(self, event: E, cb: F) -> Self::Output;
}
impl<El, At, Ch, E, F, Rndr> OnTargetAttribute<E, F, Self, Rndr>
for HtmlElement<El, At, Ch, Rndr>
impl<El, At, Ch, E, F> OnTargetAttribute<E, F, Self> for HtmlElement<El, At, Ch>
where
El: ElementType + CreateElement<Rndr> + Send,
At: Attribute<Rndr> + Send,
Ch: RenderHtml<Rndr> + Send,
El: ElementType + Send,
At: Attribute + Send,
Ch: RenderHtml + Send,
E: EventDescriptor + Send + 'static,
E::EventType: 'static,
E::EventType: From<Rndr::Event>,
F: FnMut(
Targeted<E::EventType, <Self as HasElementType>::ElementType, Rndr>,
) + 'static,
Rndr: DomRenderer,
E::EventType: From<crate::renderer::types::Event>,
F: FnMut(Targeted<E::EventType, <Self as HasElementType>::ElementType>)
+ 'static,
{
type Output = <Self as AddAnyAttr<Rndr>>::Output<
On<E, Box<dyn FnMut(E::EventType)>, Rndr>,
>;
type Output =
<Self as AddAnyAttr>::Output<On<E, Box<dyn FnMut(E::EventType)>>>;
fn on_target(self, event: E, cb: F) -> Self::Output {
self.add_any_attr(
on_target::<E, HtmlElement<El, At, Ch, Rndr>, Rndr, F>(event, cb),
)
self.add_any_attr(on_target::<E, HtmlElement<El, At, Ch>, F>(event, cb))
}
}
/// Global attributes can be added to any HTML element.
pub trait GlobalAttributes<Rndr, V>
pub trait GlobalAttributes<V>
where
Self: Sized + AddAnyAttr<Rndr>,
V: AttributeValue<Rndr>,
Rndr: Renderer,
Self: Sized + AddAnyAttr,
V: AttributeValue,
{
/// The `accesskey` global attribute provides a hint for generating a keyboard shortcut for the current element.
fn accesskey(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Accesskey, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<Accesskey, V>> {
self.add_any_attr(accesskey(value))
}
@@ -183,7 +164,7 @@ where
fn autocapitalize(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Autocapitalize, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<Autocapitalize, V>> {
self.add_any_attr(autocapitalize(value))
}
@@ -191,7 +172,7 @@ where
fn autofocus(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Autofocus, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<Autofocus, V>> {
self.add_any_attr(autofocus(value))
}
@@ -199,16 +180,12 @@ where
fn contenteditable(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Contenteditable, V, Rndr>>
{
) -> <Self as AddAnyAttr>::Output<Attr<Contenteditable, V>> {
self.add_any_attr(contenteditable(value))
}
/// The `dir` global attribute is an enumerated attribute indicating the directionality of the element's text.
fn dir(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Dir, V, Rndr>> {
fn dir(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Dir, V>> {
self.add_any_attr(dir(value))
}
@@ -216,7 +193,7 @@ where
fn draggable(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Draggable, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<Draggable, V>> {
self.add_any_attr(draggable(value))
}
@@ -224,31 +201,22 @@ where
fn enterkeyhint(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Enterkeyhint, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<Enterkeyhint, V>> {
self.add_any_attr(enterkeyhint(value))
}
/// The `hidden` global attribute is a Boolean attribute indicating that the element is not yet, or is no longer, relevant.
fn hidden(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Hidden, V, Rndr>> {
fn hidden(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Hidden, V>> {
self.add_any_attr(hidden(value))
}
/// The `id` global attribute defines a unique identifier (ID) which must be unique in the whole document.
fn id(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Id, V, Rndr>> {
fn id(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Id, V>> {
self.add_any_attr(id(value))
}
/// The `inert` global attribute is a Boolean attribute that makes an element behave inertly.
fn inert(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Inert, V, Rndr>> {
fn inert(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Inert, V>> {
self.add_any_attr(inert(value))
}
@@ -256,23 +224,17 @@ where
fn inputmode(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Inputmode, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<Inputmode, V>> {
self.add_any_attr(inputmode(value))
}
/// The `is` global attribute allows you to specify that a standard HTML element should behave like a custom built-in element.
fn is(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Is, V, Rndr>> {
fn is(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Is, V>> {
self.add_any_attr(is(value))
}
/// The `itemid` global attribute is used to specify the unique, global identifier of an item.
fn itemid(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Itemid, V, Rndr>> {
fn itemid(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Itemid, V>> {
self.add_any_attr(itemid(value))
}
@@ -280,7 +242,7 @@ where
fn itemprop(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Itemprop, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<Itemprop, V>> {
self.add_any_attr(itemprop(value))
}
@@ -288,7 +250,7 @@ where
fn itemref(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Itemref, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<Itemref, V>> {
self.add_any_attr(itemref(value))
}
@@ -296,7 +258,7 @@ where
fn itemscope(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Itemscope, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<Itemscope, V>> {
self.add_any_attr(itemscope(value))
}
@@ -304,31 +266,22 @@ where
fn itemtype(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Itemtype, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<Itemtype, V>> {
self.add_any_attr(itemtype(value))
}
/// The `lang` global attribute helps define the language of an element.
fn lang(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Lang, V, Rndr>> {
fn lang(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Lang, V>> {
self.add_any_attr(lang(value))
}
/// The `nonce` global attribute is used to specify a cryptographic nonce.
fn nonce(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Nonce, V, Rndr>> {
fn nonce(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Nonce, V>> {
self.add_any_attr(nonce(value))
}
/// The `part` global attribute identifies the element as a part of a component.
fn part(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Part, V, Rndr>> {
fn part(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Part, V>> {
self.add_any_attr(part(value))
}
@@ -336,23 +289,17 @@ where
fn popover(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Popover, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<Popover, V>> {
self.add_any_attr(popover(value))
}
/// The `role` global attribute defines the role of an element in ARIA.
fn role(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Role, V, Rndr>> {
fn role(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Role, V>> {
self.add_any_attr(role(value))
}
/// The `slot` global attribute assigns a slot in a shadow DOM.
fn slot(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Slot, V, Rndr>> {
fn slot(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Slot, V>> {
self.add_any_attr(slot(value))
}
@@ -360,7 +307,7 @@ where
fn spellcheck(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Spellcheck, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<Spellcheck, V>> {
self.add_any_attr(spellcheck(value))
}
@@ -368,15 +315,12 @@ where
fn tabindex(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Tabindex, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<Tabindex, V>> {
self.add_any_attr(tabindex(value))
}
/// The `title` global attribute contains text representing advisory information.
fn title(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Title, V, Rndr>> {
fn title(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Title, V>> {
self.add_any_attr(title(value))
}
@@ -384,7 +328,7 @@ where
fn translate(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Translate, V, Rndr>> {
) -> <Self as AddAnyAttr>::Output<Attr<Translate, V>> {
self.add_any_attr(translate(value))
}
@@ -392,20 +336,17 @@ where
fn virtualkeyboardpolicy(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<Virtualkeyboardpolicy, V, Rndr>>
{
) -> <Self as AddAnyAttr>::Output<Attr<Virtualkeyboardpolicy, V>> {
self.add_any_attr(virtualkeyboardpolicy(value))
}
}
impl<El, At, Ch, Rndr, V> GlobalAttributes<Rndr, V>
for HtmlElement<El, At, Ch, Rndr>
impl<El, At, Ch, V> GlobalAttributes<V> for HtmlElement<El, At, Ch>
where
El: ElementType + CreateElement<Rndr> + Send,
At: Attribute<Rndr> + Send,
Ch: RenderHtml<Rndr> + Send,
V: AttributeValue<Rndr>,
Rndr: Renderer,
El: ElementType + Send,
At: Attribute + Send,
Ch: RenderHtml + Send,
V: AttributeValue,
{
}
@@ -418,7 +359,7 @@ macro_rules! on_definitions {
fn $key(
self,
value: V,
) -> <Self as AddAnyAttr<Rndr>>::Output<Attr<[<$key:camel>], V, Rndr>>
) -> <Self as AddAnyAttr>::Output<Attr<[<$key:camel>], V>>
{
self.add_any_attr($key(value))
}
@@ -428,11 +369,10 @@ macro_rules! on_definitions {
}
/// Provides methods for HTML event listener attributes.
pub trait GlobalOnAttributes<Rndr, V>
pub trait GlobalOnAttributes<V>
where
Self: Sized + AddAnyAttr<Rndr>,
V: AttributeValue<Rndr>,
Rndr: Renderer,
Self: Sized + AddAnyAttr,
V: AttributeValue,
{
on_definitions! {
/// The `onabort` attribute specifies the event handler for the abort event.
@@ -575,13 +515,11 @@ where
}
}
impl<El, At, Ch, Rndr, V> GlobalOnAttributes<Rndr, V>
for HtmlElement<El, At, Ch, Rndr>
impl<El, At, Ch, V> GlobalOnAttributes<V> for HtmlElement<El, At, Ch>
where
El: ElementType + CreateElement<Rndr> + Send,
At: Attribute<Rndr> + Send,
Ch: RenderHtml<Rndr> + Send,
V: AttributeValue<Rndr>,
Rndr: Renderer,
El: ElementType + Send,
At: Attribute + Send,
Ch: RenderHtml + Send,
V: AttributeValue,
{
}

View File

@@ -1,6 +1,5 @@
use super::{Attr, AttributeValue};
use crate::renderer::Renderer;
use std::{fmt::Debug, marker::PhantomData};
use std::fmt::Debug;
/// An HTML attribute key.
pub trait AttributeKey: Clone + Send + 'static {
@@ -14,11 +13,11 @@ macro_rules! attributes {
$(
#[$meta]
#[track_caller]
pub fn $key<V, Rndr>(value: V) -> Attr<[<$key:camel>], V, Rndr>
where V: AttributeValue<Rndr>,
Rndr: Renderer
pub fn $key<V>(value: V) -> Attr<[<$key:camel>], V>
where V: AttributeValue,
{
Attr([<$key:camel>], value, PhantomData)
Attr([<$key:camel>], value)
}
#[$meta]

View File

@@ -8,28 +8,25 @@ pub mod custom;
pub mod global;
mod key;
mod value;
use crate::{
renderer::Renderer,
view::{Position, ToTemplate},
};
use crate::view::{Position, ToTemplate};
pub use key::*;
use std::{fmt::Debug, future::Future, marker::PhantomData};
use std::{fmt::Debug, future::Future};
pub use value::*;
/// Defines an attribute: anything that can modify an element.
pub trait Attribute<R: Renderer>: NextAttribute<R> + Send {
pub trait Attribute: NextAttribute + Send {
/// The minimum length of this attribute in HTML.
const MIN_LENGTH: usize;
/// The state that should be retained between building and rebuilding.
type State;
/// The type once all async data have loaded.
type AsyncOutput: Attribute<R>;
type AsyncOutput: Attribute;
/// An equivalent to this attribute that can be cloned to be shared across elements.
type Cloneable: Attribute<R> + Clone;
type Cloneable: Attribute + Clone;
/// An equivalent to this attribute that can be cloned to be shared across elements, and
/// captures no references shorter than `'static`.
type CloneableOwned: Attribute<R> + Clone + 'static;
type CloneableOwned: Attribute + Clone + 'static;
/// An approximation of the actual length of this attribute in HTML.
fn html_len(&self) -> usize;
@@ -49,10 +46,13 @@ pub trait Attribute<R: Renderer>: NextAttribute<R> + Send {
/// Adds interactivity as necessary, given DOM nodes that were created from HTML that has
/// either been rendered on the server, or cloned for a `<template>`.
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State;
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State;
/// Adds this attribute to the element during client-side rendering.
fn build(self, el: &R::Element) -> Self::State;
fn build(self, el: &crate::renderer::types::Element) -> Self::State;
/// Applies a new value for the attribute.
fn rebuild(self, state: &mut Self::State);
@@ -75,21 +75,18 @@ pub trait Attribute<R: Renderer>: NextAttribute<R> + Send {
/// Adds another attribute to this one, returning a new attribute.
///
/// This is typically achieved by creating or extending a tuple of attributes.
pub trait NextAttribute<R: Renderer> {
pub trait NextAttribute {
/// The type of the new, combined attribute.
type Output<NewAttr: Attribute<R>>: Attribute<R>;
type Output<NewAttr: Attribute>: Attribute;
/// Adds a new attribute.
fn add_any_attr<NewAttr: Attribute<R>>(
fn add_any_attr<NewAttr: Attribute>(
self,
new_attr: NewAttr,
) -> Self::Output<NewAttr>;
}
impl<R> Attribute<R> for ()
where
R: Renderer,
{
impl Attribute for () {
const MIN_LENGTH: usize = 0;
type State = ();
@@ -110,10 +107,13 @@ where
) {
}
fn hydrate<const FROM_SERVER: bool>(self, _el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(
self,
_el: &crate::renderer::types::Element,
) -> Self::State {
}
fn build(self, _el: &R::Element) -> Self::State {}
fn build(self, _el: &crate::renderer::types::Element) -> Self::State {}
fn rebuild(self, _state: &mut Self::State) {}
@@ -130,13 +130,10 @@ where
async fn resolve(self) -> Self::AsyncOutput {}
}
impl<R> NextAttribute<R> for ()
where
R: Renderer,
{
type Output<NewAttr: Attribute<R>> = (NewAttr,);
impl NextAttribute for () {
type Output<NewAttr: Attribute> = (NewAttr,);
fn add_any_attr<NewAttr: Attribute<R>>(
fn add_any_attr<NewAttr: Attribute>(
self,
new_attr: NewAttr,
) -> Self::Output<NewAttr> {
@@ -146,28 +143,25 @@ where
/// An attribute with a key and value.
#[derive(Debug)]
pub struct Attr<K, V, R>(pub K, pub V, pub PhantomData<R>)
pub struct Attr<K, V>(pub K, pub V)
where
K: AttributeKey,
V: AttributeValue<R>,
R: Renderer;
V: AttributeValue;
impl<K, V, R> Clone for Attr<K, V, R>
impl<K, V> Clone for Attr<K, V>
where
K: AttributeKey,
V: AttributeValue<R> + Clone,
R: Renderer,
V: AttributeValue + Clone,
{
fn clone(&self) -> Self {
Self(self.0.clone(), self.1.clone(), PhantomData)
Self(self.0.clone(), self.1.clone())
}
}
impl<K, V, R> ToTemplate for Attr<K, V, R>
impl<K, V> ToTemplate for Attr<K, V>
where
K: AttributeKey,
V: AttributeValue<R>,
R: Renderer,
V: AttributeValue,
{
fn to_template(
buf: &mut String,
@@ -180,18 +174,17 @@ where
}
}
impl<K, V, R> Attribute<R> for Attr<K, V, R>
impl<K, V> Attribute for Attr<K, V>
where
K: AttributeKey + Send,
V: AttributeValue<R> + Send,
R: Renderer,
V: AttributeValue + Send,
{
const MIN_LENGTH: usize = 0;
type State = V::State;
type AsyncOutput = Attr<K, V::AsyncOutput, R>;
type Cloneable = Attr<K, V::Cloneable, R>;
type CloneableOwned = Attr<K, V::CloneableOwned, R>;
type AsyncOutput = Attr<K, V::AsyncOutput>;
type Cloneable = Attr<K, V::Cloneable>;
type CloneableOwned = Attr<K, V::CloneableOwned>;
fn html_len(&self) -> usize {
K::KEY.len() + 3 + self.1.html_len()
@@ -207,11 +200,14 @@ where
self.1.to_html(K::KEY, buf);
}
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
self.1.hydrate::<FROM_SERVER>(K::KEY, el)
}
fn build(self, el: &R::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
V::build(self.1, el, K::KEY)
}
@@ -220,11 +216,11 @@ where
}
fn into_cloneable(self) -> Self::Cloneable {
Attr(self.0, self.1.into_cloneable(), PhantomData)
Attr(self.0, self.1.into_cloneable())
}
fn into_cloneable_owned(self) -> Self::CloneableOwned {
Attr(self.0, self.1.into_cloneable_owned(), PhantomData)
Attr(self.0, self.1.into_cloneable_owned())
}
fn dry_resolve(&mut self) {
@@ -232,19 +228,18 @@ where
}
async fn resolve(self) -> Self::AsyncOutput {
Attr(self.0, self.1.resolve().await, PhantomData)
Attr(self.0, self.1.resolve().await)
}
}
impl<K, V, R> NextAttribute<R> for Attr<K, V, R>
impl<K, V> NextAttribute for Attr<K, V>
where
K: AttributeKey,
V: AttributeValue<R>,
R: Renderer,
V: AttributeValue,
{
type Output<NewAttr: Attribute<R>> = (Self, NewAttr);
type Output<NewAttr: Attribute> = (Self, NewAttr);
fn add_any_attr<NewAttr: Attribute<R>>(
fn add_any_attr<NewAttr: Attribute>(
self,
new_attr: NewAttr,
) -> Self::Output<NewAttr> {
@@ -254,11 +249,11 @@ where
macro_rules! impl_attr_for_tuples {
($first:ident, $($ty:ident),* $(,)?) => {
impl<$first, $($ty),*, Rndr> Attribute<Rndr> for ($first, $($ty,)*)
impl<$first, $($ty),*> Attribute for ($first, $($ty,)*)
where
$first: Attribute<Rndr>,
$($ty: Attribute<Rndr>),*,
Rndr: Renderer
$first: Attribute,
$($ty: Attribute),*,
{
const MIN_LENGTH: usize = $first::MIN_LENGTH $(+ $ty::MIN_LENGTH)*;
@@ -280,7 +275,7 @@ macro_rules! impl_attr_for_tuples {
$($ty.to_html(buf, class, style, inner_html));*
}
fn hydrate<const FROM_SERVER: bool>(self, el: &Rndr::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(self, el: &crate::renderer::types::Element) -> Self::State {
#[allow(non_snake_case)]
let ($first, $($ty,)* ) = self;
(
@@ -289,7 +284,7 @@ macro_rules! impl_attr_for_tuples {
)
}
fn build(self, el: &Rndr::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
#[allow(non_snake_case)]
let ($first, $($ty,)*) = self;
(
@@ -342,15 +337,15 @@ macro_rules! impl_attr_for_tuples {
}
}
impl<$first, $($ty),*, Rndr> NextAttribute<Rndr> for ($first, $($ty,)*)
impl<$first, $($ty),*> NextAttribute for ($first, $($ty,)*)
where
$first: Attribute<Rndr>,
$($ty: Attribute<Rndr>),*,
Rndr: Renderer
{
type Output<NewAttr: Attribute<Rndr>> = ($first, $($ty,)* NewAttr);
$first: Attribute,
$($ty: Attribute),*,
fn add_any_attr<NewAttr: Attribute<Rndr>>(
{
type Output<NewAttr: Attribute> = ($first, $($ty,)* NewAttr);
fn add_any_attr<NewAttr: Attribute>(
self,
new_attr: NewAttr,
) -> Self::Output<NewAttr> {
@@ -364,11 +359,11 @@ macro_rules! impl_attr_for_tuples {
macro_rules! impl_attr_for_tuples_truncate_additional {
($first:ident, $($ty:ident),* $(,)?) => {
impl<$first, $($ty),*, Rndr> Attribute<Rndr> for ($first, $($ty,)*)
impl<$first, $($ty),*> Attribute for ($first, $($ty,)*)
where
$first: Attribute<Rndr>,
$($ty: Attribute<Rndr>),*,
Rndr: Renderer
$first: Attribute,
$($ty: Attribute),*,
{
const MIN_LENGTH: usize = $first::MIN_LENGTH $(+ $ty::MIN_LENGTH)*;
@@ -390,7 +385,7 @@ macro_rules! impl_attr_for_tuples_truncate_additional {
$($ty.to_html(buf, class, style, inner_html));*
}
fn hydrate<const FROM_SERVER: bool>(self, el: &Rndr::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(self, el: &crate::renderer::types::Element) -> Self::State {
#[allow(non_snake_case)]
let ($first, $($ty,)* ) = self;
(
@@ -399,7 +394,7 @@ macro_rules! impl_attr_for_tuples_truncate_additional {
)
}
fn build(self, el: &Rndr::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
#[allow(non_snake_case)]
let ($first, $($ty,)*) = self;
(
@@ -452,15 +447,15 @@ macro_rules! impl_attr_for_tuples_truncate_additional {
}
}
impl<$first, $($ty),*, Rndr> NextAttribute<Rndr> for ($first, $($ty,)*)
impl<$first, $($ty),*> NextAttribute for ($first, $($ty,)*)
where
$first: Attribute<Rndr>,
$($ty: Attribute<Rndr>),*,
Rndr: Renderer
{
type Output<NewAttr: Attribute<Rndr>> = ($first, $($ty,)*);
$first: Attribute,
$($ty: Attribute),*,
fn add_any_attr<NewAttr: Attribute<Rndr>>(
{
type Output<NewAttr: Attribute> = ($first, $($ty,)*);
fn add_any_attr<NewAttr: Attribute>(
self,
_new_attr: NewAttr,
) -> Self::Output<NewAttr> {
@@ -471,10 +466,9 @@ macro_rules! impl_attr_for_tuples_truncate_additional {
};
}
impl<A, Rndr> Attribute<Rndr> for (A,)
impl<A> Attribute for (A,)
where
A: Attribute<Rndr>,
Rndr: Renderer,
A: Attribute,
{
const MIN_LENGTH: usize = A::MIN_LENGTH;
@@ -499,12 +493,12 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
el: &Rndr::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
self.0.hydrate::<FROM_SERVER>(el)
}
fn build(self, el: &Rndr::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
self.0.build(el)
}
@@ -529,14 +523,13 @@ where
}
}
impl<A, Rndr> NextAttribute<Rndr> for (A,)
impl<A> NextAttribute for (A,)
where
A: Attribute<Rndr>,
Rndr: Renderer,
A: Attribute,
{
type Output<NewAttr: Attribute<Rndr>> = (A, NewAttr);
type Output<NewAttr: Attribute> = (A, NewAttr);
fn add_any_attr<NewAttr: Attribute<Rndr>>(
fn add_any_attr<NewAttr: Attribute>(
self,
new_attr: NewAttr,
) -> Self::Output<NewAttr> {

View File

@@ -1,4 +1,4 @@
use crate::renderer::Renderer;
use crate::renderer::Rndr;
use std::{
borrow::Cow,
future::Future,
@@ -12,12 +12,12 @@ use std::{
};
/// A possible value for an HTML attribute.
pub trait AttributeValue<R: Renderer>: Send {
pub trait AttributeValue: Send {
/// The state that should be retained between building and rebuilding.
type State;
/// The type once all async data have loaded.
type AsyncOutput: AttributeValue<R>;
type AsyncOutput: AttributeValue;
/// A version of the value that can be cloned. This can be the same type, or a
/// reference-counted type. Generally speaking, this does *not* need to refer to the same data,
@@ -25,12 +25,12 @@ pub trait AttributeValue<R: Renderer>: Send {
/// probably make it reference-counted (so that a `FnMut()` continues mutating the same
/// closure), but making a `String` cloneable does not necessarily need to make it an
/// `Arc<str>`, as two different clones of a `String` will still have the same value.
type Cloneable: AttributeValue<R> + Clone;
type Cloneable: AttributeValue + Clone;
/// A cloneable type that is also `'static`. This is used for spreading across types when the
/// spreadable attribute needs to be owned. In some cases (`&'a str` to `Arc<str>`, etc.) the owned
/// cloneable type has worse performance than the cloneable type, so they are separate.
type CloneableOwned: AttributeValue<R> + Clone + 'static;
type CloneableOwned: AttributeValue + Clone + 'static;
/// An approximation of the actual length of this attribute in HTML.
fn html_len(&self) -> usize;
@@ -46,11 +46,15 @@ pub trait AttributeValue<R: Renderer>: Send {
fn hydrate<const FROM_SERVER: bool>(
self,
key: &str,
el: &R::Element,
el: &crate::renderer::types::Element,
) -> Self::State;
/// Adds this attribute to the element during client-side rendering.
fn build(self, el: &R::Element, key: &str) -> Self::State;
fn build(
self,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State;
/// Applies a new value for the attribute.
fn rebuild(self, key: &str, state: &mut Self::State);
@@ -70,10 +74,7 @@ pub trait AttributeValue<R: Renderer>: Send {
fn resolve(self) -> impl Future<Output = Self::AsyncOutput> + Send;
}
impl<R> AttributeValue<R> for ()
where
R: Renderer,
{
impl AttributeValue for () {
type State = ();
type AsyncOutput = ();
type Cloneable = ();
@@ -87,9 +88,19 @@ where
fn to_template(_key: &str, _buf: &mut String) {}
fn hydrate<const FROM_SERVER: bool>(self, _key: &str, _el: &R::Element) {}
fn hydrate<const FROM_SERVER: bool>(
self,
_key: &str,
_el: &crate::renderer::types::Element,
) {
}
fn build(self, _el: &R::Element, _key: &str) -> Self::State {}
fn build(
self,
_el: &crate::renderer::types::Element,
_key: &str,
) -> Self::State {
}
fn rebuild(self, _key: &str, _state: &mut Self::State) {}
@@ -106,11 +117,8 @@ where
async fn resolve(self) {}
}
impl<'a, R> AttributeValue<R> for &'a str
where
R: Renderer,
{
type State = (R::Element, &'a str);
impl<'a> AttributeValue for &'a str {
type State = (crate::renderer::types::Element, &'a str);
type AsyncOutput = &'a str;
type Cloneable = &'a str;
type CloneableOwned = Arc<str>;
@@ -132,25 +140,29 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
key: &str,
el: &R::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
// if we're actually hydrating from SSRed HTML, we don't need to set the attribute
// if we're hydrating from a CSR-cloned <template>, we do need to set non-StaticAttr attributes
if !FROM_SERVER {
R::set_attribute(el, key, self);
Rndr::set_attribute(el, key, self);
}
(el.clone(), self)
}
fn build(self, el: &R::Element, key: &str) -> Self::State {
R::set_attribute(el, key, self);
fn build(
self,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
Rndr::set_attribute(el, key, self);
(el.to_owned(), self)
}
fn rebuild(self, key: &str, state: &mut Self::State) {
let (el, prev_value) = state;
if self != *prev_value {
R::set_attribute(el, key, self);
Rndr::set_attribute(el, key, self);
}
*prev_value = self;
}
@@ -171,10 +183,8 @@ where
}
#[cfg(feature = "nightly")]
impl<R, const V: &'static str> AttributeValue<R>
impl<const V: &'static str> AttributeValue
for crate::view::static_types::Static<V>
where
R: Renderer,
{
type AsyncOutput = Self;
type State = ();
@@ -186,7 +196,7 @@ where
}
fn to_html(self, key: &str, buf: &mut String) {
<&str as AttributeValue<R>>::to_html(V, key, buf);
<&str as AttributeValue>::to_html(V, key, buf);
}
fn to_template(key: &str, buf: &mut String) {
@@ -200,12 +210,16 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
_key: &str,
_el: &R::Element,
_el: &crate::renderer::types::Element,
) -> Self::State {
}
fn build(self, el: &R::Element, key: &str) -> Self::State {
<&str as AttributeValue<R>>::build(V, el, key);
fn build(
self,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
<&str as AttributeValue>::build(V, el, key);
}
fn rebuild(self, _key: &str, _state: &mut Self::State) {}
@@ -225,12 +239,9 @@ where
}
}
impl<'a, R> AttributeValue<R> for &'a String
where
R: Renderer,
{
impl<'a> AttributeValue for &'a String {
type AsyncOutput = Self;
type State = (R::Element, &'a String);
type State = (crate::renderer::types::Element, &'a String);
type Cloneable = Self;
type CloneableOwned = Arc<str>;
@@ -239,7 +250,7 @@ where
}
fn to_html(self, key: &str, buf: &mut String) {
<&str as AttributeValue<R>>::to_html(self.as_str(), key, buf);
<&str as AttributeValue>::to_html(self.as_str(), key, buf);
}
fn to_template(_key: &str, _buf: &mut String) {}
@@ -247,9 +258,9 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
key: &str,
el: &R::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
let (el, _) = <&str as AttributeValue<R>>::hydrate::<FROM_SERVER>(
let (el, _) = <&str as AttributeValue>::hydrate::<FROM_SERVER>(
self.as_str(),
key,
el,
@@ -257,15 +268,19 @@ where
(el, self)
}
fn build(self, el: &R::Element, key: &str) -> Self::State {
R::set_attribute(el, key, self);
fn build(
self,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
Rndr::set_attribute(el, key, self);
(el.clone(), self)
}
fn rebuild(self, key: &str, state: &mut Self::State) {
let (el, prev_value) = state;
if self != *prev_value {
R::set_attribute(el, key, self);
Rndr::set_attribute(el, key, self);
}
*prev_value = self;
}
@@ -285,12 +300,9 @@ where
}
}
impl<R> AttributeValue<R> for String
where
R: Renderer,
{
impl AttributeValue for String {
type AsyncOutput = Self;
type State = (R::Element, String);
type State = (crate::renderer::types::Element, String);
type Cloneable = Arc<str>;
type CloneableOwned = Arc<str>;
@@ -299,7 +311,7 @@ where
}
fn to_html(self, key: &str, buf: &mut String) {
<&str as AttributeValue<R>>::to_html(self.as_str(), key, buf);
<&str as AttributeValue>::to_html(self.as_str(), key, buf);
}
fn to_template(_key: &str, _buf: &mut String) {}
@@ -307,9 +319,9 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
key: &str,
el: &R::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
let (el, _) = <&str as AttributeValue<R>>::hydrate::<FROM_SERVER>(
let (el, _) = <&str as AttributeValue>::hydrate::<FROM_SERVER>(
self.as_str(),
key,
el,
@@ -317,15 +329,19 @@ where
(el, self)
}
fn build(self, el: &R::Element, key: &str) -> Self::State {
R::set_attribute(el, key, &self);
fn build(
self,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
Rndr::set_attribute(el, key, &self);
(el.clone(), self)
}
fn rebuild(self, key: &str, state: &mut Self::State) {
let (el, prev_value) = state;
if self != *prev_value {
R::set_attribute(el, key, &self);
Rndr::set_attribute(el, key, &self);
}
*prev_value = self;
}
@@ -345,12 +361,9 @@ where
}
}
impl<R> AttributeValue<R> for Arc<str>
where
R: Renderer,
{
impl AttributeValue for Arc<str> {
type AsyncOutput = Self;
type State = (R::Element, Arc<str>);
type State = (crate::renderer::types::Element, Arc<str>);
type Cloneable = Arc<str>;
type CloneableOwned = Arc<str>;
@@ -359,7 +372,7 @@ where
}
fn to_html(self, key: &str, buf: &mut String) {
<&str as AttributeValue<R>>::to_html(self.as_ref(), key, buf);
<&str as AttributeValue>::to_html(self.as_ref(), key, buf);
}
fn to_template(_key: &str, _buf: &mut String) {}
@@ -367,9 +380,9 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
key: &str,
el: &R::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
let (el, _) = <&str as AttributeValue<R>>::hydrate::<FROM_SERVER>(
let (el, _) = <&str as AttributeValue>::hydrate::<FROM_SERVER>(
self.as_ref(),
key,
el,
@@ -377,15 +390,19 @@ where
(el, self)
}
fn build(self, el: &R::Element, key: &str) -> Self::State {
R::set_attribute(el, key, &self);
fn build(
self,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
Rndr::set_attribute(el, key, &self);
(el.clone(), self)
}
fn rebuild(self, key: &str, state: &mut Self::State) {
let (el, prev_value) = state;
if self != *prev_value {
R::set_attribute(el, key, &self);
Rndr::set_attribute(el, key, &self);
}
*prev_value = self;
}
@@ -406,12 +423,9 @@ where
}
// TODO impl AttributeValue for Rc<str> and Arc<str> too
impl<R> AttributeValue<R> for bool
where
R: Renderer,
{
impl AttributeValue for bool {
type AsyncOutput = Self;
type State = (R::Element, bool);
type State = (crate::renderer::types::Element, bool);
type Cloneable = Self;
type CloneableOwned = Self;
@@ -431,19 +445,23 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
key: &str,
el: &R::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
// if we're actually hydrating from SSRed HTML, we don't need to set the attribute
// if we're hydrating from a CSR-cloned <template>, we do need to set non-StaticAttr attributes
if !FROM_SERVER {
R::set_attribute(el, key, "");
Rndr::set_attribute(el, key, "");
}
(el.clone(), self)
}
fn build(self, el: &R::Element, key: &str) -> Self::State {
fn build(
self,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
if self {
R::set_attribute(el, key, "");
Rndr::set_attribute(el, key, "");
}
(el.clone(), self)
}
@@ -452,9 +470,9 @@ where
let (el, prev_value) = state;
if self != *prev_value {
if self {
R::set_attribute(el, key, "");
Rndr::set_attribute(el, key, "");
} else {
R::remove_attribute(el, key);
Rndr::remove_attribute(el, key);
}
}
*prev_value = self;
@@ -475,13 +493,12 @@ where
}
}
impl<V, R> AttributeValue<R> for Option<V>
impl<V> AttributeValue for Option<V>
where
V: AttributeValue<R>,
R: Renderer,
V: AttributeValue,
{
type AsyncOutput = Option<V::AsyncOutput>;
type State = (R::Element, Option<V::State>);
type State = (crate::renderer::types::Element, Option<V::State>);
type Cloneable = Option<V::Cloneable>;
type CloneableOwned = Option<V::CloneableOwned>;
@@ -503,13 +520,17 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
key: &str,
el: &R::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
let state = self.map(|v| v.hydrate::<FROM_SERVER>(key, el));
(el.clone(), state)
}
fn build(self, el: &R::Element, key: &str) -> Self::State {
fn build(
self,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
let el = el.clone();
let v = self.map(|v| v.build(&el, key));
(el, v)
@@ -520,7 +541,7 @@ where
match (self, prev.as_mut()) {
(None, None) => {}
(None, Some(_)) => {
R::remove_attribute(el, key);
Rndr::remove_attribute(el, key);
*prev = None;
}
(Some(value), None) => {
@@ -561,12 +582,12 @@ pub(crate) fn escape_attr(value: &str) -> Cow<'_, str> {
macro_rules! render_primitive {
($($child_type:ty),* $(,)?) => {
$(
impl<R> AttributeValue<R> for $child_type
impl AttributeValue for $child_type
where
R: Renderer,
{
type AsyncOutput = $child_type;
type State = (R::Element, $child_type);
type State = (crate::renderer::types::Element, $child_type);
type Cloneable = Self;
type CloneableOwned = Self;
@@ -575,7 +596,7 @@ macro_rules! render_primitive {
}
fn to_html(self, key: &str, buf: &mut String) {
<String as AttributeValue<R>>::to_html(self.to_string(), key, buf);
<String as AttributeValue>::to_html(self.to_string(), key, buf);
}
fn to_template(_key: &str, _buf: &mut String) {}
@@ -583,25 +604,25 @@ macro_rules! render_primitive {
fn hydrate<const FROM_SERVER: bool>(
self,
key: &str,
el: &R::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
// if we're actually hydrating from SSRed HTML, we don't need to set the attribute
// if we're hydrating from a CSR-cloned <template>, we do need to set non-StaticAttr attributes
if !FROM_SERVER {
R::set_attribute(el, key, &self.to_string());
Rndr::set_attribute(el, key, &self.to_string());
}
(el.clone(), self)
}
fn build(self, el: &R::Element, key: &str) -> Self::State {
R::set_attribute(el, key, &self.to_string());
fn build(self, el: &crate::renderer::types::Element, key: &str) -> Self::State {
Rndr::set_attribute(el, key, &self.to_string());
(el.to_owned(), self)
}
fn rebuild(self, key: &str, state: &mut Self::State) {
let (el, prev_value) = state;
if self != *prev_value {
R::set_attribute(el, key, &self.to_string());
Rndr::set_attribute(el, key, &self.to_string());
}
*prev_value = self;
}

View File

@@ -1,53 +1,46 @@
use super::attribute::{Attribute, NextAttribute};
use crate::{
renderer::DomRenderer,
renderer::Rndr,
view::{Position, ToTemplate},
};
use std::{future::Future, marker::PhantomData, sync::Arc};
use std::{future::Future, sync::Arc};
/// Adds a CSS class.
#[inline(always)]
pub fn class<C, R>(class: C) -> Class<C, R>
pub fn class<C>(class: C) -> Class<C>
where
C: IntoClass<R>,
R: DomRenderer,
C: IntoClass,
{
Class {
class,
rndr: PhantomData,
}
Class { class }
}
/// A CSS class.
#[derive(Debug)]
pub struct Class<C, R> {
pub struct Class<C> {
class: C,
rndr: PhantomData<R>,
}
impl<C, R> Clone for Class<C, R>
impl<C> Clone for Class<C>
where
C: Clone,
{
fn clone(&self) -> Self {
Self {
class: self.class.clone(),
rndr: PhantomData,
}
}
}
impl<C, R> Attribute<R> for Class<C, R>
impl<C> Attribute for Class<C>
where
C: IntoClass<R>,
R: DomRenderer,
C: IntoClass,
{
const MIN_LENGTH: usize = C::MIN_LENGTH;
type AsyncOutput = Class<C::AsyncOutput, R>;
type AsyncOutput = Class<C::AsyncOutput>;
type State = C::State;
type Cloneable = Class<C::Cloneable, R>;
type CloneableOwned = Class<C::CloneableOwned, R>;
type Cloneable = Class<C::Cloneable>;
type CloneableOwned = Class<C::CloneableOwned>;
fn html_len(&self) -> usize {
self.class.html_len() + 1
@@ -64,11 +57,14 @@ where
self.class.to_html(class);
}
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
self.class.hydrate::<FROM_SERVER>(el)
}
fn build(self, el: &R::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
self.class.build(el)
}
@@ -79,14 +75,12 @@ where
fn into_cloneable(self) -> Self::Cloneable {
Class {
class: self.class.into_cloneable(),
rndr: self.rndr,
}
}
fn into_cloneable_owned(self) -> Self::CloneableOwned {
Class {
class: self.class.into_cloneable_owned(),
rndr: self.rndr,
}
}
@@ -97,19 +91,17 @@ where
async fn resolve(self) -> Self::AsyncOutput {
Class {
class: self.class.resolve().await,
rndr: self.rndr,
}
}
}
impl<C, R> NextAttribute<R> for Class<C, R>
impl<C> NextAttribute for Class<C>
where
C: IntoClass<R>,
R: DomRenderer,
C: IntoClass,
{
type Output<NewAttr: Attribute<R>> = (Self, NewAttr);
type Output<NewAttr: Attribute> = (Self, NewAttr);
fn add_any_attr<NewAttr: Attribute<R>>(
fn add_any_attr<NewAttr: Attribute>(
self,
new_attr: NewAttr,
) -> Self::Output<NewAttr> {
@@ -117,10 +109,9 @@ where
}
}
impl<C, R> ToTemplate for Class<C, R>
impl<C> ToTemplate for Class<C>
where
C: IntoClass<R>,
R: DomRenderer,
C: IntoClass,
{
const CLASS: &'static str = C::TEMPLATE;
@@ -136,20 +127,20 @@ where
}
/// A possible value for a CSS class.
pub trait IntoClass<R: DomRenderer>: Send {
pub trait IntoClass: Send {
/// The HTML that should be included in a `<template>`.
const TEMPLATE: &'static str = "";
/// The minimum length of the HTML.
const MIN_LENGTH: usize = Self::TEMPLATE.len();
/// The type after all async data have resolved.
type AsyncOutput: IntoClass<R>;
type AsyncOutput: IntoClass;
/// The view state retained between building and rebuilding.
type State;
/// An equivalent value that can be cloned.
type Cloneable: IntoClass<R> + Clone;
type Cloneable: IntoClass + Clone;
/// An equivalent value that can be cloned and is `'static`.
type CloneableOwned: IntoClass<R> + Clone + 'static;
type CloneableOwned: IntoClass + Clone + 'static;
/// The estimated length of the HTML.
fn html_len(&self) -> usize;
@@ -163,10 +154,13 @@ pub trait IntoClass<R: DomRenderer>: Send {
/// Adds interactivity as necessary, given DOM nodes that were created from HTML that has
/// either been rendered on the server, or cloned for a `<template>`.
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State;
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State;
/// Adds this class to the element during client-side rendering.
fn build(self, el: &R::Element) -> Self::State;
fn build(self, el: &crate::renderer::types::Element) -> Self::State;
/// Updates the value.
fn rebuild(self, state: &mut Self::State);
@@ -186,12 +180,9 @@ pub trait IntoClass<R: DomRenderer>: Send {
fn resolve(self) -> impl Future<Output = Self::AsyncOutput> + Send;
}
impl<'a, R> IntoClass<R> for &'a str
where
R: DomRenderer,
{
impl<'a> IntoClass for &'a str {
type AsyncOutput = Self;
type State = (R::Element, Self);
type State = (crate::renderer::types::Element, Self);
type Cloneable = Self;
type CloneableOwned = Arc<str>;
@@ -203,22 +194,25 @@ where
class.push_str(self);
}
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
if !FROM_SERVER {
R::set_attribute(el, "class", self);
Rndr::set_attribute(el, "class", self);
}
(el.clone(), self)
}
fn build(self, el: &R::Element) -> Self::State {
R::set_attribute(el, "class", self);
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
Rndr::set_attribute(el, "class", self);
(el.clone(), self)
}
fn rebuild(self, state: &mut Self::State) {
let (el, prev) = state;
if self != *prev {
R::set_attribute(el, "class", self);
Rndr::set_attribute(el, "class", self);
}
*prev = self;
}
@@ -238,12 +232,9 @@ where
}
}
impl<R> IntoClass<R> for String
where
R: DomRenderer,
{
impl IntoClass for String {
type AsyncOutput = Self;
type State = (R::Element, Self);
type State = (crate::renderer::types::Element, Self);
type Cloneable = Arc<str>;
type CloneableOwned = Arc<str>;
@@ -252,25 +243,28 @@ where
}
fn to_html(self, class: &mut String) {
IntoClass::<R>::to_html(self.as_str(), class);
IntoClass::to_html(self.as_str(), class);
}
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
if !FROM_SERVER {
R::set_attribute(el, "class", &self);
Rndr::set_attribute(el, "class", &self);
}
(el.clone(), self)
}
fn build(self, el: &R::Element) -> Self::State {
R::set_attribute(el, "class", &self);
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
Rndr::set_attribute(el, "class", &self);
(el.clone(), self)
}
fn rebuild(self, state: &mut Self::State) {
let (el, prev) = state;
if self != *prev {
R::set_attribute(el, "class", &self);
Rndr::set_attribute(el, "class", &self);
}
*prev = self;
}
@@ -290,12 +284,9 @@ where
}
}
impl<R> IntoClass<R> for Arc<str>
where
R: DomRenderer,
{
impl IntoClass for Arc<str> {
type AsyncOutput = Self;
type State = (R::Element, Self);
type State = (crate::renderer::types::Element, Self);
type Cloneable = Self;
type CloneableOwned = Self;
@@ -304,25 +295,28 @@ where
}
fn to_html(self, class: &mut String) {
IntoClass::<R>::to_html(self.as_ref(), class);
IntoClass::to_html(self.as_ref(), class);
}
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
if !FROM_SERVER {
R::set_attribute(el, "class", &self);
Rndr::set_attribute(el, "class", &self);
}
(el.clone(), self)
}
fn build(self, el: &R::Element) -> Self::State {
R::set_attribute(el, "class", &self);
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
Rndr::set_attribute(el, "class", &self);
(el.clone(), self)
}
fn rebuild(self, state: &mut Self::State) {
let (el, prev) = state;
if !Arc::ptr_eq(&self, prev) {
R::set_attribute(el, "class", &self);
Rndr::set_attribute(el, "class", &self);
}
*prev = self;
}
@@ -342,12 +336,9 @@ where
}
}
impl<R> IntoClass<R> for (&'static str, bool)
where
R: DomRenderer,
{
impl IntoClass for (&'static str, bool) {
type AsyncOutput = Self;
type State = (R::ClassList, bool);
type State = (crate::renderer::types::ClassList, bool);
type Cloneable = Self;
type CloneableOwned = Self;
@@ -362,20 +353,23 @@ where
}
}
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
let (name, include) = self;
let class_list = R::class_list(el);
let class_list = Rndr::class_list(el);
if !FROM_SERVER && include {
R::add_class(&class_list, name);
Rndr::add_class(&class_list, name);
}
(class_list, self.1)
}
fn build(self, el: &R::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
let (name, include) = self;
let class_list = R::class_list(el);
let class_list = Rndr::class_list(el);
if include {
R::add_class(&class_list, name);
Rndr::add_class(&class_list, name);
}
(class_list, self.1)
}
@@ -385,9 +379,9 @@ where
let (class_list, prev_include) = state;
if include != *prev_include {
if include {
R::add_class(class_list, name);
Rndr::add_class(class_list, name);
} else {
R::remove_class(class_list, name);
Rndr::remove_class(class_list, name);
}
}
*prev_include = include;
@@ -409,11 +403,7 @@ where
}
#[cfg(feature = "nightly")]
impl<R, const V: &'static str> IntoClass<R>
for crate::view::static_types::Static<V>
where
R: DomRenderer,
{
impl<const V: &'static str> IntoClass for crate::view::static_types::Static<V> {
const TEMPLATE: &'static str = V;
type AsyncOutput = Self;
@@ -433,11 +423,14 @@ where
class.push_str(V);
}
fn hydrate<const FROM_SERVER: bool>(self, _el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(
self,
_el: &crate::renderer::types::Element,
) -> Self::State {
}
fn build(self, el: &R::Element) -> Self::State {
R::set_attribute(el, "class", V);
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
Rndr::set_attribute(el, "class", V);
}
fn rebuild(self, _state: &mut Self::State) {}

View File

@@ -1,7 +1,6 @@
use super::attribute::{Attribute, NextAttribute};
use crate::{
prelude::AddAnyAttr,
renderer::Renderer,
view::{Position, ToTemplate},
};
use send_wrapper::SendWrapper;
@@ -9,10 +8,9 @@ use std::{marker::PhantomData, sync::Arc};
/// Adds a directive to the element, which runs some custom logic in the browser when the element
/// is created or hydrated.
pub trait DirectiveAttribute<T, P, D, Rndr>
pub trait DirectiveAttribute<T, P, D>
where
D: IntoDirective<T, P, Rndr>,
Rndr: Renderer,
D: IntoDirective<T, P>,
{
/// The type of the element with the directive added.
type Output;
@@ -22,15 +20,14 @@ where
fn directive(self, handler: D, param: P) -> Self::Output;
}
impl<V, T, P, D, Rndr> DirectiveAttribute<T, P, D, Rndr> for V
impl<V, T, P, D> DirectiveAttribute<T, P, D> for V
where
V: AddAnyAttr<Rndr>,
D: IntoDirective<T, P, Rndr>,
V: AddAnyAttr,
D: IntoDirective<T, P>,
P: Clone + 'static,
T: 'static,
Rndr: Renderer,
{
type Output = <Self as AddAnyAttr<Rndr>>::Output<Directive<T, D, P, Rndr>>;
type Output = <Self as AddAnyAttr>::Output<Directive<T, D, P>>;
fn directive(self, handler: D, param: P) -> Self::Output {
self.add_any_attr(directive(handler, param))
@@ -40,26 +37,22 @@ where
/// Adds a directive to the element, which runs some custom logic in the browser when the element
/// is created or hydrated.
#[inline(always)]
pub fn directive<T, P, D, R>(handler: D, param: P) -> Directive<T, D, P, R>
pub fn directive<T, P, D>(handler: D, param: P) -> Directive<T, D, P>
where
D: IntoDirective<T, P, R>,
R: Renderer,
D: IntoDirective<T, P>,
{
Directive(Some(SendWrapper::new(DirectiveInner {
handler,
param,
t: PhantomData,
rndr: PhantomData,
})))
}
/// Custom logic that runs in the browser when the element is created or hydrated.
#[derive(Debug)]
pub struct Directive<T, D, P, R>(
Option<SendWrapper<DirectiveInner<T, D, P, R>>>,
);
pub struct Directive<T, D, P>(Option<SendWrapper<DirectiveInner<T, D, P>>>);
impl<T, D, P, R> Clone for Directive<T, D, P, R>
impl<T, D, P> Clone for Directive<T, D, P>
where
P: Clone + 'static,
D: Clone,
@@ -70,14 +63,13 @@ where
}
#[derive(Debug)]
struct DirectiveInner<T, D, P, R> {
struct DirectiveInner<T, D, P> {
handler: D,
param: P,
t: PhantomData<T>,
rndr: PhantomData<R>,
}
impl<T, D, P, R> Clone for DirectiveInner<T, D, P, R>
impl<T, D, P> Clone for DirectiveInner<T, D, P>
where
P: Clone + 'static,
D: Clone,
@@ -87,24 +79,22 @@ where
handler: self.handler.clone(),
param: self.param.clone(),
t: PhantomData,
rndr: PhantomData,
}
}
}
impl<T, P, D, R> Attribute<R> for Directive<T, D, P, R>
impl<T, P, D> Attribute for Directive<T, D, P>
where
D: IntoDirective<T, P, R>,
D: IntoDirective<T, P>,
P: Clone + 'static, // TODO this is just here to make them cloneable
T: 'static,
R: Renderer,
{
const MIN_LENGTH: usize = 0;
type AsyncOutput = Self;
type State = R::Element;
type Cloneable = Directive<T, D::Cloneable, P, R>;
type CloneableOwned = Directive<T, D::Cloneable, P, R>;
type State = crate::renderer::types::Element;
type Cloneable = Directive<T, D::Cloneable, P>;
type CloneableOwned = Directive<T, D::Cloneable, P>;
fn html_len(&self) -> usize {
0
@@ -119,13 +109,16 @@ where
) {
}
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
let inner = self.0.expect("directive removed early").take();
inner.handler.run(el.clone(), inner.param);
el.clone()
}
fn build(self, el: &R::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
let inner = self.0.expect("directive removed early").take();
inner.handler.run(el.clone(), inner.param);
el.clone()
@@ -142,17 +135,11 @@ where
fn into_cloneable_owned(self) -> Self::CloneableOwned {
let inner = self.0.map(|inner| {
let DirectiveInner {
handler,
param,
t,
rndr,
} = inner.take();
let DirectiveInner { handler, param, t } = inner.take();
SendWrapper::new(DirectiveInner {
handler: handler.into_cloneable(),
param,
t,
rndr,
})
});
Directive(inner)
@@ -171,16 +158,15 @@ where
}
}
impl<T, D, P, R> NextAttribute<R> for Directive<T, D, P, R>
impl<T, D, P> NextAttribute for Directive<T, D, P>
where
D: IntoDirective<T, P, R>,
D: IntoDirective<T, P>,
P: Clone + 'static,
T: 'static,
R: Renderer,
{
type Output<NewAttr: Attribute<R>> = (Self, NewAttr);
type Output<NewAttr: Attribute> = (Self, NewAttr);
fn add_any_attr<NewAttr: Attribute<R>>(
fn add_any_attr<NewAttr: Attribute>(
self,
new_attr: NewAttr,
) -> Self::Output<NewAttr> {
@@ -188,7 +174,7 @@ where
}
}
impl<T, D, P, R> ToTemplate for Directive<T, D, P, R> {
impl<T, D, P> ToTemplate for Directive<T, D, P> {
const CLASS: &'static str = "";
fn to_template(
@@ -211,12 +197,12 @@ impl<T, D, P, R> ToTemplate for Directive<T, D, P, R> {
/// # use leptos::{*, html::AnyElement};
///
/// // This doesn't take an attribute value
/// fn my_directive(el: R::Element) {
/// fn my_directive(el: crate::renderer::types::Element) {
/// // do sth
/// }
///
/// // This requires an attribute value
/// fn another_directive(el: R::Element, params: i32) {
/// fn another_directive(el: crate::renderer::types::Element, params: i32) {
/// // do sth
/// }
///
@@ -247,25 +233,24 @@ impl<T, D, P, R> ToTemplate for Directive<T, D, P, R> {
/// A directive can be a function with one or two parameters.
/// The first is the element the directive is added to and the optional
/// second is the parameter that is provided in the attribute.
pub trait IntoDirective<T: ?Sized, P, R: Renderer> {
pub trait IntoDirective<T: ?Sized, P> {
/// An equivalent to this directive that is cloneable and owned.
type Cloneable: IntoDirective<T, P, R> + Clone + 'static;
type Cloneable: IntoDirective<T, P> + Clone + 'static;
/// Calls the handler function
fn run(&self, el: R::Element, param: P);
fn run(&self, el: crate::renderer::types::Element, param: P);
/// Converts this into a cloneable type.
fn into_cloneable(self) -> Self::Cloneable;
}
impl<F, R> IntoDirective<(R::Element,), (), R> for F
impl<F> IntoDirective<(crate::renderer::types::Element,), ()> for F
where
F: Fn(R::Element) + 'static,
R: Renderer,
F: Fn(crate::renderer::types::Element) + 'static,
{
type Cloneable = Arc<dyn Fn(R::Element)>;
type Cloneable = Arc<dyn Fn(crate::renderer::types::Element)>;
fn run(&self, el: R::Element, _: ()) {
fn run(&self, el: crate::renderer::types::Element, _: ()) {
self(el)
}
@@ -274,13 +259,12 @@ where
}
}
impl<R> IntoDirective<(R::Element,), (), R> for Arc<dyn Fn(R::Element)>
where
R: Renderer,
impl IntoDirective<(crate::renderer::types::Element,), ()>
for Arc<dyn Fn(crate::renderer::types::Element)>
{
type Cloneable = Arc<dyn Fn(R::Element)>;
type Cloneable = Arc<dyn Fn(crate::renderer::types::Element)>;
fn run(&self, el: R::Element, _: ()) {
fn run(&self, el: crate::renderer::types::Element, _: ()) {
self(el)
}
@@ -289,15 +273,14 @@ where
}
}
impl<F, P, R> IntoDirective<(R::Element, P), P, R> for F
impl<F, P> IntoDirective<(crate::renderer::types::Element, P), P> for F
where
F: Fn(R::Element, P) + 'static,
F: Fn(crate::renderer::types::Element, P) + 'static,
P: 'static,
R: Renderer,
{
type Cloneable = Arc<dyn Fn(R::Element, P)>;
type Cloneable = Arc<dyn Fn(crate::renderer::types::Element, P)>;
fn run(&self, el: R::Element, param: P) {
fn run(&self, el: crate::renderer::types::Element, param: P) {
self(el, param);
}
@@ -306,14 +289,14 @@ where
}
}
impl<P, R> IntoDirective<(R::Element, P), P, R> for Arc<dyn Fn(R::Element, P)>
impl<P> IntoDirective<(crate::renderer::types::Element, P), P>
for Arc<dyn Fn(crate::renderer::types::Element, P)>
where
R: Renderer,
P: 'static,
{
type Cloneable = Arc<dyn Fn(R::Element, P)>;
type Cloneable = Arc<dyn Fn(crate::renderer::types::Element, P)>;
fn run(&self, el: R::Element, param: P) {
fn run(&self, el: crate::renderer::types::Element, param: P) {
self(el, param)
}

View File

@@ -1,20 +1,16 @@
use super::ElementWithChildren;
use crate::{
html::element::{CreateElement, ElementType, HtmlElement},
renderer::{dom::Dom, Renderer},
};
use std::{fmt::Debug, marker::PhantomData};
use crate::html::element::{ElementType, HtmlElement};
use std::fmt::Debug;
/// Creates a custom element.
#[track_caller]
pub fn custom<E, Rndr>(tag: E) -> HtmlElement<Custom<E>, (), (), Rndr>
pub fn custom<E>(tag: E) -> HtmlElement<Custom<E>, (), ()>
where
E: AsRef<str>,
Rndr: Renderer,
{
HtmlElement {
tag: Custom(tag),
rndr: PhantomData,
attributes: (),
children: (),
}
@@ -40,16 +36,3 @@ where
}
impl<E> ElementWithChildren for Custom<E> {}
impl<E> CreateElement<Dom> for Custom<E>
where
E: AsRef<str>,
{
fn create_element(&self) -> <Dom as Renderer>::Element {
use wasm_bindgen::intern;
crate::dom::document()
.create_element(intern(self.0.as_ref()))
.unwrap()
}
}

View File

@@ -5,7 +5,7 @@ use crate::{
event::{on, EventDescriptor},
style::IntoStyle,
},
renderer::{dom::Dom, DomRenderer, RemoveEventHandler},
renderer::RemoveEventHandler,
};
use wasm_bindgen::JsValue;
use web_sys::Element;
@@ -31,17 +31,17 @@ pub trait ElementExt {
/// Adds an attribute to the element, at runtime.
fn attr<At>(&self, attribute: At) -> At::State
where
At: Attribute<Dom>;
At: Attribute;
/// Adds a class to the element, at runtime.
fn class<C>(&self, class: C) -> C::State
where
C: IntoClass<Dom>;
C: IntoClass;
/// Adds a style to the element, at runtime.
fn style<S>(&self, style: S) -> S::State
where
S: IntoStyle<Dom>;
S: IntoStyle;
/// Adds an event listener to the element, at runtime.
fn on<E>(
@@ -58,18 +58,17 @@ pub trait ElementExt {
impl<T> ElementExt for T
where
T: AsRef<Element>,
Dom: DomRenderer,
{
fn attr<At>(&self, attribute: At) -> At::State
where
At: Attribute<Dom>,
At: Attribute,
{
attribute.build(self.as_ref())
}
fn class<C>(&self, class: C) -> C::State
where
C: IntoClass<Dom>,
C: IntoClass,
{
class.build(self.as_ref())
}
@@ -84,12 +83,12 @@ where
E::EventType: 'static,
E::EventType: From<JsValue>,
{
on::<E, _, Dom>(ev, cb).attach(self.as_ref())
on::<E, _>(ev, cb).attach(self.as_ref())
}
fn style<S>(&self, style: S) -> S::State
where
S: IntoStyle<Dom>,
S: IntoStyle,
{
style.build(self.as_ref())
}

View File

@@ -1,18 +1,12 @@
#[cfg(feature = "sledgehammer")]
use crate::renderer::sledgehammer::Sledgehammer;
use crate::{
html::{
attribute::{Attr, Attribute, AttributeValue},
element::{
CreateElement, ElementType, ElementWithChildren, HtmlElement,
},
element::{ElementType, ElementWithChildren, HtmlElement},
},
renderer::{dom::Dom, Renderer},
view::Render,
};
use next_tuple::NextTuple;
use once_cell::unsync::Lazy;
use std::{fmt::Debug, marker::PhantomData};
use std::fmt::Debug;
macro_rules! html_element_inner {
(
@@ -26,15 +20,15 @@ macro_rules! html_element_inner {
paste::paste! {
#[$meta]
#[track_caller]
pub fn $tag<Rndr>() -> HtmlElement<$struct_name, (), (), Rndr>
pub fn $tag() -> HtmlElement<$struct_name, (), ()>
where
Rndr: Renderer
{
HtmlElement {
tag: $struct_name,
attributes: (),
children: (),
rndr: PhantomData,
}
}
@@ -43,28 +37,28 @@ macro_rules! html_element_inner {
pub struct $struct_name;
// Typed attribute methods
impl<At, Ch, Rndr> HtmlElement<$struct_name, At, Ch, Rndr>
impl<At, Ch> HtmlElement<$struct_name, At, Ch>
where
At: Attribute<Rndr>,
Ch: Render<Rndr>,
Rndr: Renderer,
At: Attribute,
Ch: Render,
{
$(
#[doc = concat!("The [`", stringify!($attr), "`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/", stringify!($tag), "#", stringify!($attr) ,") attribute on `<", stringify!($tag), ">`.")]
pub fn $attr<V>(self, value: V) -> HtmlElement <
$struct_name,
<At as NextTuple>::Output<Attr<$crate::html::attribute::[<$attr:camel>], V, Rndr>>,
Ch, Rndr
<At as NextTuple>::Output<Attr<$crate::html::attribute::[<$attr:camel>], V>>,
Ch
>
where
V: AttributeValue<Rndr>,
V: AttributeValue,
At: NextTuple,
<At as NextTuple>::Output<Attr<$crate::html::attribute::[<$attr:camel>], V, Rndr>>: Attribute<Rndr>,
<At as NextTuple>::Output<Attr<$crate::html::attribute::[<$attr:camel>], V>>: Attribute,
{
let HtmlElement { tag, rndr, children, attributes } = self;
let HtmlElement { tag, children, attributes } = self;
HtmlElement {
tag,
rndr,
children,
attributes: attributes.next_tuple($crate::html::attribute::$attr(value)),
}
@@ -86,28 +80,6 @@ macro_rules! html_element_inner {
}
impl ElementWithChildren for $struct_name {}
impl CreateElement<Dom> for $struct_name {
#[track_caller]
#[cfg_attr(feature = "tracing", tracing::instrument(level = "trace", fields(callsite = std::panic::Location::caller().to_string())))]
fn create_element(&self) -> <Dom as Renderer>::Element {
use wasm_bindgen::JsCast;
thread_local! {
static ELEMENT: Lazy<<Dom as Renderer>::Element> = Lazy::new(|| {
crate::dom::document().create_element(stringify!($tag)).unwrap()
});
}
ELEMENT.with(|e| e.clone_node()).unwrap().unchecked_into()
}
}
#[cfg(feature = "sledgehammer")]
impl CreateElement<Sledgehammer> for $struct_name {
fn create_element(&self) -> <Sledgehammer as Renderer>::Element {
Sledgehammer::element(stringify!($tag))
}
}
}
};
}
@@ -145,14 +117,14 @@ macro_rules! html_self_closing_elements {
paste::paste! {
$(
#[$meta]
pub fn $tag<Rndr>() -> HtmlElement<[<$tag:camel>], (), (), Rndr>
pub fn $tag() -> HtmlElement<[<$tag:camel>], (), ()>
where
Rndr: Renderer
{
HtmlElement {
attributes: (),
children: (),
rndr: PhantomData,
tag: [<$tag:camel>],
}
}
@@ -162,30 +134,29 @@ macro_rules! html_self_closing_elements {
pub struct [<$tag:camel>];
// Typed attribute methods
impl<At, Rndr> HtmlElement<[<$tag:camel>], At, (), Rndr>
impl<At> HtmlElement<[<$tag:camel>], At, ()>
where
At: Attribute<Rndr>,
Rndr: Renderer,
At: Attribute,
{
$(
#[doc = concat!("The [`", stringify!($attr), "`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/", stringify!($tag), "#", stringify!($attr) ,") attribute on `<", stringify!($tag), ">`.")]
pub fn $attr<V>(self, value: V) -> HtmlElement<
[<$tag:camel>],
<At as NextTuple>::Output<Attr<$crate::html::attribute::[<$attr:camel>], V, Rndr>>,
<At as NextTuple>::Output<Attr<$crate::html::attribute::[<$attr:camel>], V>>,
(),
Rndr
>
where
V: AttributeValue<Rndr>,
V: AttributeValue,
At: NextTuple,
<At as NextTuple>::Output<Attr<$crate::html::attribute::[<$attr:camel>], V, Rndr>>: Attribute<Rndr>,
<At as NextTuple>::Output<Attr<$crate::html::attribute::[<$attr:camel>], V>>: Attribute,
{
let HtmlElement { tag, rndr, children, attributes,
let HtmlElement { tag, children, attributes,
} = self;
HtmlElement {
tag,
rndr,
children,
attributes: attributes.next_tuple($crate::html::attribute::$attr(value)),
}
@@ -205,19 +176,6 @@ macro_rules! html_self_closing_elements {
Self::TAG
}
}
impl CreateElement<Dom> for [<$tag:camel>] {
fn create_element(&self) -> <Dom as Renderer>::Element {
use wasm_bindgen::JsCast;
thread_local! {
static ELEMENT: Lazy<<Dom as Renderer>::Element> = Lazy::new(|| {
crate::dom::document().create_element(stringify!($tag)).unwrap()
});
}
ELEMENT.with(|e| e.clone_node()).unwrap().unchecked_into()
}
}
)*
}
}

View File

@@ -1,10 +1,10 @@
use super::{ElementWithChildren, HtmlElement};
use crate::{
html::attribute::{Attribute, NextAttribute},
renderer::{DomRenderer, Renderer},
renderer::Rndr,
view::add_attr::AddAnyAttr,
};
use std::{future::Future, marker::PhantomData, sync::Arc};
use std::{future::Future, sync::Arc};
/// Returns an [`Attribute`] that sets the inner HTML of an element.
///
@@ -15,47 +15,40 @@ use std::{future::Future, marker::PhantomData, sync::Arc};
/// sanitize the input to avoid a cross-site scripting (XSS)
/// vulnerability.
#[inline(always)]
pub fn inner_html<T, R>(value: T) -> InnerHtml<T, R>
pub fn inner_html<T>(value: T) -> InnerHtml<T>
where
T: InnerHtmlValue<R>,
R: DomRenderer,
T: InnerHtmlValue,
{
InnerHtml {
value,
rndr: PhantomData,
}
InnerHtml { value }
}
/// Sets the inner HTML of an element.
#[derive(Debug)]
pub struct InnerHtml<T, R> {
pub struct InnerHtml<T> {
value: T,
rndr: PhantomData<R>,
}
impl<T, R> Clone for InnerHtml<T, R>
impl<T> Clone for InnerHtml<T>
where
T: Clone,
{
fn clone(&self) -> Self {
Self {
value: self.value.clone(),
rndr: PhantomData,
}
}
}
impl<T, R> Attribute<R> for InnerHtml<T, R>
impl<T> Attribute for InnerHtml<T>
where
T: InnerHtmlValue<R>,
R: DomRenderer,
T: InnerHtmlValue,
{
const MIN_LENGTH: usize = 0;
type AsyncOutput = InnerHtml<T::AsyncOutput, R>;
type AsyncOutput = InnerHtml<T::AsyncOutput>;
type State = T::State;
type Cloneable = InnerHtml<T::Cloneable, R>;
type CloneableOwned = InnerHtml<T::CloneableOwned, R>;
type Cloneable = InnerHtml<T::Cloneable>;
type CloneableOwned = InnerHtml<T::CloneableOwned>;
fn html_len(&self) -> usize {
self.value.html_len()
@@ -73,12 +66,12 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
el: &<R as Renderer>::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
self.value.hydrate::<FROM_SERVER>(el)
}
fn build(self, el: &<R as Renderer>::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
self.value.build(el)
}
@@ -89,14 +82,12 @@ where
fn into_cloneable(self) -> Self::Cloneable {
InnerHtml {
value: self.value.into_cloneable(),
rndr: self.rndr,
}
}
fn into_cloneable_owned(self) -> Self::CloneableOwned {
InnerHtml {
value: self.value.into_cloneable_owned(),
rndr: self.rndr,
}
}
@@ -107,19 +98,17 @@ where
async fn resolve(self) -> Self::AsyncOutput {
InnerHtml {
value: self.value.resolve().await,
rndr: self.rndr,
}
}
}
impl<T, R> NextAttribute<R> for InnerHtml<T, R>
impl<T> NextAttribute for InnerHtml<T>
where
T: InnerHtmlValue<R>,
R: DomRenderer,
T: InnerHtmlValue,
{
type Output<NewAttr: Attribute<R>> = (Self, NewAttr);
type Output<NewAttr: Attribute> = (Self, NewAttr);
fn add_any_attr<NewAttr: Attribute<R>>(
fn add_any_attr<NewAttr: Attribute>(
self,
new_attr: NewAttr,
) -> Self::Output<NewAttr> {
@@ -128,11 +117,11 @@ where
}
/// Sets the inner HTML of an element.
pub trait InnerHtmlAttribute<T, Rndr>
pub trait InnerHtmlAttribute<T>
where
T: InnerHtmlValue<Rndr>,
Rndr: DomRenderer,
Self: Sized + AddAnyAttr<Rndr>,
T: InnerHtmlValue,
Self: Sized + AddAnyAttr,
{
/// Sets the inner HTML of this element.
///
@@ -145,38 +134,36 @@ where
fn inner_html(
self,
value: T,
) -> <Self as AddAnyAttr<Rndr>>::Output<InnerHtml<T, Rndr>> {
) -> <Self as AddAnyAttr>::Output<InnerHtml<T>> {
self.add_any_attr(inner_html(value))
}
}
impl<T, E, At, Rndr> InnerHtmlAttribute<T, Rndr>
for HtmlElement<E, At, (), Rndr>
impl<T, E, At> InnerHtmlAttribute<T> for HtmlElement<E, At, ()>
where
Self: AddAnyAttr<Rndr>,
Self: AddAnyAttr,
E: ElementWithChildren,
At: Attribute<Rndr>,
T: InnerHtmlValue<Rndr>,
Rndr: DomRenderer,
At: Attribute,
T: InnerHtmlValue,
{
fn inner_html(
self,
value: T,
) -> <Self as AddAnyAttr<Rndr>>::Output<InnerHtml<T, Rndr>> {
) -> <Self as AddAnyAttr>::Output<InnerHtml<T>> {
self.add_any_attr(inner_html(value))
}
}
/// A possible value for [`InnerHtml`].
pub trait InnerHtmlValue<R: DomRenderer>: Send {
pub trait InnerHtmlValue: Send {
/// The type after all async data have resolved.
type AsyncOutput: InnerHtmlValue<R>;
type AsyncOutput: InnerHtmlValue;
/// The view state retained between building and rebuilding.
type State;
/// An equivalent value that can be cloned.
type Cloneable: InnerHtmlValue<R> + Clone;
type Cloneable: InnerHtmlValue + Clone;
/// An equivalent value that can be cloned and is `'static`.
type CloneableOwned: InnerHtmlValue<R> + Clone + 'static;
type CloneableOwned: InnerHtmlValue + Clone + 'static;
/// The estimated length of the HTML.
fn html_len(&self) -> usize;
@@ -189,10 +176,13 @@ pub trait InnerHtmlValue<R: DomRenderer>: Send {
/// Adds interactivity as necessary, given DOM nodes that were created from HTML that has
/// either been rendered on the server, or cloned for a `<template>`.
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State;
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State;
/// Adds this class to the element during client-side rendering.
fn build(self, el: &R::Element) -> Self::State;
fn build(self, el: &crate::renderer::types::Element) -> Self::State;
/// Updates the value.
fn rebuild(self, state: &mut Self::State);
@@ -212,12 +202,9 @@ pub trait InnerHtmlValue<R: DomRenderer>: Send {
fn resolve(self) -> impl Future<Output = Self::AsyncOutput> + Send;
}
impl<R> InnerHtmlValue<R> for String
where
R: DomRenderer,
{
impl InnerHtmlValue for String {
type AsyncOutput = Self;
type State = (R::Element, Self);
type State = (crate::renderer::types::Element, Self);
type Cloneable = Arc<str>;
type CloneableOwned = Arc<str>;
@@ -233,22 +220,22 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
el: &<R as Renderer>::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
if !FROM_SERVER {
R::set_inner_html(el, &self);
Rndr::set_inner_html(el, &self);
}
(el.clone(), self)
}
fn build(self, el: &<R as Renderer>::Element) -> Self::State {
R::set_inner_html(el, &self);
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
Rndr::set_inner_html(el, &self);
(el.clone(), self)
}
fn rebuild(self, state: &mut Self::State) {
if self != state.1 {
R::set_inner_html(&state.0, &self);
Rndr::set_inner_html(&state.0, &self);
state.1 = self;
}
}
@@ -268,12 +255,9 @@ where
}
}
impl<R> InnerHtmlValue<R> for Arc<str>
where
R: DomRenderer,
{
impl InnerHtmlValue for Arc<str> {
type AsyncOutput = Self;
type State = (R::Element, Self);
type State = (crate::renderer::types::Element, Self);
type Cloneable = Self;
type CloneableOwned = Self;
@@ -289,22 +273,22 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
el: &<R as Renderer>::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
if !FROM_SERVER {
R::set_inner_html(el, &self);
Rndr::set_inner_html(el, &self);
}
(el.clone(), self)
}
fn build(self, el: &<R as Renderer>::Element) -> Self::State {
R::set_inner_html(el, &self);
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
Rndr::set_inner_html(el, &self);
(el.clone(), self)
}
fn rebuild(self, state: &mut Self::State) {
if !Arc::ptr_eq(&self, &state.1) {
R::set_inner_html(&state.0, &self);
Rndr::set_inner_html(&state.0, &self);
state.1 = self;
}
}
@@ -324,12 +308,9 @@ where
}
}
impl<'a, R> InnerHtmlValue<R> for &'a str
where
R: DomRenderer,
{
impl<'a> InnerHtmlValue for &'a str {
type AsyncOutput = Self;
type State = (R::Element, Self);
type State = (crate::renderer::types::Element, Self);
type Cloneable = Self;
type CloneableOwned = Arc<str>;
@@ -345,22 +326,22 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
el: &<R as Renderer>::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
if !FROM_SERVER {
R::set_inner_html(el, self);
Rndr::set_inner_html(el, self);
}
(el.clone(), self)
}
fn build(self, el: &<R as Renderer>::Element) -> Self::State {
R::set_inner_html(el, self);
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
Rndr::set_inner_html(el, self);
(el.clone(), self)
}
fn rebuild(self, state: &mut Self::State) {
if self != state.1 {
R::set_inner_html(&state.0, self);
Rndr::set_inner_html(&state.0, self);
state.1 = self;
}
}
@@ -380,13 +361,12 @@ where
}
}
impl<T, R> InnerHtmlValue<R> for Option<T>
impl<T> InnerHtmlValue for Option<T>
where
T: InnerHtmlValue<R>,
R: DomRenderer,
T: InnerHtmlValue,
{
type AsyncOutput = Self;
type State = (R::Element, Option<T::State>);
type State = (crate::renderer::types::Element, Option<T::State>);
type Cloneable = Option<T::Cloneable>;
type CloneableOwned = Option<T::CloneableOwned>;
@@ -407,12 +387,12 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
el: &<R as Renderer>::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
(el.clone(), self.map(|n| n.hydrate::<FROM_SERVER>(el)))
}
fn build(self, el: &<R as Renderer>::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
(el.clone(), self.map(|n| n.build(el)))
}
@@ -420,7 +400,7 @@ where
let new_state = match (self, &mut state.1) {
(None, None) => None,
(None, Some(_)) => {
R::set_inner_html(&state.0, "");
Rndr::set_inner_html(&state.0, "");
Some(None)
}
(Some(new), None) => Some(Some(new.build(&state.0))),

View File

@@ -1,7 +1,7 @@
use crate::{
html::attribute::Attribute,
hydration::Cursor,
renderer::{CastFrom, Renderer},
renderer::{CastFrom, Rndr},
ssr::StreamBuilder,
view::{
add_attr::AddAnyAttr, Mountable, Position, PositionState, Render,
@@ -13,7 +13,7 @@ use const_str_slice_concat::{
};
use futures::future::join;
use next_tuple::NextTuple;
use std::{marker::PhantomData, ops::Deref};
use std::ops::Deref;
mod custom;
mod element_ext;
@@ -27,29 +27,26 @@ pub use inner_html::*;
/// The typed representation of an HTML element.
#[derive(Debug, PartialEq, Eq)]
pub struct HtmlElement<E, At, Ch, Rndr> {
pub struct HtmlElement<E, At, Ch> {
pub(crate) tag: E,
pub(crate) rndr: PhantomData<Rndr>,
pub(crate) attributes: At,
pub(crate) children: Ch,
}
impl<E: Clone, At: Clone, Ch: Clone, Rndr> Clone
for HtmlElement<E, At, Ch, Rndr>
{
impl<E: Clone, At: Clone, Ch: Clone> Clone for HtmlElement<E, At, Ch> {
fn clone(&self) -> Self {
HtmlElement {
tag: self.tag.clone(),
rndr: PhantomData,
attributes: self.attributes.clone(),
children: self.children.clone(),
}
}
}
impl<E: Copy, At: Copy, Ch: Copy, Rndr> Copy for HtmlElement<E, At, Ch, Rndr> {}
impl<E: Copy, At: Copy, Ch: Copy> Copy for HtmlElement<E, At, Ch> {}
/*impl<E, At, Ch, Rndr> ElementType for HtmlElement<E, At, Ch, Rndr>
/*impl<E, At, Ch> ElementType for HtmlElement<E, At, Ch>
where
E: ElementType,
{
@@ -64,48 +61,42 @@ where
}
}*/
impl<E, At, Ch, NewChild, Rndr> ElementChild<Rndr, NewChild>
for HtmlElement<E, At, Ch, Rndr>
impl<E, At, Ch, NewChild> ElementChild<NewChild> for HtmlElement<E, At, Ch>
where
E: ElementWithChildren,
Ch: Render<Rndr> + NextTuple,
<Ch as NextTuple>::Output<NewChild>: Render<Rndr>,
Rndr: Renderer,
NewChild: Render<Rndr>,
Ch: Render + NextTuple,
<Ch as NextTuple>::Output<NewChild>: Render,
NewChild: Render,
{
type Output = HtmlElement<E, At, <Ch as NextTuple>::Output<NewChild>, Rndr>;
type Output = HtmlElement<E, At, <Ch as NextTuple>::Output<NewChild>>;
fn child(self, child: NewChild) -> Self::Output {
let HtmlElement {
tag,
rndr,
attributes,
children,
} = self;
HtmlElement {
tag,
rndr,
attributes,
children: children.next_tuple(child),
}
}
}
impl<E, At, Ch, Rndr> AddAnyAttr<Rndr> for HtmlElement<E, At, Ch, Rndr>
impl<E, At, Ch> AddAnyAttr for HtmlElement<E, At, Ch>
where
E: ElementType + CreateElement<Rndr> + Send,
At: Attribute<Rndr> + Send,
Ch: RenderHtml<Rndr> + Send,
Rndr: Renderer,
E: ElementType + Send,
At: Attribute + Send,
Ch: RenderHtml + Send,
{
type Output<SomeNewAttr: Attribute<Rndr>> = HtmlElement<
E,
<At as NextAttribute<Rndr>>::Output<SomeNewAttr>,
Ch,
Rndr,
>;
type Output<SomeNewAttr: Attribute> =
HtmlElement<E, <At as NextAttribute>::Output<SomeNewAttr>, Ch>;
fn add_any_attr<NewAttr: Attribute<Rndr>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr> {
@@ -113,22 +104,19 @@ where
tag,
attributes,
children,
rndr,
} = self;
HtmlElement {
tag,
attributes: attributes.add_any_attr(attr),
children,
rndr,
}
}
}
/// Adds a child to the element.
pub trait ElementChild<Rndr, NewChild>
pub trait ElementChild<NewChild>
where
NewChild: Render<Rndr>,
Rndr: Renderer,
NewChild: Render,
{
/// The type of the element, with the child added.
type Output;
@@ -163,27 +151,20 @@ pub trait HasElementType {
pub(crate) trait ElementWithChildren {}
/// Creates an element.
pub trait CreateElement<R: Renderer> {
/// Creates an element.
fn create_element(&self) -> R::Element;
}
impl<E, At, Ch, Rndr> HasElementType for HtmlElement<E, At, Ch, Rndr>
impl<E, At, Ch> HasElementType for HtmlElement<E, At, Ch>
where
E: ElementType,
{
type ElementType = E::Output;
}
impl<E, At, Ch, Rndr> Render<Rndr> for HtmlElement<E, At, Ch, Rndr>
impl<E, At, Ch> Render for HtmlElement<E, At, Ch>
where
E: ElementType + CreateElement<Rndr>,
At: Attribute<Rndr>,
Ch: Render<Rndr>,
Rndr: Renderer,
E: ElementType,
At: Attribute,
Ch: Render,
{
type State = ElementState<At::State, Ch::State, Rndr>;
type State = ElementState<At::State, Ch::State>;
fn rebuild(self, state: &mut Self::State) {
let ElementState {
@@ -196,7 +177,7 @@ where
}
fn build(self) -> Self::State {
let el = Rndr::create_element(self.tag);
let el = Rndr::create_element(self.tag.tag());
let attrs = self.attributes.build(&el);
let children = if E::SELF_CLOSING {
@@ -210,19 +191,17 @@ where
el,
attrs,
children,
rndr: PhantomData,
}
}
}
impl<E, At, Ch, Rndr> RenderHtml<Rndr> for HtmlElement<E, At, Ch, Rndr>
impl<E, At, Ch> RenderHtml for HtmlElement<E, At, Ch>
where
E: ElementType + CreateElement<Rndr> + Send,
At: Attribute<Rndr> + Send,
Ch: RenderHtml<Rndr> + Send,
Rndr: Renderer,
E: ElementType + Send,
At: Attribute + Send,
Ch: RenderHtml + Send,
{
type AsyncOutput = HtmlElement<E, At::AsyncOutput, Ch::AsyncOutput, Rndr>;
type AsyncOutput = HtmlElement<E, At::AsyncOutput, Ch::AsyncOutput>;
const MIN_LENGTH: usize = if E::SELF_CLOSING {
3 // < ... />
@@ -247,7 +226,7 @@ where
join(self.attributes.resolve(), self.children.resolve()).await;
HtmlElement {
tag: self.tag,
rndr: PhantomData,
attributes,
children,
}
@@ -350,7 +329,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<Rndr>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
// non-Static custom elements need special support in templates
@@ -365,7 +344,8 @@ where
} else if curr_position != Position::Current {
cursor.sibling();
}
let el = Rndr::Element::cast_from(cursor.current()).unwrap();
let el = crate::renderer::types::Element::cast_from(cursor.current())
.unwrap();
let attrs = self.attributes.hydrate::<FROM_SERVER>(&el);
@@ -378,23 +358,26 @@ where
};
// go to next sibling
cursor.set(el.as_ref().clone());
cursor.set(
<crate::renderer::types::Element as AsRef<
crate::renderer::types::Node,
>>::as_ref(&el)
.clone(),
);
position.set(Position::NextChild);
ElementState {
el,
attrs,
children,
rndr: PhantomData,
}
}
}
/// Renders an [`Attribute`] (which can be one or more HTML attributes) into an HTML buffer.
pub fn attributes_to_html<At, R>(attr: At, buf: &mut String) -> String
pub fn attributes_to_html<At>(attr: At, buf: &mut String) -> String
where
At: Attribute<R>,
R: Renderer,
At: Attribute,
{
// `class` and `style` are created first, and pushed later
// this is because they can be filled by a mixture of values that include
@@ -429,36 +412,38 @@ where
}
/// The retained view state for an HTML element.
pub struct ElementState<At, Ch, R: Renderer> {
pub(crate) el: R::Element,
pub struct ElementState<At, Ch> {
pub(crate) el: crate::renderer::types::Element,
pub(crate) attrs: At,
pub(crate) children: Option<Ch>,
rndr: PhantomData<R>,
}
impl<At, Ch, R: Renderer> Deref for ElementState<At, Ch, R> {
type Target = R::Element;
impl<At, Ch> Deref for ElementState<At, Ch> {
type Target = crate::renderer::types::Element;
fn deref(&self) -> &Self::Target {
&self.el
}
}
impl<At, Ch, R> Mountable<R> for ElementState<At, Ch, R>
where
R: Renderer,
{
impl<At, Ch> Mountable for ElementState<At, Ch> {
fn unmount(&mut self) {
R::remove(self.el.as_ref());
Rndr::remove(self.el.as_ref());
}
fn mount(&mut self, parent: &R::Element, marker: Option<&R::Node>) {
R::insert_node(parent, self.el.as_ref(), marker);
fn mount(
&mut self,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
Rndr::insert_node(parent, self.el.as_ref(), marker);
}
fn insert_before_this(&self, child: &mut dyn Mountable<R>) -> bool {
if let Some(parent) = R::get_parent(self.el.as_ref()) {
if let Some(element) = R::Element::cast_from(parent) {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
if let Some(parent) = Rndr::get_parent(self.el.as_ref()) {
if let Some(element) =
crate::renderer::types::Element::cast_from(parent)
{
child.mount(&element, Some(self.el.as_ref()));
return true;
}
@@ -467,12 +452,11 @@ where
}
}
impl<E, At, Ch, Rndr> ToTemplate for HtmlElement<E, At, Ch, Rndr>
impl<E, At, Ch> ToTemplate for HtmlElement<E, At, Ch>
where
E: ElementType,
At: Attribute<Rndr> + ToTemplate,
Ch: Render<Rndr> + ToTemplate,
Rndr: Renderer,
At: Attribute + ToTemplate,
Ch: Render + ToTemplate,
{
const TEMPLATE: &'static str = str_from_buffer(&const_concat(&[
"<",
@@ -555,7 +539,7 @@ where
}
}
}
/*
#[cfg(all(test, feature = "testing"))]
mod tests {
#[cfg(feature = "nightly")]
@@ -614,3 +598,4 @@ mod tests {
assert_eq!(html.len(), allocated_len);
}
}
*/

View File

@@ -1,6 +1,6 @@
use crate::{
html::attribute::Attribute,
renderer::{CastFrom, DomRenderer, RemoveEventHandler},
renderer::{CastFrom, RemoveEventHandler, Rndr},
view::{Position, ToTemplate},
};
use send_wrapper::SendWrapper;
@@ -51,13 +51,12 @@ where
}
/// An event listener with a typed event target.
pub struct Targeted<E, T, R> {
pub struct Targeted<E, T> {
event: E,
el_ty: PhantomData<T>,
rndr: PhantomData<R>,
}
impl<E, T, R> Targeted<E, T, R> {
impl<E, T> Targeted<E, T> {
/// Returns the inner event.
pub fn into_inner(self) -> E {
self.event
@@ -66,17 +65,17 @@ impl<E, T, R> Targeted<E, T, R> {
/// Returns the event's target, as an HTML element of the correct type.
pub fn target(&self) -> T
where
T: CastFrom<R::Element>,
R: DomRenderer,
R::Event: From<E>,
T: CastFrom<crate::renderer::types::Element>,
crate::renderer::types::Event: From<E>,
E: Clone,
{
let ev = R::Event::from(self.event.clone());
R::event_target(&ev)
let ev = crate::renderer::types::Event::from(self.event.clone());
Rndr::event_target(&ev)
}
}
impl<E, T, R> Deref for Targeted<E, T, R> {
impl<E, T> Deref for Targeted<E, T> {
type Target = E;
fn deref(&self) -> &Self::Target {
@@ -84,64 +83,60 @@ impl<E, T, R> Deref for Targeted<E, T, R> {
}
}
impl<E, T, R> DerefMut for Targeted<E, T, R> {
impl<E, T> DerefMut for Targeted<E, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.event
}
}
impl<E, T, R> From<E> for Targeted<E, T, R> {
impl<E, T> From<E> for Targeted<E, T> {
fn from(event: E) -> Self {
Targeted {
event,
el_ty: PhantomData,
rndr: PhantomData,
}
}
}
/// Creates an [`Attribute`] that will add an event listener to an element.
pub fn on<E, F, R>(event: E, cb: F) -> On<E, F, R>
pub fn on<E, F>(event: E, cb: F) -> On<E, F>
where
F: FnMut(E::EventType) + 'static,
E: EventDescriptor + Send + 'static,
E::EventType: 'static,
E::EventType: From<R::Event>,
R: DomRenderer,
E::EventType: From<crate::renderer::types::Event>,
{
On {
event,
cb: Some(SendWrapper::new(cb)),
ty: PhantomData,
}
}
/// Creates an [`Attribute`] that will add an event listener with a typed target to an element.
#[allow(clippy::type_complexity)]
pub fn on_target<E, T, R, F>(
pub fn on_target<E, T, F>(
event: E,
mut cb: F,
) -> On<E, Box<dyn FnMut(E::EventType)>, R>
) -> On<E, Box<dyn FnMut(E::EventType)>>
where
T: HasElementType,
F: FnMut(Targeted<E::EventType, <T as HasElementType>::ElementType, R>)
F: FnMut(Targeted<E::EventType, <T as HasElementType>::ElementType>)
+ 'static,
E: EventDescriptor + Send + 'static,
E::EventType: 'static,
R: DomRenderer,
E::EventType: From<R::Event>,
E::EventType: From<crate::renderer::types::Event>,
{
on(event, Box::new(move |ev: E::EventType| cb(ev.into())))
}
/// An [`Attribute`] that adds an event listener to an element.
pub struct On<E, F, R> {
pub struct On<E, F> {
event: E,
cb: Option<SendWrapper<F>>,
ty: PhantomData<R>,
}
impl<E, F, R> Clone for On<E, F, R>
impl<E, F> Clone for On<E, F>
where
E: Clone,
F: Clone,
@@ -150,30 +145,33 @@ where
Self {
event: self.event.clone(),
cb: self.cb.clone(),
ty: PhantomData,
}
}
}
impl<E, F, R> On<E, F, R>
impl<E, F> On<E, F>
where
F: EventCallback<E::EventType>,
E: EventDescriptor + Send + 'static,
E::EventType: 'static,
R: DomRenderer,
E::EventType: From<R::Event>,
E::EventType: From<crate::renderer::types::Event>,
{
/// Attaches the event listener to the element.
pub fn attach(self, el: &R::Element) -> RemoveEventHandler<R::Element> {
fn attach_inner<R: DomRenderer>(
el: &R::Element,
cb: Box<dyn FnMut(R::Event)>,
pub fn attach(
self,
el: &crate::renderer::types::Element,
) -> RemoveEventHandler<crate::renderer::types::Element> {
fn attach_inner(
el: &crate::renderer::types::Element,
cb: Box<dyn FnMut(crate::renderer::types::Event)>,
name: Cow<'static, str>,
delegation_key: Option<Cow<'static, str>>,
) -> RemoveEventHandler<R::Element> {
) -> RemoveEventHandler<crate::renderer::types::Element> {
match delegation_key {
None => R::add_event_listener(el, &name, cb),
Some(key) => R::add_event_listener_delegated(el, name, key, cb),
None => Rndr::add_event_listener(el, &name, cb),
Some(key) => {
Rndr::add_event_listener_delegated(el, name, key, cb)
}
}
}
@@ -182,7 +180,7 @@ where
#[cfg(feature = "tracing")]
let span = tracing::Span::current();
let cb = Box::new(move |ev: R::Event| {
let cb = Box::new(move |ev: crate::renderer::types::Event| {
#[cfg(all(debug_assertions, feature = "reactive_graph"))]
let _rx_guard =
reactive_graph::diagnostics::SpecialNonReactiveZone::enter();
@@ -191,9 +189,9 @@ where
let ev = E::EventType::from(ev);
cb.invoke(ev);
}) as Box<dyn FnMut(R::Event)>;
}) as Box<dyn FnMut(crate::renderer::types::Event)>;
attach_inner::<R>(
attach_inner(
el,
cb,
self.event.name(),
@@ -203,30 +201,32 @@ where
}
}
impl<E, F, R> Debug for On<E, F, R>
impl<E, F> Debug for On<E, F>
where
E: Debug,
R: DomRenderer,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("On").field(&self.event).finish()
}
}
impl<E, F, R> Attribute<R> for On<E, F, R>
impl<E, F> Attribute for On<E, F>
where
F: EventCallback<E::EventType>,
E: EventDescriptor + Send + 'static,
E::EventType: 'static,
R: DomRenderer,
E::EventType: From<R::Event>,
E::EventType: From<crate::renderer::types::Event>,
{
const MIN_LENGTH: usize = 0;
type AsyncOutput = Self;
// a function that can be called once to remove the event listener
type State = (R::Element, Option<RemoveEventHandler<R::Element>>);
type Cloneable = On<E, SharedEventCallback<E::EventType>, R>;
type CloneableOwned = On<E, SharedEventCallback<E::EventType>, R>;
type State = (
crate::renderer::types::Element,
Option<RemoveEventHandler<crate::renderer::types::Element>>,
);
type Cloneable = On<E, SharedEventCallback<E::EventType>>;
type CloneableOwned = On<E, SharedEventCallback<E::EventType>>;
#[inline(always)]
fn html_len(&self) -> usize {
@@ -244,13 +244,16 @@ where
}
#[inline(always)]
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
let cleanup = self.attach(el);
(el.clone(), Some(cleanup))
}
#[inline(always)]
fn build(self, el: &R::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
let cleanup = self.attach(el);
(el.clone(), Some(cleanup))
}
@@ -268,7 +271,6 @@ where
On {
cb: self.cb.map(|cb| SendWrapper::new(cb.take().into_shared())),
event: self.event,
ty: self.ty,
}
}
@@ -276,7 +278,6 @@ where
On {
cb: self.cb.map(|cb| SendWrapper::new(cb.take().into_shared())),
event: self.event,
ty: self.ty,
}
}
@@ -293,17 +294,17 @@ where
}
}
impl<E, F, R> NextAttribute<R> for On<E, F, R>
impl<E, F> NextAttribute for On<E, F>
where
F: EventCallback<E::EventType>,
E: EventDescriptor + Send + 'static,
E::EventType: 'static,
R: DomRenderer,
E::EventType: From<R::Event>,
{
type Output<NewAttr: Attribute<R>> = (Self, NewAttr);
fn add_any_attr<NewAttr: Attribute<R>>(
E::EventType: From<crate::renderer::types::Event>,
{
type Output<NewAttr: Attribute> = (Self, NewAttr);
fn add_any_attr<NewAttr: Attribute>(
self,
new_attr: NewAttr,
) -> Self::Output<NewAttr> {
@@ -311,7 +312,7 @@ where
}
}
impl<E, F, R> ToTemplate for On<E, F, R> {
impl<E, F> ToTemplate for On<E, F> {
#[inline(always)]
fn to_template(
_buf: &mut String,
@@ -404,9 +405,9 @@ impl<E: FromWasmAbi> Custom<E> {
/// # use tachys::prelude::*;
/// # use tachys::html;
/// # use tachys::html::event as ev;
/// # fn custom_event() -> impl Render<Dom> {
/// # fn custom_event() -> impl Render {
/// let mut non_passive_wheel = ev::Custom::new("wheel");
/// non_passive_wheel.options_mut().passive(false);
/// non_passive_wheel.options_mut().set_passive(false);
///
/// let canvas =
/// html::element::canvas().on(non_passive_wheel, |e: ev::WheelEvent| {

View File

@@ -2,30 +2,26 @@ use super::attribute::Attribute;
use crate::{
hydration::Cursor,
prelude::{Render, RenderHtml},
renderer::Renderer,
ssr::StreamBuilder,
view::{add_attr::AddAnyAttr, Position, PositionState},
};
use std::marker::PhantomData;
/// An island of interactivity in an otherwise-inert HTML document.
pub struct Island<Rndr, View> {
pub struct Island<View> {
component: &'static str,
props_json: String,
view: View,
rndr: PhantomData<Rndr>,
}
const ISLAND_TAG: &str = "leptos-island";
const ISLAND_CHILDREN_TAG: &str = "leptos-children";
impl<Rndr, View> Island<Rndr, View> {
impl<View> Island<View> {
/// Creates a new island with the given component name.
pub fn new(component: &'static str, view: View) -> Self {
Island {
component,
props_json: String::new(),
view,
rndr: PhantomData,
}
}
@@ -57,10 +53,9 @@ impl<Rndr, View> Island<Rndr, View> {
}
}
impl<Rndr, View> Render<Rndr> for Island<Rndr, View>
impl<View> Render for Island<View>
where
View: Render<Rndr>,
Rndr: Renderer,
View: Render,
{
type State = View::State;
@@ -73,42 +68,38 @@ where
}
}
impl<Rndr, View> AddAnyAttr<Rndr> for Island<Rndr, View>
impl<View> AddAnyAttr for Island<View>
where
View: RenderHtml<Rndr>,
Rndr: Renderer,
View: RenderHtml,
{
type Output<SomeNewAttr: Attribute<Rndr>> =
Island<Rndr, <View as AddAnyAttr<Rndr>>::Output<SomeNewAttr>>;
type Output<SomeNewAttr: Attribute> =
Island<<View as AddAnyAttr>::Output<SomeNewAttr>>;
fn add_any_attr<NewAttr: Attribute<Rndr>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Rndr>,
Self::Output<NewAttr>: RenderHtml,
{
let Island {
component,
props_json,
view,
rndr,
} = self;
Island {
component,
props_json,
view: view.add_any_attr(attr),
rndr,
}
}
}
impl<Rndr, View> RenderHtml<Rndr> for Island<Rndr, View>
impl<View> RenderHtml for Island<View>
where
View: RenderHtml<Rndr>,
Rndr: Renderer,
View: RenderHtml,
{
type AsyncOutput = Island<Rndr, View::AsyncOutput>;
type AsyncOutput = Island<View::AsyncOutput>;
const MIN_LENGTH: usize = ISLAND_TAG.len() * 2
+ "<>".len()
@@ -125,13 +116,11 @@ where
component,
props_json,
view,
rndr,
} = self;
Island {
component,
props_json,
view: view.resolve().await,
rndr,
}
}
@@ -178,7 +167,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<Rndr>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
if position.get() == Position::FirstChild {
@@ -192,18 +181,14 @@ where
}
/// The children that will be projected into an [`Island`].
pub struct IslandChildren<Rndr, View> {
pub struct IslandChildren<View> {
view: View,
rndr: PhantomData<Rndr>,
}
impl<Rndr, View> IslandChildren<Rndr, View> {
impl<View> IslandChildren<View> {
/// Creates a new representation of the children.
pub fn new(view: View) -> Self {
IslandChildren {
view,
rndr: PhantomData,
}
IslandChildren { view }
}
fn open_tag(buf: &mut String) {
@@ -219,10 +204,9 @@ impl<Rndr, View> IslandChildren<Rndr, View> {
}
}
impl<Rndr, View> Render<Rndr> for IslandChildren<Rndr, View>
impl<View> Render for IslandChildren<View>
where
View: Render<Rndr>,
Rndr: Renderer,
View: Render,
{
type State = ();
@@ -231,35 +215,32 @@ where
fn rebuild(self, _state: &mut Self::State) {}
}
impl<Rndr, View> AddAnyAttr<Rndr> for IslandChildren<Rndr, View>
impl<View> AddAnyAttr for IslandChildren<View>
where
View: RenderHtml<Rndr>,
Rndr: Renderer,
View: RenderHtml,
{
type Output<SomeNewAttr: Attribute<Rndr>> =
IslandChildren<Rndr, <View as AddAnyAttr<Rndr>>::Output<SomeNewAttr>>;
type Output<SomeNewAttr: Attribute> =
IslandChildren<<View as AddAnyAttr>::Output<SomeNewAttr>>;
fn add_any_attr<NewAttr: Attribute<Rndr>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Rndr>,
Self::Output<NewAttr>: RenderHtml,
{
let IslandChildren { view, rndr } = self;
let IslandChildren { view } = self;
IslandChildren {
view: view.add_any_attr(attr),
rndr,
}
}
}
impl<Rndr, View> RenderHtml<Rndr> for IslandChildren<Rndr, View>
impl<View> RenderHtml for IslandChildren<View>
where
View: RenderHtml<Rndr>,
Rndr: Renderer,
View: RenderHtml,
{
type AsyncOutput = IslandChildren<Rndr, View::AsyncOutput>;
type AsyncOutput = IslandChildren<View::AsyncOutput>;
const MIN_LENGTH: usize = ISLAND_CHILDREN_TAG.len() * 2
+ "<>".len()
@@ -271,10 +252,9 @@ where
}
async fn resolve(self) -> Self::AsyncOutput {
let IslandChildren { view, rndr } = self;
let IslandChildren { view } = self;
IslandChildren {
view: view.resolve().await,
rndr,
}
}
@@ -321,7 +301,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<Rndr>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
// island children aren't hydrated

View File

@@ -3,10 +3,10 @@ use crate::{
hydration::Cursor,
no_attrs,
prelude::AddAnyAttr,
renderer::{CastFrom, DomRenderer, Renderer},
renderer::{CastFrom, Rndr},
view::{Position, PositionState, Render, RenderHtml},
};
use std::{borrow::Cow, marker::PhantomData};
use std::borrow::Cow;
/// Types for HTML attributes.
pub mod attribute;
@@ -28,20 +28,16 @@ pub mod property;
pub mod style;
/// A `<!DOCTYPE>` declaration.
pub struct Doctype<R: Renderer> {
pub struct Doctype {
value: &'static str,
rndr: PhantomData<R>,
}
/// Creates a `<!DOCTYPE>`.
pub fn doctype<R: Renderer>(value: &'static str) -> Doctype<R> {
Doctype {
value,
rndr: PhantomData,
}
pub fn doctype(value: &'static str) -> Doctype {
Doctype { value }
}
impl<R: Renderer> Render<R> for Doctype<R> {
impl Render for Doctype {
type State = ();
fn build(self) -> Self::State {}
@@ -49,12 +45,9 @@ impl<R: Renderer> Render<R> for Doctype<R> {
fn rebuild(self, _state: &mut Self::State) {}
}
no_attrs!(Doctype<R>);
no_attrs!(Doctype);
impl<R> RenderHtml<R> for Doctype<R>
where
R: Renderer + Send,
{
impl RenderHtml for Doctype {
type AsyncOutput = Self;
const MIN_LENGTH: usize = "<!DOCTYPE html>".len();
@@ -79,7 +72,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
_cursor: &Cursor<R>,
_cursor: &Cursor,
_position: &PositionState,
) -> Self::State {
}
@@ -97,11 +90,8 @@ impl InertElement {
}
}
impl<Rndr> Render<Rndr> for InertElement
where
Rndr: DomRenderer,
{
type State = Rndr::Element;
impl Render for InertElement {
type State = crate::renderer::types::Element;
fn build(self) -> Self::State {
Rndr::create_element_from_html(&self.html)
@@ -110,18 +100,15 @@ where
fn rebuild(self, _state: &mut Self::State) {}
}
impl<Rndr> AddAnyAttr<Rndr> for InertElement
where
Rndr: DomRenderer,
{
type Output<SomeNewAttr: Attribute<Rndr>> = Self;
impl AddAnyAttr for InertElement {
type Output<SomeNewAttr: Attribute> = Self;
fn add_any_attr<NewAttr: Attribute<Rndr>>(
fn add_any_attr<NewAttr: Attribute>(
self,
_attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Rndr>,
Self::Output<NewAttr>: RenderHtml,
{
panic!(
"InertElement does not support adding attributes. It should only \
@@ -130,10 +117,7 @@ where
}
}
impl<Rndr> RenderHtml<Rndr> for InertElement
where
Rndr: DomRenderer,
{
impl RenderHtml for InertElement {
type AsyncOutput = Self;
const MIN_LENGTH: usize = 0;
@@ -161,7 +145,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<Rndr>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
let curr_position = position.get();
@@ -170,7 +154,8 @@ where
} else if curr_position != Position::Current {
cursor.sibling();
}
let el = Rndr::Element::cast_from(cursor.current()).unwrap();
let el = crate::renderer::types::Element::cast_from(cursor.current())
.unwrap();
position.set(Position::NextChild);
el
}

View File

@@ -3,30 +3,27 @@ use super::{
element::ElementType,
};
use crate::{
html::element::HtmlElement, prelude::Render, renderer::Renderer,
view::add_attr::AddAnyAttr,
html::element::HtmlElement, prelude::Render, view::add_attr::AddAnyAttr,
};
use std::marker::PhantomData;
/// Describes a container that can be used to hold a reference to an HTML element.
pub trait NodeRefContainer<E, Rndr>: Send + Clone
pub trait NodeRefContainer<E>: Send + Clone
where
E: ElementType,
Rndr: Renderer,
{
/// Fills the container with the element.
fn load(self, el: &Rndr::Element);
fn load(self, el: &crate::renderer::types::Element);
}
/// An [`Attribute`] that will fill a [`NodeRefContainer`] with an HTML element.
#[derive(Debug)]
pub struct NodeRefAttr<E, C, Rndr> {
pub struct NodeRefAttr<E, C> {
container: C,
ty: PhantomData<E>,
rndr: PhantomData<Rndr>,
}
impl<E, C, Rndr> Clone for NodeRefAttr<E, C, Rndr>
impl<E, C> Clone for NodeRefAttr<E, C>
where
C: Clone,
{
@@ -34,35 +31,32 @@ where
Self {
container: self.container.clone(),
ty: PhantomData,
rndr: PhantomData,
}
}
}
/// Creates an attribute that will fill a [`NodeRefContainer`] with the element it is applied to.
pub fn node_ref<E, C, Rndr>(container: C) -> NodeRefAttr<E, C, Rndr>
pub fn node_ref<E, C>(container: C) -> NodeRefAttr<E, C>
where
E: ElementType,
C: NodeRefContainer<E, Rndr>,
Rndr: Renderer,
C: NodeRefContainer<E>,
{
NodeRefAttr {
container,
ty: PhantomData,
rndr: PhantomData,
}
}
impl<E, C, Rndr> Attribute<Rndr> for NodeRefAttr<E, C, Rndr>
impl<E, C> Attribute for NodeRefAttr<E, C>
where
E: ElementType,
C: NodeRefContainer<E, Rndr>,
Rndr: Renderer,
Rndr::Element: PartialEq,
C: NodeRefContainer<E>,
crate::renderer::types::Element: PartialEq,
{
const MIN_LENGTH: usize = 0;
type AsyncOutput = Self;
type State = Rndr::Element;
type State = crate::renderer::types::Element;
type Cloneable = ();
type CloneableOwned = ();
@@ -82,13 +76,13 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
el: &<Rndr as Renderer>::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
self.container.load(el);
el.to_owned()
}
fn build(self, el: &<Rndr as Renderer>::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
self.container.load(el);
el.to_owned()
}
@@ -112,16 +106,16 @@ where
}
}
impl<E, C, Rndr> NextAttribute<Rndr> for NodeRefAttr<E, C, Rndr>
impl<E, C> NextAttribute for NodeRefAttr<E, C>
where
E: ElementType,
C: NodeRefContainer<E, Rndr>,
Rndr: Renderer,
Rndr::Element: PartialEq,
{
type Output<NewAttr: Attribute<Rndr>> = (Self, NewAttr);
C: NodeRefContainer<E>,
fn add_any_attr<NewAttr: Attribute<Rndr>>(
crate::renderer::types::Element: PartialEq,
{
type Output<NewAttr: Attribute> = (Self, NewAttr);
fn add_any_attr<NewAttr: Attribute>(
self,
new_attr: NewAttr,
) -> Self::Output<NewAttr> {
@@ -130,35 +124,33 @@ where
}
/// Adds the `node_ref` attribute to an element.
pub trait NodeRefAttribute<E, C, Rndr>
pub trait NodeRefAttribute<E, C>
where
E: ElementType,
C: NodeRefContainer<E, Rndr>,
Rndr: Renderer,
Rndr::Element: PartialEq,
C: NodeRefContainer<E>,
crate::renderer::types::Element: PartialEq,
{
/// Binds this HTML element to a [`NodeRefContainer`].
fn node_ref(
self,
container: C,
) -> <Self as AddAnyAttr<Rndr>>::Output<NodeRefAttr<E, C, Rndr>>
) -> <Self as AddAnyAttr>::Output<NodeRefAttr<E, C>>
where
Self: Sized + AddAnyAttr<Rndr>,
<Self as AddAnyAttr<Rndr>>::Output<NodeRefAttr<E, C, Rndr>>:
Render<Rndr>,
Self: Sized + AddAnyAttr,
<Self as AddAnyAttr>::Output<NodeRefAttr<E, C>>: Render,
{
self.add_any_attr(node_ref(container))
}
}
impl<E, At, Ch, C, Rndr> NodeRefAttribute<E, C, Rndr>
for HtmlElement<E, At, Ch, Rndr>
impl<E, At, Ch, C> NodeRefAttribute<E, C> for HtmlElement<E, At, Ch>
where
E: ElementType,
At: Attribute<Rndr>,
Ch: Render<Rndr>,
C: NodeRefContainer<E, Rndr>,
Rndr: Renderer,
Rndr::Element: PartialEq,
At: Attribute,
Ch: Render,
C: NodeRefContainer<E>,
crate::renderer::types::Element: PartialEq,
{
}

View File

@@ -1,37 +1,34 @@
use super::attribute::{Attribute, NextAttribute};
use crate::{
renderer::DomRenderer,
renderer::Rndr,
view::{Position, ToTemplate},
};
use send_wrapper::SendWrapper;
use std::{borrow::Cow, marker::PhantomData, sync::Arc};
use std::{borrow::Cow, sync::Arc};
use wasm_bindgen::JsValue;
/// Creates an [`Attribute`] that will set a DOM property on an element.
#[inline(always)]
pub fn prop<K, P, R>(key: K, value: P) -> Property<K, P, R>
pub fn prop<K, P>(key: K, value: P) -> Property<K, P>
where
K: AsRef<str>,
P: IntoProperty<R>,
R: DomRenderer,
P: IntoProperty,
{
Property {
key,
value: Some(SendWrapper::new(value)),
rndr: PhantomData,
}
}
/// An [`Attribute`] that will set a DOM property on an element.
#[derive(Debug)]
pub struct Property<K, P, R> {
pub struct Property<K, P> {
key: K,
// property values will only be accessed in the browser
value: Option<SendWrapper<P>>,
rndr: PhantomData<R>,
}
impl<K, P, R> Clone for Property<K, P, R>
impl<K, P> Clone for Property<K, P>
where
K: Clone,
P: Clone,
@@ -40,23 +37,21 @@ where
Self {
key: self.key.clone(),
value: self.value.clone(),
rndr: PhantomData,
}
}
}
impl<K, P, R> Attribute<R> for Property<K, P, R>
impl<K, P> Attribute for Property<K, P>
where
K: AsRef<str> + Send,
P: IntoProperty<R>,
R: DomRenderer,
P: IntoProperty,
{
const MIN_LENGTH: usize = 0;
type AsyncOutput = Self;
type State = P::State;
type Cloneable = Property<Arc<str>, P::Cloneable, R>;
type CloneableOwned = Property<Arc<str>, P::CloneableOwned, R>;
type Cloneable = Property<Arc<str>, P::Cloneable>;
type CloneableOwned = Property<Arc<str>, P::CloneableOwned>;
#[inline(always)]
fn html_len(&self) -> usize {
@@ -72,14 +67,17 @@ where
) {
}
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
self.value
.expect("property removed early")
.take()
.hydrate::<FROM_SERVER>(el, self.key.as_ref())
}
fn build(self, el: &R::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
self.value
.expect("property removed early")
.take()
@@ -99,7 +97,6 @@ where
value: self
.value
.map(|value| SendWrapper::new(value.take().into_cloneable())),
rndr: self.rndr,
}
}
@@ -109,7 +106,6 @@ where
value: self.value.map(|value| {
SendWrapper::new(value.take().into_cloneable_owned())
}),
rndr: self.rndr,
}
}
@@ -126,15 +122,14 @@ where
}
}
impl<K, P, R> NextAttribute<R> for Property<K, P, R>
impl<K, P> NextAttribute for Property<K, P>
where
K: AsRef<str> + Send,
P: IntoProperty<R>,
R: DomRenderer,
P: IntoProperty,
{
type Output<NewAttr: Attribute<R>> = (Self, NewAttr);
type Output<NewAttr: Attribute> = (Self, NewAttr);
fn add_any_attr<NewAttr: Attribute<R>>(
fn add_any_attr<NewAttr: Attribute>(
self,
new_attr: NewAttr,
) -> Self::Output<NewAttr> {
@@ -142,11 +137,10 @@ where
}
}
impl<K, P, R> ToTemplate for Property<K, P, R>
impl<K, P> ToTemplate for Property<K, P>
where
K: AsRef<str>,
P: IntoProperty<R>,
R: DomRenderer,
P: IntoProperty,
{
fn to_template(
_buf: &mut String,
@@ -159,23 +153,27 @@ where
}
/// A possible value for a DOM property.
pub trait IntoProperty<R: DomRenderer> {
pub trait IntoProperty {
/// The view state retained between building and rebuilding.
type State;
/// An equivalent value that can be cloned.
type Cloneable: IntoProperty<R> + Clone;
type Cloneable: IntoProperty + Clone;
/// An equivalent value that can be cloned and is `'static`.
type CloneableOwned: IntoProperty<R> + Clone + 'static;
type CloneableOwned: IntoProperty + Clone + 'static;
/// Adds the property on an element created from HTML.
fn hydrate<const FROM_SERVER: bool>(
self,
el: &R::Element,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State;
/// Adds the property during client-side rendering.
fn build(self, el: &R::Element, key: &str) -> Self::State;
fn build(
self,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State;
/// Updates the property with a new value.
fn rebuild(self, state: &mut Self::State, key: &str);
@@ -189,34 +187,35 @@ pub trait IntoProperty<R: DomRenderer> {
macro_rules! prop_type {
($prop_type:ty) => {
impl<R> IntoProperty<R> for $prop_type
where
R: DomRenderer,
{
type State = (R::Element, JsValue);
impl IntoProperty for $prop_type {
type State = (crate::renderer::types::Element, JsValue);
type Cloneable = Self;
type CloneableOwned = Self;
fn hydrate<const FROM_SERVER: bool>(
self,
el: &R::Element,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
let value = self.into();
R::set_property(el, key, &value);
Rndr::set_property(el, key, &value);
(el.clone(), value)
}
fn build(self, el: &R::Element, key: &str) -> Self::State {
fn build(
self,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
let value = self.into();
R::set_property(el, key, &value);
Rndr::set_property(el, key, &value);
(el.clone(), value)
}
fn rebuild(self, state: &mut Self::State, key: &str) {
let (el, prev) = state;
let value = self.into();
R::set_property(el, key, &value);
Rndr::set_property(el, key, &value);
*prev = value;
}
@@ -229,32 +228,33 @@ macro_rules! prop_type {
}
}
impl<R> IntoProperty<R> for Option<$prop_type>
where
R: DomRenderer,
{
type State = (R::Element, JsValue);
impl IntoProperty for Option<$prop_type> {
type State = (crate::renderer::types::Element, JsValue);
type Cloneable = Self;
type CloneableOwned = Self;
fn hydrate<const FROM_SERVER: bool>(
self,
el: &R::Element,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
let was_some = self.is_some();
let value = self.into();
if was_some {
R::set_property(el, key, &value);
Rndr::set_property(el, key, &value);
}
(el.clone(), value)
}
fn build(self, el: &R::Element, key: &str) -> Self::State {
fn build(
self,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
let was_some = self.is_some();
let value = self.into();
if was_some {
R::set_property(el, key, &value);
Rndr::set_property(el, key, &value);
}
(el.clone(), value)
}
@@ -262,7 +262,7 @@ macro_rules! prop_type {
fn rebuild(self, state: &mut Self::State, key: &str) {
let (el, prev) = state;
let value = self.into();
R::set_property(el, key, &value);
Rndr::set_property(el, key, &value);
*prev = value;
}
@@ -279,34 +279,35 @@ macro_rules! prop_type {
macro_rules! prop_type_str {
($prop_type:ty) => {
impl<R> IntoProperty<R> for $prop_type
where
R: DomRenderer,
{
type State = (R::Element, JsValue);
impl IntoProperty for $prop_type {
type State = (crate::renderer::types::Element, JsValue);
type Cloneable = Arc<str>;
type CloneableOwned = Arc<str>;
fn hydrate<const FROM_SERVER: bool>(
self,
el: &R::Element,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
let value = JsValue::from(&*self);
R::set_property(el, key, &value);
Rndr::set_property(el, key, &value);
(el.clone(), value)
}
fn build(self, el: &R::Element, key: &str) -> Self::State {
fn build(
self,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
let value = JsValue::from(&*self);
R::set_property(el, key, &value);
Rndr::set_property(el, key, &value);
(el.clone(), value)
}
fn rebuild(self, state: &mut Self::State, key: &str) {
let (el, prev) = state;
let value = JsValue::from(&*self);
R::set_property(el, key, &value);
Rndr::set_property(el, key, &value);
*prev = value;
}
@@ -321,32 +322,33 @@ macro_rules! prop_type_str {
}
}
impl<R> IntoProperty<R> for Option<$prop_type>
where
R: DomRenderer,
{
type State = (R::Element, JsValue);
impl IntoProperty for Option<$prop_type> {
type State = (crate::renderer::types::Element, JsValue);
type Cloneable = Option<Arc<str>>;
type CloneableOwned = Option<Arc<str>>;
fn hydrate<const FROM_SERVER: bool>(
self,
el: &R::Element,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
let was_some = self.is_some();
let value = JsValue::from(self.map(|n| JsValue::from_str(&n)));
if was_some {
R::set_property(el, key, &value);
Rndr::set_property(el, key, &value);
}
(el.clone(), value)
}
fn build(self, el: &R::Element, key: &str) -> Self::State {
fn build(
self,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
let was_some = self.is_some();
let value = JsValue::from(self.map(|n| JsValue::from_str(&n)));
if was_some {
R::set_property(el, key, &value);
Rndr::set_property(el, key, &value);
}
(el.clone(), value)
}
@@ -354,7 +356,7 @@ macro_rules! prop_type_str {
fn rebuild(self, state: &mut Self::State, key: &str) {
let (el, prev) = state;
let value = JsValue::from(self.map(|n| JsValue::from_str(&n)));
R::set_property(el, key, &value);
Rndr::set_property(el, key, &value);
*prev = value;
}
@@ -375,34 +377,35 @@ macro_rules! prop_type_str {
};
}
impl<R> IntoProperty<R> for Arc<str>
where
R: DomRenderer,
{
type State = (R::Element, JsValue);
impl IntoProperty for Arc<str> {
type State = (crate::renderer::types::Element, JsValue);
type Cloneable = Self;
type CloneableOwned = Self;
fn hydrate<const FROM_SERVER: bool>(
self,
el: &R::Element,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
let value = JsValue::from_str(self.as_ref());
R::set_property(el, key, &value);
Rndr::set_property(el, key, &value);
(el.clone(), value)
}
fn build(self, el: &R::Element, key: &str) -> Self::State {
fn build(
self,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
let value = JsValue::from_str(self.as_ref());
R::set_property(el, key, &value);
Rndr::set_property(el, key, &value);
(el.clone(), value)
}
fn rebuild(self, state: &mut Self::State, key: &str) {
let (el, prev) = state;
let value = JsValue::from_str(self.as_ref());
R::set_property(el, key, &value);
Rndr::set_property(el, key, &value);
*prev = value;
}
@@ -415,32 +418,33 @@ where
}
}
impl<R> IntoProperty<R> for Option<Arc<str>>
where
R: DomRenderer,
{
type State = (R::Element, JsValue);
impl IntoProperty for Option<Arc<str>> {
type State = (crate::renderer::types::Element, JsValue);
type Cloneable = Self;
type CloneableOwned = Self;
fn hydrate<const FROM_SERVER: bool>(
self,
el: &R::Element,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
let was_some = self.is_some();
let value = JsValue::from(self.map(|n| JsValue::from_str(&n)));
if was_some {
R::set_property(el, key, &value);
Rndr::set_property(el, key, &value);
}
(el.clone(), value)
}
fn build(self, el: &R::Element, key: &str) -> Self::State {
fn build(
self,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
let was_some = self.is_some();
let value = JsValue::from(self.map(|n| JsValue::from_str(&n)));
if was_some {
R::set_property(el, key, &value);
Rndr::set_property(el, key, &value);
}
(el.clone(), value)
}
@@ -448,7 +452,7 @@ where
fn rebuild(self, state: &mut Self::State, key: &str) {
let (el, prev) = state;
let value = JsValue::from(self.map(|n| JsValue::from_str(&n)));
R::set_property(el, key, &value);
Rndr::set_property(el, key, &value);
*prev = value;
}

View File

@@ -2,54 +2,47 @@ use super::attribute::{Attribute, NextAttribute};
#[cfg(feature = "nightly")]
use crate::view::static_types::Static;
use crate::{
renderer::DomRenderer,
renderer::Rndr,
view::{Position, ToTemplate},
};
use std::{future::Future, marker::PhantomData, sync::Arc};
use std::{future::Future, sync::Arc};
/// Returns an [`Attribute`] that will add to an element's CSS styles.
#[inline(always)]
pub fn style<S, R>(style: S) -> Style<S, R>
pub fn style<S>(style: S) -> Style<S>
where
S: IntoStyle<R>,
R: DomRenderer,
S: IntoStyle,
{
Style {
style,
rndr: PhantomData,
}
Style { style }
}
/// An [`Attribute`] that will add to an element's CSS styles.
#[derive(Debug)]
pub struct Style<S, R> {
pub struct Style<S> {
style: S,
rndr: PhantomData<R>,
}
impl<S, R> Clone for Style<S, R>
impl<S> Clone for Style<S>
where
S: Clone,
{
fn clone(&self) -> Self {
Self {
style: self.style.clone(),
rndr: PhantomData,
}
}
}
impl<S, R> Attribute<R> for Style<S, R>
impl<S> Attribute for Style<S>
where
S: IntoStyle<R>,
R: DomRenderer,
S: IntoStyle,
{
const MIN_LENGTH: usize = 0;
type AsyncOutput = Style<S::AsyncOutput, R>;
type AsyncOutput = Style<S::AsyncOutput>;
type State = S::State;
type Cloneable = Style<S::Cloneable, R>;
type CloneableOwned = Style<S::CloneableOwned, R>;
type Cloneable = Style<S::Cloneable>;
type CloneableOwned = Style<S::CloneableOwned>;
// TODO
#[inline(always)]
@@ -67,11 +60,14 @@ where
self.style.to_html(style);
}
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
self.style.hydrate::<FROM_SERVER>(el)
}
fn build(self, el: &R::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
self.style.build(el)
}
@@ -82,14 +78,12 @@ where
fn into_cloneable(self) -> Self::Cloneable {
Style {
style: self.style.into_cloneable(),
rndr: self.rndr,
}
}
fn into_cloneable_owned(self) -> Self::CloneableOwned {
Style {
style: self.style.into_cloneable_owned(),
rndr: self.rndr,
}
}
@@ -100,19 +94,17 @@ where
async fn resolve(self) -> Self::AsyncOutput {
Style {
style: self.style.resolve().await,
rndr: self.rndr,
}
}
}
impl<S, R> NextAttribute<R> for Style<S, R>
impl<S> NextAttribute for Style<S>
where
S: IntoStyle<R>,
R: DomRenderer,
S: IntoStyle,
{
type Output<NewAttr: Attribute<R>> = (Self, NewAttr);
type Output<NewAttr: Attribute> = (Self, NewAttr);
fn add_any_attr<NewAttr: Attribute<R>>(
fn add_any_attr<NewAttr: Attribute>(
self,
new_attr: NewAttr,
) -> Self::Output<NewAttr> {
@@ -120,10 +112,9 @@ where
}
}
impl<S, R> ToTemplate for Style<S, R>
impl<S> ToTemplate for Style<S>
where
S: IntoStyle<R>,
R: DomRenderer,
S: IntoStyle,
{
fn to_template(
_buf: &mut String,
@@ -138,25 +129,28 @@ where
/// Any type that can be added to the `style` attribute or set as a style in
/// the [`CssStyleDeclaration`]. This could be a plain string, or a property name-value pair.
pub trait IntoStyle<R: DomRenderer>: Send {
pub trait IntoStyle: Send {
/// The type after all async data have resolved.
type AsyncOutput: IntoStyle<R>;
type AsyncOutput: IntoStyle;
/// The view state retained between building and rebuilding.
type State;
/// An equivalent value that can be cloned.
type Cloneable: IntoStyle<R> + Clone;
type Cloneable: IntoStyle + Clone;
/// An equivalent value that can be cloned and is `'static`.
type CloneableOwned: IntoStyle<R> + Clone + 'static;
type CloneableOwned: IntoStyle + Clone + 'static;
/// Renders the style to HTML.
fn to_html(self, style: &mut String);
/// Adds interactivity as necessary, given DOM nodes that were created from HTML that has
/// either been rendered on the server, or cloned for a `<template>`.
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State;
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State;
/// Adds this style to the element during client-side rendering.
fn build(self, el: &R::Element) -> Self::State;
fn build(self, el: &crate::renderer::types::Element) -> Self::State;
/// Updates the value.
fn rebuild(self, state: &mut Self::State);
@@ -176,12 +170,9 @@ pub trait IntoStyle<R: DomRenderer>: Send {
fn resolve(self) -> impl Future<Output = Self::AsyncOutput> + Send;
}
impl<'a, R> IntoStyle<R> for &'a str
where
R: DomRenderer,
{
impl<'a> IntoStyle for &'a str {
type AsyncOutput = Self;
type State = (R::Element, &'a str);
type State = (crate::renderer::types::Element, &'a str);
type Cloneable = Self;
type CloneableOwned = Arc<str>;
@@ -190,19 +181,22 @@ where
style.push(';');
}
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
(el.clone(), self)
}
fn build(self, el: &R::Element) -> Self::State {
R::set_attribute(el, "style", self);
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
Rndr::set_attribute(el, "style", self);
(el.clone(), self)
}
fn rebuild(self, state: &mut Self::State) {
let (el, prev) = state;
if self != *prev {
R::set_attribute(el, "style", self);
Rndr::set_attribute(el, "style", self);
}
*prev = self;
}
@@ -222,12 +216,9 @@ where
}
}
impl<R> IntoStyle<R> for Arc<str>
where
R: DomRenderer,
{
impl IntoStyle for Arc<str> {
type AsyncOutput = Self;
type State = (R::Element, Arc<str>);
type State = (crate::renderer::types::Element, Arc<str>);
type Cloneable = Self;
type CloneableOwned = Self;
@@ -236,19 +227,22 @@ where
style.push(';');
}
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
(el.clone(), self)
}
fn build(self, el: &R::Element) -> Self::State {
R::set_attribute(el, "style", &self);
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
Rndr::set_attribute(el, "style", &self);
(el.clone(), self)
}
fn rebuild(self, state: &mut Self::State) {
let (el, prev) = state;
if self != *prev {
R::set_attribute(el, "style", &self);
Rndr::set_attribute(el, "style", &self);
}
*prev = self;
}
@@ -268,12 +262,9 @@ where
}
}
impl<R> IntoStyle<R> for String
where
R: DomRenderer,
{
impl IntoStyle for String {
type AsyncOutput = Self;
type State = (R::Element, String);
type State = (crate::renderer::types::Element, String);
type Cloneable = Arc<str>;
type CloneableOwned = Arc<str>;
@@ -282,19 +273,22 @@ where
style.push(';');
}
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
(el.clone(), self)
}
fn build(self, el: &R::Element) -> Self::State {
R::set_attribute(el, "style", &self);
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
Rndr::set_attribute(el, "style", &self);
(el.clone(), self)
}
fn rebuild(self, state: &mut Self::State) {
let (el, prev) = state;
if self != *prev {
R::set_attribute(el, "style", &self);
Rndr::set_attribute(el, "style", &self);
}
*prev = self;
}
@@ -314,12 +308,9 @@ where
}
}
impl<R> IntoStyle<R> for (Arc<str>, Arc<str>)
where
R: DomRenderer,
{
impl IntoStyle for (Arc<str>, Arc<str>) {
type AsyncOutput = Self;
type State = (R::CssStyleDeclaration, Arc<str>);
type State = (crate::renderer::types::CssStyleDeclaration, Arc<str>);
type Cloneable = Self;
type CloneableOwned = Self;
@@ -331,15 +322,18 @@ where
style.push(';');
}
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State {
let style = R::style(el);
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
let style = Rndr::style(el);
(style, self.1)
}
fn build(self, el: &R::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
let (name, value) = self;
let style = R::style(el);
R::set_css_property(&style, &name, &value);
let style = Rndr::style(el);
Rndr::set_css_property(&style, &name, &value);
(style, value)
}
@@ -347,7 +341,7 @@ where
let (name, value) = self;
let (style, prev) = state;
if value != *prev {
R::set_css_property(style, &name, &value);
Rndr::set_css_property(style, &name, &value);
}
*prev = value;
}
@@ -367,12 +361,9 @@ where
}
}
impl<'a, R> IntoStyle<R> for (&'a str, &'a str)
where
R: DomRenderer,
{
impl<'a> IntoStyle for (&'a str, &'a str) {
type AsyncOutput = Self;
type State = (R::CssStyleDeclaration, &'a str);
type State = (crate::renderer::types::CssStyleDeclaration, &'a str);
type Cloneable = Self;
type CloneableOwned = (Arc<str>, Arc<str>);
@@ -384,15 +375,18 @@ where
style.push(';');
}
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State {
let style = R::style(el);
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
let style = Rndr::style(el);
(style, self.1)
}
fn build(self, el: &R::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
let (name, value) = self;
let style = R::style(el);
R::set_css_property(&style, name, value);
let style = Rndr::style(el);
Rndr::set_css_property(&style, name, value);
(style, self.1)
}
@@ -400,7 +394,7 @@ where
let (name, value) = self;
let (style, prev) = state;
if value != *prev {
R::set_css_property(style, name, value);
Rndr::set_css_property(style, name, value);
}
*prev = value;
}
@@ -420,12 +414,9 @@ where
}
}
impl<'a, R> IntoStyle<R> for (&'a str, String)
where
R: DomRenderer,
{
impl<'a> IntoStyle for (&'a str, String) {
type AsyncOutput = Self;
type State = (R::CssStyleDeclaration, String);
type State = (crate::renderer::types::CssStyleDeclaration, String);
type Cloneable = (Arc<str>, Arc<str>);
type CloneableOwned = (Arc<str>, Arc<str>);
@@ -437,15 +428,18 @@ where
style.push(';');
}
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State {
let style = R::style(el);
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
let style = Rndr::style(el);
(style, self.1)
}
fn build(self, el: &R::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
let (name, value) = &self;
let style = R::style(el);
R::set_css_property(&style, name, value);
let style = Rndr::style(el);
Rndr::set_css_property(&style, name, value);
(style, self.1)
}
@@ -453,7 +447,7 @@ where
let (name, value) = self;
let (style, prev) = state;
if value != *prev {
R::set_css_property(style, name, &value);
Rndr::set_css_property(style, name, &value);
}
*prev = value;
}
@@ -474,10 +468,7 @@ where
}
#[cfg(feature = "nightly")]
impl<'a, const V: &'static str, R> IntoStyle<R> for (&'a str, Static<V>)
where
R: DomRenderer,
{
impl<'a, const V: &'static str> IntoStyle for (&'a str, Static<V>) {
type AsyncOutput = Self;
type State = ();
type Cloneable = (Arc<str>, Static<V>);
@@ -491,13 +482,16 @@ where
style.push(';');
}
fn hydrate<const FROM_SERVER: bool>(self, _el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(
self,
_el: &crate::renderer::types::Element,
) -> Self::State {
}
fn build(self, el: &R::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
let (name, _) = &self;
let style = R::style(el);
R::set_css_property(&style, name, V);
let style = Rndr::style(el);
Rndr::set_css_property(&style, name, V);
}
fn rebuild(self, _state: &mut Self::State) {}
@@ -518,10 +512,7 @@ where
}
#[cfg(feature = "nightly")]
impl<const V: &'static str, R> IntoStyle<R> for (Arc<str>, Static<V>)
where
R: DomRenderer,
{
impl<const V: &'static str> IntoStyle for (Arc<str>, Static<V>) {
type AsyncOutput = Self;
type State = ();
type Cloneable = (Arc<str>, Static<V>);
@@ -535,13 +526,16 @@ where
style.push(';');
}
fn hydrate<const FROM_SERVER: bool>(self, _el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(
self,
_el: &crate::renderer::types::Element,
) -> Self::State {
}
fn build(self, el: &R::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
let (name, _) = &self;
let style = R::style(el);
R::set_css_property(&style, name, V);
let style = Rndr::style(el);
Rndr::set_css_property(&style, name, V);
}
fn rebuild(self, _state: &mut Self::State) {}
@@ -562,11 +556,7 @@ where
}
#[cfg(feature = "nightly")]
impl<const V: &'static str, R> IntoStyle<R>
for crate::view::static_types::Static<V>
where
R: DomRenderer,
{
impl<const V: &'static str> IntoStyle for crate::view::static_types::Static<V> {
type AsyncOutput = Self;
type State = ();
type Cloneable = Self;
@@ -579,12 +569,12 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
_el: &<R>::Element,
_el: &crate::renderer::types::Element,
) -> Self::State {
}
fn build(self, el: &<R>::Element) -> Self::State {
R::set_attribute(el, "style", V);
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
Rndr::set_attribute(el, "style", V);
}
fn rebuild(self, _state: &mut Self::State) {}

View File

@@ -1,5 +1,5 @@
use crate::{
renderer::{CastFrom, Renderer},
renderer::{CastFrom, Rndr},
view::{Position, PositionState},
};
use std::{cell::RefCell, rc::Rc};
@@ -10,27 +10,29 @@ use std::{cell::RefCell, rc::Rc};
/// implements [`RenderHtml`](crate::view::RenderHtml) knows how to advance the cursor to access
/// the nodes it needs.
#[derive(Debug)]
pub struct Cursor<R: Renderer>(Rc<RefCell<R::Node>>);
pub struct Cursor(Rc<RefCell<crate::renderer::types::Node>>);
impl<R: Renderer> Clone for Cursor<R> {
impl Clone for Cursor {
fn clone(&self) -> Self {
Self(Rc::clone(&self.0))
}
}
impl<R> Cursor<R>
impl Cursor
where
R: Renderer,
R::Element: AsRef<R::Node>,
crate::renderer::types::Element: AsRef<crate::renderer::types::Node>,
{
/// Creates a new cursor starting at the root element.
pub fn new(root: R::Element) -> Self {
Self(Rc::new(RefCell::new(root.as_ref().clone())))
pub fn new(root: crate::renderer::types::Element) -> Self {
let root = <crate::renderer::types::Element as AsRef<
crate::renderer::types::Node,
>>::as_ref(&root)
.clone();
Self(Rc::new(RefCell::new(root)))
}
/// Returns the node at which the cursor is currently located.
pub fn current(&self) -> R::Node {
pub fn current(&self) -> crate::renderer::types::Node {
self.0.borrow().clone()
}
@@ -39,14 +41,14 @@ where
/// Does nothing if there is no child.
pub fn child(&self) {
//crate::log("advancing to next child of ");
//R::log_node(&self.current());
//Rndr::log_node(&self.current());
let mut inner = self.0.borrow_mut();
if let Some(node) = R::first_child(&*inner) {
if let Some(node) = Rndr::first_child(&inner) {
*inner = node;
}
//drop(inner);
//crate::log(">> which is ");
//R::log_node(&self.current());
//Rndr::log_node(&self.current());
}
/// Advances to the next sibling of the node at which the cursor is located.
@@ -54,14 +56,14 @@ where
/// Does nothing if there is no sibling.
pub fn sibling(&self) {
//crate::log("advancing to next sibling of ");
//R::log_node(&self.current());
//Rndr::log_node(&self.current());
let mut inner = self.0.borrow_mut();
if let Some(node) = R::next_sibling(&*inner) {
if let Some(node) = Rndr::next_sibling(&inner) {
*inner = node;
}
//drop(inner);
//crate::log(">> which is ");
//R::log_node(&self.current());
//Rndr::log_node(&self.current());
}
/// Moves to the parent of the node at which the cursor is located.
@@ -69,20 +71,23 @@ where
/// Does nothing if there is no parent.
pub fn parent(&self) {
let mut inner = self.0.borrow_mut();
if let Some(node) = R::get_parent(&*inner) {
if let Some(node) = Rndr::get_parent(&inner) {
*inner = node;
}
}
/// Sets the cursor to some node.
pub fn set(&self, node: R::Node) {
pub fn set(&self, node: crate::renderer::types::Node) {
*self.0.borrow_mut() = node;
}
/// Advances to the next placeholder node.
pub fn next_placeholder(&self, position: &PositionState) -> R::Placeholder {
pub fn next_placeholder(
&self,
position: &PositionState,
) -> crate::renderer::types::Placeholder {
//crate::dom::log("looking for placeholder after");
//R::log_node(&self.current());
//Rndr::log_node(&self.current());
if position.get() == Position::FirstChild {
self.child();
} else {
@@ -90,12 +95,12 @@ where
}
let marker = self.current();
position.set(Position::NextChild);
R::Placeholder::cast_from(marker)
crate::renderer::types::Placeholder::cast_from(marker)
.expect("could not convert current node into marker node")
/*let marker2 = marker.clone();
R::Placeholder::cast_from(marker).unwrap_or_else(|| {
Rndr::Placeholder::cast_from(marker).unwrap_or_else(|| {
crate::dom::log("expecting to find a marker. instead, found");
R::log_node(&marker2);
Rndr::log_node(&marker2);
panic!("oops.");
})*/
}

View File

@@ -5,7 +5,7 @@
#![allow(incomplete_features)] // yolo
#![cfg_attr(feature = "nightly", feature(unsized_const_params))]
#![deny(missing_docs)]
//#![deny(missing_docs)]
/// Commonly-used traits.
pub mod prelude {

View File

@@ -1,16 +1,12 @@
use crate::{
html::{
attribute::{Attr, Attribute, AttributeValue},
element::{
CreateElement, ElementType, ElementWithChildren, HtmlElement,
},
element::{ElementType, ElementWithChildren, HtmlElement},
},
renderer::{dom::Dom, Renderer},
view::Render,
};
use next_tuple::NextTuple;
use once_cell::unsync::Lazy;
use std::{fmt::Debug, marker::PhantomData};
use std::fmt::Debug;
macro_rules! mathml_global {
($tag:ty, $attr:ty) => {
@@ -18,18 +14,18 @@ macro_rules! mathml_global {
/// A MathML attribute.
pub fn $attr<V>(self, value: V) -> HtmlElement <
[<$tag:camel>],
<At as NextTuple>::Output<Attr<$crate::html::attribute::[<$attr:camel>], V, Rndr>>,
Ch, Rndr
<At as NextTuple>::Output<Attr<$crate::html::attribute::[<$attr:camel>], V>>,
Ch
>
where
V: AttributeValue<Rndr>,
V: AttributeValue,
At: NextTuple,
<At as NextTuple>::Output<Attr<$crate::html::attribute::[<$attr:camel>], V, Rndr>>: Attribute<Rndr>,
<At as NextTuple>::Output<Attr<$crate::html::attribute::[<$attr:camel>], V>>: Attribute,
{
let HtmlElement { tag, rndr, children, attributes } = self;
let HtmlElement { tag, children, attributes } = self;
HtmlElement {
tag,
rndr,
children,
attributes: attributes.next_tuple($crate::html::attribute::$attr(value)),
}
@@ -45,15 +41,15 @@ macro_rules! mathml_elements {
// `tag()` function
/// A MathML element.
#[track_caller]
pub fn $tag<Rndr>() -> HtmlElement<[<$tag:camel>], (), (), Rndr>
pub fn $tag() -> HtmlElement<[<$tag:camel>], (), ()>
where
Rndr: Renderer
{
HtmlElement {
tag: [<$tag:camel>],
attributes: (),
children: (),
rndr: PhantomData,
}
}
@@ -61,11 +57,11 @@ macro_rules! mathml_elements {
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct [<$tag:camel>];
impl<At, Ch, Rndr> HtmlElement<[<$tag:camel>], At, Ch, Rndr>
impl<At, Ch> HtmlElement<[<$tag:camel>], At, Ch>
where
At: Attribute<Rndr>,
Ch: Render<Rndr>,
Rndr: Renderer,
At: Attribute,
Ch: Render,
{
mathml_global!($tag, displaystyle);
mathml_global!($tag, href);
@@ -80,18 +76,18 @@ macro_rules! mathml_elements {
/// A MathML attribute.
pub fn $attr<V>(self, value: V) -> HtmlElement <
[<$tag:camel>],
<At as NextTuple>::Output<Attr<$crate::html::attribute::[<$attr:camel>], V, Rndr>>,
Ch, Rndr
<At as NextTuple>::Output<Attr<$crate::html::attribute::[<$attr:camel>], V>>,
Ch
>
where
V: AttributeValue<Rndr>,
V: AttributeValue,
At: NextTuple,
<At as NextTuple>::Output<Attr<$crate::html::attribute::[<$attr:camel>], V, Rndr>>: Attribute<Rndr>,
<At as NextTuple>::Output<Attr<$crate::html::attribute::[<$attr:camel>], V>>: Attribute,
{
let HtmlElement { tag, rndr, children, attributes } = self;
let HtmlElement { tag, children, attributes } = self;
HtmlElement {
tag,
rndr,
children,
attributes: attributes.next_tuple($crate::html::attribute::$attr(value)),
}
@@ -113,22 +109,6 @@ macro_rules! mathml_elements {
}
impl ElementWithChildren for [<$tag:camel>] {}
impl CreateElement<Dom> for [<$tag:camel>] {
fn create_element(&self) -> <Dom as Renderer>::Element {
use wasm_bindgen::JsCast;
thread_local! {
static ELEMENT: Lazy<<Dom as Renderer>::Element> = Lazy::new(|| {
crate::dom::document().create_element_ns(
Some(wasm_bindgen::intern("http://www.w3.org/1998/Math/MathML")),
stringify!($tag)
).unwrap()
});
}
ELEMENT.with(|e| e.clone_node()).unwrap().unchecked_into()
}
}
)*
}
}

View File

@@ -3,29 +3,29 @@ use crate::{
hydration::Cursor,
no_attrs,
prelude::{Mountable, Render, RenderHtml},
renderer::{DomRenderer, Renderer},
renderer::Rndr,
view::{strings::StrState, Position, PositionState, ToTemplate},
};
use oco_ref::Oco;
/// Retained view state for [`Oco`].
pub struct OcoStrState<R: Renderer> {
node: R::Text,
pub struct OcoStrState {
node: crate::renderer::types::Text,
str: Oco<'static, str>,
}
impl<R: Renderer> Render<R> for Oco<'static, str> {
type State = OcoStrState<R>;
impl Render for Oco<'static, str> {
type State = OcoStrState;
fn build(self) -> Self::State {
let node = R::create_text_node(&self);
let node = Rndr::create_text_node(&self);
OcoStrState { node, str: self }
}
fn rebuild(self, state: &mut Self::State) {
let OcoStrState { node, str } = state;
if &self == str {
R::set_text(node, &self);
Rndr::set_text(node, &self);
*str = self;
}
}
@@ -33,10 +33,7 @@ impl<R: Renderer> Render<R> for Oco<'static, str> {
no_attrs!(Oco<'static, str>);
impl<R> RenderHtml<R> for Oco<'static, str>
where
R: Renderer,
{
impl RenderHtml for Oco<'static, str> {
type AsyncOutput = Self;
const MIN_LENGTH: usize = 0;
@@ -54,7 +51,7 @@ where
escape: bool,
mark_branches: bool,
) {
<&str as RenderHtml<R>>::to_html_with_buf(
<&str as RenderHtml>::to_html_with_buf(
&self,
buf,
position,
@@ -65,13 +62,13 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
let this: &str = self.as_ref();
let StrState { node, .. } = <&str as RenderHtml<R>>::hydrate::<
FROM_SERVER,
>(this, cursor, position);
let StrState { node, .. } = <&str as RenderHtml>::hydrate::<FROM_SERVER>(
this, cursor, position,
);
OcoStrState { node, str: self }
}
}
@@ -92,30 +89,27 @@ impl ToTemplate for Oco<'static, str> {
}
}
impl<R: Renderer> Mountable<R> for OcoStrState<R> {
impl Mountable for OcoStrState {
fn unmount(&mut self) {
self.node.unmount()
}
fn mount(
&mut self,
parent: &<R as Renderer>::Element,
marker: Option<&<R as Renderer>::Node>,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
R::insert_node(parent, self.node.as_ref(), marker);
Rndr::insert_node(parent, self.node.as_ref(), marker);
}
fn insert_before_this(&self, child: &mut dyn Mountable<R>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
self.node.insert_before_this(child)
}
}
impl<R> AttributeValue<R> for Oco<'static, str>
where
R: Renderer,
{
impl AttributeValue for Oco<'static, str> {
type AsyncOutput = Self;
type State = (R::Element, Oco<'static, str>);
type State = (crate::renderer::types::Element, Oco<'static, str>);
type Cloneable = Self;
type CloneableOwned = Self;
@@ -124,7 +118,7 @@ where
}
fn to_html(self, key: &str, buf: &mut String) {
<&str as AttributeValue<R>>::to_html(self.as_str(), key, buf);
<&str as AttributeValue>::to_html(self.as_str(), key, buf);
}
fn to_template(_key: &str, _buf: &mut String) {}
@@ -132,9 +126,9 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
key: &str,
el: &R::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
let (el, _) = <&str as AttributeValue<R>>::hydrate::<FROM_SERVER>(
let (el, _) = <&str as AttributeValue>::hydrate::<FROM_SERVER>(
self.as_str(),
key,
el,
@@ -142,15 +136,19 @@ where
(el, self)
}
fn build(self, el: &R::Element, key: &str) -> Self::State {
R::set_attribute(el, key, &self);
fn build(
self,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
Rndr::set_attribute(el, key, &self);
(el.clone(), self)
}
fn rebuild(self, key: &str, state: &mut Self::State) {
let (el, prev_value) = state;
if self != *prev_value {
R::set_attribute(el, key, &self);
Rndr::set_attribute(el, key, &self);
}
*prev_value = self;
}
@@ -174,12 +172,9 @@ where
}
}
impl<R> IntoClass<R> for Oco<'static, str>
where
R: DomRenderer,
{
impl IntoClass for Oco<'static, str> {
type AsyncOutput = Self;
type State = (R::Element, Self);
type State = (crate::renderer::types::Element, Self);
type Cloneable = Self;
type CloneableOwned = Self;
@@ -188,25 +183,28 @@ where
}
fn to_html(self, class: &mut String) {
IntoClass::<R>::to_html(self.as_str(), class);
IntoClass::to_html(self.as_str(), class);
}
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
if !FROM_SERVER {
R::set_attribute(el, "class", &self);
Rndr::set_attribute(el, "class", &self);
}
(el.clone(), self)
}
fn build(self, el: &R::Element) -> Self::State {
R::set_attribute(el, "class", &self);
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
Rndr::set_attribute(el, "class", &self);
(el.clone(), self)
}
fn rebuild(self, state: &mut Self::State) {
let (el, prev) = state;
if self != *prev {
R::set_attribute(el, "class", &self);
Rndr::set_attribute(el, "class", &self);
}
*prev = self;
}

View File

@@ -1,5 +1,5 @@
use super::{ReactiveFunction, SharedReactiveFunction, Suspend};
use crate::{html::class::IntoClass, renderer::DomRenderer};
use crate::{html::class::IntoClass, renderer::Rndr};
use any_spawner::Executor;
use futures::FutureExt;
use reactive_graph::{effect::RenderEffect, signal::guards::ReadGuard};
@@ -8,12 +8,11 @@ use std::{
sync::Arc,
};
impl<F, C, R> IntoClass<R> for F
impl<F, C> IntoClass for F
where
F: ReactiveFunction<Output = C>,
C: IntoClass<R> + 'static,
C: IntoClass + 'static,
C::State: 'static,
R: DomRenderer,
{
type AsyncOutput = C::AsyncOutput;
type State = RenderEffect<C::State>;
@@ -31,7 +30,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
mut self,
el: &R::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
// TODO FROM_SERVER vs template
let el = el.clone();
@@ -46,7 +45,7 @@ where
})
}
fn build(mut self, el: &R::Element) -> Self::State {
fn build(mut self, el: &crate::renderer::types::Element) -> Self::State {
let el = el.to_owned();
RenderEffect::new(move |prev| {
let value = self.invoke();
@@ -92,14 +91,13 @@ where
}
}
impl<F, T, R> IntoClass<R> for (&'static str, F)
impl<F, T> IntoClass for (&'static str, F)
where
F: ReactiveFunction<Output = T>,
T: Borrow<bool> + Send + 'static,
R: DomRenderer,
{
type AsyncOutput = (&'static str, bool);
type State = RenderEffect<(R::ClassList, bool)>;
type State = RenderEffect<(crate::renderer::types::ClassList, bool)>;
type Cloneable = (&'static str, SharedReactiveFunction<T>);
type CloneableOwned = (&'static str, SharedReactiveFunction<T>);
@@ -111,56 +109,63 @@ where
let (name, mut f) = self;
let include = *f.invoke().borrow();
if include {
<&str as IntoClass<R>>::to_html(name, class);
<&str as IntoClass>::to_html(name, class);
}
}
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
// TODO FROM_SERVER vs template
let (name, mut f) = self;
let class_list = R::class_list(el);
let name = R::intern(name);
let class_list = Rndr::class_list(el);
let name = Rndr::intern(name);
RenderEffect::new(move |prev: Option<(R::ClassList, bool)>| {
let include = *f.invoke().borrow();
if let Some((class_list, prev)) = prev {
if include {
if !prev {
R::add_class(&class_list, name);
}
} else if prev {
R::remove_class(&class_list, name);
}
}
(class_list.clone(), include)
})
}
fn build(self, el: &R::Element) -> Self::State {
let (name, mut f) = self;
let class_list = R::class_list(el);
let name = R::intern(name);
RenderEffect::new(move |prev: Option<(R::ClassList, bool)>| {
let include = *f.invoke().borrow();
match prev {
Some((class_list, prev)) => {
RenderEffect::new(
move |prev: Option<(crate::renderer::types::ClassList, bool)>| {
let include = *f.invoke().borrow();
if let Some((class_list, prev)) = prev {
if include {
if !prev {
R::add_class(&class_list, name);
Rndr::add_class(&class_list, name);
}
} else if prev {
R::remove_class(&class_list, name);
Rndr::remove_class(&class_list, name);
}
}
None => {
if include {
R::add_class(&class_list, name);
(class_list.clone(), include)
},
)
}
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
let (name, mut f) = self;
let class_list = Rndr::class_list(el);
let name = Rndr::intern(name);
RenderEffect::new(
move |prev: Option<(crate::renderer::types::ClassList, bool)>| {
let include = *f.invoke().borrow();
match prev {
Some((class_list, prev)) => {
if include {
if !prev {
Rndr::add_class(&class_list, name);
}
} else if prev {
Rndr::remove_class(&class_list, name);
}
}
None => {
if include {
Rndr::add_class(&class_list, name);
}
}
}
}
(class_list.clone(), include)
})
(class_list.clone(), include)
},
)
}
fn rebuild(self, state: &mut Self::State) {
@@ -173,10 +178,10 @@ where
Some((class_list, prev)) => {
if include {
if !prev {
R::add_class(&class_list, name);
Rndr::add_class(&class_list, name);
}
} else if prev {
R::remove_class(&class_list, name);
Rndr::remove_class(&class_list, name);
}
(class_list.clone(), include)
}
@@ -208,14 +213,14 @@ where
// TODO this needs a non-reactive form too to be restored
/*
impl<F, T, R> IntoClass<R> for (Vec<Cow<'static, str>>, F)
impl<F, T> IntoClass for (Vec<Cow<'static, str>>, F)
where
F: ReactiveFunction<Output = T>,
T: Borrow<bool> + Send + 'static,
R: DomRenderer,
{
type AsyncOutput = (Vec<Cow<'static, str>>, bool);
type State = RenderEffect<(R::ClassList, bool)>;
type State = RenderEffect<(crate::renderer::types::ClassList, bool)>;
type Cloneable = (Vec<Cow<'static, str>>, SharedReactiveFunction<T>);
type CloneableOwned = (Vec<Cow<'static, str>>, SharedReactiveFunction<T>);
@@ -228,29 +233,29 @@ where
let include = *f.invoke().borrow();
if include {
for name in names {
<&str as IntoClass<R>>::to_html(&name, class);
<&str as IntoClass>::to_html(&name, class);
}
}
}
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(self, el: &crate::renderer::types::Element) -> Self::State {
// TODO FROM_SERVER vs template
let (names, mut f) = self;
let class_list = R::class_list(el);
let class_list = Rndr::class_list(el);
RenderEffect::new(move |prev: Option<(R::ClassList, bool)>| {
RenderEffect::new(move |prev: Option<(crate::renderer::types::ClassList, bool)>| {
let include = *f.invoke().borrow();
if let Some((class_list, prev)) = prev {
if include {
if !prev {
for name in &names {
// TODO multi-class optimizations here
R::add_class(&class_list, name);
Rndr::add_class(&class_list, name);
}
}
} else if prev {
for name in &names {
R::remove_class(&class_list, name);
Rndr::remove_class(&class_list, name);
}
}
}
@@ -258,30 +263,30 @@ where
})
}
fn build(self, el: &R::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
let (names, mut f) = self;
let class_list = R::class_list(el);
let class_list = Rndr::class_list(el);
RenderEffect::new(move |prev: Option<(R::ClassList, bool)>| {
RenderEffect::new(move |prev: Option<(crate::renderer::types::ClassList, bool)>| {
let include = *f.invoke().borrow();
match prev {
Some((class_list, prev)) => {
if include {
for name in &names {
if !prev {
R::add_class(&class_list, name);
Rndr::add_class(&class_list, name);
}
}
} else if prev {
for name in &names {
R::remove_class(&class_list, name);
Rndr::remove_class(&class_list, name);
}
}
}
None => {
if include {
for name in &names {
R::add_class(&class_list, name);
Rndr::add_class(&class_list, name);
}
}
}
@@ -295,19 +300,19 @@ where
let prev_value = state.take_value();
*state = RenderEffect::new_with_value(
move |prev: Option<(R::ClassList, bool)>| {
move |prev: Option<(crate::renderer::types::ClassList, bool)>| {
let include = *f.invoke().borrow();
match prev {
Some((class_list, prev)) => {
if include {
for name in &names {
if !prev {
R::add_class(&class_list, name);
Rndr::add_class(&class_list, name);
}
}
} else if prev {
for name in &names {
R::remove_class(&class_list, name);
Rndr::remove_class(&class_list, name);
}
}
(class_list.clone(), include)
@@ -339,13 +344,12 @@ where
}
*/
impl<G, R> IntoClass<R> for ReadGuard<String, G>
impl<G> IntoClass for ReadGuard<String, G>
where
G: Deref<Target = String> + Send,
R: DomRenderer,
{
type AsyncOutput = Self;
type State = <String as IntoClass<R>>::State;
type State = <String as IntoClass>::State;
type Cloneable = Arc<str>;
type CloneableOwned = Arc<str>;
@@ -354,25 +358,25 @@ where
}
fn to_html(self, class: &mut String) {
<&str as IntoClass<R>>::to_html(self.deref().as_str(), class);
<&str as IntoClass>::to_html(self.deref().as_str(), class);
}
fn hydrate<const FROM_SERVER: bool>(
self,
el: &<R>::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
<String as IntoClass<R>>::hydrate::<FROM_SERVER>(
<String as IntoClass>::hydrate::<FROM_SERVER>(
self.deref().to_owned(),
el,
)
}
fn build(self, el: &<R>::Element) -> Self::State {
<String as IntoClass<R>>::build(self.deref().to_owned(), el)
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
<String as IntoClass>::build(self.deref().to_owned(), el)
}
fn rebuild(self, state: &mut Self::State) {
<String as IntoClass<R>>::rebuild(self.deref().to_owned(), state)
<String as IntoClass>::rebuild(self.deref().to_owned(), state)
}
fn into_cloneable(self) -> Self::Cloneable {
@@ -390,13 +394,12 @@ where
}
}
impl<G, R> IntoClass<R> for (&'static str, ReadGuard<bool, G>)
impl<G> IntoClass for (&'static str, ReadGuard<bool, G>)
where
G: Deref<Target = bool> + Send,
R: DomRenderer,
{
type AsyncOutput = Self;
type State = <(&'static str, bool) as IntoClass<R>>::State;
type State = <(&'static str, bool) as IntoClass>::State;
type Cloneable = (&'static str, bool);
type CloneableOwned = (&'static str, bool);
@@ -405,7 +408,7 @@ where
}
fn to_html(self, class: &mut String) {
<(&'static str, bool) as IntoClass<R>>::to_html(
<(&'static str, bool) as IntoClass>::to_html(
(self.0, *self.1.deref()),
class,
);
@@ -413,23 +416,23 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
el: &<R>::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
<(&'static str, bool) as IntoClass<R>>::hydrate::<FROM_SERVER>(
<(&'static str, bool) as IntoClass>::hydrate::<FROM_SERVER>(
(self.0, *self.1.deref()),
el,
)
}
fn build(self, el: &<R>::Element) -> Self::State {
<(&'static str, bool) as IntoClass<R>>::build(
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
<(&'static str, bool) as IntoClass>::build(
(self.0, *self.1.deref()),
el,
)
}
fn rebuild(self, state: &mut Self::State) {
<(&'static str, bool) as IntoClass<R>>::rebuild(
<(&'static str, bool) as IntoClass>::rebuild(
(self.0, *self.1.deref()),
state,
)
@@ -454,14 +457,13 @@ where
mod stable {
macro_rules! class_signal_arena {
($sig:ident) => {
impl<C, R, S> IntoClass<R> for $sig<C, S>
impl<C, S> IntoClass for $sig<C, S>
where
$sig<C, S>: Get<Value = C>,
S: Send + Sync + 'static,
S: Storage<C> + Storage<Option<C>>,
C: IntoClass<R> + Send + Sync + Clone + 'static,
C: IntoClass + Send + Sync + Clone + 'static,
C::State: 'static,
R: DomRenderer,
{
type AsyncOutput = Self;
type State = RenderEffect<C::State>;
@@ -479,12 +481,15 @@ mod stable {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &R::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
(move || self.get()).hydrate::<FROM_SERVER>(el)
}
fn build(self, el: &R::Element) -> Self::State {
fn build(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
(move || self.get()).build(el)
}
@@ -507,15 +512,15 @@ mod stable {
}
}
impl<R, S> IntoClass<R> for (&'static str, $sig<bool, S>)
impl<S> IntoClass for (&'static str, $sig<bool, S>)
where
$sig<bool, S>: Get<Value = bool>,
S: Send + 'static,
S: Storage<bool>,
R: DomRenderer,
{
type AsyncOutput = Self;
type State = RenderEffect<(R::ClassList, bool)>;
type State =
RenderEffect<(crate::renderer::types::ClassList, bool)>;
type Cloneable = Self;
type CloneableOwned = Self;
@@ -527,29 +532,29 @@ mod stable {
let (name, f) = self;
let include = f.get();
if include {
<&str as IntoClass<R>>::to_html(name, class);
<&str as IntoClass>::to_html(name, class);
}
}
fn hydrate<const FROM_SERVER: bool>(
self,
el: &R::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
IntoClass::<R>::hydrate::<FROM_SERVER>(
IntoClass::hydrate::<FROM_SERVER>(
(self.0, move || self.1.get()),
el,
)
}
fn build(self, el: &R::Element) -> Self::State {
IntoClass::<R>::build((self.0, move || self.1.get()), el)
fn build(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
IntoClass::build((self.0, move || self.1.get()), el)
}
fn rebuild(self, state: &mut Self::State) {
IntoClass::<R>::rebuild(
(self.0, move || self.1.get()),
state,
)
IntoClass::rebuild((self.0, move || self.1.get()), state)
}
fn into_cloneable(self) -> Self::Cloneable {
@@ -571,12 +576,11 @@ mod stable {
macro_rules! class_signal {
($sig:ident) => {
impl<C, R> IntoClass<R> for $sig<C>
impl<C> IntoClass for $sig<C>
where
$sig<C>: Get<Value = C>,
C: IntoClass<R> + Send + Sync + Clone + 'static,
C: IntoClass + Send + Sync + Clone + 'static,
C::State: 'static,
R: DomRenderer,
{
type AsyncOutput = Self;
type State = RenderEffect<C::State>;
@@ -594,12 +598,15 @@ mod stable {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &R::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
(move || self.get()).hydrate::<FROM_SERVER>(el)
}
fn build(self, el: &R::Element) -> Self::State {
fn build(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
(move || self.get()).build(el)
}
@@ -622,13 +629,13 @@ mod stable {
}
}
impl<R> IntoClass<R> for (&'static str, $sig<bool>)
impl IntoClass for (&'static str, $sig<bool>)
where
$sig<bool>: Get<Value = bool>,
R: DomRenderer,
{
type AsyncOutput = Self;
type State = RenderEffect<(R::ClassList, bool)>;
type State =
RenderEffect<(crate::renderer::types::ClassList, bool)>;
type Cloneable = Self;
type CloneableOwned = Self;
@@ -640,29 +647,29 @@ mod stable {
let (name, f) = self;
let include = f.get();
if include {
<&str as IntoClass<R>>::to_html(name, class);
<&str as IntoClass>::to_html(name, class);
}
}
fn hydrate<const FROM_SERVER: bool>(
self,
el: &R::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
IntoClass::<R>::hydrate::<FROM_SERVER>(
IntoClass::hydrate::<FROM_SERVER>(
(self.0, move || self.1.get()),
el,
)
}
fn build(self, el: &R::Element) -> Self::State {
IntoClass::<R>::build((self.0, move || self.1.get()), el)
fn build(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
IntoClass::build((self.0, move || self.1.get()), el)
}
fn rebuild(self, state: &mut Self::State) {
IntoClass::<R>::rebuild(
(self.0, move || self.1.get()),
state,
)
IntoClass::rebuild((self.0, move || self.1.get()), state)
}
fn into_cloneable(self) -> Self::Cloneable {
@@ -683,7 +690,7 @@ mod stable {
}
use super::RenderEffect;
use crate::{html::class::IntoClass, renderer::DomRenderer};
use crate::html::class::IntoClass;
use reactive_graph::{
computed::{ArcMemo, Memo},
owner::Storage,
@@ -703,14 +710,13 @@ mod stable {
class_signal!(ArcSignal);
}
impl<Fut, Rndr> IntoClass<Rndr> for Suspend<Fut>
impl<Fut> IntoClass for Suspend<Fut>
where
Fut: Clone + Future + Send + 'static,
Fut::Output: IntoClass<Rndr>,
Rndr: DomRenderer + 'static,
Fut::Output: IntoClass,
{
type AsyncOutput = Fut::Output;
type State = Rc<RefCell<Option<<Fut::Output as IntoClass<Rndr>>::State>>>;
type State = Rc<RefCell<Option<<Fut::Output as IntoClass>::State>>>;
type Cloneable = Self;
type CloneableOwned = Self;
@@ -728,7 +734,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
el: &<Rndr>::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
let el = el.to_owned();
let state = Rc::new(RefCell::new(None));
@@ -743,7 +749,7 @@ where
state
}
fn build(self, el: &<Rndr>::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
let el = el.to_owned();
let state = Rc::new(RefCell::new(None));
Executor::spawn_local({

View File

@@ -1,16 +1,12 @@
use super::{ReactiveFunction, SharedReactiveFunction};
use crate::{
html::element::InnerHtmlValue,
renderer::{DomRenderer, Renderer},
};
use crate::html::element::InnerHtmlValue;
use reactive_graph::effect::RenderEffect;
impl<F, V, R> InnerHtmlValue<R> for F
impl<F, V> InnerHtmlValue for F
where
F: ReactiveFunction<Output = V>,
V: InnerHtmlValue<R> + 'static,
V: InnerHtmlValue + 'static,
V::State: 'static,
R: DomRenderer,
{
type AsyncOutput = V::AsyncOutput;
type State = RenderEffect<V::State>;
@@ -30,7 +26,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
mut self,
el: &<R as Renderer>::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
let el = el.to_owned();
RenderEffect::new(move |prev| {
@@ -44,7 +40,7 @@ where
})
}
fn build(mut self, el: &<R as Renderer>::Element) -> Self::State {
fn build(mut self, el: &crate::renderer::types::Element) -> Self::State {
let el = el.to_owned();
RenderEffect::new(move |prev| {
let value = self.invoke();
@@ -92,10 +88,7 @@ where
#[cfg(not(feature = "nightly"))]
mod stable {
use crate::{
html::element::InnerHtmlValue,
renderer::{DomRenderer, Renderer},
};
use crate::html::element::InnerHtmlValue;
use reactive_graph::{
computed::{ArcMemo, Memo},
effect::RenderEffect,
@@ -107,12 +100,11 @@ mod stable {
macro_rules! inner_html_signal {
($sig:ident) => {
impl<V, R> InnerHtmlValue<R> for $sig<V>
impl<V> InnerHtmlValue for $sig<V>
where
$sig<V>: Get<Value = V>,
V: InnerHtmlValue<R> + Send + Sync + Clone + 'static,
V: InnerHtmlValue + Send + Sync + Clone + 'static,
V::State: 'static,
R: DomRenderer,
{
type AsyncOutput = Self;
type State = RenderEffect<V::State>;
@@ -132,12 +124,15 @@ mod stable {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &<R as Renderer>::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
(move || self.get()).hydrate::<FROM_SERVER>(el)
}
fn build(self, el: &<R as Renderer>::Element) -> Self::State {
fn build(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
(move || self.get()).build(el)
}
@@ -164,14 +159,13 @@ mod stable {
macro_rules! inner_html_signal_arena {
($sig:ident) => {
impl<V, R, S> InnerHtmlValue<R> for $sig<V, S>
impl<V, S> InnerHtmlValue for $sig<V, S>
where
$sig<V, S>: Get<Value = V>,
S: Send + Sync + 'static,
S: Storage<V>,
V: InnerHtmlValue<R> + Send + Sync + Clone + 'static,
V: InnerHtmlValue + Send + Sync + Clone + 'static,
V::State: 'static,
R: DomRenderer,
{
type AsyncOutput = Self;
type State = RenderEffect<V::State>;
@@ -191,12 +185,15 @@ mod stable {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &<R as Renderer>::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
(move || self.get()).hydrate::<FROM_SERVER>(el)
}
fn build(self, el: &<R as Renderer>::Element) -> Self::State {
fn build(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
(move || self.get()).build(el)
}

View File

@@ -1,7 +1,7 @@
use crate::{
html::attribute::{Attribute, AttributeValue},
hydration::Cursor,
renderer::Renderer,
renderer::Rndr,
ssr::StreamBuilder,
view::{
add_attr::AddAnyAttr, Mountable, Position, PositionState, Render,
@@ -47,12 +47,11 @@ where
}
}
impl<F, V, R> Render<R> for F
impl<F, V> Render for F
where
F: ReactiveFunction<Output = V>,
V: Render<R>,
V: Render,
V::State: 'static,
R: Renderer,
{
type State = RenderEffectState<V::State>;
@@ -88,10 +87,9 @@ impl<T> From<RenderEffect<T>> for RenderEffectState<T> {
}
}
impl<T, R> Mountable<R> for RenderEffectState<T>
impl<T> Mountable for RenderEffectState<T>
where
T: Mountable<R>,
R: Renderer,
T: Mountable,
{
fn unmount(&mut self) {
if let Some(ref mut inner) = self.0 {
@@ -99,13 +97,17 @@ where
}
}
fn mount(&mut self, parent: &R::Element, marker: Option<&R::Node>) {
fn mount(
&mut self,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
if let Some(ref mut inner) = self.0 {
inner.mount(parent, marker);
}
}
fn insert_before_this(&self, child: &mut dyn Mountable<R>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
if let Some(inner) = &self.0 {
inner.insert_before_this(child)
} else {
@@ -114,13 +116,11 @@ where
}
}
impl<F, V, R> RenderHtml<R> for F
impl<F, V> RenderHtml for F
where
F: ReactiveFunction<Output = V>,
V: RenderHtml<R> + 'static,
V: RenderHtml + 'static,
V::State: 'static,
R: Renderer + 'static,
{
type AsyncOutput = V::AsyncOutput;
@@ -169,7 +169,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
mut self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
let cursor = cursor.clone();
@@ -187,31 +187,29 @@ where
}
}
impl<F, V, R> AddAnyAttr<R> for F
impl<F, V> AddAnyAttr for F
where
F: ReactiveFunction<Output = V>,
V: RenderHtml<R> + 'static,
R: Renderer + 'static,
V: RenderHtml + 'static,
{
type Output<SomeNewAttr: Attribute<R>> =
type Output<SomeNewAttr: Attribute> =
Box<dyn FnMut() -> V::Output<SomeNewAttr::CloneableOwned> + Send>;
fn add_any_attr<NewAttr: Attribute<R>>(
fn add_any_attr<NewAttr: Attribute>(
mut self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<R>,
Self::Output<NewAttr>: RenderHtml,
{
let attr = attr.into_cloneable_owned();
Box::new(move || self.invoke().add_any_attr(attr.clone()))
}
}
impl<M, R> Mountable<R> for RenderEffect<M>
impl<M> Mountable for RenderEffect<M>
where
M: Mountable<R> + 'static,
R: Renderer,
M: Mountable + 'static,
{
fn unmount(&mut self) {
self.with_value_mut(|state| state.unmount());
@@ -219,24 +217,23 @@ where
fn mount(
&mut self,
parent: &<R as Renderer>::Element,
marker: Option<&<R as Renderer>::Node>,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
self.with_value_mut(|state| {
state.mount(parent, marker);
});
}
fn insert_before_this(&self, child: &mut dyn Mountable<R>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
self.with_value_mut(|value| value.insert_before_this(child))
.unwrap_or(false)
}
}
impl<M, E, R> Mountable<R> for Result<M, E>
impl<M, E> Mountable for Result<M, E>
where
M: Mountable<R>,
R: Renderer,
M: Mountable,
{
fn unmount(&mut self) {
if let Ok(ref mut inner) = self {
@@ -246,15 +243,15 @@ where
fn mount(
&mut self,
parent: &<R as Renderer>::Element,
marker: Option<&<R as Renderer>::Node>,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
if let Ok(ref mut inner) = self {
inner.mount(parent, marker);
}
}
fn insert_before_this(&self, child: &mut dyn Mountable<R>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
if let Ok(inner) = &self {
inner.insert_before_this(child)
} else {
@@ -264,12 +261,11 @@ where
}
// Dynamic attributes
impl<F, V, R> AttributeValue<R> for F
impl<F, V> AttributeValue for F
where
F: ReactiveFunction<Output = V>,
V: AttributeValue<R> + 'static,
V: AttributeValue + 'static,
V::State: 'static,
R: Renderer,
{
type AsyncOutput = V::AsyncOutput;
type State = RenderEffect<V::State>;
@@ -290,9 +286,9 @@ where
fn hydrate<const FROM_SERVER: bool>(
mut self,
key: &str,
el: &<R as Renderer>::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
let key = R::intern(key);
let key = Rndr::intern(key);
let key = key.to_owned();
let el = el.to_owned();
@@ -309,10 +305,10 @@ where
fn build(
mut self,
el: &<R as Renderer>::Element,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
let key = R::intern(key);
let key = Rndr::intern(key);
let key = key.to_owned();
let el = el.to_owned();
@@ -328,7 +324,7 @@ where
}
fn rebuild(mut self, key: &str, state: &mut Self::State) {
let key = R::intern(key);
let key = Rndr::intern(key);
let key = key.to_owned();
let prev_value = state.take_value();
@@ -363,12 +359,11 @@ where
}
}
impl<Fut, V, R> AttributeValue<R> for Suspend<Fut>
impl<Fut, V> AttributeValue for Suspend<Fut>
where
Fut: Future<Output = V> + Send + 'static,
V: AttributeValue<R> + 'static,
V: AttributeValue + 'static,
V::State: 'static,
R: Renderer,
{
type State = Rc<RefCell<Option<V::State>>>;
type AsyncOutput = V;
@@ -391,7 +386,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
key: &str,
el: &<R as Renderer>::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
let key = key.to_owned();
let el = el.to_owned();
@@ -407,7 +402,11 @@ where
state
}
fn build(self, el: &<R as Renderer>::Element, key: &str) -> Self::State {
fn build(
self,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
let key = key.to_owned();
let el = el.to_owned();
let state = Rc::new(RefCell::new(None));
@@ -502,11 +501,10 @@ mod stable {
use crate::{
html::attribute::{Attribute, AttributeValue},
hydration::Cursor,
renderer::Renderer,
ssr::StreamBuilder,
view::{
add_attr::AddAnyAttr, Mountable, Position, PositionState, Render,
RenderHtml, WrappedView,
RenderHtml,
},
};
use reactive_graph::{
@@ -520,13 +518,11 @@ mod stable {
macro_rules! signal_impl {
($sig:ident $dry_resolve:literal) => {
impl<V, R> Render<R> for $sig<V>
impl<V> Render for $sig<V>
where
$sig<V>: Get<Value = V>,
V: Render<R> + Clone + Send + Sync + 'static,
V: Render + Clone + Send + Sync + 'static,
V::State: 'static,
R: Renderer,
{
type State = RenderEffectState<V::State>;
@@ -544,34 +540,30 @@ mod stable {
}
}
impl<V, R> AddAnyAttr<R> for $sig<V>
impl<V> AddAnyAttr for $sig<V>
where
$sig<V>: Get<Value = V>,
V: RenderHtml<R> + Clone + Send + Sync + 'static,
V: RenderHtml + Clone + Send + Sync + 'static,
V::State: 'static,
R: Renderer + 'static,
{
// WrappedView is necessary here for compiler reasons I don't completely understand
type Output<SomeNewAttr: Attribute<R>> = WrappedView<$sig<V>>;
type Output<SomeNewAttr: Attribute> = Self;
fn add_any_attr<NewAttr: Attribute<R>>(
fn add_any_attr<NewAttr: Attribute>(
self,
_attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<R>,
Self::Output<NewAttr>: RenderHtml,
{
WrappedView::new(self)
todo!()
}
}
impl<V, R> RenderHtml<R> for $sig<V>
impl<V> RenderHtml for $sig<V>
where
$sig<V>: Get<Value = V>,
V: RenderHtml<R> + Clone + Send + Sync + 'static,
V: RenderHtml + Clone + Send + Sync + 'static,
V::State: 'static,
R: Renderer + 'static,
{
type AsyncOutput = Self;
@@ -622,7 +614,7 @@ mod stable {
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
(move || self.get())
@@ -630,12 +622,11 @@ mod stable {
}
}
impl<V, R> AttributeValue<R> for $sig<V>
impl<V> AttributeValue for $sig<V>
where
$sig<V>: Get<Value = V>,
V: AttributeValue<R> + Clone + Send + Sync + 'static,
V: AttributeValue + Clone + Send + Sync + 'static,
V::State: 'static,
R: Renderer,
{
type AsyncOutput = Self;
type State = RenderEffect<V::State>;
@@ -656,14 +647,14 @@ mod stable {
fn hydrate<const FROM_SERVER: bool>(
self,
key: &str,
el: &<R as Renderer>::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
(move || self.get()).hydrate::<FROM_SERVER>(key, el)
}
fn build(
self,
el: &<R as Renderer>::Element,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
(move || self.get()).build(el, key)
@@ -692,15 +683,13 @@ mod stable {
macro_rules! signal_impl_arena {
($sig:ident $dry_resolve:literal) => {
impl<V, R, S> Render<R> for $sig<V, S>
impl<V, S> Render for $sig<V, S>
where
$sig<V, S>: Get<Value = V>,
S: Send + Sync + 'static,
S: Storage<V> + Storage<Option<V>>,
V: Render<R> + Send + Sync + Clone + 'static,
V: Render + Send + Sync + Clone + 'static,
V::State: 'static,
R: Renderer,
{
type State = RenderEffectState<V::State>;
@@ -718,38 +707,34 @@ mod stable {
}
}
impl<V, R, S> AddAnyAttr<R> for $sig<V, S>
impl<V, S> AddAnyAttr for $sig<V, S>
where
$sig<V, S>: Get<Value = V>,
S: Send + Sync + 'static,
S: Storage<V> + Storage<Option<V>>,
V: RenderHtml<R> + Clone + Send + Sync + 'static,
V: RenderHtml + Clone + Send + Sync + 'static,
V::State: 'static,
R: Renderer + 'static,
{
type Output<SomeNewAttr: Attribute<R>> =
WrappedView<$sig<V, S>>;
type Output<SomeNewAttr: Attribute> = $sig<V, S>;
fn add_any_attr<NewAttr: Attribute<R>>(
fn add_any_attr<NewAttr: Attribute>(
self,
_attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<R>,
Self::Output<NewAttr>: RenderHtml,
{
WrappedView::new(self)
todo!()
}
}
impl<V, R, S> RenderHtml<R> for $sig<V, S>
impl<V, S> RenderHtml for $sig<V, S>
where
$sig<V, S>: Get<Value = V>,
S: Send + Sync + 'static,
S: Storage<V> + Storage<Option<V>>,
V: RenderHtml<R> + Clone + Send + Sync + 'static,
V: RenderHtml + Clone + Send + Sync + 'static,
V::State: 'static,
R: Renderer + 'static,
{
type AsyncOutput = Self;
@@ -800,7 +785,7 @@ mod stable {
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
(move || self.get())
@@ -808,14 +793,13 @@ mod stable {
}
}
impl<V, R, S> AttributeValue<R> for $sig<V, S>
impl<V, S> AttributeValue for $sig<V, S>
where
$sig<V, S>: Get<Value = V>,
S: Storage<V> + Storage<Option<V>>,
S: Send + Sync + 'static,
V: AttributeValue<R> + Send + Sync + Clone + 'static,
V: AttributeValue + Send + Sync + Clone + 'static,
V::State: 'static,
R: Renderer,
{
type AsyncOutput = Self;
type State = RenderEffect<V::State>;
@@ -836,14 +820,14 @@ mod stable {
fn hydrate<const FROM_SERVER: bool>(
self,
key: &str,
el: &<R as Renderer>::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
(move || self.get()).hydrate::<FROM_SERVER>(key, el)
}
fn build(
self,
el: &<R as Renderer>::Element,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
(move || self.get()).build(el, key)

View File

@@ -1,7 +1,4 @@
use crate::{
html::{element::ElementType, node_ref::NodeRefContainer},
renderer::{dom::Dom, Renderer},
};
use crate::html::{element::ElementType, node_ref::NodeRefContainer};
use reactive_graph::{
signal::RwSignal,
traits::{DefinedAt, Set, Track, WithUntracked},
@@ -55,12 +52,12 @@ where
{
}
impl<E> NodeRefContainer<E, Dom> for NodeRef<E>
impl<E> NodeRefContainer<E> for NodeRef<E>
where
E: ElementType,
E::Output: JsCast + 'static,
{
fn load(self, el: &<Dom as Renderer>::Element) {
fn load(self, el: &crate::renderer::types::Element) {
// safe to construct SendWrapper here, because it will only run in the browser
// so it will always be accessed or dropped from the main thread
self.0

View File

@@ -2,75 +2,56 @@ use crate::{
html::attribute::Attribute,
hydration::Cursor,
prelude::Mountable,
renderer::Renderer,
ssr::StreamBuilder,
view::{add_attr::AddAnyAttr, Position, PositionState, Render, RenderHtml},
};
use reactive_graph::{computed::ScopedFuture, owner::Owner};
use std::marker::PhantomData;
/// A view wrapper that sets the reactive [`Owner`] to a particular owner whenever it is rendered.
#[derive(Debug, Clone)]
pub struct OwnedView<T, R> {
pub struct OwnedView<T> {
owner: Owner,
view: T,
rndr: PhantomData<R>,
}
impl<T, R> OwnedView<T, R> {
impl<T> OwnedView<T> {
/// Wraps a view with the current owner.
pub fn new(view: T) -> Self {
let owner = Owner::current().expect("no reactive owner");
Self {
owner,
view,
rndr: PhantomData,
}
Self { owner, view }
}
/// Wraps a view with the given owner.
pub fn new_with_owner(view: T, owner: Owner) -> Self {
Self {
owner,
view,
rndr: PhantomData,
}
Self { owner, view }
}
}
/// Retained view state for an [`OwnedView`].
#[derive(Debug, Clone)]
pub struct OwnedViewState<T, R>
pub struct OwnedViewState<T>
where
T: Mountable<R>,
R: Renderer,
T: Mountable,
{
owner: Owner,
state: T,
rndr: PhantomData<R>,
}
impl<T, R> OwnedViewState<T, R>
impl<T> OwnedViewState<T>
where
T: Mountable<R>,
R: Renderer,
T: Mountable,
{
/// Wraps a state with the given owner.
fn new(state: T, owner: Owner) -> Self {
Self {
owner,
state,
rndr: PhantomData,
}
Self { owner, state }
}
}
impl<T, R> Render<R> for OwnedView<T, R>
impl<T> Render for OwnedView<T>
where
T: Render<R>,
R: Renderer,
T: Render,
{
type State = OwnedViewState<T::State, R>;
type State = OwnedViewState<T::State>;
fn build(self) -> Self::State {
let state = self.owner.with(|| self.view.build());
@@ -84,37 +65,33 @@ where
}
}
impl<T, R> AddAnyAttr<R> for OwnedView<T, R>
impl<T> AddAnyAttr for OwnedView<T>
where
T: AddAnyAttr<R>,
R: Renderer,
T: AddAnyAttr,
{
type Output<SomeNewAttr: Attribute<R>> =
OwnedView<T::Output<SomeNewAttr>, R>;
type Output<SomeNewAttr: Attribute> = OwnedView<T::Output<SomeNewAttr>>;
fn add_any_attr<NewAttr: Attribute<R>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<R>,
Self::Output<NewAttr>: RenderHtml,
{
let OwnedView { owner, view, rndr } = self;
let OwnedView { owner, view } = self;
OwnedView {
owner,
view: view.add_any_attr(attr),
rndr,
}
}
}
impl<T, R> RenderHtml<R> for OwnedView<T, R>
impl<T> RenderHtml for OwnedView<T>
where
T: RenderHtml<R>,
R: Renderer,
T: RenderHtml,
{
// TODO
type AsyncOutput = OwnedView<T::AsyncOutput, R>;
type AsyncOutput = OwnedView<T::AsyncOutput>;
const MIN_LENGTH: usize = T::MIN_LENGTH;
@@ -158,7 +135,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
let state = self
@@ -168,11 +145,11 @@ where
}
async fn resolve(self) -> Self::AsyncOutput {
let OwnedView { owner, view, rndr } = self;
let OwnedView { owner, view } = self;
let view = owner
.with(|| ScopedFuture::new(async move { view.resolve().await }))
.await;
OwnedView { owner, view, rndr }
OwnedView { owner, view }
}
fn dry_resolve(&mut self) {
@@ -180,10 +157,9 @@ where
}
}
impl<T, R> Mountable<R> for OwnedViewState<T, R>
impl<T> Mountable for OwnedViewState<T>
where
T: Mountable<R>,
R: Renderer,
T: Mountable,
{
fn unmount(&mut self) {
self.state.unmount();
@@ -191,13 +167,13 @@ where
fn mount(
&mut self,
parent: &<R as Renderer>::Element,
marker: Option<&<R as Renderer>::Node>,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
self.state.mount(parent, marker);
}
fn insert_before_this(&self, child: &mut dyn Mountable<R>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
self.state.insert_before_this(child)
}
}

View File

@@ -1,17 +1,13 @@
use super::{ReactiveFunction, SharedReactiveFunction};
use crate::{
html::property::IntoProperty,
renderer::{DomRenderer, Renderer},
};
use crate::{html::property::IntoProperty, renderer::Rndr};
use reactive_graph::effect::RenderEffect;
// These do update during hydration because properties don't exist in the DOM
impl<F, V, R> IntoProperty<R> for F
impl<F, V> IntoProperty for F
where
F: ReactiveFunction<Output = V>,
V: IntoProperty<R> + 'static,
V: IntoProperty + 'static,
V::State: 'static,
R: DomRenderer,
{
type State = RenderEffect<V::State>;
type Cloneable = SharedReactiveFunction<V>;
@@ -19,10 +15,10 @@ where
fn hydrate<const FROM_SERVER: bool>(
mut self,
el: &<R as Renderer>::Element,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
let key = R::intern(key);
let key = Rndr::intern(key);
let key = key.to_owned();
let el = el.to_owned();
@@ -39,10 +35,10 @@ where
fn build(
mut self,
el: &<R as Renderer>::Element,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
let key = R::intern(key);
let key = Rndr::intern(key);
let key = key.to_owned();
let el = el.to_owned();
@@ -85,10 +81,7 @@ where
#[cfg(not(feature = "nightly"))]
mod stable {
use crate::{
html::property::IntoProperty,
renderer::{DomRenderer, Renderer},
};
use crate::html::property::IntoProperty;
use reactive_graph::{
computed::{ArcMemo, Memo},
effect::RenderEffect,
@@ -100,12 +93,11 @@ mod stable {
macro_rules! property_signal {
($sig:ident) => {
impl<V, R> IntoProperty<R> for $sig<V>
impl<V> IntoProperty for $sig<V>
where
$sig<V>: Get<Value = V>,
V: IntoProperty<R> + Send + Sync + Clone + 'static,
V: IntoProperty + Send + Sync + Clone + 'static,
V::State: 'static,
R: DomRenderer,
{
type State = RenderEffect<V::State>;
type Cloneable = Self;
@@ -113,7 +105,7 @@ mod stable {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &<R as Renderer>::Element,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
(move || self.get()).hydrate::<FROM_SERVER>(el, key)
@@ -121,7 +113,7 @@ mod stable {
fn build(
self,
el: &<R as Renderer>::Element,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
(move || self.get()).build(el, key)
@@ -144,14 +136,13 @@ mod stable {
macro_rules! property_signal_arena {
($sig:ident) => {
impl<V, R, S> IntoProperty<R> for $sig<V, S>
impl<V, S> IntoProperty for $sig<V, S>
where
$sig<V, S>: Get<Value = V>,
S: Send + Sync + 'static,
S: Storage<V> + Storage<Option<V>>,
V: IntoProperty<R> + Send + Sync + Clone + 'static,
V: IntoProperty + Send + Sync + Clone + 'static,
V::State: 'static,
R: DomRenderer,
{
type State = RenderEffect<V::State>;
type Cloneable = Self;
@@ -159,7 +150,7 @@ mod stable {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &<R as Renderer>::Element,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
(move || self.get()).hydrate::<FROM_SERVER>(el, key)
@@ -167,7 +158,7 @@ mod stable {
fn build(
self,
el: &<R as Renderer>::Element,
el: &crate::renderer::types::Element,
key: &str,
) -> Self::State {
(move || self.get()).build(el, key)

View File

@@ -1,18 +1,20 @@
use super::{ReactiveFunction, SharedReactiveFunction, Suspend};
use crate::{html::style::IntoStyle, renderer::DomRenderer};
use crate::{html::style::IntoStyle, renderer::Rndr};
use any_spawner::Executor;
use futures::FutureExt;
use reactive_graph::effect::RenderEffect;
use std::{borrow::Cow, cell::RefCell, future::Future, rc::Rc};
impl<F, S, R> IntoStyle<R> for (&'static str, F)
impl<F, S> IntoStyle for (&'static str, F)
where
F: ReactiveFunction<Output = S>,
S: Into<Cow<'static, str>> + 'static,
R: DomRenderer,
{
type AsyncOutput = Self;
type State = RenderEffect<(R::CssStyleDeclaration, Cow<'static, str>)>;
type State = RenderEffect<(
crate::renderer::types::CssStyleDeclaration,
Cow<'static, str>,
)>;
type Cloneable = (&'static str, SharedReactiveFunction<S>);
type CloneableOwned = (&'static str, SharedReactiveFunction<S>);
@@ -25,20 +27,23 @@ where
style.push(';');
}
fn hydrate<const FROM_SERVER: bool>(self, el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
let (name, mut f) = self;
let name = R::intern(name);
let name = Rndr::intern(name);
// TODO FROM_SERVER vs template
let style = R::style(el);
let style = Rndr::style(el);
RenderEffect::new(move |prev| {
let value = f.invoke().into();
if let Some(mut state) = prev {
let (style, prev): &mut (
R::CssStyleDeclaration,
crate::renderer::types::CssStyleDeclaration,
Cow<'static, str>,
) = &mut state;
if &value != prev {
R::set_css_property(style, name, &value);
Rndr::set_css_property(style, name, &value);
}
*prev = value;
state
@@ -46,32 +51,32 @@ where
// only set the style in template mode
// in server mode, it's already been set
if !FROM_SERVER {
R::set_css_property(&style, name, &value);
Rndr::set_css_property(&style, name, &value);
}
(style.clone(), value)
}
})
}
fn build(self, el: &R::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
let (name, mut f) = self;
let name = R::intern(name);
let style = R::style(el);
let name = Rndr::intern(name);
let style = Rndr::style(el);
RenderEffect::new(move |prev| {
let value = f.invoke().into();
if let Some(mut state) = prev {
let (style, prev): &mut (
R::CssStyleDeclaration,
crate::renderer::types::CssStyleDeclaration,
Cow<'static, str>,
) = &mut state;
if &value != prev {
R::set_css_property(style, name, &value);
Rndr::set_css_property(style, name, &value);
}
*prev = value;
state
} else {
// always set the style initially without checking
R::set_css_property(&style, name, &value);
Rndr::set_css_property(&style, name, &value);
(style.clone(), value)
}
})
@@ -86,7 +91,7 @@ where
if let Some(mut state) = prev {
let (style, prev) = &mut state;
if &value != prev {
R::set_css_property(style, name, &value);
Rndr::set_css_property(style, name, &value);
}
*prev = value;
state
@@ -115,12 +120,11 @@ where
}
}
impl<F, C, R> IntoStyle<R> for F
impl<F, C> IntoStyle for F
where
F: ReactiveFunction<Output = C>,
C: IntoStyle<R> + 'static,
C: IntoStyle + 'static,
C::State: 'static,
R: DomRenderer,
{
type AsyncOutput = C::AsyncOutput;
type State = RenderEffect<C::State>;
@@ -134,7 +138,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
mut self,
el: &R::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
// TODO FROM_SERVER vs template
let el = el.clone();
@@ -149,7 +153,7 @@ where
})
}
fn build(mut self, el: &R::Element) -> Self::State {
fn build(mut self, el: &crate::renderer::types::Element) -> Self::State {
let el = el.clone();
RenderEffect::new(move |prev| {
let value = self.invoke();
@@ -199,12 +203,11 @@ where
mod stable {
macro_rules! style_signal {
($sig:ident) => {
impl<C, R> IntoStyle<R> for $sig<C>
impl<C> IntoStyle for $sig<C>
where
$sig<C>: Get<Value = C>,
C: IntoStyle<R> + Clone + Send + Sync + 'static,
C: IntoStyle + Clone + Send + Sync + 'static,
C::State: 'static,
R: DomRenderer,
{
type AsyncOutput = Self;
type State = RenderEffect<C::State>;
@@ -218,12 +221,15 @@ mod stable {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &R::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
(move || self.get()).hydrate::<FROM_SERVER>(el)
}
fn build(self, el: &R::Element) -> Self::State {
fn build(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
(move || self.get()).build(el)
}
@@ -246,44 +252,42 @@ mod stable {
}
}
impl<R, S> IntoStyle<R> for (&'static str, $sig<S>)
impl<S> IntoStyle for (&'static str, $sig<S>)
where
$sig<S>: Get<Value = S>,
S: Into<Cow<'static, str>> + Send + Sync + Clone + 'static,
R: DomRenderer,
{
type AsyncOutput = Self;
type State =
RenderEffect<(R::CssStyleDeclaration, Cow<'static, str>)>;
type State = RenderEffect<(
crate::renderer::types::CssStyleDeclaration,
Cow<'static, str>,
)>;
type Cloneable = Self;
type CloneableOwned = Self;
fn to_html(self, style: &mut String) {
IntoStyle::<R>::to_html(
(self.0, move || self.1.get()),
style,
)
IntoStyle::to_html((self.0, move || self.1.get()), style)
}
fn hydrate<const FROM_SERVER: bool>(
self,
el: &R::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
IntoStyle::<R>::hydrate::<FROM_SERVER>(
IntoStyle::hydrate::<FROM_SERVER>(
(self.0, move || self.1.get()),
el,
)
}
fn build(self, el: &R::Element) -> Self::State {
IntoStyle::<R>::build((self.0, move || self.1.get()), el)
fn build(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
IntoStyle::build((self.0, move || self.1.get()), el)
}
fn rebuild(self, state: &mut Self::State) {
IntoStyle::<R>::rebuild(
(self.0, move || self.1.get()),
state,
)
IntoStyle::rebuild((self.0, move || self.1.get()), state)
}
fn into_cloneable(self) -> Self::Cloneable {
@@ -305,14 +309,13 @@ mod stable {
macro_rules! style_signal_arena {
($sig:ident) => {
impl<C, R, S> IntoStyle<R> for $sig<C, S>
impl<C, S> IntoStyle for $sig<C, S>
where
$sig<C, S>: Get<Value = C>,
S: Storage<C> + Storage<Option<C>>,
S: Send + Sync + 'static,
C: IntoStyle<R> + Send + Sync + Clone + 'static,
C: IntoStyle + Send + Sync + Clone + 'static,
C::State: 'static,
R: DomRenderer,
{
type AsyncOutput = Self;
type State = RenderEffect<C::State>;
@@ -326,12 +329,15 @@ mod stable {
fn hydrate<const FROM_SERVER: bool>(
self,
el: &R::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
(move || self.get()).hydrate::<FROM_SERVER>(el)
}
fn build(self, el: &R::Element) -> Self::State {
fn build(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
(move || self.get()).build(el)
}
@@ -354,46 +360,44 @@ mod stable {
}
}
impl<R, S, St> IntoStyle<R> for (&'static str, $sig<S, St>)
impl<S, St> IntoStyle for (&'static str, $sig<S, St>)
where
$sig<S, St>: Get<Value = S>,
St: Send + Sync + 'static,
St: Storage<S> + Storage<Option<S>>,
S: Into<Cow<'static, str>> + Send + Sync + Clone + 'static,
R: DomRenderer,
{
type AsyncOutput = Self;
type State =
RenderEffect<(R::CssStyleDeclaration, Cow<'static, str>)>;
type State = RenderEffect<(
crate::renderer::types::CssStyleDeclaration,
Cow<'static, str>,
)>;
type Cloneable = Self;
type CloneableOwned = Self;
fn to_html(self, style: &mut String) {
IntoStyle::<R>::to_html(
(self.0, move || self.1.get()),
style,
)
IntoStyle::to_html((self.0, move || self.1.get()), style)
}
fn hydrate<const FROM_SERVER: bool>(
self,
el: &R::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
IntoStyle::<R>::hydrate::<FROM_SERVER>(
IntoStyle::hydrate::<FROM_SERVER>(
(self.0, move || self.1.get()),
el,
)
}
fn build(self, el: &R::Element) -> Self::State {
IntoStyle::<R>::build((self.0, move || self.1.get()), el)
fn build(
self,
el: &crate::renderer::types::Element,
) -> Self::State {
IntoStyle::build((self.0, move || self.1.get()), el)
}
fn rebuild(self, state: &mut Self::State) {
IntoStyle::<R>::rebuild(
(self.0, move || self.1.get()),
state,
)
IntoStyle::rebuild((self.0, move || self.1.get()), state)
}
fn into_cloneable(self) -> Self::Cloneable {
@@ -414,7 +418,7 @@ mod stable {
}
use super::RenderEffect;
use crate::{html::style::IntoStyle, renderer::DomRenderer};
use crate::html::style::IntoStyle;
use reactive_graph::{
computed::{ArcMemo, Memo},
owner::Storage,
@@ -435,14 +439,13 @@ mod stable {
style_signal!(ArcSignal);
}
impl<Fut, Rndr> IntoStyle<Rndr> for Suspend<Fut>
impl<Fut> IntoStyle for Suspend<Fut>
where
Fut: Clone + Future + Send + 'static,
Fut::Output: IntoStyle<Rndr>,
Rndr: DomRenderer + 'static,
Fut::Output: IntoStyle,
{
type AsyncOutput = Fut::Output;
type State = Rc<RefCell<Option<<Fut::Output as IntoStyle<Rndr>>::State>>>;
type State = Rc<RefCell<Option<<Fut::Output as IntoStyle>::State>>>;
type Cloneable = Self;
type CloneableOwned = Self;
@@ -456,7 +459,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
el: &<Rndr>::Element,
el: &crate::renderer::types::Element,
) -> Self::State {
let el = el.to_owned();
let state = Rc::new(RefCell::new(None));
@@ -471,7 +474,7 @@ where
state
}
fn build(self, el: &<Rndr>::Element) -> Self::State {
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
let el = el.to_owned();
let state = Rc::new(RefCell::new(None));
Executor::spawn_local({

View File

@@ -1,7 +1,6 @@
use crate::{
html::attribute::Attribute,
hydration::Cursor,
renderer::Renderer,
ssr::StreamBuilder,
view::{
add_attr::AddAnyAttr, iterators::OptionState, Mountable, Position,
@@ -130,39 +129,40 @@ impl<Fut> Debug for Suspend<Fut> {
}
/// Retained view state for [`Suspend`].
pub struct SuspendState<T, Rndr>
pub struct SuspendState<T>
where
T: Render<Rndr>,
Rndr: Renderer,
T: Render,
{
inner: Rc<RefCell<OptionState<T, Rndr>>>,
inner: Rc<RefCell<OptionState<T>>>,
}
impl<T, Rndr> Mountable<Rndr> for SuspendState<T, Rndr>
impl<T> Mountable for SuspendState<T>
where
T: Render<Rndr>,
Rndr: Renderer,
T: Render,
{
fn unmount(&mut self) {
self.inner.borrow_mut().unmount();
}
fn mount(&mut self, parent: &Rndr::Element, marker: Option<&Rndr::Node>) {
fn mount(
&mut self,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
self.inner.borrow_mut().mount(parent, marker);
}
fn insert_before_this(&self, child: &mut dyn Mountable<Rndr>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
self.inner.borrow_mut().insert_before_this(child)
}
}
impl<Fut, Rndr> Render<Rndr> for Suspend<Fut>
impl<Fut> Render for Suspend<Fut>
where
Fut: Future + 'static,
Fut::Output: Render<Rndr>,
Rndr: Renderer + 'static,
Fut::Output: Render,
{
type State = SuspendState<Fut::Output, Rndr>;
type State = SuspendState<Fut::Output>;
// TODO cancelation if it fires multiple times
fn build(self) -> Self::State {
@@ -235,17 +235,16 @@ where
}
}
impl<Fut, Rndr> AddAnyAttr<Rndr> for Suspend<Fut>
impl<Fut> AddAnyAttr for Suspend<Fut>
where
Fut: Future + Send + 'static,
Fut::Output: AddAnyAttr<Rndr>,
Rndr: Renderer + 'static,
Fut::Output: AddAnyAttr,
{
type Output<SomeNewAttr: Attribute<Rndr>> = Suspend<
type Output<SomeNewAttr: Attribute> = Suspend<
Pin<
Box<
dyn Future<
Output = <Fut::Output as AddAnyAttr<Rndr>>::Output<
Output = <Fut::Output as AddAnyAttr>::Output<
SomeNewAttr::CloneableOwned,
>,
> + Send,
@@ -253,12 +252,12 @@ where
>,
>;
fn add_any_attr<NewAttr: Attribute<Rndr>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Rndr>,
Self::Output<NewAttr>: RenderHtml,
{
let attr = attr.into_cloneable_owned();
Suspend::new(Box::pin(async move {
@@ -268,11 +267,10 @@ where
}
}
impl<Fut, Rndr> RenderHtml<Rndr> for Suspend<Fut>
impl<Fut> RenderHtml for Suspend<Fut>
where
Fut: Future + Send + 'static,
Fut::Output: RenderHtml<Rndr>,
Rndr: Renderer + 'static,
Fut::Output: RenderHtml,
{
type AsyncOutput = Option<Fut::Output>;
@@ -329,7 +327,7 @@ where
// wrapped by suspense markers
if OUT_OF_ORDER {
let mut fallback_position = *position;
buf.push_fallback::<(), Rndr>(
buf.push_fallback::<()>(
(),
&mut fallback_position,
mark_branches,
@@ -364,7 +362,7 @@ where
// TODO cancellation
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<Rndr>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
let Self { subscriber, inner } = self;

View File

@@ -1,4 +1,4 @@
use super::{CastFrom, DomRenderer, RemoveEventHandler, Renderer};
use super::{CastFrom, RemoveEventHandler};
use crate::{
dom::{document, window},
ok_or_debug, or_debug,
@@ -9,10 +9,7 @@ use once_cell::unsync::Lazy;
use rustc_hash::FxHashSet;
use std::{any::TypeId, borrow::Cow, cell::RefCell};
use wasm_bindgen::{intern, prelude::Closure, JsCast, JsValue};
use web_sys::{
Comment, CssStyleDeclaration, DomTokenList, Element, Event, HtmlElement,
HtmlTemplateElement, Node, Text,
};
use web_sys::{Comment, HtmlTemplateElement};
/// A [`Renderer`] that uses `web-sys` to manipulate DOM elements in the browser.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
@@ -22,22 +19,30 @@ thread_local! {
pub(crate) static GLOBAL_EVENTS: RefCell<FxHashSet<Cow<'static, str>>> = Default::default();
}
impl Renderer for Dom {
type Node = Node;
type Text = Text;
type Element = Element;
type Placeholder = Comment;
pub type Node = web_sys::Node;
pub type Text = web_sys::Text;
pub type Element = web_sys::Element;
pub type Placeholder = web_sys::Comment;
pub type Event = wasm_bindgen::JsValue;
pub type ClassList = web_sys::DomTokenList;
pub type CssStyleDeclaration = web_sys::CssStyleDeclaration;
pub type TemplateElement = web_sys::HtmlTemplateElement;
fn intern(text: &str) -> &str {
impl Dom {
pub fn intern(text: &str) -> &str {
intern(text)
}
pub fn create_element(tag: &str) -> Element {
document().create_element(tag).unwrap()
}
#[cfg_attr(feature = "tracing", tracing::instrument(level = "trace"))]
fn create_text_node(text: &str) -> Self::Text {
pub fn create_text_node(text: &str) -> Text {
document().create_text_node(text)
}
fn create_placeholder() -> Self::Placeholder {
pub fn create_placeholder() -> Placeholder {
thread_local! {
static COMMENT: Lazy<Comment> = Lazy::new(|| {
document().create_comment("")
@@ -47,25 +52,25 @@ impl Renderer for Dom {
}
#[cfg_attr(feature = "tracing", tracing::instrument(level = "trace"))]
fn set_text(node: &Self::Text, text: &str) {
pub fn set_text(node: &Text, text: &str) {
node.set_node_value(Some(text));
}
#[cfg_attr(feature = "tracing", tracing::instrument(level = "trace"))]
fn set_attribute(node: &Self::Element, name: &str, value: &str) {
pub fn set_attribute(node: &Element, name: &str, value: &str) {
or_debug!(node.set_attribute(name, value), node, "setAttribute");
}
#[cfg_attr(feature = "tracing", tracing::instrument(level = "trace"))]
fn remove_attribute(node: &Self::Element, name: &str) {
pub fn remove_attribute(node: &Element, name: &str) {
or_debug!(node.remove_attribute(name), node, "removeAttribute");
}
#[cfg_attr(feature = "tracing", tracing::instrument(level = "trace"))]
fn insert_node(
parent: &Self::Element,
new_child: &Self::Node,
anchor: Option<&Self::Node>,
pub fn insert_node(
parent: &Element,
new_child: &Node,
anchor: Option<&Node>,
) {
ok_or_debug!(
parent.insert_before(new_child, anchor),
@@ -75,23 +80,20 @@ impl Renderer for Dom {
}
#[cfg_attr(feature = "tracing", tracing::instrument(level = "trace"))]
fn remove_node(
parent: &Self::Element,
child: &Self::Node,
) -> Option<Self::Node> {
pub fn remove_node(parent: &Element, child: &Node) -> Option<Node> {
ok_or_debug!(parent.remove_child(child), parent, "removeNode")
}
#[cfg_attr(feature = "tracing", tracing::instrument(level = "trace"))]
fn remove(node: &Self::Node) {
pub fn remove(node: &Node) {
node.unchecked_ref::<Element>().remove();
}
fn get_parent(node: &Self::Node) -> Option<Self::Node> {
pub fn get_parent(node: &Node) -> Option<Node> {
node.parent_node()
}
fn first_child(node: &Self::Node) -> Option<Self::Node> {
pub fn first_child(node: &Node) -> Option<Node> {
#[cfg(debug_assertions)]
{
let node = node.first_child();
@@ -116,7 +118,7 @@ impl Renderer for Dom {
}
}
fn next_sibling(node: &Self::Node) -> Option<Self::Node> {
pub fn next_sibling(node: &Node) -> Option<Node> {
#[cfg(debug_assertions)]
{
let node = node.next_sibling();
@@ -141,23 +143,49 @@ impl Renderer for Dom {
}
}
fn log_node(node: &Self::Node) {
pub fn log_node(node: &Node) {
web_sys::console::log_1(node);
}
#[cfg_attr(feature = "tracing", tracing::instrument(level = "trace"))]
fn clear_children(parent: &Self::Element) {
pub fn clear_children(parent: &Element) {
parent.set_text_content(Some(""));
}
}
impl DomRenderer for Dom {
type Event = JsValue;
type ClassList = DomTokenList;
type CssStyleDeclaration = CssStyleDeclaration;
type TemplateElement = HtmlTemplateElement;
/// Mounts the new child before the marker as its sibling.
///
/// ## Panics
/// The default implementation panics if `before` does not have a parent [`crate::renderer::types::Element`].
pub fn mount_before<M>(new_child: &mut M, before: &Node)
where
M: Mountable,
{
let parent = Element::cast_from(
Self::get_parent(before).expect("could not find parent element"),
)
.expect("placeholder parent should be Element");
new_child.mount(&parent, Some(before));
}
fn set_property(el: &Self::Element, key: &str, value: &JsValue) {
/// Tries to mount the new child before the marker as its sibling.
///
/// Returns `false` if the child did not have a valid parent.
#[track_caller]
pub fn try_mount_before<M>(new_child: &mut M, before: &Node) -> bool
where
M: Mountable,
{
if let Some(parent) =
Self::get_parent(before).and_then(Element::cast_from)
{
new_child.mount(&parent, Some(before));
true
} else {
false
}
}
pub fn set_property(el: &Element, key: &str, value: &JsValue) {
or_debug!(
js_sys::Reflect::set(
el,
@@ -169,11 +197,11 @@ impl DomRenderer for Dom {
);
}
fn add_event_listener(
el: &Self::Element,
pub fn add_event_listener(
el: &Element,
name: &str,
cb: Box<dyn FnMut(Self::Event)>,
) -> RemoveEventHandler<Self::Element> {
cb: Box<dyn FnMut(Event)>,
) -> RemoveEventHandler<Element> {
let cb = wasm_bindgen::closure::Closure::wrap(cb);
let name = intern(name);
or_debug!(
@@ -191,7 +219,7 @@ impl DomRenderer for Dom {
// safe to construct this here, because it will only run in the browser
// so it will always be accessed or dropped from the main thread
let cb = send_wrapper::SendWrapper::new(cb);
move |el: &Self::Element| {
move |el: &Element| {
or_debug!(
el.remove_event_listener_with_callback(
intern(&name),
@@ -204,24 +232,24 @@ impl DomRenderer for Dom {
})
}
fn event_target<T>(ev: &Self::Event) -> T
pub fn event_target<T>(ev: &Event) -> T
where
T: CastFrom<Self::Element>,
T: CastFrom<Element>,
{
let el = ev
.unchecked_ref::<Event>()
.unchecked_ref::<web_sys::Event>()
.target()
.expect("event.target not found")
.unchecked_into::<Element>();
T::cast_from(el).expect("incorrect element type")
}
fn add_event_listener_delegated(
el: &Self::Element,
pub fn add_event_listener_delegated(
el: &Element,
name: Cow<'static, str>,
delegation_key: Cow<'static, str>,
cb: Box<dyn FnMut(Self::Event)>,
) -> RemoveEventHandler<Self::Element> {
cb: Box<dyn FnMut(Event)>,
) -> RemoveEventHandler<Element> {
let cb = Closure::wrap(cb);
let key = intern(&delegation_key);
or_debug!(
@@ -304,7 +332,7 @@ impl DomRenderer for Dom {
// safe to construct this here, because it will only run in the browser
// so it will always be accessed or dropped from the main thread
let cb = send_wrapper::SendWrapper::new(cb);
move |el: &Self::Element| {
move |el: &Element| {
drop(cb.take());
or_debug!(
js_sys::Reflect::delete_property(
@@ -318,24 +346,24 @@ impl DomRenderer for Dom {
})
}
fn class_list(el: &Self::Element) -> Self::ClassList {
pub fn class_list(el: &Element) -> ClassList {
el.class_list()
}
fn add_class(list: &Self::ClassList, name: &str) {
pub fn add_class(list: &ClassList, name: &str) {
or_debug!(list.add_1(name), list.unchecked_ref(), "add()");
}
fn remove_class(list: &Self::ClassList, name: &str) {
pub fn remove_class(list: &ClassList, name: &str) {
or_debug!(list.remove_1(name), list.unchecked_ref(), "remove()");
}
fn style(el: &Self::Element) -> Self::CssStyleDeclaration {
el.unchecked_ref::<HtmlElement>().style()
pub fn style(el: &Element) -> CssStyleDeclaration {
el.unchecked_ref::<web_sys::HtmlElement>().style()
}
fn set_css_property(
style: &Self::CssStyleDeclaration,
pub fn set_css_property(
style: &CssStyleDeclaration,
name: &str,
value: &str,
) {
@@ -346,11 +374,11 @@ impl DomRenderer for Dom {
);
}
fn set_inner_html(el: &Self::Element, html: &str) {
pub fn set_inner_html(el: &Element, html: &str) {
el.set_inner_html(html);
}
fn get_template<V>() -> Self::TemplateElement
pub fn get_template<V>() -> TemplateElement
where
V: ToTemplate + 'static,
{
@@ -384,14 +412,14 @@ impl DomRenderer for Dom {
})
}
fn clone_template(tpl: &Self::TemplateElement) -> Self::Element {
pub fn clone_template(tpl: &TemplateElement) -> Element {
tpl.content()
.clone_node_with_deep(true)
.unwrap()
.unchecked_into()
}
fn create_element_from_html(html: &str) -> Self::Element {
pub fn create_element_from_html(html: &str) -> Element {
// TODO can be optimized to cache HTML strings or cache <template>?
let tpl = document().create_element("template").unwrap();
tpl.set_inner_html(html);
@@ -399,7 +427,7 @@ impl DomRenderer for Dom {
}
}
impl Mountable<Dom> for Node {
impl Mountable for Node {
fn unmount(&mut self) {
todo!()
}
@@ -408,7 +436,7 @@ impl Mountable<Dom> for Node {
Dom::insert_node(parent, self, marker);
}
fn insert_before_this(&self, child: &mut dyn Mountable<Dom>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
let parent = Dom::get_parent(self).and_then(Element::cast_from);
if let Some(parent) = parent {
child.mount(&parent, Some(self));
@@ -418,7 +446,7 @@ impl Mountable<Dom> for Node {
}
}
impl Mountable<Dom> for Text {
impl Mountable for Text {
fn unmount(&mut self) {
self.remove();
}
@@ -427,7 +455,7 @@ impl Mountable<Dom> for Text {
Dom::insert_node(parent, self, marker);
}
fn insert_before_this(&self, child: &mut dyn Mountable<Dom>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
let parent =
Dom::get_parent(self.as_ref()).and_then(Element::cast_from);
if let Some(parent) = parent {
@@ -438,7 +466,7 @@ impl Mountable<Dom> for Text {
}
}
impl Mountable<Dom> for Comment {
impl Mountable for Comment {
fn unmount(&mut self) {
self.remove();
}
@@ -447,7 +475,7 @@ impl Mountable<Dom> for Comment {
Dom::insert_node(parent, self, marker);
}
fn insert_before_this(&self, child: &mut dyn Mountable<Dom>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
let parent =
Dom::get_parent(self.as_ref()).and_then(Element::cast_from);
if let Some(parent) = parent {
@@ -458,7 +486,7 @@ impl Mountable<Dom> for Comment {
}
}
impl Mountable<Dom> for Element {
impl Mountable for Element {
fn unmount(&mut self) {
self.remove();
}
@@ -467,7 +495,7 @@ impl Mountable<Dom> for Element {
Dom::insert_node(parent, self, marker);
}
fn insert_before_this(&self, child: &mut dyn Mountable<Dom>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
let parent =
Dom::get_parent(self.as_ref()).and_then(Element::cast_from);
if let Some(parent) = parent {

View File

@@ -420,7 +420,7 @@ impl Mountable<MockDom> for Placeholder {
}
impl<E: ElementType> CreateElement<MockDom> for E {
fn create_element(&self) -> <MockDom as Renderer>::Element {
fn create_element(&self) -> crate::renderer::types::Element {
document().create_element(E::TAG)
}
}

View File

@@ -1,55 +1,56 @@
use crate::{
html::element::CreateElement,
view::{Mountable, ToTemplate},
};
use crate::view::{Mountable, ToTemplate};
use std::{borrow::Cow, fmt::Debug};
use wasm_bindgen::JsValue;
/// A DOM renderer.
pub mod dom;
#[cfg(feature = "testing")]
pub type Rndr = dom::Dom;
pub mod types {
pub use super::dom::{
ClassList, CssStyleDeclaration, Element, Event, Node, Placeholder,
TemplateElement, Text,
};
}
/* #[cfg(feature = "testing")]
/// A renderer based on a mock DOM.
pub mod mock_dom;
/// A DOM renderer optimized for element creation.
#[cfg(feature = "sledgehammer")]
pub mod sledgehammer;
pub mod sledgehammer; */
/// Implements the instructions necessary to render an interface on some platform.
///
/// By default, this is implemented for the Document Object Model (DOM) in a Web
/// browser, but implementing this trait for some other platform allows you to use
/// the library to render any tree-based UI.
pub trait Renderer: Send + Sized + Debug + 'static {
/// The basic type of node in the view tree.
type Node: Mountable<Self> + Clone + 'static;
type Node: Mountable + Clone + 'static;
/// A visible element in the view tree.
type Element: AsRef<Self::Node>
+ CastFrom<Self::Node>
+ Mountable<Self>
+ Mountable
+ Clone
+ 'static;
/// A text node in the view tree.
type Text: AsRef<Self::Node>
+ CastFrom<Self::Node>
+ Mountable<Self>
+ Mountable
+ Clone
+ 'static;
/// A placeholder node, which can be inserted into the tree but does not
/// appear (e.g., a comment node in the DOM).
type Placeholder: AsRef<Self::Node>
+ CastFrom<Self::Node>
+ Mountable<Self>
+ Mountable
+ Clone
+ 'static;
/// Interns a string slice, if that is available on this platform and useful as an optimization.
fn intern(text: &str) -> &str;
/// Creates a new element node.
#[track_caller]
fn create_element<E: CreateElement<Self>>(tag: E) -> Self::Element {
tag.create_element()
}
/// Creates a new text node.
fn create_text_node(text: &str) -> Self::Text;
@@ -73,39 +74,6 @@ pub trait Renderer: Send + Sized + Debug + 'static {
marker: Option<&Self::Node>,
);
/// Mounts the new child before the marker as its sibling.
///
/// ## Panics
/// The default implementation panics if `before` does not have a parent [`R::Element`].
fn mount_before<M>(new_child: &mut M, before: &Self::Node)
where
M: Mountable<Self>,
{
let parent = Self::Element::cast_from(
Self::get_parent(before).expect("could not find parent element"),
)
.expect("placeholder parent should be Element");
new_child.mount(&parent, Some(before));
}
/// Tries to mount the new child before the marker as its sibling.
///
/// Returns `false` if the child did not have a valid parent.
#[track_caller]
fn try_mount_before<M>(new_child: &mut M, before: &Self::Node) -> bool
where
M: Mountable<Self>,
{
if let Some(parent) =
Self::get_parent(before).and_then(Self::Element::cast_from)
{
new_child.mount(&parent, Some(before));
true
} else {
false
}
}
/// Removes the child node from the parents, and returns the removed node.
fn remove_node(
parent: &Self::Element,

View File

@@ -1,7 +1,4 @@
use crate::{
renderer::Renderer,
view::{Position, RenderHtml},
};
use crate::view::{Position, RenderHtml};
use futures::Stream;
use std::{
collections::VecDeque,
@@ -100,14 +97,13 @@ impl StreamBuilder {
// Out-of-Order Streaming
/// Pushes a fallback for out-of-order streaming.
pub fn push_fallback<View, Rndr>(
pub fn push_fallback<View>(
&mut self,
fallback: View,
position: &mut Position,
mark_branches: bool,
) where
View: RenderHtml<Rndr>,
Rndr: Renderer,
View: RenderHtml,
{
self.write_chunk_marker(true);
fallback.to_html_with_buf(
@@ -158,14 +154,13 @@ impl StreamBuilder {
}
/// Injects an out-of-order chunk into the stream.
pub fn push_async_out_of_order<View, Rndr>(
pub fn push_async_out_of_order<View>(
&mut self,
view: impl Future<Output = Option<View>> + Send + 'static,
position: &mut Position,
mark_branches: bool,
) where
View: RenderHtml<Rndr>,
Rndr: Renderer,
View: RenderHtml,
{
let id = self.clone_id();
// copy so it's not updated by additional iterations

View File

@@ -1,15 +1,11 @@
use crate::{
html::{
attribute::Attribute,
element::{
CreateElement, ElementType, ElementWithChildren, HtmlElement,
},
element::{ElementType, ElementWithChildren, HtmlElement},
},
renderer::{dom::Dom, Renderer},
view::Render,
};
use once_cell::unsync::Lazy;
use std::{fmt::Debug, marker::PhantomData};
use std::fmt::Debug;
macro_rules! svg_elements {
($($tag:ident [$($attr:ty),*]),* $(,)?) => {
@@ -18,15 +14,15 @@ macro_rules! svg_elements {
/// An SVG attribute.
// `tag()` function
#[allow(non_snake_case)]
pub fn $tag<Rndr>() -> HtmlElement<[<$tag:camel>], (), (), Rndr>
pub fn $tag() -> HtmlElement<[<$tag:camel>], (), ()>
where
Rndr: Renderer
{
HtmlElement {
tag: [<$tag:camel>],
attributes: (),
children: (),
rndr: PhantomData,
}
}
@@ -34,30 +30,30 @@ macro_rules! svg_elements {
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct [<$tag:camel>];
impl<At, Ch, Rndr> HtmlElement<[<$tag:camel>], At, Ch, Rndr>
impl<At, Ch> HtmlElement<[<$tag:camel>], At, Ch>
where
At: Attribute<Rndr>,
Ch: Render<Rndr>,
Rndr: Renderer,
At: Attribute,
Ch: Render,
{
$(
pub fn $attr<V>(self, value: V) -> HtmlElement <
[<$tag:camel>],
<At as NextTuple<Attr<$crate::html::attribute::[<$attr:camel>], V, Rndr>>>::Output,
Ch, Rndr
<At as NextTuple<Attr<$crate::html::attribute::[<$attr:camel>], V>>>::Output,
Ch
>
where
V: AttributeValue<Rndr>,
At: NextTuple<Attr<$crate::html::attribute::[<$attr:camel>], V, Rndr>>,
<At as NextTuple<Attr<$crate::html::attribute::[<$attr:camel>], V, Rndr>>>::Output: Attribute<Rndr>,
V: AttributeValue,
At: NextTuple<Attr<$crate::html::attribute::[<$attr:camel>], V>>,
<At as NextTuple<Attr<$crate::html::attribute::[<$attr:camel>], V>>>::Output: Attribute,
{
let HtmlElement { tag, rndr, children, attributes,
let HtmlElement { tag, children, attributes,
#[cfg(debug_assertions)]
defined_at
} = self;
HtmlElement {
tag,
rndr,
children,
attributes: attributes.next_tuple($crate::html::attribute::$attr(value)),
#[cfg(debug_assertions)]
@@ -81,22 +77,6 @@ macro_rules! svg_elements {
}
impl ElementWithChildren for [<$tag:camel>] {}
impl CreateElement<Dom> for [<$tag:camel>] {
fn create_element(&self) -> <Dom as Renderer>::Element {
use wasm_bindgen::JsCast;
thread_local! {
static ELEMENT: Lazy<<Dom as Renderer>::Element> = Lazy::new(|| {
crate::dom::document().create_element_ns(
Some(wasm_bindgen::intern("http://www.w3.org/2000/svg")),
stringify!($tag)
).unwrap()
});
}
ELEMENT.with(|e| e.clone_node()).unwrap().unchecked_into()
}
}
)*
}
}

View File

@@ -1,5 +1,5 @@
use super::{BoxedView, RenderHtml, WrappedView};
use crate::{html::attribute::Attribute, renderer::Renderer};
use super::RenderHtml;
use crate::html::attribute::Attribute;
/// Allows adding a new attribute to some type, before it is rendered.
/// This takes place at compile time as part of the builder syntax for creating a statically typed
@@ -8,34 +8,28 @@ use crate::{html::attribute::Attribute, renderer::Renderer};
/// Normally, this is used to add an attribute to an HTML element. But it is required to be
/// implemented for all types that implement [`RenderHtml`], so that attributes can be spread onto
/// other structures like the return type of a component.
pub trait AddAnyAttr<Rndr>
where
Rndr: Renderer,
{
pub trait AddAnyAttr {
/// The new type once the attribute has been added.
type Output<SomeNewAttr: Attribute<Rndr>>: RenderHtml<Rndr>;
type Output<SomeNewAttr: Attribute>: RenderHtml;
/// Adds an attribute to the view.
fn add_any_attr<NewAttr: Attribute<Rndr>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Rndr>;
Self::Output<NewAttr>: RenderHtml;
}
/// Declares that spreading attributes onto a particular type has no effect.
#[macro_export]
macro_rules! no_attrs {
($ty_name:ty) => {
impl<'a, R> $crate::view::add_attr::AddAnyAttr<R> for $ty_name
where
R: Renderer,
{
type Output<SomeNewAttr: $crate::html::attribute::Attribute<R>> =
impl<'a> $crate::view::add_attr::AddAnyAttr for $ty_name {
type Output<SomeNewAttr: $crate::html::attribute::Attribute> =
$ty_name;
fn add_any_attr<NewAttr: $crate::html::attribute::Attribute<R>>(
fn add_any_attr<NewAttr: $crate::html::attribute::Attribute>(
self,
_attr: NewAttr,
) -> Self::Output<NewAttr> {
@@ -44,41 +38,3 @@ macro_rules! no_attrs {
}
};
}
impl<T, Rndr> AddAnyAttr<Rndr> for BoxedView<T>
where
T: AddAnyAttr<Rndr>,
Rndr: Renderer,
{
type Output<SomeNewAttr: Attribute<Rndr>> =
BoxedView<T::Output<SomeNewAttr>>;
fn add_any_attr<NewAttr: Attribute<Rndr>>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Rndr>,
{
BoxedView::new(self.into_inner().add_any_attr(attr))
}
}
impl<T, Rndr> AddAnyAttr<Rndr> for WrappedView<T>
where
T: AddAnyAttr<Rndr>,
Rndr: Renderer,
{
type Output<SomeNewAttr: Attribute<Rndr>> =
WrappedView<T::Output<SomeNewAttr>>;
fn add_any_attr<NewAttr: Attribute<Rndr>>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Rndr>,
{
WrappedView::new(self.into_inner().add_any_attr(attr))
}
}

View File

@@ -1,17 +1,15 @@
#[cfg(feature = "ssr")]
use super::MarkBranch;
use super::{
add_attr::AddAnyAttr, BoxedView, Mountable, Position, PositionState,
Render, RenderHtml,
add_attr::AddAnyAttr, Mountable, Position, PositionState, Render,
RenderHtml,
};
use crate::{
html::attribute::Attribute, hydration::Cursor, renderer::Renderer,
ssr::StreamBuilder,
html::attribute::Attribute, hydration::Cursor, ssr::StreamBuilder,
};
use std::{
any::{Any, TypeId},
fmt::Debug,
marker::PhantomData,
};
#[cfg(feature = "ssr")]
use std::{future::Future, pin::Pin};
@@ -26,10 +24,7 @@ use std::{future::Future, pin::Pin};
/// Generally speaking, using `AnyView` restricts the amount of information available to the
/// compiler and should be limited to situations in which it is necessary to preserve the maximum
/// amount of type information possible.
pub struct AnyView<R>
where
R: Renderer,
{
pub struct AnyView {
type_id: TypeId,
value: Box<dyn Any + Send>,
@@ -47,38 +42,34 @@ where
#[cfg(feature = "ssr")]
to_html_async_ooo:
fn(Box<dyn Any>, &mut StreamBuilder, &mut Position, bool, bool),
build: fn(Box<dyn Any>) -> AnyViewState<R>,
rebuild: fn(TypeId, Box<dyn Any>, &mut AnyViewState<R>),
build: fn(Box<dyn Any>) -> AnyViewState,
rebuild: fn(TypeId, Box<dyn Any>, &mut AnyViewState),
#[cfg(feature = "ssr")]
#[allow(clippy::type_complexity)]
resolve:
fn(Box<dyn Any>) -> Pin<Box<dyn Future<Output = AnyView<R>> + Send>>,
resolve: fn(Box<dyn Any>) -> Pin<Box<dyn Future<Output = AnyView> + Send>>,
#[cfg(feature = "ssr")]
dry_resolve: fn(&mut Box<dyn Any + Send>),
#[cfg(feature = "hydrate")]
#[cfg(feature = "hydrate")]
#[allow(clippy::type_complexity)]
hydrate_from_server:
fn(Box<dyn Any>, &Cursor<R>, &PositionState) -> AnyViewState<R>,
fn(Box<dyn Any>, &Cursor, &PositionState) -> AnyViewState,
}
/// Retained view state for [`AnyView`].
pub struct AnyViewState<R>
where
R: Renderer,
{
pub struct AnyViewState {
type_id: TypeId,
state: Box<dyn Any>,
unmount: fn(&mut dyn Any),
mount: fn(&mut dyn Any, parent: &R::Element, marker: Option<&R::Node>),
insert_before_this: fn(&dyn Any, child: &mut dyn Mountable<R>) -> bool,
rndr: PhantomData<R>,
mount: fn(
&mut dyn Any,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
),
insert_before_this: fn(&dyn Any, child: &mut dyn Mountable) -> bool,
}
impl<R> Debug for AnyViewState<R>
where
R: Renderer,
{
impl Debug for AnyViewState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AnyViewState")
.field("type_id", &self.type_id)
@@ -86,28 +77,23 @@ where
.field("unmount", &self.unmount)
.field("mount", &self.mount)
.field("insert_before_this", &self.insert_before_this)
.field("rndr", &self.rndr)
.finish()
}
}
/// Allows converting some view into [`AnyView`].
pub trait IntoAny<R>
where
R: Renderer,
{
pub trait IntoAny {
/// Converts the view into a type-erased [`AnyView`].
fn into_any(self) -> AnyView<R>;
fn into_any(self) -> AnyView;
}
fn mount_any<R, T>(
fn mount_any<T>(
state: &mut dyn Any,
parent: &R::Element,
marker: Option<&R::Node>,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) where
T: Render<R>,
T: Render,
T::State: 'static,
R: Renderer,
{
let state = state
.downcast_mut::<T::State>()
@@ -115,11 +101,10 @@ fn mount_any<R, T>(
state.mount(parent, marker)
}
fn unmount_any<R, T>(state: &mut dyn Any)
fn unmount_any<T>(state: &mut dyn Any)
where
T: Render<R>,
T: Render,
T::State: 'static,
R: Renderer,
{
let state = state
.downcast_mut::<T::State>()
@@ -127,14 +112,10 @@ where
state.unmount();
}
fn insert_before_this<R, T>(
state: &dyn Any,
child: &mut dyn Mountable<R>,
) -> bool
fn insert_before_this<T>(state: &dyn Any, child: &mut dyn Mountable) -> bool
where
T: Render<R>,
T: Render,
T::State: 'static,
R: Renderer + 'static,
{
let state = state
.downcast_ref::<T::State>()
@@ -142,17 +123,16 @@ where
state.insert_before_this(child)
}
impl<T, R> IntoAny<R> for T
impl<T> IntoAny for T
where
T: Send,
T: RenderHtml<R> + 'static,
T: RenderHtml + 'static,
T::State: 'static,
R: Renderer + 'static,
{
// inlining allows the compiler to remove the unused functions
// i.e., doesn't ship HTML-generating code that isn't used
#[inline(always)]
fn into_any(self) -> AnyView<R> {
fn into_any(self) -> AnyView {
#[cfg(feature = "ssr")]
let html_len = self.html_len();
@@ -172,7 +152,7 @@ where
.downcast::<T>()
.expect("AnyView::resolve could not be downcast");
Box::pin(async move { value.resolve().await.into_any() })
as Pin<Box<dyn Future<Output = AnyView<R>> + Send>>
as Pin<Box<dyn Future<Output = AnyView> + Send>>
};
#[cfg(feature = "ssr")]
let to_html = |value: Box<dyn Any>,
@@ -245,17 +225,15 @@ where
AnyViewState {
type_id: TypeId::of::<T>(),
state,
rndr: PhantomData,
mount: mount_any::<R, T>,
unmount: unmount_any::<R, T>,
insert_before_this: insert_before_this::<R, T>,
mount: mount_any::<T>,
unmount: unmount_any::<T>,
insert_before_this: insert_before_this::<T>,
}
};
#[cfg(feature = "hydrate")]
let hydrate_from_server =
|value: Box<dyn Any>,
cursor: &Cursor<R>,
position: &PositionState| {
|value: Box<dyn Any>, cursor: &Cursor, position: &PositionState| {
let value = value
.downcast::<T>()
.expect("AnyView::hydrate_from_server couldn't downcast");
@@ -264,16 +242,16 @@ where
AnyViewState {
type_id: TypeId::of::<T>(),
state,
rndr: PhantomData,
mount: mount_any::<R, T>,
unmount: unmount_any::<R, T>,
insert_before_this: insert_before_this::<R, T>,
mount: mount_any::<T>,
unmount: unmount_any::<T>,
insert_before_this: insert_before_this::<T>,
}
};
let rebuild = |new_type_id: TypeId,
value: Box<dyn Any>,
state: &mut AnyViewState<R>| {
state: &mut AnyViewState| {
let value = value
.downcast::<T>()
.expect("AnyView::rebuild couldn't downcast value");
@@ -290,6 +268,7 @@ where
*state = new;
}
};
AnyView {
type_id: TypeId::of::<T>(),
value,
@@ -313,11 +292,8 @@ where
}
}
impl<R> Render<R> for AnyView<R>
where
R: Renderer + 'static,
{
type State = AnyViewState<R>;
impl Render for AnyView {
type State = AnyViewState;
fn build(self) -> Self::State {
(self.build)(self.value)
@@ -328,27 +304,21 @@ where
}
}
impl<R> AddAnyAttr<R> for AnyView<R>
where
R: Renderer + 'static,
{
type Output<SomeNewAttr: Attribute<R>> = Self;
impl AddAnyAttr for AnyView {
type Output<SomeNewAttr: Attribute> = Self;
fn add_any_attr<NewAttr: Attribute<R>>(
fn add_any_attr<NewAttr: Attribute>(
self,
_attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<R>,
Self::Output<NewAttr>: RenderHtml,
{
todo!()
}
}
impl<R> RenderHtml<R> for AnyView<R>
where
R: Renderer + 'static,
{
impl RenderHtml for AnyView {
type AsyncOutput = Self;
fn dry_resolve(&mut self) {
@@ -441,7 +411,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
#[cfg(feature = "hydrate")]
@@ -476,19 +446,20 @@ where
}
}
impl<R> Mountable<R> for AnyViewState<R>
where
R: Renderer + 'static,
{
impl Mountable for AnyViewState {
fn unmount(&mut self) {
(self.unmount)(&mut *self.state)
}
fn mount(&mut self, parent: &R::Element, marker: Option<&R::Node>) {
fn mount(
&mut self,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
(self.mount)(&mut *self.state, parent, marker)
}
fn insert_before_this(&self, child: &mut dyn Mountable<R>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
(self.insert_before_this)(&*self.state, child)
}
}

View File

@@ -3,18 +3,15 @@ use super::{
Render, RenderHtml,
};
use crate::{
html::attribute::Attribute, hydration::Cursor, renderer::Renderer,
ssr::StreamBuilder,
html::attribute::Attribute, hydration::Cursor, ssr::StreamBuilder,
};
use either_of::*;
use futures::future::join;
use std::marker::PhantomData;
impl<A, B, Rndr> Render<Rndr> for Either<A, B>
impl<A, B> Render for Either<A, B>
where
A: Render<Rndr>,
B: Render<Rndr>,
Rndr: Renderer,
A: Render,
B: Render,
{
type State = Either<A::State, B::State>;
@@ -49,11 +46,10 @@ where
}
}
impl<A, B, Rndr> Mountable<Rndr> for Either<A, B>
impl<A, B> Mountable for Either<A, B>
where
A: Mountable<Rndr>,
B: Mountable<Rndr>,
Rndr: Renderer,
A: Mountable,
B: Mountable,
{
fn unmount(&mut self) {
match self {
@@ -64,8 +60,8 @@ where
fn mount(
&mut self,
parent: &<Rndr as Renderer>::Element,
marker: Option<&<Rndr as Renderer>::Node>,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
match self {
Either::Left(left) => left.mount(parent, marker),
@@ -73,7 +69,7 @@ where
}
}
fn insert_before_this(&self, child: &mut dyn Mountable<Rndr>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
match &self {
Either::Left(left) => left.insert_before_this(child),
Either::Right(right) => right.insert_before_this(child),
@@ -81,23 +77,22 @@ where
}
}
impl<A, B, Rndr> AddAnyAttr<Rndr> for Either<A, B>
impl<A, B> AddAnyAttr for Either<A, B>
where
A: RenderHtml<Rndr>,
B: RenderHtml<Rndr>,
Rndr: Renderer,
A: RenderHtml,
B: RenderHtml,
{
type Output<SomeNewAttr: Attribute<Rndr>> = Either<
<A as AddAnyAttr<Rndr>>::Output<SomeNewAttr>,
<B as AddAnyAttr<Rndr>>::Output<SomeNewAttr>,
type Output<SomeNewAttr: Attribute> = Either<
<A as AddAnyAttr>::Output<SomeNewAttr>,
<B as AddAnyAttr>::Output<SomeNewAttr>,
>;
fn add_any_attr<NewAttr: Attribute<Rndr>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Rndr>,
Self::Output<NewAttr>: RenderHtml,
{
match self {
Either::Left(i) => Either::Left(i.add_any_attr(attr)),
@@ -119,11 +114,10 @@ const fn max_usize(vals: &[usize]) -> usize {
max
}
impl<A, B, Rndr> RenderHtml<Rndr> for Either<A, B>
impl<A, B> RenderHtml for Either<A, B>
where
A: RenderHtml<Rndr>,
B: RenderHtml<Rndr>,
Rndr: Renderer,
A: RenderHtml,
B: RenderHtml,
{
type AsyncOutput = Either<A::AsyncOutput, B::AsyncOutput>;
@@ -223,7 +217,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<Rndr>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
match self {
@@ -254,11 +248,10 @@ pub struct EitherKeepAliveState<A, B> {
showing_b: bool,
}
impl<A, B, Rndr> Render<Rndr> for EitherKeepAlive<A, B>
impl<A, B> Render for EitherKeepAlive<A, B>
where
A: Render<Rndr>,
B: Render<Rndr>,
Rndr: Renderer,
A: Render,
B: Render,
{
type State = EitherKeepAliveState<A::State, B::State>;
@@ -307,23 +300,22 @@ where
}
}
impl<A, B, Rndr> AddAnyAttr<Rndr> for EitherKeepAlive<A, B>
impl<A, B> AddAnyAttr for EitherKeepAlive<A, B>
where
A: RenderHtml<Rndr>,
B: RenderHtml<Rndr>,
Rndr: Renderer,
A: RenderHtml,
B: RenderHtml,
{
type Output<SomeNewAttr: Attribute<Rndr>> = EitherKeepAlive<
<A as AddAnyAttr<Rndr>>::Output<SomeNewAttr::Cloneable>,
<B as AddAnyAttr<Rndr>>::Output<SomeNewAttr::Cloneable>,
type Output<SomeNewAttr: Attribute> = EitherKeepAlive<
<A as AddAnyAttr>::Output<SomeNewAttr::Cloneable>,
<B as AddAnyAttr>::Output<SomeNewAttr::Cloneable>,
>;
fn add_any_attr<NewAttr: Attribute<Rndr>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Rndr>,
Self::Output<NewAttr>: RenderHtml,
{
let EitherKeepAlive { a, b, show_b } = self;
let attr = attr.into_cloneable();
@@ -335,11 +327,10 @@ where
}
}
impl<A, B, Rndr> RenderHtml<Rndr> for EitherKeepAlive<A, B>
impl<A, B> RenderHtml for EitherKeepAlive<A, B>
where
A: RenderHtml<Rndr>,
B: RenderHtml<Rndr>,
Rndr: Renderer,
A: RenderHtml,
B: RenderHtml,
{
type AsyncOutput = EitherKeepAlive<A::AsyncOutput, B::AsyncOutput>;
@@ -424,7 +415,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<Rndr>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
let showing_b = self.show_b;
@@ -447,11 +438,10 @@ where
}
}
impl<A, B, Rndr> Mountable<Rndr> for EitherKeepAliveState<A, B>
impl<A, B> Mountable for EitherKeepAliveState<A, B>
where
A: Mountable<Rndr>,
B: Mountable<Rndr>,
Rndr: Renderer,
A: Mountable,
B: Mountable,
{
fn unmount(&mut self) {
if self.showing_b {
@@ -463,8 +453,8 @@ where
fn mount(
&mut self,
parent: &<Rndr as Renderer>::Element,
marker: Option<&<Rndr as Renderer>::Node>,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
if self.showing_b {
self.b
@@ -479,7 +469,7 @@ where
}
}
fn insert_before_this(&self, child: &mut dyn Mountable<Rndr>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
if self.showing_b {
self.b
.as_ref()
@@ -498,21 +488,19 @@ macro_rules! tuples {
($num:literal => $($ty:ident),*) => {
paste::paste! {
#[doc = concat!("Retained view state for ", stringify!([<EitherOf $num>]), ".")]
pub struct [<EitherOf $num State>]<$($ty,)* Rndr>
pub struct [<EitherOf $num State>]<$($ty,)*>
where
$($ty: Render<Rndr>,)*
Rndr: Renderer
$($ty: Render,)*
{
/// Which child view state is being displayed.
pub state: [<EitherOf $num>]<$($ty::State,)*>,
/// The renderer.
pub rndr: PhantomData<Rndr>
}
impl<$($ty,)* Rndr> Mountable<Rndr> for [<EitherOf $num State>]<$($ty,)* Rndr>
impl<$($ty,)*> Mountable for [<EitherOf $num State>]<$($ty,)*>
where
$($ty: Render<Rndr>,)*
Rndr: Renderer
$($ty: Render,)*
{
fn unmount(&mut self) {
match &mut self.state {
@@ -522,8 +510,8 @@ macro_rules! tuples {
fn mount(
&mut self,
parent: &<Rndr as Renderer>::Element,
marker: Option<&<Rndr as Renderer>::Node>,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
match &mut self.state {
$([<EitherOf $num>]::$ty(this) => [<EitherOf $num>]::$ty(this.mount(parent, marker)),)*
@@ -531,7 +519,7 @@ macro_rules! tuples {
}
fn insert_before_this(&self,
child: &mut dyn Mountable<Rndr>,
child: &mut dyn Mountable,
) -> bool {
match &self.state {
$([<EitherOf $num>]::$ty(this) =>this.insert_before_this(child),)*
@@ -539,19 +527,19 @@ macro_rules! tuples {
}
}
impl<Rndr, $($ty,)*> Render<Rndr> for [<EitherOf $num>]<$($ty,)*>
impl<$($ty,)*> Render for [<EitherOf $num>]<$($ty,)*>
where
$($ty: Render<Rndr>,)*
Rndr: Renderer
$($ty: Render,)*
{
type State = [<EitherOf $num State>]<$($ty,)* Rndr>;
type State = [<EitherOf $num State>]<$($ty,)*>;
fn build(self) -> Self::State {
let state = match self {
$([<EitherOf $num>]::$ty(this) => [<EitherOf $num>]::$ty(this.build()),)*
};
Self::State { state, rndr: PhantomData }
Self::State { state }
}
fn rebuild(self, state: &mut Self::State) {
@@ -576,21 +564,21 @@ macro_rules! tuples {
}
}
impl<Rndr, $($ty,)*> AddAnyAttr<Rndr> for [<EitherOf $num>]<$($ty,)*>
impl<$($ty,)*> AddAnyAttr for [<EitherOf $num>]<$($ty,)*>
where
$($ty: RenderHtml<Rndr>,)*
Rndr: Renderer,
$($ty: RenderHtml,)*
{
type Output<SomeNewAttr: Attribute<Rndr>> = [<EitherOf $num>]<
$(<$ty as AddAnyAttr<Rndr>>::Output<SomeNewAttr>,)*
type Output<SomeNewAttr: Attribute> = [<EitherOf $num>]<
$(<$ty as AddAnyAttr>::Output<SomeNewAttr>,)*
>;
fn add_any_attr<NewAttr: Attribute<Rndr>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Rndr>,
Self::Output<NewAttr>: RenderHtml,
{
match self {
$([<EitherOf $num>]::$ty(this) => [<EitherOf $num>]::$ty(this.add_any_attr(attr)),)*
@@ -598,10 +586,10 @@ macro_rules! tuples {
}
}
impl<Rndr, $($ty,)*> RenderHtml<Rndr> for [<EitherOf $num>]<$($ty,)*>
impl<$($ty,)*> RenderHtml for [<EitherOf $num>]<$($ty,)*>
where
$($ty: RenderHtml<Rndr>,)*
Rndr: Renderer,
$($ty: RenderHtml,)*
{
type AsyncOutput = [<EitherOf $num>]<$($ty::AsyncOutput,)*>;
@@ -663,7 +651,7 @@ macro_rules! tuples {
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<Rndr>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
let state = match self {
@@ -672,7 +660,7 @@ macro_rules! tuples {
})*
};
Self::State { state, rndr: PhantomData }
Self::State { state }
}
}
}

View File

@@ -3,26 +3,25 @@ use crate::{
html::attribute::Attribute,
hydration::Cursor,
ssr::StreamBuilder,
view::{iterators::OptionState, Mountable, Render, Renderer},
view::{iterators::OptionState, Mountable, Render},
};
use either_of::Either;
use std::sync::Arc;
use throw_error::{Error as AnyError, ErrorHook};
impl<R, T, E> Render<R> for Result<T, E>
impl<T, E> Render for Result<T, E>
where
T: Render<R>,
R: Renderer,
T: Render,
E: Into<AnyError> + 'static,
{
type State = ResultState<T, R>;
type State = ResultState<T>;
fn build(self) -> Self::State {
let hook = throw_error::get_error_hook();
let (state, error) = match self {
Ok(view) => (Either::Left(view.build()), None),
Err(e) => (
Either::Right(Render::<R>::build(())),
Either::Right(Render::build(())),
Some(throw_error::throw(e.into())),
),
};
@@ -42,7 +41,7 @@ where
}
// Ok => Err: unmount, replace with marker, and throw
(Either::Left(old), Err(err)) => {
let mut new_state = Render::<R>::build(());
let mut new_state = Render::build(());
old.insert_before_this(&mut new_state);
old.unmount();
state.state = Either::Right(new_state);
@@ -63,21 +62,19 @@ where
}
/// View state for a `Result<_, _>` view.
pub struct ResultState<T, R>
pub struct ResultState<T>
where
T: Render<R>,
R: Renderer,
T: Render,
{
/// The view state.
state: OptionState<T, R>,
state: OptionState<T>,
error: Option<throw_error::ErrorId>,
hook: Option<Arc<dyn ErrorHook>>,
}
impl<T, R> Drop for ResultState<T, R>
impl<T> Drop for ResultState<T>
where
T: Render<R>,
R: Renderer,
T: Render,
{
fn drop(&mut self) {
// when the state is cleared, unregister this error; this item is being dropped and its
@@ -88,48 +85,50 @@ where
}
}
impl<T, R> Mountable<R> for ResultState<T, R>
impl<T> Mountable for ResultState<T>
where
T: Render<R>,
R: Renderer,
T: Render,
{
fn unmount(&mut self) {
self.state.unmount();
}
fn mount(&mut self, parent: &R::Element, marker: Option<&R::Node>) {
fn mount(
&mut self,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
self.state.mount(parent, marker);
}
fn insert_before_this(&self, child: &mut dyn Mountable<R>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
self.state.insert_before_this(child)
}
}
impl<R, T, E> AddAnyAttr<R> for Result<T, E>
impl<T, E> AddAnyAttr for Result<T, E>
where
T: AddAnyAttr<R>,
R: Renderer,
T: AddAnyAttr,
E: Into<AnyError> + Send + 'static,
{
type Output<SomeNewAttr: Attribute<R>> =
Result<<T as AddAnyAttr<R>>::Output<SomeNewAttr>, E>;
type Output<SomeNewAttr: Attribute> =
Result<<T as AddAnyAttr>::Output<SomeNewAttr>, E>;
fn add_any_attr<NewAttr: Attribute<R>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<R>,
Self::Output<NewAttr>: RenderHtml,
{
self.map(|inner| inner.add_any_attr(attr))
}
}
impl<R, T, E> RenderHtml<R> for Result<T, E>
impl<T, E> RenderHtml for Result<T, E>
where
T: RenderHtml<R>,
R: Renderer,
T: RenderHtml,
E: Into<AnyError> + Send + 'static,
{
type AsyncOutput = Result<T::AsyncOutput, E>;
@@ -199,7 +198,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
let hook = throw_error::get_error_hook();
@@ -209,11 +208,8 @@ where
None,
),
Err(e) => {
let state = RenderHtml::<R>::hydrate::<FROM_SERVER>(
(),
cursor,
position,
);
let state =
RenderHtml::hydrate::<FROM_SERVER>((), cursor, position);
(Either::Right(state), Some(throw_error::throw(e.into())))
}
};

View File

@@ -1,72 +1,69 @@
use super::any_view::{AnyView, IntoAny};
use crate::renderer::Renderer;
/// A typed-erased collection of different views.
pub struct Fragment<R: Renderer> {
pub struct Fragment {
/// The nodes contained in the fragment.
pub nodes: Vec<AnyView<R>>,
pub nodes: Vec<AnyView>,
}
/// Converts some view into a type-erased collection of views.
pub trait IntoFragment<R: Renderer> {
pub trait IntoFragment {
/// Converts some view into a type-erased collection of views.
fn into_fragment(self) -> Fragment<R>;
fn into_fragment(self) -> Fragment;
}
impl<R: Renderer> FromIterator<AnyView<R>> for Fragment<R> {
fn from_iter<T: IntoIterator<Item = AnyView<R>>>(iter: T) -> Self {
impl FromIterator<AnyView> for Fragment {
fn from_iter<T: IntoIterator<Item = AnyView>>(iter: T) -> Self {
Fragment::new(iter.into_iter().collect())
}
}
impl<R: Renderer> From<AnyView<R>> for Fragment<R> {
fn from(view: AnyView<R>) -> Self {
impl From<AnyView> for Fragment {
fn from(view: AnyView) -> Self {
Fragment::new(vec![view])
}
}
impl<R: Renderer> From<Fragment<R>> for AnyView<R> {
fn from(value: Fragment<R>) -> Self {
impl From<Fragment> for AnyView {
fn from(value: Fragment) -> Self {
value.nodes.into_any()
}
}
impl<R: Renderer> Fragment<R> {
impl Fragment {
/// Creates a new [`Fragment`].
#[inline(always)]
pub fn new(nodes: Vec<AnyView<R>>) -> Self {
pub fn new(nodes: Vec<AnyView>) -> Self {
Self { nodes }
}
}
impl<T, R> IntoFragment<R> for Vec<T>
impl<T> IntoFragment for Vec<T>
where
T: IntoAny<R>,
R: Renderer,
T: IntoAny,
{
fn into_fragment(self) -> Fragment<R> {
fn into_fragment(self) -> Fragment {
Fragment::new(self.into_iter().map(IntoAny::into_any).collect())
}
}
impl<const N: usize, T, R> IntoFragment<R> for [T; N]
impl<const N: usize, T> IntoFragment for [T; N]
where
T: IntoAny<R>,
R: Renderer,
T: IntoAny,
{
fn into_fragment(self) -> Fragment<R> {
fn into_fragment(self) -> Fragment {
Fragment::new(self.into_iter().map(IntoAny::into_any).collect())
}
}
macro_rules! tuples {
($($ty:ident),*) => {
impl<$($ty),*, Rndr> IntoFragment<Rndr> for ($($ty,)*)
impl<$($ty),*> IntoFragment for ($($ty,)*)
where
$($ty: IntoAny<Rndr>),*,
Rndr: Renderer
$($ty: IntoAny),*,
{
fn into_fragment(self) -> Fragment<Rndr> {
fn into_fragment(self) -> Fragment {
#[allow(non_snake_case)]
let ($($ty,)*) = self;
Fragment::new(vec![$($ty.into_any(),)*])

View File

@@ -3,22 +3,20 @@ use super::{
RenderHtml,
};
use crate::{
html::attribute::Attribute, hydration::Cursor, renderer::Renderer,
html::attribute::Attribute, hydration::Cursor, renderer::Rndr,
ssr::StreamBuilder,
};
use either_of::Either;
use itertools::Itertools;
/// Retained view state for an `Option`.
pub type OptionState<T, R> =
Either<<T as Render<R>>::State, <() as Render<R>>::State>;
pub type OptionState<T> = Either<<T as Render>::State, <() as Render>::State>;
impl<T, R> Render<R> for Option<T>
impl<T> Render for Option<T>
where
T: Render<R>,
R: Renderer,
T: Render,
{
type State = OptionState<T, R>;
type State = OptionState<T>;
fn build(self) -> Self::State {
match self {
@@ -37,29 +35,27 @@ where
}
}
impl<T, R> AddAnyAttr<R> for Option<T>
impl<T> AddAnyAttr for Option<T>
where
T: AddAnyAttr<R>,
R: Renderer,
T: AddAnyAttr,
{
type Output<SomeNewAttr: Attribute<R>> =
Option<<T as AddAnyAttr<R>>::Output<SomeNewAttr>>;
type Output<SomeNewAttr: Attribute> =
Option<<T as AddAnyAttr>::Output<SomeNewAttr>>;
fn add_any_attr<NewAttr: Attribute<R>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<R>,
Self::Output<NewAttr>: RenderHtml,
{
self.map(|n| n.add_any_attr(attr))
}
}
impl<T, R> RenderHtml<R> for Option<T>
impl<T> RenderHtml for Option<T>
where
T: RenderHtml<R>,
R: Renderer,
T: RenderHtml,
{
type AsyncOutput = Option<T::AsyncOutput>;
@@ -123,7 +119,7 @@ where
#[track_caller]
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
match self {
@@ -134,15 +130,14 @@ where
}
}
impl<T, R> Render<R> for Vec<T>
impl<T> Render for Vec<T>
where
T: Render<R>,
R: Renderer,
T: Render,
{
type State = VecState<T::State, R>;
type State = VecState<T::State>;
fn build(self) -> Self::State {
let marker = R::create_placeholder();
let marker = Rndr::create_placeholder();
VecState {
states: self.into_iter().map(T::build).collect(),
marker,
@@ -156,7 +151,7 @@ where
if old.is_empty() {
let mut new = self.build().states;
for item in new.iter_mut() {
R::mount_before(item, marker.as_ref());
Rndr::mount_before(item, marker.as_ref());
}
*old = new;
} else if self.is_empty() {
@@ -175,7 +170,7 @@ where
}
itertools::EitherOrBoth::Left(new) => {
let mut new_state = new.build();
R::mount_before(&mut new_state, marker.as_ref());
Rndr::mount_before(&mut new_state, marker.as_ref());
adds.push(new_state);
}
itertools::EitherOrBoth::Right(old) => {
@@ -191,23 +186,21 @@ where
}
/// Retained view state for a `Vec<_>`.
pub struct VecState<T, R>
pub struct VecState<T>
where
T: Mountable<R>,
R: Renderer,
T: Mountable,
{
states: Vec<T>,
// Vecs keep a placeholder because they have the potential to add additional items,
// after their own items but before the next neighbor. It is much easier to add an
// item before a known placeholder than to add it after the last known item, so we
// just leave a placeholder here unlike zero-or-one iterators (Option, Result, etc.)
marker: R::Placeholder,
marker: crate::renderer::types::Placeholder,
}
impl<T, R> Mountable<R> for VecState<T, R>
impl<T> Mountable for VecState<T>
where
T: Mountable<R>,
R: Renderer,
T: Mountable,
{
fn unmount(&mut self) {
for state in self.states.iter_mut() {
@@ -218,8 +211,8 @@ where
fn mount(
&mut self,
parent: &<R as Renderer>::Element,
marker: Option<&<R as Renderer>::Node>,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
for state in self.states.iter_mut() {
state.mount(parent, marker);
@@ -227,7 +220,7 @@ where
self.marker.mount(parent, marker);
}
fn insert_before_this(&self, child: &mut dyn Mountable<R>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
if let Some(first) = self.states.first() {
first.insert_before_this(child)
} else {
@@ -236,20 +229,19 @@ where
}
}
impl<T, R> AddAnyAttr<R> for Vec<T>
impl<T> AddAnyAttr for Vec<T>
where
T: AddAnyAttr<R>,
R: Renderer,
T: AddAnyAttr,
{
type Output<SomeNewAttr: Attribute<R>> =
Vec<<T as AddAnyAttr<R>>::Output<SomeNewAttr::Cloneable>>;
type Output<SomeNewAttr: Attribute> =
Vec<<T as AddAnyAttr>::Output<SomeNewAttr::Cloneable>>;
fn add_any_attr<NewAttr: Attribute<R>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<R>,
Self::Output<NewAttr>: RenderHtml,
{
let attr = attr.into_cloneable();
self.into_iter()
@@ -258,10 +250,9 @@ where
}
}
impl<T, R> RenderHtml<R> for Vec<T>
impl<T> RenderHtml for Vec<T>
where
T: RenderHtml<R>,
R: Renderer,
T: RenderHtml,
{
type AsyncOutput = Vec<T::AsyncOutput>;
@@ -332,7 +323,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
let states = self

View File

@@ -5,84 +5,74 @@ use super::{
use crate::{
html::attribute::Attribute,
hydration::Cursor,
renderer::{CastFrom, Renderer},
renderer::{CastFrom, Rndr},
ssr::StreamBuilder,
};
use drain_filter_polyfill::VecExt as VecDrainFilterExt;
use indexmap::IndexSet;
use rustc_hash::FxHasher;
use std::{
hash::{BuildHasherDefault, Hash},
marker::PhantomData,
};
use std::hash::{BuildHasherDefault, Hash};
type FxIndexSet<T> = IndexSet<T, BuildHasherDefault<FxHasher>>;
/// Creates a keyed list of views.
pub fn keyed<T, I, K, KF, VF, VFS, V, Rndr>(
pub fn keyed<T, I, K, KF, VF, VFS, V>(
items: I,
key_fn: KF,
view_fn: VF,
) -> Keyed<T, I, K, KF, VF, VFS, V, Rndr>
) -> Keyed<T, I, K, KF, VF, VFS, V>
where
I: IntoIterator<Item = T>,
K: Eq + Hash + 'static,
KF: Fn(&T) -> K,
V: Render<Rndr>,
V: Render,
VF: Fn(usize, T) -> (VFS, V),
VFS: Fn(usize),
Rndr: Renderer,
{
Keyed {
items,
key_fn,
view_fn,
rndr: PhantomData,
}
}
/// A keyed list of views.
pub struct Keyed<T, I, K, KF, VF, VFS, V, Rndr>
pub struct Keyed<T, I, K, KF, VF, VFS, V>
where
I: IntoIterator<Item = T>,
K: Eq + Hash + 'static,
KF: Fn(&T) -> K,
VF: Fn(usize, T) -> (VFS, V),
VFS: Fn(usize),
Rndr: Renderer,
{
items: I,
key_fn: KF,
view_fn: VF,
rndr: PhantomData<Rndr>,
}
/// Retained view state for a keyed list.
pub struct KeyedState<K, VFS, V, Rndr>
pub struct KeyedState<K, VFS, V>
where
K: Eq + Hash + 'static,
VFS: Fn(usize),
V: Render<Rndr>,
Rndr: Renderer,
V: Render,
{
parent: Option<Rndr::Element>,
marker: Rndr::Placeholder,
parent: Option<crate::renderer::types::Element>,
marker: crate::renderer::types::Placeholder,
hashed_items: IndexSet<K, BuildHasherDefault<FxHasher>>,
rendered_items: Vec<Option<(VFS, V::State)>>,
}
impl<T, I, K, KF, VF, VFS, V, Rndr> Render<Rndr>
for Keyed<T, I, K, KF, VF, VFS, V, Rndr>
impl<T, I, K, KF, VF, VFS, V> Render for Keyed<T, I, K, KF, VF, VFS, V>
where
I: IntoIterator<Item = T>,
K: Eq + Hash + 'static,
KF: Fn(&T) -> K,
V: Render<Rndr>,
V: Render,
VF: Fn(usize, T) -> (VFS, V),
VFS: Fn(usize),
Rndr: Renderer,
{
type State = KeyedState<K, VFS, V, Rndr>;
type State = KeyedState<K, VFS, V>;
// TODO fallible state and try_build()/try_rebuild() here
fn build(self) -> Self::State {
@@ -139,20 +129,18 @@ where
}
}
impl<T, I, K, KF, VF, VFS, V, Rndr> AddAnyAttr<Rndr>
for Keyed<T, I, K, KF, VF, VFS, V, Rndr>
impl<T, I, K, KF, VF, VFS, V> AddAnyAttr for Keyed<T, I, K, KF, VF, VFS, V>
where
I: IntoIterator<Item = T> + Send,
K: Eq + Hash + 'static,
KF: Fn(&T) -> K + Send,
V: RenderHtml<Rndr>,
V: RenderHtml,
V: 'static,
VF: Fn(usize, T) -> (VFS, V) + Send + 'static,
VFS: Fn(usize) + 'static,
T: 'static,
Rndr: Renderer,
{
type Output<SomeNewAttr: Attribute<Rndr>> = Keyed<
type Output<SomeNewAttr: Attribute> = Keyed<
T,
I,
K,
@@ -163,28 +151,24 @@ where
T,
) -> (
VFS,
<V as AddAnyAttr<Rndr>>::Output<
SomeNewAttr::CloneableOwned,
>,
<V as AddAnyAttr>::Output<SomeNewAttr::CloneableOwned>,
) + Send,
>,
VFS,
V::Output<SomeNewAttr::CloneableOwned>,
Rndr,
>;
fn add_any_attr<NewAttr: Attribute<Rndr>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Rndr>,
Self::Output<NewAttr>: RenderHtml,
{
let Keyed {
items,
key_fn,
view_fn,
rndr,
} = self;
let attr = attr.into_cloneable_owned();
Keyed {
@@ -194,22 +178,19 @@ where
let (index, view) = view_fn(index, item);
(index, view.add_any_attr(attr.clone()))
}),
rndr,
}
}
}
impl<T, I, K, KF, VF, VFS, V, Rndr> RenderHtml<Rndr>
for Keyed<T, I, K, KF, VF, VFS, V, Rndr>
impl<T, I, K, KF, VF, VFS, V> RenderHtml for Keyed<T, I, K, KF, VF, VFS, V>
where
I: IntoIterator<Item = T> + Send,
K: Eq + Hash + 'static,
KF: Fn(&T) -> K + Send,
V: RenderHtml<Rndr> + 'static,
V: RenderHtml + 'static,
VF: Fn(usize, T) -> (VFS, V) + Send + 'static,
VFS: Fn(usize) + 'static,
T: 'static,
Rndr: Renderer,
{
type AsyncOutput = Vec<V::AsyncOutput>; // TODO
@@ -268,7 +249,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<Rndr>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
// get parent and position
@@ -279,7 +260,7 @@ where
Rndr::get_parent(&current)
.expect("first child of keyed list has no parent")
};
let parent = Rndr::Element::cast_from(parent)
let parent = crate::renderer::types::Element::cast_from(parent)
.expect("parent of keyed list should be an element");
// build list
@@ -304,14 +285,17 @@ where
}
}
impl<K, VFS, V, Rndr> Mountable<Rndr> for KeyedState<K, VFS, V, Rndr>
impl<K, VFS, V> Mountable for KeyedState<K, VFS, V>
where
K: Eq + Hash + 'static,
VFS: Fn(usize),
V: Render<Rndr>,
Rndr: Renderer,
V: Render,
{
fn mount(&mut self, parent: &Rndr::Element, marker: Option<&Rndr::Node>) {
fn mount(
&mut self,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
self.parent = Some(parent.clone());
for (_, item) in self.rendered_items.iter_mut().flatten() {
item.mount(parent, marker);
@@ -326,7 +310,7 @@ where
self.marker.unmount();
}
fn insert_before_this(&self, child: &mut dyn Mountable<Rndr>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
self.rendered_items
.first()
.map(|item| {
@@ -519,17 +503,16 @@ impl Default for DiffOpAddMode {
}
}
fn apply_diff<T, VFS, V, Rndr>(
parent: &Rndr::Element,
marker: &Rndr::Placeholder,
fn apply_diff<T, VFS, V>(
parent: &crate::renderer::types::Element,
marker: &crate::renderer::types::Placeholder,
diff: Diff,
children: &mut Vec<Option<(VFS, V::State)>>,
view_fn: impl Fn(usize, T) -> (VFS, V),
mut items: Vec<Option<T>>,
) where
VFS: Fn(usize),
V: Render<Rndr>,
Rndr: Renderer,
V: Render,
{
// The order of cmds needs to be:
// 1. Clear

View File

@@ -1,13 +1,7 @@
use self::add_attr::AddAnyAttr;
use crate::{hydration::Cursor, renderer::Renderer, ssr::StreamBuilder};
use crate::{hydration::Cursor, ssr::StreamBuilder};
use parking_lot::RwLock;
use std::{
cell::RefCell,
future::Future,
ops::{Deref, DerefMut},
rc::Rc,
sync::Arc,
};
use std::{cell::RefCell, future::Future, rc::Rc, sync::Arc};
/// Add attributes to typed views.
pub mod add_attr;
@@ -38,12 +32,12 @@ pub mod tuples;
///
/// It is generic over the renderer itself, as long as that implements the [`Renderer`]
/// trait.
pub trait Render<R: Renderer>: Sized {
pub trait Render: Sized {
/// The “view state” for this type, which can be retained between updates.
///
/// For example, for a text node, `State` might be the actual DOM text node
/// and the previous string, to allow for diffing between updates.
type State: Mountable<R>;
type State: Mountable;
/// Creates the view for the first time, without hydrating from existing HTML.
fn build(self) -> Self::State;
@@ -98,12 +92,12 @@ impl MarkBranch for StreamBuilder {
/// can be transformed into some HTML that is used to create a `<template>` node, which
/// can be cloned many times and “hydrated,” which is more efficient than creating the
/// whole view piece by piece.
pub trait RenderHtml<R: Renderer>
pub trait RenderHtml
where
Self: Render<R> + AddAnyAttr<R> + Send,
Self: Render + AddAnyAttr + Send,
{
/// The type of the view after waiting for all asynchronous data to load.
type AsyncOutput: RenderHtml<R>;
type AsyncOutput: RenderHtml;
/// The minimum length of HTML created when this view is rendered.
const MIN_LENGTH: usize;
@@ -253,14 +247,14 @@ where
/// (e.g., into a `<template>` element).
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State;
/// Hydrates using [`RenderHtml::hydrate`], beginning at the given element.
fn hydrate_from<const FROM_SERVER: bool>(
self,
el: &R::Element,
el: &crate::renderer::types::Element,
) -> Self::State
where
Self: Sized,
@@ -271,7 +265,7 @@ where
/// Hydrates using [`RenderHtml::hydrate`], beginning at the given element and position.
fn hydrate_from_position<const FROM_SERVER: bool>(
self,
el: &R::Element,
el: &crate::renderer::types::Element,
position: Position,
) -> Self::State
where
@@ -284,24 +278,28 @@ where
}
/// Allows a type to be mounted to the DOM.
pub trait Mountable<R: Renderer> {
pub trait Mountable {
/// Detaches the view from the DOM.
fn unmount(&mut self);
/// Mounts a node to the interface.
fn mount(&mut self, parent: &R::Element, marker: Option<&R::Node>);
fn mount(
&mut self,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
);
/// Inserts another `Mountable` type before this one. Returns `false` if
/// this does not actually exist in the UI (for example, `()`).
fn insert_before_this(&self, child: &mut dyn Mountable<R>) -> bool;
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool;
/// Inserts another `Mountable` type before this one, or before the marker
/// if this one doesn't exist in the UI (for example, `()`).
fn insert_before_this_or_marker(
&self,
parent: &R::Element,
child: &mut dyn Mountable<R>,
marker: Option<&R::Node>,
parent: &crate::renderer::types::Element,
child: &mut dyn Mountable,
marker: Option<&crate::renderer::types::Node>,
) {
if !self.insert_before_this(child) {
child.mount(parent, marker);
@@ -310,20 +308,16 @@ pub trait Mountable<R: Renderer> {
}
/// Indicates where a node should be mounted to its parent.
pub enum MountKind<R>
where
R: Renderer,
{
pub enum MountKind {
/// Node should be mounted before this marker node.
Before(R::Node),
Before(crate::renderer::types::Node),
/// Node should be appended to the parents children.
Append,
}
impl<T, R> Mountable<R> for Option<T>
impl<T> Mountable for Option<T>
where
T: Mountable<R>,
R: Renderer,
T: Mountable,
{
fn unmount(&mut self) {
if let Some(ref mut mounted) = self {
@@ -331,33 +325,40 @@ where
}
}
fn mount(&mut self, parent: &R::Element, marker: Option<&R::Node>) {
fn mount(
&mut self,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
if let Some(ref mut inner) = self {
inner.mount(parent, marker);
}
}
fn insert_before_this(&self, child: &mut dyn Mountable<R>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
self.as_ref()
.map(|inner| inner.insert_before_this(child))
.unwrap_or(false)
}
}
impl<T, R> Mountable<R> for Rc<RefCell<T>>
impl<T> Mountable for Rc<RefCell<T>>
where
T: Mountable<R>,
R: Renderer,
T: Mountable,
{
fn unmount(&mut self) {
self.borrow_mut().unmount()
}
fn mount(&mut self, parent: &R::Element, marker: Option<&R::Node>) {
fn mount(
&mut self,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
self.borrow_mut().mount(parent, marker);
}
fn insert_before_this(&self, child: &mut dyn Mountable<R>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
self.borrow().insert_before_this(child)
}
}
@@ -431,229 +432,3 @@ pub enum Position {
/// This is the last child of its parent.
LastChild,
}
/// A view stored on the heap.
///
/// This is a newtype around `Box<_>` that allows us to implement rendering traits on it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BoxedView<T>(Box<T>);
impl<T> BoxedView<T> {
/// Stores view on the heap.
pub fn new(value: T) -> Self {
Self(Box::new(value))
}
/// Deferences the view to its inner value.
pub fn into_inner(self) -> T {
*self.0
}
}
impl<T> AsRef<T> for BoxedView<T> {
fn as_ref(&self) -> &T {
&self.0
}
}
impl<T> AsMut<T> for BoxedView<T> {
fn as_mut(&mut self) -> &mut T {
&mut self.0
}
}
impl<T> Deref for BoxedView<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for BoxedView<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T, Rndr> Render<Rndr> for BoxedView<T>
where
T: Render<Rndr>,
Rndr: Renderer,
{
type State = T::State;
fn build(self) -> Self::State {
self.into_inner().build()
}
fn rebuild(self, state: &mut Self::State) {
self.into_inner().rebuild(state);
}
}
impl<T, Rndr> RenderHtml<Rndr> for BoxedView<T>
where
T: RenderHtml<Rndr>,
Rndr: Renderer,
{
type AsyncOutput = BoxedView<T::AsyncOutput>;
const MIN_LENGTH: usize = T::MIN_LENGTH;
fn dry_resolve(&mut self) {
self.as_mut().dry_resolve();
}
async fn resolve(self) -> Self::AsyncOutput {
let inner = self.into_inner().resolve().await;
BoxedView::new(inner)
}
fn to_html_with_buf(
self,
buf: &mut String,
position: &mut Position,
escape: bool,
mark_branches: bool,
) {
self.into_inner()
.to_html_with_buf(buf, position, escape, mark_branches)
}
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<Rndr>,
position: &PositionState,
) -> Self::State {
self.into_inner().hydrate::<FROM_SERVER>(cursor, position)
}
}
impl<T> ToTemplate for BoxedView<T>
where
T: ToTemplate,
{
fn to_template(
buf: &mut String,
class: &mut String,
style: &mut String,
inner_html: &mut String,
position: &mut Position,
) {
T::to_template(buf, class, style, inner_html, position);
}
}
/// A newtype around a view that allows us to get out of certain compile errors.
///
/// It is unlikely that you need this in your own work.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct WrappedView<T>(T);
impl<T> WrappedView<T> {
/// Wraps the view.
pub fn new(value: T) -> Self {
Self(value)
}
/// Unwraps the view to its inner value.
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> Deref for WrappedView<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for WrappedView<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T> AsRef<T> for WrappedView<T> {
fn as_ref(&self) -> &T {
&self.0
}
}
impl<T> AsMut<T> for WrappedView<T> {
fn as_mut(&mut self) -> &mut T {
&mut self.0
}
}
impl<T, Rndr> Render<Rndr> for WrappedView<T>
where
T: Render<Rndr>,
Rndr: Renderer,
{
type State = T::State;
fn build(self) -> Self::State {
self.into_inner().build()
}
fn rebuild(self, state: &mut Self::State) {
self.into_inner().rebuild(state);
}
}
impl<T, Rndr> RenderHtml<Rndr> for WrappedView<T>
where
T: RenderHtml<Rndr>,
Rndr: Renderer,
{
type AsyncOutput = BoxedView<T::AsyncOutput>;
const MIN_LENGTH: usize = T::MIN_LENGTH;
fn dry_resolve(&mut self) {
self.as_mut().dry_resolve();
}
async fn resolve(self) -> Self::AsyncOutput {
let inner = self.into_inner().resolve().await;
BoxedView::new(inner)
}
fn to_html_with_buf(
self,
buf: &mut String,
position: &mut Position,
escape: bool,
mark_branches: bool,
) {
self.into_inner()
.to_html_with_buf(buf, position, escape, mark_branches)
}
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<Rndr>,
position: &PositionState,
) -> Self::State {
self.into_inner().hydrate::<FROM_SERVER>(cursor, position)
}
}
impl<T> ToTemplate for WrappedView<T>
where
T: ToTemplate,
{
fn to_template(
buf: &mut String,
class: &mut String,
style: &mut String,
inner_html: &mut String,
position: &mut Position,
) {
T::to_template(buf, class, style, inner_html, position);
}
}

View File

@@ -2,7 +2,7 @@ use super::{Mountable, Position, PositionState, Render, RenderHtml};
use crate::{
hydration::Cursor,
no_attrs,
renderer::{CastFrom, Renderer},
renderer::{CastFrom, Rndr},
view::ToTemplate,
};
use std::{
@@ -20,41 +20,41 @@ macro_rules! render_primitive {
($($child_type:ty),* $(,)?) => {
$(
paste::paste! {
pub struct [<$child_type:camel State>]<R>(R::Text, $child_type) where R: Renderer;
pub struct [<$child_type:camel State>](crate::renderer::types::Text, $child_type);
impl<R: Renderer> Mountable<R> for [<$child_type:camel State>]<R> {
impl Mountable for [<$child_type:camel State>] {
fn unmount(&mut self) {
self.0.unmount()
}
fn mount(
&mut self,
parent: &<R as Renderer>::Element,
marker: Option<&<R as Renderer>::Node>,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
R::insert_node(parent, self.0.as_ref(), marker);
Rndr::insert_node(parent, self.0.as_ref(), marker);
}
fn insert_before_this(&self,
child: &mut dyn Mountable<R>,
child: &mut dyn Mountable,
) -> bool {
self.0.insert_before_this(child)
}
}
impl<R: Renderer> Render<R> for $child_type {
type State = [<$child_type:camel State>]<R>;
impl Render for $child_type {
type State = [<$child_type:camel State>];
fn build(self) -> Self::State {
let node = R::create_text_node(&self.to_string());
let node = Rndr::create_text_node(&self.to_string());
[<$child_type:camel State>](node, self)
}
fn rebuild(self, state: &mut Self::State) {
let [<$child_type:camel State>](node, this) = state;
if &self != this {
R::set_text(node, &self.to_string());
Rndr::set_text(node, &self.to_string());
*this = self;
}
}
@@ -62,9 +62,7 @@ macro_rules! render_primitive {
no_attrs!($child_type);
impl<R> RenderHtml<R> for $child_type
where
R: Renderer,
impl RenderHtml for $child_type
{
type AsyncOutput = Self;
@@ -87,7 +85,7 @@ macro_rules! render_primitive {
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
if position.get() == Position::FirstChild {
@@ -102,11 +100,11 @@ macro_rules! render_primitive {
}
let node = cursor.current();
let node = R::Text::cast_from(node)
let node = crate::renderer::types::Text::cast_from(node)
.expect("couldn't cast text node from node");
if !FROM_SERVER {
R::set_text(&node, &self.to_string());
Rndr::set_text(&node, &self.to_string());
}
position.set(Position::NextChildAfterText);

View File

@@ -1,11 +1,11 @@
use super::{
add_attr::AddAnyAttr, Mountable, Position, PositionState, Render,
RenderHtml, ToTemplate, WrappedView,
RenderHtml, ToTemplate,
};
use crate::{
html::attribute::{Attribute, AttributeKey, AttributeValue, NextAttribute},
hydration::Cursor,
renderer::Renderer,
renderer::Rndr,
};
use std::marker::PhantomData;
@@ -54,10 +54,9 @@ where
}
}
impl<K, const V: &'static str, R> Attribute<R> for StaticAttr<K, V>
impl<K, const V: &'static str> Attribute for StaticAttr<K, V>
where
K: AttributeKey,
R: Renderer,
{
const MIN_LENGTH: usize = K::KEY.len() + 3 + V.len(); // K::KEY + ="..." + V
@@ -78,14 +77,17 @@ where
_style: &mut String,
_inner_html: &mut String,
) {
AttributeValue::<R>::to_html(V, K::KEY, buf)
AttributeValue::to_html(V, K::KEY, buf)
}
fn hydrate<const FROM_SERVER: bool>(self, _el: &R::Element) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(
self,
_el: &crate::renderer::types::Element,
) -> Self::State {
}
fn build(self, el: &R::Element) -> Self::State {
R::set_attribute(el, K::KEY, V);
fn build(self, el: &crate::renderer::types::Element) -> Self::State {
Rndr::set_attribute(el, K::KEY, V);
}
fn rebuild(self, _state: &mut Self::State) {}
@@ -105,14 +107,13 @@ where
}
}
impl<K, const V: &'static str, R> NextAttribute<R> for StaticAttr<K, V>
impl<K, const V: &'static str> NextAttribute for StaticAttr<K, V>
where
K: AttributeKey,
R: Renderer,
{
type Output<NewAttr: Attribute<R>> = (Self, NewAttr);
type Output<NewAttr: Attribute> = (Self, NewAttr);
fn add_any_attr<NewAttr: Attribute<R>>(
fn add_any_attr<NewAttr: Attribute>(
self,
new_attr: NewAttr,
) -> Self::Output<NewAttr> {
@@ -138,27 +139,22 @@ impl<const V: &'static str> AsRef<str> for Static<V> {
}
}
impl<const V: &'static str, R: Renderer> Render<R> for Static<V>
impl<const V: &'static str> Render for Static<V>
where
R::Text: Mountable<R>,
crate::renderer::types::Text: Mountable,
{
type State = Option<R::Text>;
type State = Option<crate::renderer::types::Text>;
fn build(self) -> Self::State {
// a view state has to be returned so it can be mounted
Some(R::create_text_node(V))
Some(Rndr::create_text_node(V))
}
// This type is specified as static, so no rebuilding is done.
fn rebuild(self, _state: &mut Self::State) {}
}
impl<const V: &'static str, R> RenderHtml<R> for Static<V>
where
R: Renderer,
R::Text: Mountable<R>,
{
impl<const V: &'static str> RenderHtml for Static<V> {
type AsyncOutput = Self;
const MIN_LENGTH: usize = V.len();
@@ -194,7 +190,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
if position.get() == Position::FirstChild {
@@ -212,20 +208,17 @@ where
}
}
impl<R, const V: &'static str> AddAnyAttr<R> for Static<V>
where
R: Renderer,
{
type Output<SomeNewAttr: Attribute<R>> = WrappedView<Static<V>>;
impl<const V: &'static str> AddAnyAttr for Static<V> {
type Output<SomeNewAttr: Attribute> = Static<V>;
fn add_any_attr<NewAttr: Attribute<R>>(
fn add_any_attr<NewAttr: Attribute>(
self,
_attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<R>,
Self::Output<NewAttr>: RenderHtml,
{
WrappedView::new(self)
todo!()
}
}

View File

@@ -4,7 +4,7 @@ use super::{
use crate::{
hydration::Cursor,
no_attrs,
renderer::{CastFrom, Renderer},
renderer::{CastFrom, Rndr},
};
use std::{borrow::Cow, rc::Rc, sync::Arc};
@@ -14,32 +14,29 @@ no_attrs!(Arc<str>);
no_attrs!(Cow<'a, str>);
/// Retained view state for `&str`.
pub struct StrState<'a, R: Renderer> {
pub(crate) node: R::Text,
pub struct StrState<'a> {
pub(crate) node: crate::renderer::types::Text,
str: &'a str,
}
impl<'a, R: Renderer> Render<R> for &'a str {
type State = StrState<'a, R>;
impl<'a> Render for &'a str {
type State = StrState<'a>;
fn build(self) -> Self::State {
let node = R::create_text_node(self);
let node = Rndr::create_text_node(self);
StrState { node, str: self }
}
fn rebuild(self, state: &mut Self::State) {
let StrState { node, str } = state;
if &self != str {
R::set_text(node, self);
Rndr::set_text(node, self);
*str = self;
}
}
}
impl<'a, R> RenderHtml<R> for &'a str
where
R: Renderer,
{
impl<'a> RenderHtml for &'a str {
type AsyncOutput = Self;
const MIN_LENGTH: usize = 0;
@@ -78,7 +75,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
if position.get() == Position::FirstChild {
@@ -93,11 +90,11 @@ where
}
let node = cursor.current();
let node = R::Text::cast_from(node)
let node = crate::renderer::types::Text::cast_from(node)
.expect("couldn't cast text node from node");
if !FROM_SERVER {
R::set_text(&node, self);
Rndr::set_text(&node, self);
}
position.set(Position::NextChildAfterText);
@@ -123,54 +120,48 @@ impl<'a> ToTemplate for &'a str {
}
}
impl<'a, R> Mountable<R> for StrState<'a, R>
where
R: Renderer,
{
impl<'a> Mountable for StrState<'a> {
fn unmount(&mut self) {
self.node.unmount()
}
fn mount(
&mut self,
parent: &<R as Renderer>::Element,
marker: Option<&<R as Renderer>::Node>,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
R::insert_node(parent, self.node.as_ref(), marker);
Rndr::insert_node(parent, self.node.as_ref(), marker);
}
fn insert_before_this(&self, child: &mut dyn Mountable<R>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
self.node.insert_before_this(child)
}
}
/// Retained view state for `String`.
pub struct StringState<R: Renderer> {
node: R::Text,
pub struct StringState {
node: crate::renderer::types::Text,
str: String,
}
impl<R: Renderer> Render<R> for String {
type State = StringState<R>;
impl Render for String {
type State = StringState;
fn build(self) -> Self::State {
let node = R::create_text_node(&self);
let node = Rndr::create_text_node(&self);
StringState { node, str: self }
}
fn rebuild(self, state: &mut Self::State) {
let StringState { node, str } = state;
if &self != str {
R::set_text(node, &self);
Rndr::set_text(node, &self);
*str = self;
}
}
}
impl<R> RenderHtml<R> for String
where
R: Renderer,
{
impl RenderHtml for String {
const MIN_LENGTH: usize = 0;
type AsyncOutput = Self;
@@ -191,7 +182,7 @@ where
escape: bool,
mark_branches: bool,
) {
<&str as RenderHtml<R>>::to_html_with_buf(
<&str as RenderHtml>::to_html_with_buf(
self.as_str(),
buf,
position,
@@ -202,7 +193,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
let StrState { node, .. } =
@@ -227,42 +218,42 @@ impl ToTemplate for String {
}
}
impl<R: Renderer> Mountable<R> for StringState<R> {
impl Mountable for StringState {
fn unmount(&mut self) {
self.node.unmount()
}
fn mount(
&mut self,
parent: &<R as Renderer>::Element,
marker: Option<&<R as Renderer>::Node>,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
R::insert_node(parent, self.node.as_ref(), marker);
Rndr::insert_node(parent, self.node.as_ref(), marker);
}
fn insert_before_this(&self, child: &mut dyn Mountable<R>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
self.node.insert_before_this(child)
}
}
/// Retained view state for `Rc<str>`.
pub struct RcStrState<R: Renderer> {
node: R::Text,
pub struct RcStrState {
node: crate::renderer::types::Text,
str: Rc<str>,
}
impl<R: Renderer> Render<R> for Rc<str> {
type State = RcStrState<R>;
impl Render for Rc<str> {
type State = RcStrState;
fn build(self) -> Self::State {
let node = R::create_text_node(&self);
let node = Rndr::create_text_node(&self);
RcStrState { node, str: self }
}
fn rebuild(self, state: &mut Self::State) {
let RcStrState { node, str } = state;
if !Rc::ptr_eq(&self, str) {
R::set_text(node, &self);
Rndr::set_text(node, &self);
*str = self;
}
}
@@ -271,9 +262,9 @@ impl<R: Renderer> Render<R> for Rc<str> {
// can't Send an Rc<str> between threads, so can't implement async HTML rendering that might need
// to send it
/*
impl<R> RenderHtml<R> for Rc<str>
impl RenderHtml for Rc<str>
where
R: Renderer,
{
type AsyncOutput = Self;
@@ -288,12 +279,12 @@ where
}
fn to_html_with_buf(self, buf: &mut String, position: &mut Position, escape: bool, mark_branches: bool) {
<&str as RenderHtml<R>>::to_html_with_buf(&self, buf, position)
<&str as RenderHtml>::to_html_with_buf(&self, buf, position)
}
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
let this: &str = self.as_ref();
@@ -319,51 +310,48 @@ impl ToTemplate for Rc<str> {
}
}
impl<R: Renderer> Mountable<R> for RcStrState<R> {
impl Mountable for RcStrState {
fn unmount(&mut self) {
self.node.unmount()
}
fn mount(
&mut self,
parent: &<R as Renderer>::Element,
marker: Option<&<R as Renderer>::Node>,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
R::insert_node(parent, self.node.as_ref(), marker);
Rndr::insert_node(parent, self.node.as_ref(), marker);
}
fn insert_before_this(&self, child: &mut dyn Mountable<R>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
self.node.insert_before_this(child)
}
}
/// Retained view state for `Arc<str>`.
pub struct ArcStrState<R: Renderer> {
node: R::Text,
pub struct ArcStrState {
node: crate::renderer::types::Text,
str: Arc<str>,
}
impl<R: Renderer> Render<R> for Arc<str> {
type State = ArcStrState<R>;
impl Render for Arc<str> {
type State = ArcStrState;
fn build(self) -> Self::State {
let node = R::create_text_node(&self);
let node = Rndr::create_text_node(&self);
ArcStrState { node, str: self }
}
fn rebuild(self, state: &mut Self::State) {
let ArcStrState { node, str } = state;
if !Arc::ptr_eq(&self, str) {
R::set_text(node, &self);
Rndr::set_text(node, &self);
*str = self;
}
}
}
impl<R> RenderHtml<R> for Arc<str>
where
R: Renderer,
{
impl RenderHtml for Arc<str> {
type AsyncOutput = Self;
const MIN_LENGTH: usize = 0;
@@ -385,7 +373,7 @@ where
escape: bool,
mark_branches: bool,
) {
<&str as RenderHtml<R>>::to_html_with_buf(
<&str as RenderHtml>::to_html_with_buf(
&self,
buf,
position,
@@ -396,7 +384,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
let this: &str = self.as_ref();
@@ -422,51 +410,48 @@ impl ToTemplate for Arc<str> {
}
}
impl<R: Renderer> Mountable<R> for ArcStrState<R> {
impl Mountable for ArcStrState {
fn unmount(&mut self) {
self.node.unmount()
}
fn mount(
&mut self,
parent: &<R as Renderer>::Element,
marker: Option<&<R as Renderer>::Node>,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
R::insert_node(parent, self.node.as_ref(), marker);
Rndr::insert_node(parent, self.node.as_ref(), marker);
}
fn insert_before_this(&self, child: &mut dyn Mountable<R>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
self.node.insert_before_this(child)
}
}
/// Retained view state for `Cow<'_, str>`.
pub struct CowStrState<'a, R: Renderer> {
node: R::Text,
pub struct CowStrState<'a> {
node: crate::renderer::types::Text,
str: Cow<'a, str>,
}
impl<'a, R: Renderer> Render<R> for Cow<'a, str> {
type State = CowStrState<'a, R>;
impl<'a> Render for Cow<'a, str> {
type State = CowStrState<'a>;
fn build(self) -> Self::State {
let node = R::create_text_node(&self);
let node = Rndr::create_text_node(&self);
CowStrState { node, str: self }
}
fn rebuild(self, state: &mut Self::State) {
let CowStrState { node, str } = state;
if self != *str {
R::set_text(node, &self);
Rndr::set_text(node, &self);
*str = self;
}
}
}
impl<'a, R> RenderHtml<R> for Cow<'a, str>
where
R: Renderer,
{
impl<'a> RenderHtml for Cow<'a, str> {
type AsyncOutput = Self;
const MIN_LENGTH: usize = 0;
@@ -488,7 +473,7 @@ where
escape: bool,
mark_branches: bool,
) {
<&str as RenderHtml<R>>::to_html_with_buf(
<&str as RenderHtml>::to_html_with_buf(
&self,
buf,
position,
@@ -499,7 +484,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
let this: &str = self.as_ref();
@@ -525,20 +510,20 @@ impl<'a> ToTemplate for Cow<'a, str> {
}
}
impl<'a, R: Renderer> Mountable<R> for CowStrState<'a, R> {
impl<'a> Mountable for CowStrState<'a> {
fn unmount(&mut self) {
self.node.unmount()
}
fn mount(
&mut self,
parent: &<R as Renderer>::Element,
marker: Option<&<R as Renderer>::Node>,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
R::insert_node(parent, self.node.as_ref(), marker);
Rndr::insert_node(parent, self.node.as_ref(), marker);
}
fn insert_before_this(&self, child: &mut dyn Mountable<R>) -> bool {
fn insert_before_this(&self, child: &mut dyn Mountable) -> bool {
self.node.insert_before_this(child)
}
}

View File

@@ -2,44 +2,35 @@ use super::{
add_attr::AddAnyAttr, Mountable, Position, PositionState, Render,
RenderHtml, ToTemplate,
};
use crate::{
html::attribute::Attribute, hydration::Cursor, renderer::DomRenderer,
};
use std::marker::PhantomData;
use crate::{html::attribute::Attribute, hydration::Cursor, renderer::Rndr};
/// A view wrapper that uses a `<template>` node to optimize DOM node creation.
///
/// Rather than creating all of the DOM nodes each time it is built, this template will create a
/// single `<template>` node once, then use `.cloneNode(true)` to clone that entire tree, and
/// hydrate it to add event listeners and interactivity for this instance.
pub struct ViewTemplate<V, R> {
pub struct ViewTemplate<V> {
view: V,
rndr: PhantomData<R>,
}
impl<V, R> ViewTemplate<V, R>
impl<V> ViewTemplate<V>
where
V: Render<R> + ToTemplate + 'static,
R: DomRenderer,
V: Render + ToTemplate + 'static,
{
/// Creates a new view template.
pub fn new(view: V) -> Self {
Self {
view,
rndr: PhantomData,
}
Self { view }
}
fn to_template() -> R::TemplateElement {
R::get_template::<V>()
fn to_template() -> crate::renderer::types::TemplateElement {
Rndr::get_template::<V>()
}
}
impl<V, R> Render<R> for ViewTemplate<V, R>
impl<V> Render for ViewTemplate<V>
where
V: Render<R> + RenderHtml<R> + ToTemplate + 'static,
V::State: Mountable<R>,
R: DomRenderer,
V: Render + RenderHtml + ToTemplate + 'static,
V::State: Mountable,
{
type State = V::State;
@@ -47,7 +38,7 @@ where
fn build(self) -> Self::State {
let tpl = Self::to_template();
let contents = R::clone_template(&tpl);
let contents = Rndr::clone_template(&tpl);
self.view
.hydrate::<false>(&Cursor::new(contents), &Default::default())
}
@@ -57,30 +48,28 @@ where
}
}
impl<V, R> AddAnyAttr<R> for ViewTemplate<V, R>
impl<V> AddAnyAttr for ViewTemplate<V>
where
V: RenderHtml<R> + ToTemplate + 'static,
V::State: Mountable<R>,
R: DomRenderer,
V: RenderHtml + ToTemplate + 'static,
V::State: Mountable,
{
type Output<SomeNewAttr: Attribute<R>> = ViewTemplate<V, R>;
type Output<SomeNewAttr: Attribute> = ViewTemplate<V>;
fn add_any_attr<NewAttr: Attribute<R>>(
fn add_any_attr<NewAttr: Attribute>(
self,
_attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<R>,
Self::Output<NewAttr>: RenderHtml,
{
panic!("AddAnyAttr not supported on ViewTemplate");
}
}
impl<V, R> RenderHtml<R> for ViewTemplate<V, R>
impl<V> RenderHtml for ViewTemplate<V>
where
V: RenderHtml<R> + ToTemplate + 'static,
V::State: Mountable<R>,
R: DomRenderer,
V: RenderHtml + ToTemplate + 'static,
V::State: Mountable,
{
type AsyncOutput = V::AsyncOutput;
@@ -99,7 +88,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
self.view.hydrate::<FROM_SERVER>(cursor, position)
@@ -114,11 +103,10 @@ where
}
}
impl<V, R> ToTemplate for ViewTemplate<V, R>
impl<V> ToTemplate for ViewTemplate<V>
where
V: RenderHtml<R> + ToTemplate + 'static,
V::State: Mountable<R>,
R: DomRenderer,
V: RenderHtml + ToTemplate + 'static,
V::State: Mountable,
{
const TEMPLATE: &'static str = V::TEMPLATE;

View File

@@ -1,30 +1,27 @@
use super::{
Mountable, Position, PositionState, Render, RenderHtml, Renderer,
ToTemplate,
Mountable, Position, PositionState, Render, RenderHtml, ToTemplate,
};
use crate::{
html::attribute::Attribute,
hydration::Cursor,
renderer::Rndr,
view::{add_attr::AddAnyAttr, StreamBuilder},
};
use const_str_slice_concat::{
const_concat, const_concat_with_separator, str_from_buffer,
};
impl<R: Renderer> Render<R> for () {
type State = R::Placeholder;
impl Render for () {
type State = crate::renderer::types::Placeholder;
fn build(self) -> Self::State {
R::create_placeholder()
Rndr::create_placeholder()
}
fn rebuild(self, _state: &mut Self::State) {}
}
impl<R> RenderHtml<R> for ()
where
R: Renderer,
{
impl RenderHtml for () {
type AsyncOutput = ();
const MIN_LENGTH: usize = 3;
@@ -43,7 +40,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
cursor.next_placeholder(position)
@@ -54,29 +51,30 @@ where
fn dry_resolve(&mut self) {}
}
impl<Rndr> AddAnyAttr<Rndr> for ()
where
Rndr: Renderer,
{
type Output<SomeNewAttr: Attribute<Rndr>> = ();
impl AddAnyAttr for () {
type Output<SomeNewAttr: Attribute> = ();
fn add_any_attr<NewAttr: Attribute<Rndr>>(
fn add_any_attr<NewAttr: Attribute>(
self,
_attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Rndr>,
Self::Output<NewAttr>: RenderHtml,
{
().add_any_attr(_attr)
}
}
impl<R: Renderer> Mountable<R> for () {
impl Mountable for () {
fn unmount(&mut self) {}
fn mount(&mut self, _parent: &R::Element, _marker: Option<&R::Node>) {}
fn mount(
&mut self,
_parent: &crate::renderer::types::Element,
_marker: Option<&crate::renderer::types::Node>,
) {
}
fn insert_before_this(&self, _child: &mut dyn Mountable<R>) -> bool {
fn insert_before_this(&self, _child: &mut dyn Mountable) -> bool {
false
}
}
@@ -95,7 +93,7 @@ impl ToTemplate for () {
}
}
impl<A: Render<R>, R: Renderer> Render<R> for (A,) {
impl<A: Render> Render for (A,) {
type State = A::State;
fn build(self) -> Self::State {
@@ -107,10 +105,9 @@ impl<A: Render<R>, R: Renderer> Render<R> for (A,) {
}
}
impl<A, R> RenderHtml<R> for (A,)
impl<A> RenderHtml for (A,)
where
A: RenderHtml<R>,
R: Renderer,
A: RenderHtml,
{
type AsyncOutput = (A::AsyncOutput,);
@@ -150,7 +147,7 @@ where
fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor<R>,
cursor: &Cursor,
position: &PositionState,
) -> Self::State {
self.0.hydrate::<FROM_SERVER>(cursor, position)
@@ -181,19 +178,18 @@ impl<A: ToTemplate> ToTemplate for (A,) {
}
}
impl<A, Rndr> AddAnyAttr<Rndr> for (A,)
impl<A> AddAnyAttr for (A,)
where
A: AddAnyAttr<Rndr>,
Rndr: Renderer,
A: AddAnyAttr,
{
type Output<SomeNewAttr: Attribute<Rndr>> = (A::Output<SomeNewAttr>,);
type Output<SomeNewAttr: Attribute> = (A::Output<SomeNewAttr>,);
fn add_any_attr<NewAttr: Attribute<Rndr>>(
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Rndr>,
Self::Output<NewAttr>: RenderHtml,
{
(self.0.add_any_attr(attr),)
}
@@ -201,11 +197,11 @@ where
macro_rules! impl_view_for_tuples {
($first:ident, $($ty:ident),* $(,)?) => {
impl<$first, $($ty),*, Rndr> Render<Rndr> for ($first, $($ty,)*)
impl<$first, $($ty),*> Render for ($first, $($ty,)*)
where
$first: Render<Rndr>,
$($ty: Render<Rndr>),*,
Rndr: Renderer
$first: Render,
$($ty: Render),*,
{
type State = ($first::State, $($ty::State,)*);
@@ -229,11 +225,11 @@ macro_rules! impl_view_for_tuples {
}
}
impl<$first, $($ty),*, Rndr> RenderHtml<Rndr> for ($first, $($ty,)*)
impl<$first, $($ty),*> RenderHtml for ($first, $($ty,)*)
where
$first: RenderHtml<Rndr>,
$($ty: RenderHtml<Rndr>),*,
Rndr: Renderer,
$first: RenderHtml,
$($ty: RenderHtml),*,
{
type AsyncOutput = ($first::AsyncOutput, $($ty::AsyncOutput,)*);
@@ -264,7 +260,7 @@ macro_rules! impl_view_for_tuples {
$($ty.to_html_async_with_buf::<OUT_OF_ORDER>(buf, position, escape, mark_branches));*
}
fn hydrate<const FROM_SERVER: bool>(self, cursor: &Cursor<Rndr>, position: &PositionState) -> Self::State {
fn hydrate<const FROM_SERVER: bool>(self, cursor: &Cursor, position: &PositionState) -> Self::State {
#[allow(non_snake_case)]
let ($first, $($ty,)* ) = self;
(
@@ -311,10 +307,10 @@ macro_rules! impl_view_for_tuples {
}
}
impl<$first, $($ty),*, Rndr> Mountable<Rndr> for ($first, $($ty,)*) where
$first: Mountable<Rndr>,
$($ty: Mountable<Rndr>),*,
Rndr: Renderer
impl<$first, $($ty),*> Mountable for ($first, $($ty,)*) where
$first: Mountable,
$($ty: Mountable),*,
{
fn unmount(&mut self) {
#[allow(non_snake_case)] // better macro performance
@@ -325,8 +321,8 @@ macro_rules! impl_view_for_tuples {
fn mount(
&mut self,
parent: &Rndr::Element,
marker: Option<&Rndr::Node>,
parent: &crate::renderer::types::Element,
marker: Option<&crate::renderer::types::Node>,
) {
#[allow(non_snake_case)] // better macro performance
let ($first, $($ty,)*) = self;
@@ -335,7 +331,7 @@ macro_rules! impl_view_for_tuples {
}
fn insert_before_this(&self,
child: &mut dyn Mountable<Rndr>,
child: &mut dyn Mountable,
) -> bool {
#[allow(non_snake_case)] // better macro performance
let ($first, $($ty,)*) = self;
@@ -344,20 +340,20 @@ macro_rules! impl_view_for_tuples {
}
}
impl<$first, $($ty,)* Rndr> AddAnyAttr<Rndr> for ($first, $($ty,)*)
impl<$first, $($ty,)*> AddAnyAttr for ($first, $($ty,)*)
where
$first: AddAnyAttr<Rndr>,
$($ty: AddAnyAttr<Rndr>),*,
Rndr: Renderer,
{
type Output<SomeNewAttr: Attribute<Rndr>> = ($first::Output<SomeNewAttr::Cloneable>, $($ty::Output<SomeNewAttr::Cloneable>,)*);
$first: AddAnyAttr,
$($ty: AddAnyAttr),*,
fn add_any_attr<NewAttr: Attribute<Rndr>>(
{
type Output<SomeNewAttr: Attribute> = ($first::Output<SomeNewAttr::Cloneable>, $($ty::Output<SomeNewAttr::Cloneable>,)*);
fn add_any_attr<NewAttr: Attribute>(
self,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml<Rndr>,
Self::Output<NewAttr>: RenderHtml,
{
let shared = attr.into_cloneable();
#[allow(non_snake_case)] // better macro performance