It's now possible to have multiple static children

This commit is contained in:
Pauan 2020-06-02 03:24:31 +02:00
parent c498b9e2b1
commit 9b1169fb88
2 changed files with 30 additions and 16 deletions

View File

@ -385,8 +385,9 @@ fn set_property<A, B, C>(element: &A, name: &B, value: C) where A: AsRef<JsValue
pub struct DomBuilder<A> {
element: A,
callbacks: Callbacks,
children: usize,
// TODO verify this with static types instead ?
has_children: bool,
dynamic_children: bool,
}
impl<A> DomBuilder<A> where A: JsCast {
@ -415,7 +416,8 @@ impl<A> DomBuilder<A> {
Self {
element: value,
callbacks: Callbacks::new(),
has_children: false,
children: 0,
dynamic_children: false,
}
}
@ -577,22 +579,29 @@ impl<A> DomBuilder<A> where A: AsRef<EventTarget> {
impl<A> DomBuilder<A> where A: AsRef<Node> {
#[inline]
fn check_children(&mut self) {
assert_eq!(self.has_children, false);
self.has_children = true;
fn check_children(&self) {
assert_eq!(self.dynamic_children, false);
}
#[inline]
pub fn child(mut self, mut child: Dom) -> Self {
self.check_children();
operations::insert_children_one(self.element.as_ref(), &mut self.callbacks, &mut self.children, &mut child);
self
}
// TODO figure out how to make this owned rather than &mut
#[inline]
pub fn children<B: BorrowMut<Dom>, C: IntoIterator<Item = B>>(mut self, children: C) -> Self {
self.check_children();
operations::insert_children_iter(self.element.as_ref(), &mut self.callbacks, children);
operations::insert_children_iter(self.element.as_ref(), &mut self.callbacks, &mut self.children, children);
self
}
#[inline]
pub fn text(mut self, value: &str) -> Self {
self.check_children();
self.children += 1;
// TODO should this intern ?
bindings::append_child(self.element.as_ref(), &bindings::create_text_node(value));
self
@ -604,6 +613,7 @@ impl<A> DomBuilder<A> where A: AsRef<Node> {
C: Signal<Item = B> + 'static {
self.check_children();
self.children += 1;
let element = make_text_signal(&mut self.callbacks, value);
bindings::append_child(self.element.as_ref(), &element);
self
@ -614,6 +624,7 @@ impl<A> DomBuilder<A> where A: AsRef<Node> {
where B: SignalVec<Item = Dom> + 'static {
self.check_children();
self.dynamic_children = true;
operations::insert_children_signal_vec(self.element.as_ref().clone(), &mut self.callbacks, children);
self
}
@ -623,6 +634,7 @@ impl<A> DomBuilder<A> where A: AsRef<Node> {
where B: Signal<Item = Option<Dom>> + 'static {
self.check_children();
self.dynamic_children = true;
operations::insert_child_signal(self.element.as_ref().clone(), &mut self.callbacks, child);
self
}

View File

@ -53,19 +53,21 @@ fn for_each_vec<A, B>(signal: A, mut callback: B) -> CancelableFutureHandle
}
pub(crate) fn insert_children_one(element: &Node, callbacks: &mut Callbacks, children: &mut usize, dom: &mut Dom) {
// TODO can this be made more efficient ?
callbacks.after_insert.append(&mut dom.callbacks.after_insert);
callbacks.after_remove.append(&mut dom.callbacks.after_remove);
bindings::append_child(element, &dom.element);
*children += 1;
}
#[inline]
pub(crate) fn insert_children_iter<A: std::borrow::BorrowMut<Dom>, B: IntoIterator<Item = A>>(element: &Node, callbacks: &mut Callbacks, value: B) {
fn insert_children_one(element: &Node, callbacks: &mut Callbacks, dom: &mut Dom) {
// TODO can this be made more efficient ?
callbacks.after_insert.append(&mut dom.callbacks.after_insert);
callbacks.after_remove.append(&mut dom.callbacks.after_remove);
bindings::append_child(element, &dom.element);
}
pub(crate) fn insert_children_iter<A: std::borrow::BorrowMut<Dom>, B: IntoIterator<Item = A>>(element: &Node, callbacks: &mut Callbacks, children: &mut usize, value: B) {
for mut dom in value {
let dom = std::borrow::BorrowMut::borrow_mut(&mut dom);
insert_children_one(element, callbacks, dom);
insert_children_one(element, callbacks, children, dom);
}
}