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| {
b.iter(|| {
let mut dom = VirtualDom::new(App);
let g = dom.rebuild().unwrap();
let g = dom.rebuild();
assert!(g.edits.len() > 1);
})
});
+1 -1
View File
@@ -6,7 +6,7 @@ use std::fmt::Display;
fn main() {
let mut dom = VirtualDom::new(App);
let g = dom.rebuild().unwrap();
let g = dom.rebuild();
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.
///
/// 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
// large functions tend to be difficult for the compiler to work with
while let Some(instruction) = self.stack.pop() {
@@ -190,8 +190,6 @@ impl<'bump> DiffMachine<'bump> {
}
};
}
Ok(())
}
fn mount(&mut self, and: MountType<'bump>) {
+24 -22
View File
@@ -602,8 +602,31 @@ pub mod on {
}
pub trait KeyboardEventInner {
fn alt_key(&self) -> bool;
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.
///
/// 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
fn ctrl_key(&self) -> bool;
/// 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;
fn get_modifier_state(&self, key_code: &str) -> bool;
// fn key(&self) -> String;
fn locale(&self) -> String;
fn location(&self) -> usize;
fn meta_key(&self) -> bool;
fn repeat(&self) -> bool;
fn shift_key(&self) -> bool;
fn which(&self) -> usize;
fn get_modifier_state(&self, key_code: usize) -> bool;
}
pub trait FocusEventInner {
+2 -2
View File
@@ -139,8 +139,8 @@ impl<'a> Mutations<'a> {
// refs are only assigned once
pub struct NodeRefMutation<'a> {
element: &'a mut Option<once_cell::sync::OnceCell<Box<dyn Any>>>,
element_id: ElementId,
pub element: &'a mut Option<once_cell::sync::OnceCell<Box<dyn Any>>>,
pub element_id: ElementId,
}
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 {
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> {
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
@@ -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
/// 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();
loop {
@@ -163,7 +149,11 @@ impl VirtualDom {
}
/// 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 cur_component = self
@@ -176,7 +166,7 @@ impl VirtualDom {
diff_machine
.stack
.create_node(cur_component.frames.fin_head(), MountType::Append);
diff_machine.work().await.unwrap();
diff_machine.work().await;
} else {
// todo: should this be a hard error?
log::warn!(
@@ -185,11 +175,20 @@ impl VirtualDom {
);
}
Ok(diff_machine.mutations)
diff_machine.mutations
}
// diff the dom with itself
pub async fn diff_async<'s>(&'s mut self) -> Result<Mutations<'s>> {
pub fn diff_sync<'s>(&'s mut self) -> 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 cur_component = self
@@ -204,15 +203,15 @@ impl VirtualDom {
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.
///
/// 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!()
// use futures_util::FutureExt;
// let mut is_ready = || false;
+7 -7
View File
@@ -22,7 +22,7 @@ fn test_original_diff() {
};
let mut dom = VirtualDom::new(App);
let mutations = dom.rebuild().unwrap();
let mutations = dom.rebuild();
assert_eq!(
mutations.edits,
[
@@ -61,7 +61,7 @@ async fn create() {
test_logging::set_up_logging(LOGGING_ENABLED);
let mut dom = VirtualDom::new(App);
let mutations = dom.rebuild_async().await.unwrap();
let mutations = dom.rebuild_async().await;
assert_eq!(
mutations.edits,
[
@@ -103,7 +103,7 @@ async fn create_list() {
test_logging::set_up_logging(LOGGING_ENABLED);
let mut dom = VirtualDom::new(App);
let mutations = dom.rebuild_async().await.unwrap();
let mutations = dom.rebuild_async().await;
// copilot wrote this test :P
assert_eq!(
@@ -146,7 +146,7 @@ async fn create_simple() {
test_logging::set_up_logging(LOGGING_ENABLED);
let mut dom = VirtualDom::new(App);
let mutations = dom.rebuild_async().await.unwrap();
let mutations = dom.rebuild_async().await;
// copilot wrote this test :P
assert_eq!(
@@ -182,7 +182,7 @@ async fn create_components() {
test_logging::set_up_logging(LOGGING_ENABLED);
let mut dom = VirtualDom::new(App);
let mutations = dom.rebuild_async().await.unwrap();
let mutations = dom.rebuild_async().await;
assert_eq!(
mutations.edits,
@@ -228,7 +228,7 @@ async fn anchors() {
test_logging::set_up_logging(LOGGING_ENABLED);
let mut dom = VirtualDom::new(App);
let mutations = dom.rebuild_async().await.unwrap();
let mutations = dom.rebuild_async().await;
assert_eq!(
mutations.edits,
[
@@ -254,7 +254,7 @@ async fn suspended() {
test_logging::set_up_logging(LOGGING_ENABLED);
let mut dom = VirtualDom::new(App);
let mutations = dom.rebuild_async().await.unwrap();
let mutations = dom.rebuild_async().await;
assert_eq!(
mutations.edits,
+2 -2
View File
@@ -40,9 +40,9 @@ async fn test_iterative_create_components() {
let mut dom = VirtualDom::new(App);
let mutations = dom.rebuild_async().await.unwrap();
let mutations = dom.rebuild_async().await;
dbg!(mutations);
let mutations = dom.diff_async().await.unwrap();
let mutations = dom.diff_async().await;
dbg!(mutations);
}
+1 -1
View File
@@ -14,7 +14,7 @@ async fn event_queue_works() {
};
let mut dom = VirtualDom::new(App);
let edits = dom.rebuild().unwrap();
let edits = dom.rebuild();
async_std::task::spawn_local(async move {
match dom.run_unbounded().await {
+6 -6
View File
@@ -19,7 +19,7 @@ fn app_runs() {
cx.render(rsx!( div{"hello"} ))
};
let mut vdom = VirtualDom::new(App);
let edits = vdom.rebuild().unwrap();
let edits = vdom.rebuild();
dbg!(edits);
}
@@ -32,7 +32,7 @@ fn fragments_work() {
))
};
let mut vdom = VirtualDom::new(App);
let edits = vdom.rebuild().unwrap();
let edits = vdom.rebuild();
// should result in a final "appendchildren n=2"
dbg!(edits);
}
@@ -46,7 +46,7 @@ fn lists_work() {
))
};
let mut vdom = VirtualDom::new(App);
let edits = vdom.rebuild().unwrap();
let edits = vdom.rebuild();
dbg!(edits);
}
@@ -61,7 +61,7 @@ fn conditional_rendering() {
};
let mut vdom = VirtualDom::new(App);
let mutations = vdom.rebuild().unwrap();
let mutations = vdom.rebuild();
dbg!(&mutations);
// the "false" fragment should generate an empty placeholder to re-visit
assert!(mutations.edits[mutations.edits.len() - 2].is("CreatePlaceholder"));
@@ -82,7 +82,7 @@ fn child_components() {
))
};
let mut vdom = VirtualDom::new(App);
let edits = vdom.rebuild().unwrap();
let edits = vdom.rebuild();
dbg!(edits);
}
@@ -94,6 +94,6 @@ fn suspended_works() {
};
let mut vdom = VirtualDom::new(App);
let edits = vdom.rebuild().unwrap();
let edits = vdom.rebuild();
dbg!(edits);
}
+1 -1
View File
@@ -22,7 +22,7 @@ fn worksync() {
let mutations = loop {
let g = (&mut fut).now_or_never();
if g.is_some() {
break g.unwrap().unwrap();
break g.unwrap();
}
};