Browse Source

wip: add test_dom

Jonathan Kelley 3 years ago
parent
commit
a652090dc5

+ 1 - 1
packages/core/src/diff.rs

@@ -136,7 +136,7 @@ impl<'a> SavedDiffWork<'a> {
 }
 
 impl<'bump> DiffMachine<'bump> {
-    pub(crate) fn new(edits: Mutations<'bump>, shared: &'bump mut ResourcePool) -> Self {
+    pub(crate) fn new(edits: Mutations<'bump>, shared: &'bump ResourcePool) -> Self {
         Self {
             stack: DiffStack::new(),
             mutations: edits,

+ 8 - 1
packages/core/src/events.rs

@@ -13,7 +13,10 @@ pub struct UiEvent {
     /// The optional real node associated with the trigger
     pub mounted_dom_id: Option<ElementId>,
 
-    pub listener_type: &'static str,
+    /// The event type IE "onclick" or "onmouseover"
+    ///
+    /// The name that the renderer will use to mount the listener.
+    pub name: &'static str,
 
     /// The type of event
     pub event: SyntheticEvent,
@@ -406,6 +409,10 @@ pub mod on {
             ///
             ontoggle
         ];
+
+        GenericEventInner(GenericEvent): [
+
+        ];
     }
 
     pub trait GenericEventInner {

+ 1 - 0
packages/core/src/lib.rs

@@ -26,6 +26,7 @@ pub mod mutations;
 pub mod nodes;
 pub mod scheduler;
 pub mod scope;
+pub mod test_dom;
 pub mod util;
 pub mod virtual_dom;
 

+ 82 - 0
packages/core/src/test_dom.rs

@@ -0,0 +1,82 @@
+//! A DOM for testing - both internal and external code.
+use bumpalo::Bump;
+
+use crate::innerlude::*;
+use crate::nodes::IntoVNode;
+
+pub struct TestDom {
+    bump: Bump,
+    scheduler: Scheduler,
+}
+
+impl TestDom {
+    pub fn new() -> TestDom {
+        let bump = Bump::new();
+        let scheduler = Scheduler::new();
+        TestDom { bump, scheduler }
+    }
+    pub fn new_factory<'a>(&'a self) -> NodeFactory<'a> {
+        NodeFactory::new(&self.bump)
+    }
+
+    pub fn render<'a, F>(&'a self, lazy_nodes: LazyNodes<'a, F>) -> VNode<'a>
+    where
+        F: FnOnce(NodeFactory<'a>) -> VNode<'a>,
+    {
+        lazy_nodes.into_vnode(NodeFactory::new(&self.bump))
+    }
+
+    pub fn diff<'a>(&'a self, old: &'a VNode<'a>, new: &'a VNode<'a>) -> Mutations<'a> {
+        let mutations = Mutations::new();
+        let mut machine = DiffMachine::new(mutations, &self.scheduler.pool);
+        machine.stack.push(DiffInstruction::DiffNode { new, old });
+
+        machine.mutations
+    }
+
+    pub fn create<'a, F1>(&'a self, left: LazyNodes<'a, F1>) -> Mutations<'a>
+    where
+        F1: FnOnce(NodeFactory<'a>) -> VNode<'a>,
+    {
+        let old = self.bump.alloc(self.render(left));
+
+        let mut machine = DiffMachine::new(Mutations::new(), &self.scheduler.pool);
+
+        machine.stack.create_node(old, MountType::Append);
+
+        machine.work(&mut || false);
+
+        machine.mutations
+    }
+
+    pub fn lazy_diff<'a, F1, F2>(
+        &'a self,
+        left: LazyNodes<'a, F1>,
+        right: LazyNodes<'a, F2>,
+    ) -> (Mutations<'a>, Mutations<'a>)
+    where
+        F1: FnOnce(NodeFactory<'a>) -> VNode<'a>,
+        F2: FnOnce(NodeFactory<'a>) -> VNode<'a>,
+    {
+        let old = self.bump.alloc(self.render(left));
+
+        let new = self.bump.alloc(self.render(right));
+
+        let mut machine = DiffMachine::new(Mutations::new(), &self.scheduler.pool);
+
+        machine.stack.create_node(old, MountType::Append);
+
+        machine.work(&mut || false);
+        let create_edits = machine.mutations;
+
+        let mut machine = DiffMachine::new(Mutations::new(), &self.scheduler.pool);
+
+        machine.stack.push(DiffInstruction::DiffNode { old, new });
+
+        machine.work(&mut || false);
+
+        let edits = machine.mutations;
+
+        (create_edits, edits)
+    }
+}

+ 1 - 1
packages/core/tests/create_iterative.rs

@@ -1,7 +1,7 @@
 //! tests to prove that the iterative implementation works
 
 use anyhow::{Context, Result};
-use dioxus::{diff::DiffMachine, prelude::*, scheduler::Scheduler, DomEdit, Mutations};
+use dioxus::{prelude::*, DomEdit, Mutations};
 mod test_logging;
 use dioxus_core as dioxus;
 use dioxus_html as dioxus_elements;

+ 1 - 1
packages/core/tests/diff_iterative.rs

@@ -43,6 +43,6 @@ async fn test_iterative_create_components() {
     let mutations = dom.rebuild_async().await;
     dbg!(mutations);
 
-    let mutations = dom.diff_async().await;
+    let mutations = dom.diff();
     dbg!(mutations);
 }

+ 0 - 84
packages/core/tests/diffing.rs

@@ -15,90 +15,6 @@ use DomEdit::*;
 // feel free to enable while debugging
 const IS_LOGGING_ENABLED: bool = false;
 
-struct TestDom {
-    bump: Bump,
-    resources: Scheduler,
-}
-impl TestDom {
-    fn new() -> TestDom {
-        test_logging::set_up_logging(IS_LOGGING_ENABLED);
-        let bump = Bump::new();
-        let resources = Scheduler::new();
-        TestDom { bump, resources }
-    }
-    fn new_factory<'a>(&'a self) -> NodeFactory<'a> {
-        NodeFactory::new(&self.bump)
-    }
-
-    fn render<'a, F>(&'a self, lazy_nodes: LazyNodes<'a, F>) -> VNode<'a>
-    where
-        F: FnOnce(NodeFactory<'a>) -> VNode<'a>,
-    {
-        use dioxus_core::nodes::{IntoVNode, IntoVNodeList};
-        lazy_nodes.into_vnode(NodeFactory::new(&self.bump))
-    }
-
-    fn diff<'a>(&'a self, old: &'a VNode<'a>, new: &'a VNode<'a>) -> Mutations<'a> {
-        // let mut edits = Vec::new();
-        let mut machine = DiffMachine::new_headless(&self.resources);
-
-        machine.stack.push(DiffInstruction::DiffNode { new, old });
-
-        machine.mutations
-    }
-
-    fn create<'a, F1>(&'a self, left: LazyNodes<'a, F1>) -> Mutations<'a>
-    where
-        F1: FnOnce(NodeFactory<'a>) -> VNode<'a>,
-    {
-        let old = self.bump.alloc(self.render(left));
-
-        let mut machine = DiffMachine::new_headless(&self.resources);
-
-        machine.stack.create_node(old, MountType::Append);
-
-        work_sync(&mut machine);
-
-        machine.mutations
-    }
-
-    fn lazy_diff<'a, F1, F2>(
-        &'a self,
-        left: LazyNodes<'a, F1>,
-        right: LazyNodes<'a, F2>,
-    ) -> (Mutations<'a>, Mutations<'a>)
-    where
-        F1: FnOnce(NodeFactory<'a>) -> VNode<'a>,
-        F2: FnOnce(NodeFactory<'a>) -> VNode<'a>,
-    {
-        let old = self.bump.alloc(self.render(left));
-
-        let new = self.bump.alloc(self.render(right));
-
-        let mut machine = DiffMachine::new_headless(&self.resources);
-
-        machine.stack.create_node(old, MountType::Append);
-
-        work_sync(&mut machine);
-        let create_edits = machine.mutations;
-
-        let mut machine = DiffMachine::new_headless(&self.resources);
-        machine.stack.push(DiffInstruction::DiffNode { old, new });
-        work_sync(&mut machine);
-        let edits = machine.mutations;
-
-        (create_edits, edits)
-    }
-}
-
-fn work_sync(machine: &mut DiffMachine) {
-    let mut fut = machine.work().boxed_local();
-
-    while let None = (&mut fut).now_or_never() {
-        //
-    }
-}
-
 #[test]
 fn diffing_works() {}
 

+ 1 - 1
packages/core/tests/eventsystem.rs

@@ -1,7 +1,7 @@
 use bumpalo::Bump;
 
 use anyhow::{Context, Result};
-use dioxus::{diff::DiffMachine, prelude::*, scheduler::Scheduler, DomEdit};
+use dioxus::{prelude::*, DomEdit};
 use dioxus_core as dioxus;
 use dioxus_html as dioxus_elements;
 

+ 1 - 1
packages/core/tests/hooks.rs

@@ -1,5 +1,5 @@
 use anyhow::{Context, Result};
-use dioxus::{diff::DiffMachine, prelude::*, scheduler::Scheduler, DomEdit};
+use dioxus::prelude::*;
 use dioxus_core as dioxus;
 use dioxus_html as dioxus_elements;
 

+ 1 - 1
packages/core/tests/work_sync.rs

@@ -3,7 +3,7 @@
 //! This means you can actually call it synchronously if you want.
 
 use anyhow::{Context, Result};
-use dioxus::{diff::DiffMachine, prelude::*, scheduler::Scheduler, scope::Scope};
+use dioxus::{prelude::*, scope::Scope};
 use dioxus_core as dioxus;
 use dioxus_html as dioxus_elements;
 use futures_util::FutureExt;

+ 1 - 0
packages/desktop/src/events.rs

@@ -25,6 +25,7 @@ pub fn trigger_from_serialized(val: serde_json::Value) -> UiEvent {
     let scope = ScopeId(data.scope as usize);
     let mounted_dom_id = Some(ElementId(data.mounted_dom_id as usize));
     UiEvent {
+        name: todo!(),
         event,
         scope,
         mounted_dom_id,

+ 94 - 6
packages/web/src/dom.rs

@@ -530,13 +530,13 @@ fn decode_trigger(event: &web_sys::Event) -> anyhow::Result<UiEvent> {
 
     let typ = event.type_();
 
-    use anyhow::Context;
+    // let attrs = target.attributes();
+    // for x in 0..attrs.length() {
+    //     let attr = attrs.item(x).unwrap();
+    //     log::debug!("attrs include: {:#?}", attr);
+    // }
 
-    let attrs = target.attributes();
-    for x in 0..attrs.length() {
-        let attr = attrs.item(x).unwrap();
-        log::debug!("attrs include: {:#?}", attr);
-    }
+    use anyhow::Context;
 
     // The error handling here is not very descriptive and needs to be replaced with a zero-cost error system
     let val: String = target
@@ -562,6 +562,7 @@ fn decode_trigger(event: &web_sys::Event) -> anyhow::Result<UiEvent> {
     // let triggered_scope: ScopeId = KeyData::from_ffi(gi_id).into();
     log::debug!("Triggered scope is {:#?}", triggered_scope);
     Ok(UiEvent {
+        name: event_name_from_typ(&typ),
         event: virtual_event_from_websys_event(event.clone()),
         mounted_dom_id: Some(ElementId(real_id as usize)),
         scope: ScopeId(triggered_scope as usize),
@@ -578,3 +579,90 @@ pub fn load_document() -> Document {
         .document()
         .expect("should have access to the Document")
 }
+
+pub fn event_name_from_typ(typ: &str) -> &'static str {
+    match typ {
+        "copy" => "oncopy",
+        "cut" => "oncut",
+        "paste" => "onpaste",
+        "compositionend" => "oncompositionend",
+        "compositionstart" => "oncompositionstart",
+        "compositionupdate" => "oncompositionupdate",
+        "keydown" => "onkeydown",
+        "keypress" => "onkeypress",
+        "keyup" => "onkeyup",
+        "focus" => "onfocus",
+        "blur" => "onblur",
+        "change" => "onchange",
+        "input" => "oninput",
+        "invalid" => "oninvalid",
+        "reset" => "onreset",
+        "submit" => "onsubmit",
+        "click" => "onclick",
+        "contextmenu" => "oncontextmenu",
+        "doubleclick" => "ondoubleclick",
+        "drag" => "ondrag",
+        "dragend" => "ondragend",
+        "dragenter" => "ondragenter",
+        "dragexit" => "ondragexit",
+        "dragleave" => "ondragleave",
+        "dragover" => "ondragover",
+        "dragstart" => "ondragstart",
+        "drop" => "ondrop",
+        "mousedown" => "onmousedown",
+        "mouseenter" => "onmouseenter",
+        "mouseleave" => "onmouseleave",
+        "mousemove" => "onmousemove",
+        "mouseout" => "onmouseout",
+        "mouseover" => "onmouseover",
+        "mouseup" => "onmouseup",
+        "pointerdown" => "onpointerdown",
+        "pointermove" => "onpointermove",
+        "pointerup" => "onpointerup",
+        "pointercancel" => "onpointercancel",
+        "gotpointercapture" => "ongotpointercapture",
+        "lostpointercapture" => "onlostpointercapture",
+        "pointerenter" => "onpointerenter",
+        "pointerleave" => "onpointerleave",
+        "pointerover" => "onpointerover",
+        "pointerout" => "onpointerout",
+        "select" => "onselect",
+        "touchcancel" => "ontouchcancel",
+        "touchend" => "ontouchend",
+        "touchmove" => "ontouchmove",
+        "touchstart" => "ontouchstart",
+        "scroll" => "onscroll",
+        "wheel" => "onwheel",
+        "animationstart" => "onanimationstart",
+        "animationend" => "onanimationend",
+        "animationiteration" => "onanimationiteration",
+        "transitionend" => "ontransitionend",
+        "abort" => "onabort",
+        "canplay" => "oncanplay",
+        "canplaythrough" => "oncanplaythrough",
+        "durationchange" => "ondurationchange",
+        "emptied" => "onemptied",
+        "encrypted" => "onencrypted",
+        "ended" => "onended",
+        "error" => "onerror",
+        "loadeddata" => "onloadeddata",
+        "loadedmetadata" => "onloadedmetadata",
+        "loadstart" => "onloadstart",
+        "pause" => "onpause",
+        "play" => "onplay",
+        "playing" => "onplaying",
+        "progress" => "onprogress",
+        "ratechange" => "onratechange",
+        "seeked" => "onseeked",
+        "seeking" => "onseeking",
+        "stalled" => "onstalled",
+        "suspend" => "onsuspend",
+        "timeupdate" => "ontimeupdate",
+        "volumechange" => "onvolumechange",
+        "waiting" => "onwaiting",
+        "toggle" => "ontoggle",
+        _ => {
+            panic!("unsupported event type")
+        }
+    }
+}