feat: rebuild doesn't return errors

This commit is contained in:
Jonathan Kelley committed 2021-08-24 15:12:20 -04:00
1 parent df8aa77198
commit f457b71131
18 files changed
+233 -189

No files matched your search

+1 -1
View File
@@ -45,7 +45,7 @@ fn create_rows(c: &mut Criterion) {
c.bench_function("create rows", |b| { c.bench_function("create rows", |b| {
b.iter(|| { b.iter(|| {
let mut dom = VirtualDom::new(App); let mut dom = VirtualDom::new(App);
let g = dom.rebuild().unwrap(); let g = dom.rebuild();
assert!(g.edits.len() > 1); assert!(g.edits.len() > 1);
}) })
}); });
+1 -1
View File
@@ -6,7 +6,7 @@ use std::fmt::Display;
fn main() { fn main() {
let mut dom = VirtualDom::new(App); let mut dom = VirtualDom::new(App);
let g = dom.rebuild().unwrap(); let g = dom.rebuild();
assert!(g.edits.len() > 1); assert!(g.edits.len() > 1);
} }
+1 -3
View File
@@ -153,7 +153,7 @@ impl<'bump> DiffMachine<'bump> {
/// This method implements a depth-first iterative tree traversal. /// This method implements a depth-first iterative tree traversal.
/// ///
/// We do depth-first to maintain high cache locality (nodes were originally generated recursively). /// We do depth-first to maintain high cache locality (nodes were originally generated recursively).
pub async fn work(&mut self) -> Result<()> { pub async fn work(&mut self) {
// defer to individual functions so the compiler produces better code // defer to individual functions so the compiler produces better code
// large functions tend to be difficult for the compiler to work with // large functions tend to be difficult for the compiler to work with
while let Some(instruction) = self.stack.pop() { while let Some(instruction) = self.stack.pop() {
@@ -190,8 +190,6 @@ impl<'bump> DiffMachine<'bump> {
} }
}; };
} }
Ok(())
} }
fn mount(&mut self, and: MountType<'bump>) { fn mount(&mut self, and: MountType<'bump>) {
+24 -22
View File
@@ -602,8 +602,31 @@ pub mod on {
} }
pub trait KeyboardEventInner { pub trait KeyboardEventInner {
fn alt_key(&self) -> bool;
fn char_code(&self) -> u32; fn char_code(&self) -> u32;
/// Identify which "key" was entered.
///
/// This is the best method to use for all languages. They key gets mapped to a String sequence which you can match on.
/// The key isn't an enum because there are just so many context-dependent keys.
///
/// A full list on which keys to use is available at:
/// <https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values>
///
/// # Example
///
/// ```rust
/// match event.key().as_str() {
/// "Esc" | "Escape" => {}
/// "ArrowDown" => {}
/// "ArrowLeft" => {}
/// _ => {}
/// }
/// ```
///
fn key(&self) -> String;
/// Get the key code as an enum Variant. /// Get the key code as an enum Variant.
/// ///
/// This is intended for things like arrow keys, escape keys, function keys, and other non-international keys. /// This is intended for things like arrow keys, escape keys, function keys, and other non-international keys.
@@ -627,35 +650,14 @@ pub mod on {
/// Check if the ctrl key was pressed down /// Check if the ctrl key was pressed down
fn ctrl_key(&self) -> bool; fn ctrl_key(&self) -> bool;
/// Identify which "key" was entered. fn get_modifier_state(&self, key_code: &str) -> bool;
///
/// This is the best method to use for all languages. They key gets mapped to a String sequence which you can match on.
/// The key isn't an enum because there are just so many context-dependent keys.
///
/// A full list on which keys to use is available at:
/// <https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values>
///
/// # Example
///
/// ```rust
/// match event.key().as_str() {
/// "Esc" | "Escape" => {}
/// "ArrowDown" => {}
/// "ArrowLeft" => {}
/// _ => {}
/// }
/// ```
///
fn key(&self) -> String;
// fn key(&self) -> String;
fn locale(&self) -> String; fn locale(&self) -> String;
fn location(&self) -> usize; fn location(&self) -> usize;
fn meta_key(&self) -> bool; fn meta_key(&self) -> bool;
fn repeat(&self) -> bool; fn repeat(&self) -> bool;
fn shift_key(&self) -> bool; fn shift_key(&self) -> bool;
fn which(&self) -> usize; fn which(&self) -> usize;
fn get_modifier_state(&self, key_code: usize) -> bool;
} }
pub trait FocusEventInner { pub trait FocusEventInner {
+2 -2
View File
@@ -139,8 +139,8 @@ impl<'a> Mutations<'a> {
// refs are only assigned once // refs are only assigned once
pub struct NodeRefMutation<'a> { pub struct NodeRefMutation<'a> {
element: &'a mut Option<once_cell::sync::OnceCell<Box<dyn Any>>>, pub element: &'a mut Option<once_cell::sync::OnceCell<Box<dyn Any>>>,
element_id: ElementId, pub element_id: ElementId,
} }
impl<'a> std::fmt::Debug for NodeRefMutation<'a> { impl<'a> std::fmt::Debug for NodeRefMutation<'a> {
+24 -25
View File
@@ -124,26 +124,12 @@ impl VirtualDom {
} }
} }
pub fn launch_in_place(root: FC<()>) -> Self {
let mut s = Self::new(root);
s.rebuild().unwrap();
s
}
/// Creates a new virtualdom and immediately rebuilds it in place, not caring about the RealDom to write into.
///
pub fn launch_with_props_in_place<P: Properties + 'static>(root: FC<P>, root_props: P) -> Self {
let mut s = Self::new_with_props(root, root_props);
s.rebuild().unwrap();
s
}
pub fn base_scope(&self) -> &Scope { pub fn base_scope(&self) -> &Scope {
unsafe { self.shared.get_scope(self.base_scope).unwrap() } self.shared.get_scope(self.base_scope).unwrap()
} }
pub fn get_scope(&self, id: ScopeId) -> Option<&Scope> { pub fn get_scope(&self, id: ScopeId) -> Option<&Scope> {
unsafe { self.shared.get_scope(id) } self.shared.get_scope(id)
} }
/// Performs a *full* rebuild of the virtual dom, returning every edit required to generate the actual dom rom scratch /// Performs a *full* rebuild of the virtual dom, returning every edit required to generate the actual dom rom scratch
@@ -152,7 +138,7 @@ impl VirtualDom {
/// ///
/// Events like garabge collection, application of refs, etc are not handled by this method and can only be progressed /// Events like garabge collection, application of refs, etc are not handled by this method and can only be progressed
/// through "run". We completely avoid the task scheduler infrastructure. /// through "run". We completely avoid the task scheduler infrastructure.
pub fn rebuild<'s>(&'s mut self) -> Result<Mutations<'s>> { pub fn rebuild<'s>(&'s mut self) -> Mutations<'s> {
let mut fut = self.rebuild_async().boxed_local(); let mut fut = self.rebuild_async().boxed_local();
loop { loop {
@@ -163,7 +149,11 @@ impl VirtualDom {
} }
/// Rebuild the dom from the ground up /// Rebuild the dom from the ground up
pub async fn rebuild_async<'s>(&'s mut self) -> Result<Mutations<'s>> { ///
/// This method is asynchronous to prevent the application from blocking while the dom is being rebuilt. Computing
/// the diff and creating nodes can be expensive, so we provide this method to avoid blocking the main thread. This
/// method can be useful when needing to perform some crucial periodic tasks.
pub async fn rebuild_async<'s>(&'s mut self) -> Mutations<'s> {
let mut diff_machine = DiffMachine::new(Mutations::new(), self.base_scope, &self.shared); let mut diff_machine = DiffMachine::new(Mutations::new(), self.base_scope, &self.shared);
let cur_component = self let cur_component = self
@@ -176,7 +166,7 @@ impl VirtualDom {
diff_machine diff_machine
.stack .stack
.create_node(cur_component.frames.fin_head(), MountType::Append); .create_node(cur_component.frames.fin_head(), MountType::Append);
diff_machine.work().await.unwrap(); diff_machine.work().await;
} else { } else {
// todo: should this be a hard error? // todo: should this be a hard error?
log::warn!( log::warn!(
@@ -185,11 +175,20 @@ impl VirtualDom {
); );
} }
Ok(diff_machine.mutations) diff_machine.mutations
} }
// diff the dom with itself pub fn diff_sync<'s>(&'s mut self) -> Mutations<'s> {
pub async fn diff_async<'s>(&'s mut self) -> Result<Mutations<'s>> { let mut fut = self.diff_async().boxed_local();
loop {
if let Some(edits) = (&mut fut).now_or_never() {
break edits;
}
}
}
pub async fn diff_async<'s>(&'s mut self) -> Mutations<'s> {
let mut diff_machine = DiffMachine::new(Mutations::new(), self.base_scope, &self.shared); let mut diff_machine = DiffMachine::new(Mutations::new(), self.base_scope, &self.shared);
let cur_component = self let cur_component = self
@@ -204,15 +203,15 @@ impl VirtualDom {
new: cur_component.frames.fin_head(), new: cur_component.frames.fin_head(),
}); });
diff_machine.work().await.unwrap(); diff_machine.work().await;
Ok(diff_machine.mutations) diff_machine.mutations
} }
/// Runs the virtualdom immediately, not waiting for any suspended nodes to complete. /// Runs the virtualdom immediately, not waiting for any suspended nodes to complete.
/// ///
/// This method will not wait for any suspended nodes to complete. /// This method will not wait for any suspended nodes to complete.
pub fn run_immediate<'s>(&'s mut self) -> Result<Mutations<'s>> { pub fn run_immediate<'s>(&'s mut self) -> Mutations<'s> {
todo!() todo!()
// use futures_util::FutureExt; // use futures_util::FutureExt;
// let mut is_ready = || false; // let mut is_ready = || false;
+7 -7
View File
@@ -22,7 +22,7 @@ fn test_original_diff() {
}; };
let mut dom = VirtualDom::new(App); let mut dom = VirtualDom::new(App);
let mutations = dom.rebuild().unwrap(); let mutations = dom.rebuild();
assert_eq!( assert_eq!(
mutations.edits, mutations.edits,
[ [
@@ -61,7 +61,7 @@ async fn create() {
test_logging::set_up_logging(LOGGING_ENABLED); test_logging::set_up_logging(LOGGING_ENABLED);
let mut dom = VirtualDom::new(App); let mut dom = VirtualDom::new(App);
let mutations = dom.rebuild_async().await.unwrap(); let mutations = dom.rebuild_async().await;
assert_eq!( assert_eq!(
mutations.edits, mutations.edits,
[ [
@@ -103,7 +103,7 @@ async fn create_list() {
test_logging::set_up_logging(LOGGING_ENABLED); test_logging::set_up_logging(LOGGING_ENABLED);
let mut dom = VirtualDom::new(App); let mut dom = VirtualDom::new(App);
let mutations = dom.rebuild_async().await.unwrap(); let mutations = dom.rebuild_async().await;
// copilot wrote this test :P // copilot wrote this test :P
assert_eq!( assert_eq!(
@@ -146,7 +146,7 @@ async fn create_simple() {
test_logging::set_up_logging(LOGGING_ENABLED); test_logging::set_up_logging(LOGGING_ENABLED);
let mut dom = VirtualDom::new(App); let mut dom = VirtualDom::new(App);
let mutations = dom.rebuild_async().await.unwrap(); let mutations = dom.rebuild_async().await;
// copilot wrote this test :P // copilot wrote this test :P
assert_eq!( assert_eq!(
@@ -182,7 +182,7 @@ async fn create_components() {
test_logging::set_up_logging(LOGGING_ENABLED); test_logging::set_up_logging(LOGGING_ENABLED);
let mut dom = VirtualDom::new(App); let mut dom = VirtualDom::new(App);
let mutations = dom.rebuild_async().await.unwrap(); let mutations = dom.rebuild_async().await;
assert_eq!( assert_eq!(
mutations.edits, mutations.edits,
@@ -228,7 +228,7 @@ async fn anchors() {
test_logging::set_up_logging(LOGGING_ENABLED); test_logging::set_up_logging(LOGGING_ENABLED);
let mut dom = VirtualDom::new(App); let mut dom = VirtualDom::new(App);
let mutations = dom.rebuild_async().await.unwrap(); let mutations = dom.rebuild_async().await;
assert_eq!( assert_eq!(
mutations.edits, mutations.edits,
[ [
@@ -254,7 +254,7 @@ async fn suspended() {
test_logging::set_up_logging(LOGGING_ENABLED); test_logging::set_up_logging(LOGGING_ENABLED);
let mut dom = VirtualDom::new(App); let mut dom = VirtualDom::new(App);
let mutations = dom.rebuild_async().await.unwrap(); let mutations = dom.rebuild_async().await;
assert_eq!( assert_eq!(
mutations.edits, mutations.edits,
+2 -2
View File
@@ -40,9 +40,9 @@ async fn test_iterative_create_components() {
let mut dom = VirtualDom::new(App); let mut dom = VirtualDom::new(App);
let mutations = dom.rebuild_async().await.unwrap(); let mutations = dom.rebuild_async().await;
dbg!(mutations); dbg!(mutations);
let mutations = dom.diff_async().await.unwrap(); let mutations = dom.diff_async().await;
dbg!(mutations); dbg!(mutations);
} }
+1 -1
View File
@@ -14,7 +14,7 @@ async fn event_queue_works() {
}; };
let mut dom = VirtualDom::new(App); let mut dom = VirtualDom::new(App);
let edits = dom.rebuild().unwrap(); let edits = dom.rebuild();
async_std::task::spawn_local(async move { async_std::task::spawn_local(async move {
match dom.run_unbounded().await { match dom.run_unbounded().await {
+6 -6
View File
@@ -19,7 +19,7 @@ fn app_runs() {
cx.render(rsx!( div{"hello"} )) cx.render(rsx!( div{"hello"} ))
}; };
let mut vdom = VirtualDom::new(App); let mut vdom = VirtualDom::new(App);
let edits = vdom.rebuild().unwrap(); let edits = vdom.rebuild();
dbg!(edits); dbg!(edits);
} }
@@ -32,7 +32,7 @@ fn fragments_work() {
)) ))
}; };
let mut vdom = VirtualDom::new(App); let mut vdom = VirtualDom::new(App);
let edits = vdom.rebuild().unwrap(); let edits = vdom.rebuild();
// should result in a final "appendchildren n=2" // should result in a final "appendchildren n=2"
dbg!(edits); dbg!(edits);
} }
@@ -46,7 +46,7 @@ fn lists_work() {
)) ))
}; };
let mut vdom = VirtualDom::new(App); let mut vdom = VirtualDom::new(App);
let edits = vdom.rebuild().unwrap(); let edits = vdom.rebuild();
dbg!(edits); dbg!(edits);
} }
@@ -61,7 +61,7 @@ fn conditional_rendering() {
}; };
let mut vdom = VirtualDom::new(App); let mut vdom = VirtualDom::new(App);
let mutations = vdom.rebuild().unwrap(); let mutations = vdom.rebuild();
dbg!(&mutations); dbg!(&mutations);
// the "false" fragment should generate an empty placeholder to re-visit // the "false" fragment should generate an empty placeholder to re-visit
assert!(mutations.edits[mutations.edits.len() - 2].is("CreatePlaceholder")); assert!(mutations.edits[mutations.edits.len() - 2].is("CreatePlaceholder"));
@@ -82,7 +82,7 @@ fn child_components() {
)) ))
}; };
let mut vdom = VirtualDom::new(App); let mut vdom = VirtualDom::new(App);
let edits = vdom.rebuild().unwrap(); let edits = vdom.rebuild();
dbg!(edits); dbg!(edits);
} }
@@ -94,6 +94,6 @@ fn suspended_works() {
}; };
let mut vdom = VirtualDom::new(App); let mut vdom = VirtualDom::new(App);
let edits = vdom.rebuild().unwrap(); let edits = vdom.rebuild();
dbg!(edits); dbg!(edits);
} }
+1 -1
View File
@@ -22,7 +22,7 @@ fn worksync() {
let mutations = loop { let mutations = loop {
let g = (&mut fut).now_or_never(); let g = (&mut fut).now_or_never();
if g.is_some() { if g.is_some() {
break g.unwrap().unwrap(); break g.unwrap();
} }
}; };
+1 -1
View File
@@ -5,7 +5,7 @@ use dioxus_html as dioxus_elements;
fn main() { fn main() {
let mut dom = VirtualDom::new(App); let mut dom = VirtualDom::new(App);
dom.rebuild().unwrap(); dom.rebuild();
println!( println!(
"{}", "{}",
dioxus_ssr::render_vdom(&dom, |c| c.newline(true).indent(true)) dioxus_ssr::render_vdom(&dom, |c| c.newline(true).indent(true))
+3 -1
View File
@@ -19,7 +19,9 @@ async fn main() -> Result<(), std::io::Error> {
.map(|f| f.parse().unwrap_or("...?".to_string())) .map(|f| f.parse().unwrap_or("...?".to_string()))
.unwrap_or("...?".to_string()); .unwrap_or("...?".to_string());
let dom = VirtualDom::launch_with_props_in_place(Example, ExampleProps { initial_name }); let mut dom = VirtualDom::new_with_props(Example, ExampleProps { initial_name });
dom.rebuild();
Ok(Response::builder(200) Ok(Response::builder(200)
.body(format!("{}", dioxus_ssr::render_vdom(&dom, |c| c))) .body(format!("{}", dioxus_ssr::render_vdom(&dom, |c| c)))
+2 -1
View File
@@ -13,7 +13,8 @@ fn main() {
let mut file = File::create("example.html").unwrap(); let mut file = File::create("example.html").unwrap();
let mut dom = VirtualDom::new(App); let mut dom = VirtualDom::new(App);
dom.rebuild().expect("failed to run virtualdom");
dom.rebuild();
file.write_fmt(format_args!( file.write_fmt(format_args!(
"{}", "{}",
+6 -6
View File
@@ -286,28 +286,28 @@ mod tests {
#[test] #[test]
fn to_string_works() { fn to_string_works() {
let mut dom = VirtualDom::new(SIMPLE_APP); let mut dom = VirtualDom::new(SIMPLE_APP);
dom.rebuild().expect("failed to run virtualdom"); dom.rebuild();
dbg!(render_vdom(&dom, |c| c)); dbg!(render_vdom(&dom, |c| c));
} }
#[test] #[test]
fn hydration() { fn hydration() {
let mut dom = VirtualDom::new(NESTED_APP); let mut dom = VirtualDom::new(NESTED_APP);
dom.rebuild().expect("failed to run virtualdom"); dom.rebuild();
dbg!(render_vdom(&dom, |c| c.pre_render(true))); dbg!(render_vdom(&dom, |c| c.pre_render(true)));
} }
#[test] #[test]
fn nested() { fn nested() {
let mut dom = VirtualDom::new(NESTED_APP); let mut dom = VirtualDom::new(NESTED_APP);
dom.rebuild().expect("failed to run virtualdom"); dom.rebuild();
dbg!(render_vdom(&dom, |c| c)); dbg!(render_vdom(&dom, |c| c));
} }
#[test] #[test]
fn fragment_app() { fn fragment_app() {
let mut dom = VirtualDom::new(FRAGMENT_APP); let mut dom = VirtualDom::new(FRAGMENT_APP);
dom.rebuild().expect("failed to run virtualdom"); dom.rebuild();
dbg!(render_vdom(&dom, |c| c)); dbg!(render_vdom(&dom, |c| c));
} }
@@ -319,7 +319,7 @@ mod tests {
let mut file = File::create("index.html").unwrap(); let mut file = File::create("index.html").unwrap();
let mut dom = VirtualDom::new(SLIGHTLY_MORE_COMPLEX); let mut dom = VirtualDom::new(SLIGHTLY_MORE_COMPLEX);
dom.rebuild().expect("failed to run virtualdom"); dom.rebuild();
file.write_fmt(format_args!( file.write_fmt(format_args!(
"{}", "{}",
@@ -337,7 +337,7 @@ mod tests {
}; };
let mut dom = VirtualDom::new(STLYE_APP); let mut dom = VirtualDom::new(STLYE_APP);
dom.rebuild().expect("failed to run virtualdom"); dom.rebuild();
dbg!(render_vdom(&dom, |c| c)); dbg!(render_vdom(&dom, |c| c));
} }
} }
+37 -11
View File
@@ -1,19 +1,17 @@
//! Basic example that renders a simple VNode to the browser. //! Basic example that renders a simple VNode to the browser.
// all these imports are done automatically with the `dioxus` crate and `prelude`
// need to do them manually for this example
use dioxus::events::on::MouseEvent; use dioxus::events::on::MouseEvent;
use dioxus_core as dioxus; use dioxus_core as dioxus;
use dioxus_core::prelude::*; use dioxus_core::prelude::*;
use dioxus_hooks::*; use dioxus_hooks::*;
use dioxus_html as dioxus_elements; use dioxus_html as dioxus_elements;
// use wasm_timer;
use std::future::Future;
use std::{pin::Pin, time::Duration};
use dioxus::prelude::*; use dioxus::prelude::*;
use dioxus_web::*; use dioxus_web::*;
use std::future::Future;
use std::{pin::Pin, time::Duration};
fn main() { fn main() {
// Setup logging // Setup logging
@@ -21,17 +19,33 @@ fn main() {
console_error_panic_hook::set_once(); console_error_panic_hook::set_once();
// Run the app // Run the app
dioxus_web::launch(App, |c| c) dioxus_web::launch(APP, |c| c)
} }
static App: FC<()> = |cx| { static APP: FC<()> = |cx| {
let mut count = use_state(cx, || 3); let mut count = use_state(cx, || 3);
cx.render(rsx! { cx.render(rsx! {
div { div {
button { button { onclick: move |_| count += 1, "add" }
"add" div {
onclick: move |_| count += 1 div {
div {
div {
div {
Fragment {
a {"wo"}
p {"wo"}
li {"wo"}
em {"wo"}
}
}
}
Child {}
}
Child {}
}
Child {}
} }
ul { ul {
{(0..*count).map(|f| rsx!{ {(0..*count).map(|f| rsx!{
@@ -43,3 +57,15 @@ static App: FC<()> = |cx| {
} }
}) })
}; };
static Child: FC<()> = |cx| {
cx.render(rsx! {
div {
div {
div {
"hello child"
}
}
}
})
};
+113 -97
View File
@@ -2,6 +2,7 @@ use std::{collections::HashMap, rc::Rc, sync::Arc};
use dioxus_core::{ use dioxus_core::{
events::{EventTrigger, VirtualEvent}, events::{EventTrigger, VirtualEvent},
mutations::NodeRefMutation,
DomEdit, ElementId, ScopeId, DomEdit, ElementId, ScopeId,
}; };
use fxhash::FxHashMap; use fxhash::FxHashMap;
@@ -77,6 +78,18 @@ impl WebsysDom {
} }
} }
pub fn apply_refs(&mut self, refs: &[NodeRefMutation]) {
for item in refs {
if let Some(bla) = &item.element {
let node = self.nodes[item.element_id.as_u64() as usize]
.as_ref()
.unwrap()
.clone();
bla.set(Box::new(node)).unwrap();
}
}
}
pub fn process_edits(&mut self, edits: &mut Vec<DomEdit>) { pub fn process_edits(&mut self, edits: &mut Vec<DomEdit>) {
for edit in edits.drain(..) { for edit in edits.drain(..) {
log::info!("Handling edit: {:#?}", edit); log::info!("Handling edit: {:#?}", edit);
@@ -92,12 +105,13 @@ impl WebsysDom {
DomEdit::CreateElementNs { tag, id, ns } => self.create_element(tag, Some(ns), id), DomEdit::CreateElementNs { tag, id, ns } => self.create_element(tag, Some(ns), id),
DomEdit::CreatePlaceholder { id } => self.create_placeholder(id), DomEdit::CreatePlaceholder { id } => self.create_placeholder(id),
DomEdit::NewEventListener { DomEdit::NewEventListener {
event_name: event, event_name,
scope, scope,
mounted_node_id: node, mounted_node_id,
} => self.new_event_listener(event, scope, node), } => self.new_event_listener(event_name, scope, mounted_node_id),
DomEdit::RemoveEventListener { event } => todo!(), DomEdit::RemoveEventListener { event } => todo!(),
DomEdit::SetText { text } => self.set_text(text), DomEdit::SetText { text } => self.set_text(text),
DomEdit::SetAttribute { field, value, ns } => self.set_attribute(field, value, ns), DomEdit::SetAttribute { field, value, ns } => self.set_attribute(field, value, ns),
DomEdit::RemoveAttribute { name } => self.remove_attribute(name), DomEdit::RemoveAttribute { name } => self.remove_attribute(name),
@@ -118,6 +132,7 @@ impl WebsysDom {
self.stack.push(real_node); self.stack.push(real_node);
} }
// drop the node off the stack // drop the node off the stack
fn pop(&mut self) { fn pop(&mut self) {
self.stack.pop(); self.stack.pop();
@@ -154,44 +169,21 @@ impl WebsysDom {
} }
fn replace_with(&mut self, m: u32, root: u64) { fn replace_with(&mut self, m: u32, root: u64) {
let mut new_nodes = vec![];
for _ in 0..m {
new_nodes.push(self.stack.pop());
}
let old = self.nodes[root as usize].as_ref().unwrap(); let old = self.nodes[root as usize].as_ref().unwrap();
let arr: js_sys::Array = new_nodes.iter().collect();
let el = old.dyn_ref::<Element>().unwrap();
el.replace_with_with_node(&arr).unwrap();
// let arr = js_sys::Array::from(); let arr: js_sys::Array = self
.stack
.list
.drain((self.stack.list.len() - m as usize)..)
.collect();
// TODO: use different-sized replace withs if let Some(el) = old.dyn_ref::<Element>() {
// if m == 1 { el.replace_with_with_node(&arr).unwrap();
// if old_node.has_type::<Element>() { } else if let Some(el) = old.dyn_ref::<web_sys::CharacterData>() {
// old_node el.replace_with_with_node(&arr).unwrap();
// .dyn_ref::<Element>() } else if let Some(el) = old.dyn_ref::<web_sys::DocumentType>() {
// .unwrap() el.replace_with_with_node(&arr).unwrap();
// .replace_with_with_node_1(&new_node) }
// .unwrap();
// } else if old_node.has_type::<web_sys::CharacterData>() {
// old_node
// .dyn_ref::<web_sys::CharacterData>()
// .unwrap()
// .replace_with_with_node_1(&new_node)
// .unwrap();
// } else if old_node.has_type::<web_sys::DocumentType>() {
// old_node
// .dyn_ref::<web_sys::DocumentType>()
// .unwrap()
// .replace_with_with_node_1(&new_node)
// .unwrap();
// } else {
// panic!("Cannot replace node: {:?}", old_node);
// }
// }
// self.stack.push(new_node);
} }
fn remove(&mut self, root: u64) { fn remove(&mut self, root: u64) {
@@ -213,22 +205,22 @@ impl WebsysDom {
self.create_element("pre", None, id); self.create_element("pre", None, id);
self.set_attribute("hidden", "", None); self.set_attribute("hidden", "", None);
} }
fn create_text_node(&mut self, text: &str, id: u64) {
// let nid = self.node_counter.next();
fn create_text_node(&mut self, text: &str, id: u64) {
let textnode = self let textnode = self
.document .document
.create_text_node(text) .create_text_node(text)
.dyn_into::<Node>() .dyn_into::<Node>()
.unwrap(); .unwrap();
let id = id as usize;
self.stack.push(textnode.clone()); self.stack.push(textnode.clone());
self.nodes[id] = Some(textnode);
self.nodes[(id as usize)] = Some(textnode);
} }
fn create_element(&mut self, tag: &str, ns: Option<&'static str>, id: u64) { fn create_element(&mut self, tag: &str, ns: Option<&'static str>, id: u64) {
let tag = wasm_bindgen::intern(tag); let tag = wasm_bindgen::intern(tag);
let el = match ns { let el = match ns {
Some(ns) => self Some(ns) => self
.document .document
@@ -243,17 +235,12 @@ impl WebsysDom {
.dyn_into::<Node>() .dyn_into::<Node>()
.unwrap(), .unwrap(),
}; };
let id = id as usize;
self.stack.push(el.clone()); self.stack.push(el.clone());
self.nodes[id] = Some(el); self.nodes[(id as usize)] = Some(el);
// let nid = self.node_counter.?next();
// let nid = self.nodes.insert(el).data().as_ffi();
// ElementId::new(nid)
} }
fn new_event_listener(&mut self, event: &'static str, scope: ScopeId, real_id: u64) { fn new_event_listener(&mut self, event: &'static str, scope: ScopeId, real_id: u64) {
// let (_on, event) = event.split_at(2);
let event = wasm_bindgen::intern(event); let event = wasm_bindgen::intern(event);
// attach the correct attributes to the element // attach the correct attributes to the element
@@ -313,7 +300,8 @@ impl WebsysDom {
} }
fn set_attribute(&mut self, name: &str, value: &str, ns: Option<&str>) { fn set_attribute(&mut self, name: &str, value: &str, ns: Option<&str>) {
if let Some(el) = self.stack.top().dyn_ref::<Element>() { let node = self.stack.top();
if let Some(el) = node.dyn_ref::<Element>() {
match ns { match ns {
// inline style support // inline style support
Some("style") => { Some("style") => {
@@ -323,11 +311,20 @@ impl WebsysDom {
} }
_ => el.set_attribute(name, value).unwrap(), _ => el.set_attribute(name, value).unwrap(),
} }
match name { }
"value" => {}
"checked" => {} if let Some(node) = node.dyn_ref::<HtmlInputElement>() {
"selected" => {} if name == "value" {
_ => {} node.set_value(value);
}
if name == "checked" {
node.set_checked(true);
}
}
if let Some(node) = node.dyn_ref::<HtmlOptionElement>() {
if name == "selected" {
node.set_selected(true);
} }
} }
} }
@@ -355,35 +352,50 @@ impl WebsysDom {
} }
fn insert_after(&mut self, n: u32, root: u64) { fn insert_after(&mut self, n: u32, root: u64) {
let mut new_nodes = vec![]; let old = self.nodes[root as usize].as_ref().unwrap();
for _ in 0..n {
new_nodes.push(self.stack.pop()); let arr: js_sys::Array = self
.stack
.list
.drain((self.stack.list.len() - n as usize)..)
.collect();
if let Some(el) = old.dyn_ref::<Element>() {
el.after_with_node(&arr).unwrap();
} else if let Some(el) = old.dyn_ref::<web_sys::CharacterData>() {
el.after_with_node(&arr).unwrap();
} else if let Some(el) = old.dyn_ref::<web_sys::DocumentType>() {
el.after_with_node(&arr).unwrap();
} }
let after = self.stack.top().clone();
let arr: js_sys::Array = new_nodes.iter().collect();
let el = after.dyn_into::<Element>().unwrap();
el.after_with_node(&arr).unwrap();
// let mut old_nodes = vec![];
// for _ in 0..n {
// old_nodes.push(self.stack.pop());
// }
// let el = self.stack.top();
} }
fn insert_before(&mut self, n: u32, root: u64) { fn insert_before(&mut self, n: u32, root: u64) {
let n = n as usize; let after = self.nodes[root as usize].as_ref().unwrap();
let root = self
.stack if n == 1 {
.list let before = self.stack.pop();
.get(self.stack.list.len() - n)
.unwrap() after
.clone(); .parent_node()
for _ in 0..n { .unwrap()
let el = self.stack.pop(); .insert_before(&before, Some(&after))
root.insert_before(&el, None).unwrap(); .unwrap();
after.insert_before(&before, None).unwrap();
} else {
let arr: js_sys::Array = self
.stack
.list
.drain((self.stack.list.len() - n as usize)..)
.collect();
if let Some(el) = after.dyn_ref::<Element>() {
el.before_with_node(&arr).unwrap();
} else if let Some(el) = after.dyn_ref::<web_sys::CharacterData>() {
el.before_with_node(&arr).unwrap();
} else if let Some(el) = after.dyn_ref::<web_sys::DocumentType>() {
el.before_with_node(&arr).unwrap();
}
} }
} }
} }
@@ -447,46 +459,50 @@ fn virtual_event_from_websys_event(event: &web_sys::Event) -> VirtualEvent {
"keydown" | "keypress" | "keyup" => { "keydown" | "keypress" | "keyup" => {
struct Event(web_sys::KeyboardEvent); struct Event(web_sys::KeyboardEvent);
impl KeyboardEventInner for Event { impl KeyboardEventInner for Event {
fn alt_key(&self) -> bool {
self.0.alt_key()
}
fn char_code(&self) -> u32 { fn char_code(&self) -> u32 {
todo!() self.0.char_code()
} }
fn key_code(&self) -> KeyCode { fn key(&self) -> String {
todo!() self.0.key()
}
fn ctrl_key(&self) -> bool {
todo!()
} }
fn key(&self) -> String { fn key_code(&self) -> KeyCode {
todo!() KeyCode::from_raw_code(self.0.key_code() as u8)
}
fn ctrl_key(&self) -> bool {
self.0.ctrl_key()
}
fn get_modifier_state(&self, key_code: &str) -> bool {
self.0.get_modifier_state(key_code)
} }
fn locale(&self) -> String { fn locale(&self) -> String {
todo!() todo!("Locale is currently not supported. :(")
} }
fn location(&self) -> usize { fn location(&self) -> usize {
todo!() self.0.location() as usize
} }
fn meta_key(&self) -> bool { fn meta_key(&self) -> bool {
todo!() self.0.meta_key()
} }
fn repeat(&self) -> bool { fn repeat(&self) -> bool {
todo!() self.0.repeat()
} }
fn shift_key(&self) -> bool { fn shift_key(&self) -> bool {
todo!() self.0.shift_key()
} }
fn which(&self) -> usize { fn which(&self) -> usize {
todo!() self.0.which() as usize
}
fn get_modifier_state(&self, key_code: usize) -> bool {
todo!()
} }
} }
let evt: web_sys::KeyboardEvent = event.clone().dyn_into().unwrap(); let evt: web_sys::KeyboardEvent = event.clone().dyn_into().unwrap();
+1 -1
View File
@@ -137,7 +137,7 @@ pub async fn run_with_props<T: Properties + 'static>(
let mut websys_dom = dom::WebsysDom::new(root_el, cfg, sender_callback); let mut websys_dom = dom::WebsysDom::new(root_el, cfg, sender_callback);
let mut mutations = dom.rebuild().unwrap(); let mut mutations = dom.rebuild();
log::info!("Mutations: {:#?}", mutations); log::info!("Mutations: {:#?}", mutations);
// hydrating is simply running the dom for a single render. If the page is already written, then the corresponding // hydrating is simply running the dom for a single render. If the page is already written, then the corresponding