Comments (7)
For example for patch
:
pub fn patch<N: Into<Node>>(root_node: N, patches: &Vec<Patch>) {
let root_node = root_node.into();
...
This way you can patch a Node
, an Element
or any other subtype of Node
.
from percy.
Hi. I started a refactoring of the VirtualNode struct.
Awesome! It's in dire need of some love!
This is both a bit ugly and causes confusion. It also makes it possible to represent illegal states (e.g. a text node with children).
Totally agree!
Is this intended, do we want the root node to always be an element node? Or should I convert the DomUpdater to use a web_sys::Node as root?
So Element
is there for no other reason than that it currently makes the tests pass.
This is because the dom updater tests are young and immature and don't test for text root nodes.
So I'm thinking we need another dom updater test in that same file looks something like:
#[wasm_bindgen_test]
fn text_root_node () {
let document = web_sys::window().unwrap().document().unwrap();
let vdom = html! { Old text };
let mut dom_updater = DomUpdater::new(vdom);
let new_vdom = html! { New text };
dom_updater.update(new_vdom);
let wrapper = document.create_element("div").unwrap();
document
.body()
.unwrap()
.append_child(&wrapper);
wrapper
.append_child(&dom_updater.root_node());
assert_eq!(&wrapper.inner_html(), "New text");
}
and then DomUpdater
can use web_sys::Node
in order to make that test pass.
Also, are you aware of an enum or trait that covers all web_sys subtypes of Node, like Element or Text?
Not entirely sure what you mean here. What are you looking to do? As in something that would let you use a trait object to accept any of those subtypes? (Sorry if I'm missing something!)
Thanks for diving into this! As always I'm happy to answer any questions / bounce ideas! This already sounds way cleaner!
from percy.
Not entirely sure what you mean here. What are you looking to do? As in something that would let you use a trait object to accept any of those subtypes? (Sorry if I'm missing something!)
It would allow me to implement as_dom_node(&self)
on VirtualNode
directly, which would then delegate the implementation to the variants. Right now we always have to match on the variants, and then call as_(text|element)_node
on there.
Thanks for the test case!
from percy.
Ahhhh gotcha thanks!
Yeah I haven't come across anything for this so matching sounds great for now.
Also heads up just added another failing test case in a different file (currently commented out) to complement the example that I posted above
Also also these changes will certainly make the html macro tests fail - so let me know if/where/how I can help there!
from percy.
Yeah I haven't come across anything for this so matching sounds great for now.
I just realized that I can use web_sys::Node
and simply dyn_cast
the child types into the Node
type! Won't work for all purposes, but for many.
from percy.
Ahhh good point - forgot that we do that but it looks like we do do that when patching text nodes
percy/crates/virtual-dom-rs/src/patch.rs
Line 240 in 824a44f
Sweet!
from percy.
Actually it's even simpler. As per https://rustwasm.github.io/rfcs/002-wasm-bindgen-inheritance-casting.html#example-upcasting you can upcast using into()
.
let text: Text = create_text();
let element: Element = create_element();
let node1: Node = text.into();
let node2: Node = element.into();
This is very nice in combination with an Into<Node>
trait bound on functions:
fn do_something_with_node<N: Into<Node>>(node: N) {
do_something(node.into());
}
from percy.
Related Issues (20)
- How alive is this project? HOT 3
- Use request_animation_frame from rust instead of Global Js call for update view HOT 3
- Free hosting? HOT 5
- Sub-routes not working? HOT 1
- Bash files in examples to rust-script? HOT 5
- Curly braces for value interpolation HOT 1
- Implement Performance Metrics HOT 2
- Implement a Formatting Tool HOT 2
- Consider Pure JS Transpilation HOT 2
- FYI: branding/naming clash HOT 1
- Crate renames HOT 1
- Docs: Broken CSS HOT 1
- Broken tests in html-macro 0.7.0 and 0.7.1 HOT 4
- Building nojs-first applications (and some thoughts about an isomorphic data layer) HOT 1
- Free Hosting for Demos HOT 1
- Allow Support for multiple parameters in `JSX` syntax HOT 4
- Spaces removed when isomorphic app is hydrated HOT 3
- File referred to in documentation doesn't exist
- Fix parenthesized blocks in html macro
- Panic happened in /allocator/free_list.rs that `new_page == 0` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from percy.